Skip to Content

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() 组合。

Nagy 数值精度

Nagy 方法先归一化并补齐输入矩阵,再构造酉扩张。当前实现通过同一次奇异值分解 计算两侧缺陷矩阵:

这里 是归一化并补齐后的矩阵,max 对奇异值对应的对角元素逐项截断,避免舍入导致负数开方。两侧共用同一组奇异值及平方根,修复分别构造缺陷矩阵时的一致性精度问题,尤其应关注奇异值接近 1 的情况。内部缺陷矩阵计算使用复双精度;这不表示任意输入、精度或后端都有固定的误差保证。

block_encode() 的调用方式、归一化因子 alpha 和 BlockEncodingResult 的公共字段不变;eps 仍不控制 Nagy 方法的误差。小矩阵可同时检查完整扩张的酉性与恢复后的输入矩阵:

U = encoded.get_unitary_matrix() unitarity_error = np.max(np.abs(U.conj().T @ U - np.eye(U.shape[0]))) restored = encoded.get_encoded_matrix() print(unitarity_error) assert np.allclose(restored, A, atol=1e-10)

get_encoded_matrix() 返回已乘回 alpha 的输入矩阵近似值;完整酉矩阵左上块对应归一化后的矩阵。unitarity_error 与矩阵恢复误差是不同的检查项。

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 的次数随逼近精度和函数形状增加;先用低阶、小矩阵验证。
  • 组合块编码时必须保证相邻结果的矩阵维度兼容。

相关页面

最后更新于