Optimizers & Gradients
Variational algorithms pair a quantum cost function with a classical optimizer. The
qalgora-Q specification defines gradient-free and gradient-based optimizer interfaces; the open
reference build does not bundle them yet. Today, build the cost function from
qalgora.observe and drive it with a third-party optimizer (SciPy, NLopt,
scikit-optimize).
qalgora.optimizers.* and qalgora.gradients.* is a
documented specification interface; the open reference build does not include it. To run code
today, see the third-party optimizers section below — that is the
current recommended path.
Gradient-free optimizers (spec interface)
Need only the cost value — robust when gradients are noisy or unavailable. The
qalgora.optimizers interface below is specification-only; it is illustrative and not
runnable on the reference build.
# Spec interface — qalgora.optimizers is not in the reference build. Illustrative:
# opt = COBYLA()
# opt.max_iterations = 100
# opt.initial_parameters = [0.1, 0.1]
# energy, params = opt.optimize(dimensions=2, function=cost)Gradient-based optimizers (spec interface)
Converge faster when a gradient is available. Pair an optimizer with a gradient strategy. The
qalgora.optimizers / qalgora.gradients interfaces below are
specification-only.
# Spec interface — qalgora.optimizers / qalgora.gradients are not in the reference build:
# opt = LBFGS()
# grad = ParameterShift()
# energy, params = opt.optimize(dimensions=4, function=cost, gradient=grad)Specified optimizers & gradients (planned)
| Optimizer | Type |
|---|---|
COBYLA, NelderMead | Gradient-free |
LBFGS, GradientDescent, Adam, SGD | Gradient-based |
SPSA | Stochastic perturbation — noise-robust, cheap |
| Gradient | Method |
|---|---|
ParameterShift | Exact analytic gradient via shifted evaluations |
CentralDifference / ForwardDifference | Finite-difference approximations |
Parallel parameter-shift (planned)
The parameter-shift rule evaluates the circuit at ±π/2-shifted angles. Those evaluations are independent, so on a planned multi-QPU target they could run in parallel — one gradient costing little more than one cost evaluation. The multi-QPU backend below is planned (规划中) and not in the CPU reference build.
# Planned multi-QPU backend — not in the CPU reference build:
# qalgora.set_target("gpu", option="mqpu") # shard shifted evaluations
# grad = ParameterShift() # spec gradient interface
Third-party optimizers (recommended today)
The cost function is plain Python, so any external optimizer (SciPy, NLopt, scikit-optimize)
works — just hand it cost. This is the recommended path on the reference build today.
from scipy.optimize import minimize
result = minimize(cost, x0=[0.1, 0.1], method="COBYLA")
rx/ry/rz), not arbitrary gates. For those it evaluates the
circuit at shifted angles with no finite-difference truncation error.
优化器与梯度
变分算法将量子代价函数与经典优化器结合。qalgora-Q 规范设计了无梯度/基于梯度优化器接口;开放参考实现暂未内置;当前可用 SciPy 等第三方优化器 + qalgora.observe 构造代价函数。
qalgora.optimizers.* 与 qalgora.gradients.* 下的所有内容均属规范中的接口,开放参考实现并未包含。如需立即运行,请见下方第三方优化器一节——这是当前推荐路径。
无梯度优化器(规范接口)
只看代价函数的取值即可——梯度噪声大或者拿不到梯度时,这类方法更稳。下面的 qalgora.optimizers 接口仅为规范,参考实现上无法运行。
# 规范接口——qalgora.optimizers 未包含于参考实现,仅作说明:
# opt = COBYLA()
# opt.max_iterations = 100
# opt.initial_parameters = [0.1, 0.1]
# energy, params = opt.optimize(dimensions=2, function=cost)基于梯度的优化器(规范接口)
有梯度可用时收敛更快。把优化器和某种梯度策略搭配起来用即可。下面的 qalgora.optimizers / qalgora.gradients 接口仅为规范。
# 规范接口——qalgora.optimizers / qalgora.gradients 未包含于参考实现:
# opt = LBFGS()
# grad = ParameterShift()
# energy, params = opt.optimize(dimensions=4, function=cost, gradient=grad)规范的优化器与梯度(规划中)
| 优化器 | 类型 |
|---|---|
COBYLA, NelderMead | 无梯度 |
LBFGS, GradientDescent, Adam, SGD | 基于梯度 |
SPSA | 随机扰动——抗噪,计算开销小 |
| 梯度 | 方法 |
|---|---|
ParameterShift | 通过移位求值得到精确解析梯度 |
CentralDifference / ForwardDifference | 有限差分近似 |
并行参数移位(规划中)
参数移位规则在 ±π/2 移位角度处对线路求值,这些求值相互独立,因此在规划中的多 QPU 目标上可并行执行——一次梯度计算的开销几乎不超过一次代价函数求值。下面的多 QPU 后端属规划中(规划中),CPU 参考实现并不包含。
# 规划中的多 QPU 后端——CPU 参考实现暂未包含:
# qalgora.set_target("gpu", option="mqpu") # 分片移位求值
# grad = ParameterShift() # 规范梯度接口
第三方优化器(当前推荐)
代价函数是纯 Python 函数,因此任何外部优化器(SciPy、NLopt、scikit-optimize)均可直接使用——只需将 cost 传入即可。这是当前在参考实现上推荐的路径。
from scipy.optimize import minimize
result = minimize(cost, x0=[0.1, 0.1], method="COBYLA")
rx/ry/rz),并非任意门。对这类门,它通过在移位角度处对线路求值得到精确解析梯度,无有限差分截断误差。