Applications & Algorithms
This section collects quantum algorithms and application examples built from qalgora-Q kernels. Among them, VQE and QAOA are typical quantum–classical hybrid algorithms: the quantum circuit prepares a parametrized state and estimates expectation values, while a classical optimizer updates the parameters.
Variational Quantum Eigensolver (VQE)
VQE finds the ground-state energy of a Hamiltonian by classically optimizing the parameters of a quantum ansatz.
import qalgora
from qalgora import spin
@qalgora.kernel
def ansatz(theta: float):
q = qalgora.qvector(2)
x(q[0])
ry(theta, q[1])
x.ctrl(q[1], q[0])
hamiltonian = (5.907 - 2.143 * spin.x(0) * spin.x(1)
- 2.143 * spin.y(0) * spin.y(1) + 0.218 * spin.z(0))
optimizer = qalgora.optimizers.COBYLA()
energy, params = qalgora.vqe(ansatz, hamiltonian, optimizer, parameter_count=1)
print("Ground-state energy:", energy)The Hamiltonian above is a small two-qubit model used only to show the VQE call flow; its
coefficients are illustrative, not a full molecular Hamiltonian. Likewise the single-parameter
ansatz is a minimal teaching circuit — a real problem needs an ansatz designed for the
system's symmetries, particle-number conservation, or hardware connectivity.
Quantum Approximate Optimization (QAOA)
QAOA tackles combinatorial optimization (Max-Cut, portfolio, scheduling) by alternating
cost and mixer layers. The skeleton below shows that structure; the cost layer (the problem-specific
ZZ couplings, plus the final expectation measurement and optimizer loop) is left as a
comment, so treat it as pseudocode rather than a runnable script.
@qalgora.kernel
def qaoa(gammas: list[float], betas: list[float], n: int, layers: int):
q = qalgora.qvector(n)
for i in range(n):
h(q[i]) # Hadamard on every qubit
for l in range(layers):
# cost layer — problem-specific ZZ couplings, e.g. for each edge (a, b),
# assuming Rz(theta) = exp(-i theta Z / 2) and a per-edge cost term
# 0.5 * (Z_i Z_j - 1), so the rotation angle is gamma (not 2*gamma):
# x.ctrl(q[a], q[b]); rz(gammas[l], q[b]); x.ctrl(q[a], q[b])
# mixer layer (Rx(theta) = exp(-i theta X / 2), so exp(-i beta X) is rx(2*beta))
for i in range(n):
rx(2.0 * betas[l], q[i])
Other domains
- Quantum chemistry — molecular ground states via VQE and ADAPT-VQE.
- Quantum machine learning — parameterized circuits as trainable models.
- Error correction — stabilizer codes via the qalgora-QX QEC library.
应用与算法
本节汇总基于 qalgora-Q 内核构建的量子算法与应用示例,其中 VQE 与 QAOA 是典型的量子-经典混合算法:量子线路负责制备参数化态并估计期望值,经典优化器负责更新参数。
变分量子本征求解器 (VQE)
VQE 通过经典优化量子线路拟设的参数,求解哈密顿量的基态能量。
import qalgora
from qalgora import spin
@qalgora.kernel
def ansatz(theta: float):
q = qalgora.qvector(2)
x(q[0])
ry(theta, q[1])
x.ctrl(q[1], q[0])
hamiltonian = (5.907 - 2.143 * spin.x(0) * spin.x(1)
- 2.143 * spin.y(0) * spin.y(1) + 0.218 * spin.z(0))
optimizer = qalgora.optimizers.COBYLA()
energy, params = qalgora.vqe(ansatz, hamiltonian, optimizer, parameter_count=1)
print("Ground-state energy:", energy)上面的哈密顿量是一个仅用于演示 VQE 调用流程的二量子比特小模型,系数仅作说明,并不代表完整的分子哈密顿量。同样,单参数 ansatz 只是最小的教学线路——实际问题需要根据体系对称性、粒子数守恒或硬件连通性来设计拟设。
量子近似优化算法 (QAOA)
QAOA 通过交替施加代价层与混合层来求解组合优化问题(最大割、投资组合优化、调度等)。下面的骨架展示了这一结构;代价层(与问题相关的 ZZ 耦合,以及末尾的期望值测量与优化器循环)以注释形式留空,因此应视为伪代码,而非可直接运行的脚本。
@qalgora.kernel
def qaoa(gammas: list[float], betas: list[float], n: int, layers: int):
q = qalgora.qvector(n)
for i in range(n):
h(q[i]) # 对每个比特施加一个 Hadamard 门
for l in range(layers):
# 代价层——与问题相关的 ZZ 耦合,例如对每条边 (a, b),
# 约定 Rz(theta) = exp(-i theta Z / 2)、单边代价项 0.5 * (Z_i Z_j - 1),
# 故旋转角为 gamma(而非 2*gamma):
# x.ctrl(q[a], q[b]); rz(gammas[l], q[b]); x.ctrl(q[a], q[b])
# 混合层(Rx(theta) = exp(-i theta X / 2),故 exp(-i beta X) 为 rx(2*beta))
for i in range(n):
rx(2.0 * betas[l], q[i])
其他应用领域
- 量子化学 — 通过 VQE 和 ADAPT-VQE 求解分子基态。
- 量子机器学习 — 将参数化电路用作可训练模型。
- 量子纠错 — 通过 qalgora-QX QEC 库实现稳定子码。