qalgora-Q Docs Hub量子文档 ✦ Ask AI✦ 问问文档

VQE with Gradients, Active Spaces & Gate Fusion

◐ Design-level API
This page documents qalgora-Q API design, architecture, or adaptation workflows. Code examples illustrate intended usage and are not guaranteed to run in the current reference implementation.

Three techniques that make VQE practical on larger molecules: analytic gradients, active-space reduction, and gate fusion. We first build intuition for what problem each one solves, then show how qalgora-Q switches it on in a single line of code.

The problem VQE solves

We want the ground-state energy of a molecule — the energy of its most stable electronic arrangement. Diagonalising the Hamiltonian H directly blows up exponentially with the number of electrons on a classical machine, so it quickly becomes intractable. VQE takes a different route: pick a trial state |ψ(θ)⟩ controlled by parameters θ (the ansatz), prepare it on a quantum device and measure its energy E(θ) = ⟨ψ(θ)|H|ψ(θ)⟩, then use a classical optimiser to tune θ and push that number down.

The reason this works is the variational principle: for any normalised trial state, the energy expectation can never fall below the true ground-state energy,

E(θ) = ⟨ψ(θ)|H|ψ(θ)⟩ ≥ E0.

In other words, the energy is a downhill slope with a floor — you can never sink through the true value, only approach it from above. Finding the ground state therefore becomes a minimisation problem: drive E(θ) down as far as it will go.

The intuition — let the quantum machine measure, let the classical one search

VQE is a hybrid loop. The quantum processor does what it is good at — preparing an entangled many-electron state and returning a single number, its energy. The classical optimiser does what it is good at — finding a downhill direction in parameter space. The catch is that this loop may have to run hundreds or thousands of times, with many shots taken on the quantum device every time. The three techniques on this page attack that cost from three angles: how to find the downhill direction more cleverly, how to make the problem itself smaller, and how to make each evaluation faster.

The math of gradients — the parameter-shift rule

To go downhill efficiently you need the gradient ∇θE. You cannot backpropagate through a quantum circuit the way you would through an ordinary program, because the intermediate states are not observable. But for the common rotation gates (whose generators have eigenvalues ±1/2) the gradient has an exact closed form — the parameter-shift rule:

∂E/∂θk = ½ [ E(θk + π/2) − E(θk − π/2) ]

The ½ assumes the standard rotation convention RP(θ) = exp(−iθP/2) (generator eigenvalues ±½), as used by qalgora-Q's rx/ry/rz; a different generator normalisation changes the coefficient.

Note that this is not a finite-difference approximation: shifting one angle of the same circuit by exactly ±π/2 and taking half the difference of the two energies yields the true analytic derivative. The price is two extra circuit runs per parameter, but the parameters are independent and can be evaluated in parallel. Compared with a blunt numerical difference it has no truncation error and is far more robust to noise — which is exactly what lets the optimisation converge reliably.

Why not finite differences
Finite differences force you to choose a step size: too large introduces bias, too small drowns in sampling noise. For this class of gates the parameter-shift rule gives a mathematically exact gradient with no step-size knob to tune, so it behaves far more stably under noisy real-world measurements.

Mechanism one — active-space reduction

In any molecule most orbitals are either fully occupied or completely empty and barely take part in chemistry. Freeze these chemically inactive orbitals and explicitly simulate only the handful of electrons and orbitals near the Fermi level that are genuinely correlated, and you can cut the qubit count dramatically while losing almost no accuracy. A smaller active space means shallower circuits, fewer parameters, and cheaper sampling — the most direct lever for pushing VQE from toy molecules toward real ones.

Mechanism two — measurement grouping and gate fusion

A Hamiltonian is usually written as a sum of many Pauli strings, each of which in principle has to be measured separately. Sorting terms that commute and can be measured simultaneously into a single group lets one measurement setting estimate several expectation values at once, driving down the number of circuit runs needed. Gate fusion speeds things up from the other end: adjacent single- and two-qubit gates are pre-merged into one equivalent dense little matrix, so the simulator does fewer matrix multiplications per step. Neither changes the result — they just make every energy evaluation cheaper.

