哈密顿量模拟
概述
unitarylab_algorithms.hamiltonian_simulation 包提供五种近似哈密顿量 时演算符 的方法。除 Cartan 分解外,其余四种方法均对输入做统一的格式化校验(_format_system)并接受相同的核心输入(H、t、error),在近似策略、线路深度和精度保证上各有不同。
| 方法 | 类 | 策略 |
|---|---|---|
| Suzuki-Trotter 乘积公式演化算法 | TrotterAlgorithm | 乘积公式分解 |
| qDrift 算法 | QDriftAlgorithm | 随机乘积公式 |
| 泰勒级数哈密顿量模拟 | TaylorAlgorithm | 截断 Taylor 级数 |
| 量子信号处理哈密顿量模拟(QSP-HS) | QSPHSAlgorithm | 量子信号处理多项式 |
| Cartan 分解算法 | CartanDecompositionAlgorithm | Lie 代数 Cartan–Lax 流 |
命名易混淆提示:本类中的
QSPHSAlgorithm位于unitarylab_algorithms.hamiltonian_simulation.qsp.algorithm,用于哈密顿量时间演化近似;unitarylab_algorithms.linear_algebra包中还有一个同名文件夹qsp/,其中的QSPAlgorithm用于线性方程组求解(见 线性代数算法),二者功能完全不同,导入路径也不同,使用时请注意区分。
方法选择建议
| 场景 | 推荐方法 |
|---|---|
| 短时演化、简单哈密顿量 | Suzuki-Trotter(一阶或二阶) |
| 随机/概率模拟 | qDrift |
| 高精度、中等深度 | 泰勒级数 |
| 稀疏哈密顿量、长时演化 | QSP-HS |
| 实对称哈密顿量、精确分解 | Cartan 分解 |
统一说明:status 与 error 参数
- 本类 5 个算法的
.run()均在成功完成计算后无条件将self.status设为"success",且_build_return_dict()的成功实参恒为True——也就是说返回字典的顶层status永远是'ok',不反映近似误差是否达到error的预期水平。若需要评估精度,请自行比较返回结果中的Frobenius norm of error(或 Cartan 的Final total error)与传入的error阈值。 error在 QSP-HS、Taylor 和 Trotter 中参与精度或规模控制;qDrift 主要通过steps控制精度。
Suzuki-Trotter 乘积公式演化算法
背景
Trotter–Suzuki 乘积公式将 近似为 (一阶或更高阶)。该方法实现简单,是工程中最广泛使用的哈密顿量模拟方法。
一阶误差: 二阶(Suzuki):
导入
from unitarylab_algorithms import TrotterAlgorithm.run() 参数
def run(self, H: np.ndarray, t: float, error: float, order: int = 1, steps: int = 1000,
backend='torch', device='cpu', dtype=np.complex128)| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
H | np.ndarray | — (必填) | 厄米哈密顿量矩阵(方阵);非厄米或非方阵会抛出 ValueError;维度非 2 的幂时自动补零到最近的 2 的幂 |
t | float | — (必填) | 总演化时间 |
error | float | — (必填) | 目标近似误差;实际用于计算 steps 的上限,并非未使用的占位参数 |
order | int | 1 | Trotter–Suzuki 公式阶数(1 或高阶偶数) |
steps | int | 1000 | 时间步数 的上限——真实使用的步数是理论公式 与该上限之间取较小者, 为 的谱范数、 为比特数 |
返回值
circuit_path 是两个路径组成的列表:[完整重复线路路径, 单个时间切片线路路径],与本包其他 4 个算法(单一字符串)不同。
{
'status': 'ok', # 恒为 'ok',不反映真实近似精度
'circuit_path': ['/path/.../trotter_full.svg', '/path/.../trotter_slice.svg'],
'plot': [{'format': 'txt', 'filename': 'trotter_hamiltonian_simulation_algorithm_result.txt'}],
'circuit': <Circuit>, # 完整重复线路
'Approximate evolution matrix': array([...]),
'Exact evolution matrix': array([...]),
'Frobenius norm of error': 1.2e-05,
}示例
import numpy as np
from unitarylab_algorithms import TrotterAlgorithm
H = np.array([[2, 1], [1, 3]])
algo = TrotterAlgorithm()
result = algo.run(H=H, t=1.0, error=1e-8, order=1, steps=1000)
print(result['Frobenius norm of error'])快速演示
from unitarylab_algorithms.hamiltonian_simulation.trotter.algorithm import test
test()注意事项
steps实际是上限而非固定步数——真实步数由公式根据t、order、error、 的谱范数自动计算,并与传入的steps取较小值,因此传入很大的steps不一定会得到对应数量的步数。error参与步数上限的计算。order为高阶(如 2、4)时使用 Suzuki 递归公式(_recurse),理论上仅支持 1 或偶数阶。
qDrift 算法
背景
qDrift 是一种随机乘积公式,按系数量级对 Pauli 项进行采样,并以适当缩放角度应用它们。这产生了一个无偏的随机近似,当样本数 增大时收敛到精确演化。
门复杂度: ,其中
导入
from unitarylab_algorithms import QDriftAlgorithm.run() 参数
def run(self, H: np.ndarray, t: float, error: float, steps: int = 5000,
backend='torch', device='cpu', dtype=np.complex128)| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
H | np.ndarray | — (必填) | 厄米哈密顿量矩阵 |
t | float | — (必填) | 总演化时间 |
error | float | — (必填) | 目标近似误差——本包 5 个算法中唯一真正未被使用的 error,仅做 error > 0 的合法性校验 |
steps | int | 5000 | 随机样本数(越大精度越高,线路深度线性增长) |
返回值
{
'status': 'ok',
'circuit_path': '/path/to/qdrift_algorithm_circuit.svg',
'plot': [{'format': 'txt', 'filename': 'qdrift_algorithm_result.txt'}],
'circuit': <Circuit>,
'Approximate evolution matrix': array([...]),
'Exact evolution matrix': array([...]),
'Frobenius norm of error': 3.4e-03,
}示例
import numpy as np
from unitarylab_algorithms import QDriftAlgorithm
H = np.array([[2, 1], [1, 3]])
algo = QDriftAlgorithm()
result = algo.run(H=H, t=1.0, error=1e-8, steps=5000)
print(result['Frobenius norm of error'])快速演示
from unitarylab_algorithms.hamiltonian_simulation.qdrift.algorithm import test
test()注意事项
error用于合法性校验;steps是控制精度的关键参数。- 由于依赖随机采样(
np.random.choice,未设置固定种子),每次运行的具体线路和Frobenius norm of error都会不同,不可复现;如需复现结果,需要自行在调用前设置np.random.seed(...)。
泰勒级数哈密顿量模拟
背景
Taylor 方法将 展开为截断 Taylor 级数到 阶,然后使用酉算符线性组合(LCU)实现每一项。可以用 项达到精度 。
导入
from unitarylab_algorithms import TaylorAlgorithm.run() 参数
def run(self, H: np.ndarray, t: float, error: float, degree: int = 15,
backend='torch', device='cpu', dtype=np.complex128)| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
H | np.ndarray | — (必填) | 厄米哈密顿量矩阵 |
t | float | — (必填) | 总演化时间 |
error | float | — (必填) | 目标近似误差;真实参与有效阶数的计算,见下方注意事项 |
degree | int | 15 | Taylor 展开阶数上限;有效阶数会被硬性封顶在 15,即使传入更大的值也不会超过 15 |
网页端
parameters.json的默认值是d=10,与.run()自身的默认值degree=15不一致;Python API 调用若不显式传参,使用的是15。
返回值
{
'status': 'ok',
'circuit_path': '/path/to/taylor_hamiltonian_simulation_algorithm_circuit.svg',
'plot': [{'format': 'txt', 'filename': 'taylor_hamiltonian_simulation_algorithm_result.txt'}],
'circuit': <Circuit>, # LCU 电路对象(未 decompose)
'Approximate evolution matrix': array([...]),
'Exact evolution matrix': array([...]),
'Frobenius norm of error': 5.6e-06,
}示例
import numpy as np
from unitarylab_algorithms import TaylorAlgorithm
H = np.array([[2, 1], [1, 3]])
algo = TaylorAlgorithm()
result = algo.run(H=H, t=1.0, error=1e-8, degree=15)
print(result['Frobenius norm of error'])快速演示
from unitarylab_algorithms.hamiltonian_simulation.taylor.algorithm import test
test()注意事项
- 有效展开阶数由
degree = min(max(degree, ⌈1.5λ + 1.5·ln(1/error)⌉), 15)计算(),硬性上限为 15——传入degree=100实际仍按不超过 15 阶计算,这是当前实现的固有限制,而非配置疏漏。 error参与上述有效阶数计算。- 保存的线路图对应
circuit.decompose()(分解后的门序列),而返回值circuit字段是未分解的 LCU 电路对象,两者结构不同。
量子信号处理哈密顿量模拟(QSP-HS)
背景
基于 QSP 的哈密顿量模拟构造一个量子线路,通过将 的本征值编码为信号并应用一系列受控酉算符和单比特旋转,来近似时演算符 。对于稀疏哈密顿量,QSP 可达到近最优门复杂度。内部先对 做块编码(block_encode(H, method="nagy")),再通过 Chebyshev/Bessel 系数构造 、 两个分量,最后用 LCU 组合。
门复杂度:
导入
from unitarylab_algorithms import QSPHSAlgorithm该模块的类名为
QSPHSAlgorithm(区别于linear_algebra.qsp中同名文件夹下的QSPAlgorithm,参见本页顶部提示)。
.run() 参数
def run(self, H: np.ndarray, t: float, error: float, degree: int = 15, beta: float = 0.7,
backend='torch', device='cpu', dtype=np.complex128) -> Dict[str, Any]| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
H | np.ndarray | — (必填) | 厄米哈密顿量矩阵 |
t | float | — (必填) | 总演化时间;内部会根据 degree 自动选择时间切片数 time_slices(以 2 的幂递增),只构造一个代表性时间切片的 QSP 电路,再对该切片的演化矩阵使用 np.linalg.matrix_power(..., time_slices) 做经典矩阵幂合并;不会为每个切片分别构造线路 |
error | float | — (必填) | 目标近似误差;真实参与每个时间切片所需 QSP 阶数的估计 |
degree | int | 15 | QSP 多项式阶数上限;网页端 parameters.json 中该参数名为 d(而不是 degree),调用 Python API 时以 degree 为准 |
beta | float | 0.7 | 数值稳定性预条件因子,必须满足 ,否则抛出 ValueError |
返回值
{
'status': 'ok',
'circuit_path': '/path/to/qsp_hamiltonian_simulation_algorithm_circuit.svg',
'plot': [{'format': 'txt', 'filename': 'qsp_hamiltonian_simulation_algorithm_result.txt'}],
'circuit': <Circuit>, # 单个时间切片的 QSP+LCU 电路
'Approximate evolution matrix': array([...]),
'Exact evolution matrix': array([...]),
'Frobenius norm of error': 8.9e-07,
}示例
import numpy as np
from unitarylab_algorithms import QSPHSAlgorithm
H = np.array([[2, 1], [1, 3]])
algo = QSPHSAlgorithm()
result = algo.run(H=H, t=1.0, error=1e-8, degree=15, beta=0.7)
print(result['Frobenius norm of error'])快速演示
from unitarylab_algorithms.hamiltonian_simulation.qsp.algorithm import test
test()注意事项
error参与时间切片数与每切片所需阶数的估计(_estimate_required_degree)。- 网页端参数面板中该算法的阶数参数名为
d,与 Python.run()方法的degree形参名不一致,属于命名不统一,调用 Python API 时请使用degree。
Cartan 分解算法
背景
Cartan–Lax 流算法通过将 Lie 代数 分解为对称子代数 和反对称空间 ,来分解时演算符。所得线路形式为 。内部直接委托给 unitarylab.library.hamiltonian.hamiltonian_simulation(H, evol_time, method='cartan-lax', ...)。
要求: 必须是实对称矩阵(但与其余 4 个算法不同,本算法在 Python 层不做厄米/方阵/2 的幂维度的显式校验——校验发生在委托的底层 hamiltonian_simulation() 内部)。
导入
from unitarylab_algorithms import CartanDecompositionAlgorithm.run() 参数
def run(self, H: Union[np.ndarray, list], t: float, error: float,
backend: str = "torch", device: str = "cpu", dtype=np.complex128, **kwargs: Any)| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
H | np.ndarray | list | — (必填) | 实对称哈密顿量 |
t | float | — (必填) | 总演化时间 |
error | float | — (必填) | 非对角分量范数的停止容差,作为 target_error 传给底层求解器 |
evol_time(**kwargs) | float | t | 覆盖传给底层模拟器的演化时间 |
lr(**kwargs) | float | 1e-3 | Lax 流积分基础步长 |
max_steps(**kwargs) | int | 100000 | Lax 更新步数上限 |
reps(**kwargs) | int | 5000 | 自适应缩放前的迭代预算 |
返回值
返回的 output 字段名与其余 4 个算法完全不同(其余 4 个统一使用 Approximate evolution matrix/Exact evolution matrix/Frobenius norm of error,本算法则是):
{
'status': 'ok',
'circuit_path': '/path/to/cartan_algorithm_circuit.svg',
'plot': [{'format': 'txt', 'filename': 'cartan_algorithm_result.txt'}],
'circuit': <Circuit>,
'Evolution result': <底层模拟器返回的演化结果对象>,
'Final total error': 4.2e-08,
'Computation time (s)': 0.0345, # 唯一在 output 中包含计算耗时的算法
'Exact evolution': array([...]), # 精确演化矩阵 exp(-iHt),key 名也与其余算法(Exact evolution matrix)不同
}示例
import numpy as np
from unitarylab_algorithms import CartanDecompositionAlgorithm
H = np.array([[2, 1], [1, 2]])
algo = CartanDecompositionAlgorithm()
result = algo.run(H=H, t=1.0, error=1e-3)
print(result['Final total error'])快速演示
from unitarylab_algorithms.hamiltonian_simulation.cartan.algorithm import test
test()注意事项
- Cartan 方法目前仅支持矩阵形式的哈密顿量,暂不支持 Pauli 字符串输入。
- 对于病态哈密顿量,减小
lr并增大reps可以改善收敛性。 - 输出字段命名(
Evolution result/Final total error/Exact evolution)与其余 4 个算法(Approximate evolution matrix/Exact evolution matrix/Frobenius norm of error)不一致,混用多种方法时注意区分键名,不要假设所有hamiltonian_simulation算法共享统一的输出字段结构。