Numerical Integrators
Choose how the Schrödinger or master equation is integrated in time — trading accuracy against cost.
Selecting an integrator
The snippets below are illustrative; construct the Hamiltonian and initial state for your own model.
import qalgora
from qalgora.dynamics import integrators
import numpy as np
psi0 = np.array([1.0, 0.0], dtype=complex)
result = qalgora.evolve(
hamiltonian, dimensions={0: 2},
schedule=qalgora.Schedule(steps=np.linspace(0, 1, 200), parameters=["t"]),
initial_state=qalgora.State.from_data(psi0),
integrator=integrators.RungeKutta(order=4, max_step=1e-3),
)
Available integrators
Fixed-step solvers are cheapest and predictable; adaptive solvers control local error automatically; the differentiable Torch family lets gradients flow through the whole trajectory for optimal-control and machine-learning loops.
| Integrator | Family | Use case | Status |
|---|---|---|---|
RungeKutta(order=1|2|4) | fixed step | general-purpose, default order 4 | reference impl |
Euler / Midpoint | fixed step | cheap, low-order baselines | reference impl |
CrankNicolson | fixed step, implicit | stable for stiff master equations | reference impl |
MagnusExpansion | fixed step, geometric | strongly time-dependent drives, stays unitary | reference impl |
ScipyZvode | adaptive, CPU | stiff systems, tight tolerances | reference impl |
TorchDiffEqDopri5 / Dopri8 | adaptive, GPU, differentiable | high-accuracy GPU runs, gradients | spec — not in reference impl |
TorchDiffEqBosh3 / AdaptiveHeun | adaptive, GPU, differentiable | lower-order adaptive GPU stepping | spec — not in reference impl |
TorchDiffEqRK4 / Euler / Midpoint | fixed, GPU, differentiable | fixed-step on GPU with autodiff | spec — not in reference impl |
TorchDiffEqExplicitAdams / ImplicitAdams | multistep, GPU, differentiable | smooth long-time evolutions | spec — not in reference impl |
Tolerances and step control
Adaptive integrators take relative and absolute tolerances; fixed-step ones take a step size.
from qalgora.dynamics import integrators
# adaptive: control error, let the solver pick the step
integ = integrators.TorchDiffEqDopri5(atol=1e-8, rtol=1e-6)
# fixed: you set the step explicitly
integ = integrators.RungeKutta(order=4, max_step=1e-3)Differentiable evolution for control
A Torch-based integrator keeps the trajectory in the autograd graph, so you can differentiate a final-state cost with respect to pulse parameters and optimise a control directly.
import torch
amp = torch.tensor(0.5, requires_grad=True) # a control knob
def cost(amp):
H = operators.spin.z(0) + operators.scalar(lambda t: amp) * operators.spin.x(0)
r = qalgora.evolve(H, dimensions={0: 2}, schedule=schedule, initial_state=psi0,
integrator=integrators.TorchDiffEqDopri5(),
observables=[operators.spin.z(0)])
# expectation_values() has shape [n_observables, n_steps];
# [0] selects the first observable, [-1] its value at the final time
return r.expectation_values()[0][-1] # final <Z>
cost(amp).backward() # gradient flows back to ampmax_step
until the expectation values stop changing, then back off slightly for speed; for adaptive
solvers, tighten atol/rtol instead.
数值积分器
选择薛定谔方程或主方程的时间积分方式,在精度与开销之间取舍。
选择积分器
以下为片段示例,哈密顿量/初态需按模型自行构造。
import qalgora
from qalgora.dynamics import integrators
import numpy as np
psi0 = np.array([1.0, 0.0], dtype=complex)
result = qalgora.evolve(
hamiltonian, dimensions={0: 2},
schedule=qalgora.Schedule(steps=np.linspace(0, 1, 200), parameters=["t"]),
initial_state=qalgora.State.from_data(psi0),
integrator=integrators.RungeKutta(order=4, max_step=1e-3),
)
可用积分器
定步长求解器开销最小、行为可预测;自适应求解器会自动控制局部误差;可微的 Torch 系列让梯度贯穿整条轨迹,专为最优控制与机器学习回路而设。
| 积分器 | 类别 | 适用 | 状态 |
|---|---|---|---|
RungeKutta(order=1|2|4) | 定步长 | 通用,默认四阶 | 参考实现已支持 |
Euler / Midpoint | 定步长 | 低阶廉价基线 | 参考实现已支持 |
CrankNicolson | 定步长,隐式 | 刚性主方程下稳定 | 参考实现已支持 |
MagnusExpansion | 定步长,几何 | 强含时驱动,保持幺正性 | 参考实现已支持 |
ScipyZvode | 自适应,CPU | 刚性系统,严格容差 | 参考实现已支持 |
TorchDiffEqDopri5 / Dopri8 | 自适应,GPU,可微 | GPU 上的高精度求解与梯度 | 规范接口·参考实现暂未包含 |
TorchDiffEqBosh3 / AdaptiveHeun | 自适应,GPU,可微 | GPU 上的低阶自适应步进 | 规范接口·参考实现暂未包含 |
TorchDiffEqRK4 / Euler / Midpoint | 定步长,GPU,可微 | GPU 上的定步长,带自动微分 | 规范接口·参考实现暂未包含 |
TorchDiffEqExplicitAdams / ImplicitAdams | 多步,GPU,可微 | 平滑的长时演化 | 规范接口·参考实现暂未包含 |
容差与步长控制
自适应积分器接收相对容差和绝对容差,定步长积分器则接收步长。
from qalgora.dynamics import integrators
# 自适应 控制误差 由求解器自行选步
integ = integrators.TorchDiffEqDopri5(atol=1e-8, rtol=1e-6)
# 定步长 你显式设定步长
integ = integrators.RungeKutta(order=4, max_step=1e-3)用于控制的可微演化
基于 Torch 的积分器会把整条轨迹保留在自动微分图中,于是你可以对脉冲参数求终态代价的梯度,直接优化一段控制。
import torch
amp = torch.tensor(0.5, requires_grad=True) # 一个控制旋钮
def cost(amp):
H = operators.spin.z(0) + operators.scalar(lambda t: amp) * operators.spin.x(0)
r = qalgora.evolve(H, dimensions={0: 2}, schedule=schedule, initial_state=psi0,
integrator=integrators.TorchDiffEqDopri5(),
observables=[operators.spin.z(0)])
# expectation_values() 形状为 [n_observables, n_steps]
# [0] 选第一个可观测量 [-1] 取其终时刻的值
return r.expectation_values()[0][-1] # 终态 <Z>
cost(amp).backward() # 梯度回流到 ampmax_step 直到期望值不再变化,再略微放宽以提速;对自适应求解器,则改为收紧 atol/rtol。