The honest limits

  • The variational principle only guarantees "not below" the true value, not "close enough". The quality of the result is bounded by the expressivity of the ansatz: if the form of |ψ(θ)⟩ simply cannot reach the true ground state, no amount of optimisation will do better than settle at an energy that is too high.
  • The optimisation landscape can be rugged. More parameters bring local minima and "barren plateaus" — regions where the gradient is near zero everywhere and no downhill direction can be discerned.
  • Sampling noise is real. Every energy value comes from a finite number of measurements and carries statistical fluctuation of its own; the parameter-shift rule gives an unbiased gradient, but each component still needs enough shots to be stable.
  • The three techniques save cost, they don't perform magic. They widen the range of molecules VQE can reach, but they do not change any of the fundamental constraints above.

Reading it in code

The three snippets below line up exactly with the three mechanisms above. ParameterShift() is that exact-gradient rule, handed to the quasi-Newton optimiser LBFGS to walk downhill; active_space(...) confines the simulation to 4 electrons in 4 orbitals around the Fermi level, shrinking the problem; and set_simulation_option("gate_fusion", level=4) turns on gate fusion to accelerate every energy evaluation. Stacked together, they let VQE reach larger molecules that a naive loop could never touch.

Parallel parameter-shift gradients

import qalgora
grad = qalgora.gradients.ParameterShift()
opt = qalgora.optimizers.LBFGS()
energy, params = qalgora.vqe(ansatz, hamiltonian, opt,
                             parameter_count=n, gradient=grad)
Specification API — not in the open reference build yet
This example shows a qalgora-Q specification API (or a third-party library) that the open reference build does not bundle today. It documents the intended interface; to run code now, use the reference build’s supported core API.

Active space reduction

Freeze chemically-inactive orbitals so only the correlated electrons are simulated — fewer qubits for the same accuracy.

# keep 4 electrons in 4 orbitals around the Fermi level
h_active = qalgora.chemistry.active_space(molecule,
                                          n_active_electrons=4,
                                          n_active_orbitals=4)
Specification API — not in the open reference build yet
This example shows a qalgora-Q specification API (or a third-party library) that the open reference build does not bundle today. It documents the intended interface; to run code now, use the reference build’s supported core API.

Gate fusion for larger circuits

qalgora.set_simulation_option("gate_fusion", level=4)
Specification API — not in the open reference build yet
This example shows a qalgora-Q specification API (or a third-party library) that the open reference build does not bundle today. It documents the intended interface; to run code now, use the reference build’s supported core API.
Stack them
Active space shrinks the qubit count, parameter-shift gives exact gradients, and gate fusion speeds each evaluation — together they push VQE to molecules a naive loop can't reach.

VQE:梯度、活性空间与门融合

◐ 设计接口
本页描述的是 qalgora-Q 的接口设计、架构设计或适配工作流。相关代码用于说明预期用法,当前参考实现不保证可以直接运行。

三项让 VQE 在更大分子上切实可行的技术:解析梯度、活性空间压缩与门融合。先理解它们各自要解决什么问题,再看 qalgora-Q 如何用一行代码把它们打开。

变分量子本征求解器要解决的问题

我们想知道一个分子的基态能量——电子排布最稳定时的那个能量。直接对角化哈密顿量 H 在经典上随电子数指数级膨胀,很快就算不动了。VQE 换一条路:选一个由参数 θ 控制的试探态 |ψ(θ)⟩(即线路拟设),在量子机上制备它并测量能量 E(θ) = ⟨ψ(θ)|H|ψ(θ)⟩,再用经典优化器调 θ 把这个数往下压。

之所以这样行得通,靠的是变分原理:对任意归一化的试探态,其能量期望都不会低于真实基态能量,

E(θ) = ⟨ψ(θ)|H|ψ(θ)⟩ ≥ E0

换句话说,能量是一道有底的下坡——你永远不会"跌穿"真值,只会从上方逼近它。于是求基态就变成了一道最小化问题:把 E(θ) 降到不能再降。

直觉 把测量交给量子 把搜索交给经典

VQE 是一套混合循环:量子处理器负责它擅长的事——制备纠缠的多电子态并给出能量这一个数;经典优化器负责它擅长的事——在参数空间里找下坡方向。难点在于这个循环可能要跑成百上千次,每次都要在量子机上多次采样。本页的三项技术,分别从三个角度削减这笔开销:怎样更聪明地求下坡方向、怎样让问题本身变小、怎样让每次求值更快。

梯度的数学 参数移位规则

要高效下坡就得知道梯度 ∇θE。在量子线路上你无法像普通程序那样反向传播,因为中间态不可观测。但对常见的旋转门(生成元本征值为 ±1/2),梯度有一个精确的闭式表达——这就是参数移位规则

∂E/∂θk = ½ [ E(θk + π/2) − E(θk − π/2) ]

