Quantum Chemistry with VQE
Estimate the ground-state energy of a molecule by mapping its electronic Hamiltonian onto qubits and minimising the energy with a variational ansatz. The variational quantum eigensolver (VQE) is the workhorse method for quantum chemistry in the NISQ era.
The problem it solves — electronic structure
Almost every chemical property of a molecule — its stable geometry, reaction barriers, spectra —
depends on the ground-state energy of its electrons, the lowest eigenvalue of the
electronic Hamiltonian H. Solving this electronic structure problem is the heart of
computational chemistry.
The difficulty is that the Hilbert space needed for an exact solution grows exponentially with the number of electrons (orbitals). Exact classical methods (full configuration interaction) quickly become infeasible, so chemists have long relied on approximations. The appeal of a quantum computer is that it natively represents a wavefunction as a quantum state and can, in principle, encode these exponentially large states efficiently.
From molecule to qubit Hamiltonian
The electronic Hamiltonian is first written as a sum of fermionic operators (creation and annihilation operators), because electrons are fermions obeying antisymmetry. Qubits are not fermions, so a mapping is needed to translate fermionic operators into Pauli operators.
The classic mapping is the Jordan–Wigner transformation: it assigns each spin orbital to one qubit (occupied = 1, empty = 0) and properly encodes the fermionic antisymmetry signs through Pauli strings. Other mappings (Bravyi–Kitaev, parity) make different trade-offs between locality and qubit count. After mapping, the Hamiltonian becomes a sum of Pauli strings, each with a real coefficient:
H = Σk ck · Pk, where each Pk is a tensor product of I, X, Y, Z.
The variational principle — why minimizing the energy suffices
The theoretical foundation of VQE is the variational principle: for any normalized
trial state |ψ(θ)⟩, its energy expectation value can never fall below the true ground-state
energy:
E(θ) = ⟨ψ(θ)| H |ψ(θ)⟩ ≥ E0
This gives a clear strategy: scan a family of parameterized trial states and go as low as you
can. The lowest E(θ) you reach is an upper bound on E0, and the closer it gets, the better
your ansatz. So finding the ground state becomes a minimization problem — the quantum circuit prepares
|ψ(θ)⟩ and measures E(θ), while a classical optimizer tunes θ.
The circuit ansatz — writing electron correlation into parameters
The ansatz is the parameterized circuit that prepares |ψ(θ)⟩; it
determines how large and how faithful a space of states you can search. Two common choices:
- UCCSD (unitary coupled cluster with singles and doubles) — chemically motivated, exciting electron pairs out of a Hartree–Fock reference; accurate but with a deeper circuit.
- Hardware-efficient ansatz — alternating single-qubit rotations and native entangling gates; shallow and hardware-friendly, but lacking chemical priors and more prone to barren plateaus.
A good ansatz balances expressive power (can it cover the true ground state) against trainability / executability (is the circuit shallow enough, are the gradients optimizable).
Measurement cost — an underrated bottleneck
The energy E(θ) is the weighted sum of the expectation value of each Pauli string in the Hamiltonian:
E(θ) = Σk ck ⟨Pk⟩. Each ⟨Pk⟩ must be estimated by
repeated sampling in the appropriate measurement basis. As the molecule grows, the number of Pauli strings
can reach order O(N4) (N = number of orbitals), and estimating every term to
sufficient precision demands an enormous number of measurements.
This is one of the real costs of VQE and has spawned many mitigation techniques: grouping commuting Pauli strings for joint measurement, allocating the measurement budget by coefficient, classical shadows, and more. Understanding this is essential to a clear-eyed view of VQE's practical scalability.
What it is good at — and what it isn't
- Built for NISQ. VQE shifts the burden of deep circuits onto an outer classical optimizer, keeping circuits relatively shallow and suited to today's noisy hardware — which is why it is popular.
- Limited by ansatz quality. If the ansatz cannot represent the true ground state, the energy stalls at a too-high upper bound; expressive power and trainability are hard to have at once.
- Huge measurement overhead. The number of Pauli strings grows fast with molecule size, and sampling cost is often the main obstacle to practicality.
- Hard optimization, eroded by noise. Barren plateaus, local minima, plus hardware noise biasing the expectation values, make convergence to chemical accuracy challenging.
- Advantage not yet confirmed at practical scale. For small molecules, classical methods are still fast and accurate; when and for which systems VQE truly wins is an active research question.
The algorithm, step by step
- A chemistry frontend produces a fermionic Hamiltonian, mapped (e.g. via Jordan–Wigner) to a sum of Pauli strings.
- Choose a parameterized ansatz
|ψ(θ)⟩(UCCSD or hardware-efficient). - Prepare
|ψ(θ)⟩on the quantum circuit and measure each Pauli string to estimate E(θ). - Hand E(θ) to the classical optimizer, which updates θ toward lower energy per the variational principle.
- Repeat until convergence; the lowest E(θ) is the best upper-bound estimate of the ground-state energy.
Seeing it in code
Below, the H₂ Hamiltonian is written directly as a sum of Pauli strings — exactly the post-mapping form
above. Each coefficient ck corresponds one-to-one with a Pauli string (such as
spin.z(0) * spin.z(1)); here parity tapering has already reduced the qubit count to 2.
import qalgora
from qalgora import spin
# H2 molecule at 0.735 A bond length, mapped to 2 qubits (parity tapering)
hamiltonian = (-1.0524 + 0.3979 * spin.z(0) - 0.3979 * spin.z(1)
- 0.0112 * spin.z(0) * spin.z(1)
+ 0.1809 * spin.x(0) * spin.x(1))
These coefficients are illustrative values for one specific molecule, basis, and fermion-to-qubit mapping (H₂ at 0.735 Å, minimal basis, parity tapering) — they are not a universal VQE Hamiltonian, and any other molecule, geometry, basis, or mapping yields different terms.
This Hamiltonian represents only the electronic energy; it does not include the nuclear-repulsion term. The total molecular energy requires adding the nuclear-repulsion energy at this bond length.
This is a deliberately minimal one-parameter teaching ansatz for the two-qubit reduced model of
H₂: x(q[0]) prepares the Hartree–Fock reference state |10⟩, and the following
ry(theta, …) plus controlled gate introduce correlation near that reference with a single
parameter θ. It reflects the spirit of UCC-type ansätze, but it is not a full UCCSD circuit; real
chemistry uses richer parameterizations.
@qalgora.kernel
def ansatz(theta: float):
q = qalgora.qvector(2)
x(q[0]) # Hartree-Fock reference |10>
ry(theta, q[1])
x.ctrl(q[1], q[0])
The final step is the variational loop: inside qalgora.vqe, the ansatz is repeatedly
prepared, the Hamiltonian measured to obtain E(θ), and COBYLA pushes θ toward lower energy per
the variational principle. The returned energy is the upper-bound estimate of the ground-state
energy. Under ideal, noise-free conditions with exact expectation-value estimation, the VQE energy is an
upper bound on the ground-state energy; on real hardware and with finite sampling this upper-bound property
can be broken by noise and statistical error.
qalgora.vqe driver — together with the chemistry frontends it builds on
(qalgora.chemistry.*, molecular_hamiltonian, active_space,
create_molecule) — is a specification interface; the open reference build does not bundle
these yet, so the snippet below is illustrative. You can reproduce the same loop today with the cost
function and optimizer shown on the Cost Minimization page.
optimizer = qalgora.optimizers.COBYLA()
energy, params = qalgora.vqe(ansatz, hamiltonian, optimizer, parameter_count=1)
print(f"ground-state energy: {energy:.6f} Hartree")
# electronic energy ~ -1.857 Ha; this Hamiltonian excludes nuclear repulsion
# (+0.72 Ha at this bond length), giving ~ -1.137 Ha totalScaling to larger molecules
- Use the Solvers library's
adapt_vqeto grow the ansatz automatically. - Offload the expectation evaluation to the
gputarget as qubit counts grow. - Batch multiple bond lengths to trace the full dissociation curve.
References
- A. Peruzzo, J. McClean, P. Shadbolt, M.-H. Yung, X.-Q. Zhou, P. J. Love, A. Aspuru-Guzik, J. L. O'Brien, "A variational eigenvalue solver on a photonic quantum processor," Nat. Commun. 5, 4213 (2014). doi:10.1038/ncomms5213
基于 VQE 的量子化学
通过将分子的电子哈密顿量映射到量子比特上,并利用变分线路拟设最小化能量,估算分子基态能量。变分量子本征求解器 (VQE) 是 NISQ 时代量子化学的主力方法。
它解决的问题 电子结构
分子的几乎一切化学性质——稳定构型、反应能垒、光谱——都取决于其电子的基态能量,即电子哈密顿量 H 的最低本征值。求解这一电子结构问题是计算化学的核心。
难点在于:精确求解所需的希尔伯特空间维度随电子(轨道)数指数增长。经典精确方法(全组态相互作用)很快变得不可行,于是化学家长期依赖各种近似。量子计算机的吸引力在于:它天然以量子态表示波函数,原则上可以高效编码这些指数大的态。
从分子到量子比特哈密顿量
电子哈密顿量最初写成费米子算符(产生与湮灭算符)之和,因为电子是遵循反对称性的费米子。量子比特并不是费米子,所以需要一次映射把费米子算符翻译成泡利算符。
最经典的映射是 Jordan–Wigner 变换:它把每个自旋轨道对应到一个量子比特(占据为 1、空置为 0),并通过泡利串妥善编码费米子的反对称符号。其他映射(如 Bravyi–Kitaev、奇偶映射)则在局域性与比特数之间做不同权衡。映射之后,哈密顿量成为一组泡利串之和,每串配一个实系数:
H = Σk ck · Pk,其中每个 Pk 是若干 I、X、Y、Z 的张量积。
变分原理 为何最小化能量就够了
VQE 的理论基石是变分原理:对任意归一化的试探态 |ψ(θ)⟩,其能量期望值绝不会低于真实基态能量:
E(θ) = ⟨ψ(θ)| H |ψ(θ)⟩ ≥ E0
这给了我们一个清晰的策略:用一族带参数 θ 的试探态扫描,越往下越好。我们能取到的最低 E(θ) 就是 E0 的一个上界,且越接近越说明拟设越好。于是基态求解变成了一个最小化问题——量子线路负责制备 |ψ(θ)⟩ 并测量 E(θ),经典优化器负责调节 θ。
线路拟设 把电子相关写进参数
拟设(ansatz)是制备 |ψ(θ)⟩ 的参数化线路,它决定了我们能搜索的态有多大、多贴合真实波函数。两类常见选择:
- UCCSD(幺正耦合簇,含单激发与双激发)——化学动机明确,从 Hartree–Fock 参考态出发激发电子对,精度高,但线路较深。
- 硬件高效拟设——交替施加单比特旋转与原生纠缠门,线路浅、对硬件友好,但缺乏化学先验,更容易陷入贫瘠高原。
好的拟设要在表达力(能否覆盖真实基态)与可训练性 / 可执行性(线路是否够浅、梯度是否可优化)之间取得平衡。
测量代价 一个被低估的瓶颈
能量 E(θ) 是哈密顿量中每个泡利串期望值的加权和:E(θ) = Σk ck ⟨Pk⟩。每个 ⟨Pk⟩ 都要在合适的测量基下反复采样估计。随着分子增大,泡利串数目可达 O(N4) 量级(N 为轨道数),要把每一项都估到足够精度,所需测量次数十分庞大。
这是 VQE 的真实开销之一,催生了大量缓解技术:把可对易的泡利串分组共同测量、依系数分配测量预算、采用经典阴影等。理解这一点,才能对 VQE 的实际可扩展性有清醒认识。
它擅长什么 又不擅长什么
- 面向 NISQ 而生。VQE 把深线路的负担转移到外层经典优化,线路相对浅,适配当前含噪硬件——这正是它流行的原因。
- 受拟设质量制约。若拟设无法表示真实基态,能量就停在一个偏高的上界;表达力与可训练性难以兼得。
- 测量开销巨大。泡利串数目随分子规模快速增长,采样成本常是实用化的主要障碍。
- 优化困难且受噪声侵蚀。贫瘠高原、局部极小,叠加硬件噪声对期望值的偏移,使收敛到化学精度颇具挑战。
- 优势尚未在实用规模上确证。对小分子,经典方法依然又快又准;VQE 何时、对哪些体系能真正胜出,仍是活跃的研究问题。
算法逐步拆解
- 由化学前端生成费米子哈密顿量,经 Jordan–Wigner 等映射转为泡利串之和。
- 选定参数化拟设
|ψ(θ)⟩(UCCSD 或硬件高效型)。 - 在量子线路上制备
|ψ(θ)⟩,逐项测量各泡利串以估计 E(θ)。 - 把 E(θ) 交给经典优化器,依变分原理向更低能量更新 θ。
- 重复直至收敛,得到的最低 E(θ) 即基态能量的最佳上界估计。
对照代码理解
下面把 H₂ 分子的哈密顿量直接写成泡利串之和——正是上文映射后的形态。每一项的系数 ck 与泡利串(如 spin.z(0) * spin.z(1))一一对应;此处用奇偶映射 (parity tapering) 已把比特数压到 2。
import qalgora
from qalgora import spin
# H2 molecule at 0.735 A bond length, mapped to 2 qubits (parity tapering)
hamiltonian = (-1.0524 + 0.3979 * spin.z(0) - 0.3979 * spin.z(1)
- 0.0112 * spin.z(0) * spin.z(1)
+ 0.1809 * spin.x(0) * spin.x(1))
这些系数是针对某一特定分子、基组与费米子-比特映射的示意数值(H₂、0.735 Å、最小基组、奇偶映射),并非通用的 VQE 哈密顿量;换一个分子、几何构型、基组或映射,得到的项就完全不同。
该 Hamiltonian 仅表示电子能量部分,未含核排斥项;总分子能量需另加该键长下的核排斥能。
这是 H₂ 二比特约化模型的教学型一参数 ansatz,可在 Hartree–Fock 参考态附近引入相关性:x(q[0]) 制备 Hartree–Fock 参考态 |10⟩,随后的 ry(theta, …) 加受控门用单个参数 θ 在参考态附近引入相关性。它体现 UCC 类 ansatz 的思想,但并不是完整的 UCCSD 线路;真实化学计算会采用更丰富的参数化。
@qalgora.kernel
def ansatz(theta: float):
q = qalgora.qvector(2)
x(q[0]) # Hartree-Fock reference |10>
ry(theta, q[1])
x.ctrl(q[1], q[0])
最后一步即变分循环:qalgora.vqe 内部反复制备拟设、测量哈密顿量得到 E(θ),并用 COBYLA 依变分原理把 θ 推向更低能量。返回的 energy 就是基态能量的上界估计。在理想无噪声且期望值精确估计的条件下,VQE 能量是基态能量的上界;实际硬件和有限采样下,该上界性质可能被噪声和统计误差破坏。
qalgora.vqe 驱动器,以及它所依赖的化学前端(qalgora.chemistry.*、molecular_hamiltonian、active_space、create_molecule)均属于规范接口;开源参考实现尚未内置这些功能,故下面代码仅作示意。你今天可以用代价最小化一页中的代价函数与优化器复现同一循环。
optimizer = qalgora.optimizers.COBYLA()
energy, params = qalgora.vqe(ansatz, hamiltonian, optimizer, parameter_count=1)
print(f"ground-state energy: {energy:.6f} Hartree")
# 电子能量约 -1.857 Ha;该 Hamiltonian 不含核排斥项
# (此键长下约 +0.72 Ha),合计约 -1.137 Ha 总能量扩展至更大分子
- 使用 Solvers 库的
adapt_vqe自动增长线路拟设。 - 随着量子比特数增加,将期望值计算卸载到
gpu后端。 - 批量计算多个键长,绘制完整解离曲线。
参考文献
- A. Peruzzo, J. McClean, P. Shadbolt, M.-H. Yung, X.-Q. Zhou, P. J. Love, A. Aspuru-Guzik, J. L. O'Brien, "A variational eigenvalue solver on a photonic quantum processor," Nat. Commun. 5, 4213 (2014). doi:10.1038/ncomms5213