QSP、QSVT 与块编码
块编码把矩阵嵌入酉矩阵;QSP 和 QSVT 则在该表示上实现多项式或标量函数变换。本页区分低层线路构造与高层结果对象。
块编码
import numpy as np
from unitarylab.library import block_encode
A = np.array([[0.5, 0.1], [0.2, 0.3]], dtype=complex)
encoded = block_encode(A, method="nagy")
print(encoded.circuit)
print(encoded.alpha)
print(encoded.total_qubits, encoded.target_qubits)block_encode(matrix, method="nagy", eps=1e-3, verbose=False)| 方法 | 特点 | 参数 |
|---|---|---|
nagy | 基于扩张的块编码,适合一般小矩阵 | eps 不参与该方法 |
fable | 快速近似块编码,可压缩小旋转项 | eps 为压缩阈值,None 表示不压缩 |
输入可为二维数组、列表或元组;一维输入会按列向量处理。BlockEncodingResult 包含 circuit、alpha、total_qubits、target_qubits、method、matrix 和 eps,并提供 get_encoded_matrix()、get_unitary_matrix()、get_max_error()。多个结果可使用 unitarylab.library.block_encoding.chain_matmul() 组合。
QSVT
import numpy as np
from unitarylab.library import QSVT
H = np.array([[0.8, 0.0], [0.0, 0.4]], dtype=complex)
result = QSVT(
H,
function=lambda x: x * x,
target_error=1e-5,
block_encoding_method="nagy",
backend="torch",
device="cpu",
)
print(result.degree)
print(result.circuit)
print(result.total_error)高层入口签名为:
QSVT(H, function, target_error=1e-6,
block_encoding_method="nagy",
backend="torch", device="cpu", dtype=np.complex128)H 必须为厄米矩阵,function 应能接受 NumPy 标量或数组,target_error 必须为正。返回的 QSVTResult 提供 H、function、degree、circuit、total_error 和 evolution_result;后三项按需计算。需要分阶段控制拟合时可使用 unitarylab.library._qsvt.algorithm.QSVTHermitian。
低层 QSP
QSP(U, n, m, coef, parity,
eps=1e-12, maxiter=200, is_coef_cheby=False)U:待变换的信号酉线路。n:目标寄存器量子比特数;m:多项式阶数相关参数。coef:多项式系数;parity:目标多项式奇偶性。is_coef_cheby=False时按普通多项式系数处理;设为True时按 Chebyshev 系数处理。eps和maxiter控制相位求解收敛。
哈密顿量专用封装返回五元组:
circuit, factor, n_ancilla, n_qubits, degree = QSP_hamiltonian_simulation(
U_H, n, alpha, m, t, epsilon, beta, flag
)其中 U_H 是 的块编码线路,flag 控制构造分支。输入尺度必须与块编码归一化因子 alpha 一致,否则变换不代表目标哈密顿量。
约束与成本
- 块编码矩阵必须为有限二维数值;未知方法会触发
ValueError。 - 构造完整酉矩阵和误差会显著增加内存消耗。
- QSP/QSVT 的次数随逼近精度和函数形状增加;先用低阶、小矩阵验证。
- 组合块编码时必须保证相邻结果的矩阵维度兼容。
相关页面
最后更新于