此处的 ½ 假设采用标准旋转门约定 RP(θ) = exp(−iθP/2)(生成元本征值 ±½),与 qalgora-Q 的 rx/ry/rz 一致;若生成元归一化不同,系数也会改变。

注意它不是有限差分近似:把同一条线路的某个角度精确地正负移动 π/2,两次能量之差的一半就给出真实的解析导数。代价是每个参数要额外跑两条线路,但每个参数彼此独立,可以并行求值。相比一刀切的数值差分,它无截断误差、对噪声更稳健,这也是优化能可靠收敛的关键。

为什么不用有限差分
有限差分要选步长:步长太大引入偏差,太小则被采样噪声淹没。参数移位规则对这一类门给出的是数学上严格的梯度,没有步长这个旋钮要调,因此在含噪的真实测量下表现稳定得多。

机制一 活性空间压缩

一个分子里大多数轨道要么填满、要么空着,几乎不参与化学变化。把这些化学上不活跃的轨道冻结,只显式模拟费米面附近真正发生关联的那几个电子和轨道,就能在几乎不损失精度的前提下大幅减少量子比特数。活性空间越小,线路越浅、参数越少、采样越省——这是把 VQE 从玩具分子推向真实分子最直接的杠杆。

机制二 测量分组与门融合

哈密顿量通常写成许多泡利串之和,每一项原则上都要单独测量。把彼此对易、可同时测量的项分到一组,就能用一次测量设置同时估计多项期望,把所需的线路次数压下来。门融合则从另一头加速:把相邻的若干单/双比特门预先合并成一个等价的稠密小矩阵,模拟器每步要做的矩阵乘法就更少。两者都不改变结果,只是让每一次能量求值更便宜。

诚实的边界

  • 变分原理只保证不低于真值,不保证够接近。结果的好坏受限于拟设的表达力:如果 |ψ(θ)⟩ 的形式根本够不到真实基态,再怎么优化也只会停在一个偏高的能量上。
  • 优化地形可能崎岖。参数多了会出现局部极小,以及"贫瘠高原"——梯度处处近乎为零,下坡方向无从辨认。
  • 采样噪声真实存在。每个能量值都来自有限次测量,本身带统计涨落;参数移位规则给的是无偏梯度,但每个分量仍要足够多的样本才稳。
  • 三项技术是省钱而非变魔术。它们扩大了 VQE 能触及的分子范围,却改变不了上面这些根本约束。

对照代码理解

下面三段代码正好对应上面三个机制。ParameterShift() 就是那条精确梯度规则,交给 LBFGS 这个拟牛顿优化器去走下坡;active_space(...) 把模拟限制在费米面附近的 4 电子 4 轨道,缩小问题规模;set_simulation_option("gate_fusion", level=4) 则打开门融合,加速每一次能量求值。把它们叠加起来用,VQE 就能伸向朴素循环够不到的更大分子。

并行参数移位梯度

import qalgora
grad = qalgora.gradients.ParameterShift()
opt = qalgora.optimizers.LBFGS()
energy, params = qalgora.vqe(ansatz, hamiltonian, opt,
                             parameter_count=n, gradient=grad)
规范接口 · 参考实现暂未包含
此示例展示的是 qalgora-Q 规范中的接口(或第三方库),开放参考实现目前尚未内置,仅用于说明预期用法;如需立即运行,请使用参考实现已支持的核心 API。

活性空间压缩

冻结化学上不活跃的轨道,仅对关联电子进行模拟——在相同精度下使用更少的量子比特。

# keep 4 electrons in 4 orbitals around the Fermi level
h_active = qalgora.chemistry.active_space(molecule,
                                          n_active_electrons=4,
                                          n_active_orbitals=4)
规范接口 · 参考实现暂未包含
此示例展示的是 qalgora-Q 规范中的接口(或第三方库),开放参考实现目前尚未内置,仅用于说明预期用法;如需立即运行,请使用参考实现已支持的核心 API。

大规模线路的门融合

qalgora.set_simulation_option("gate_fusion", level=4)
规范接口 · 参考实现暂未包含
此示例展示的是 qalgora-Q 规范中的接口(或第三方库),开放参考实现目前尚未内置,仅用于说明预期用法;如需立即运行,请使用参考实现已支持的核心 API。
组合使用
活性空间缩减量子比特数,参数移位提供精确梯度,门融合加速每次求值——三者结合,让 VQE 能够处理朴素循环无法触及的更大分子。