上一章: 量子信息
下一章: 量子性能分析
简介
哈密顿量,记作 H 是量子力学和经典力学中的一个基本概念。它表示系统的总能量,并且是系统中粒子的坐标和动量(或速度)的函数。
在经典力学中,哈密顿量由拉格朗日函数进行勒让德变换(Legendre transformation)得到,并通过哈密顿方程(Hamilton's equations of motion)描述系统的动力学。哈密顿方程比牛顿运动定律提供了一种更为广泛且更强大的粒子运动描述方法。
在量子力学中,哈密顿量对应于能量算子,其本征值代表系统可能的能级。描述量子系统随时间演化的薛定谔方程可由哈密顿量推导得出。
QPanda3 中使用 Hamiltonian
类实现对哈密顿量的抽象。
模拟
使用泡利算子模拟哈密顿量。
API文档链接
构造Hamiltonian对象
使用Python字典构造
Python 字典格式示例:{ "X0 Z1 ":2 + 0j, "X1 Y2 ":3 + 0j, }
。
从包含表示泡利算子的字符串及对应系数的字典构造 Hamiltonian
对象。
Python
Ham = Hamiltonian({"X0 Z1":2,"X1 Y2":3})
print("Ham\n",Ham)
执行结果如下所示:
Ham
{ qbit_total = 3, pauli_with_coef_s = { 'X0 Z1 ':2 + 0j, 'X1 Y2 ':3 + 0j, } }
从Python列表构造
列表格式示例:[("XXZ", [0, 1, 4], 1 + 2j), ("ZZ", [1, 2], -1 + 1j)]
。
从包含表示泡利算子的字符串、索引和系数的列表构造 Hamiltonian
对象。
Python
Ham = Hamiltonian([("XXZ", [0, 1, 4], 1 + 2j), ("ZZ", [1, 2], -1 + 1j)] )
print("Ham:\n",Ham)
执行结果如下所示:
Ham:
{ qbit_total = 5, pauli_with_coef_s = { 'X0 X1 Z4 ':1 + 2j, 'Z1 Z2 ':-1 + 1j, } }
从PauliOperator对象构造
Python
op = PauliOperator([("XXZ", [0, 1, 4], 1 + 2j), ("ZZ", [1, 2], -1 + 1j)] )
Ham = Hamiltonian(op)
print("Ham:\n",Ham)
执行结果如下所示:
Ham:
{ qbit_total = 5, pauli_with_coef_s = { 'X0 X1 Z4 ':1 + 2j, 'Z1 Z2 ':-1 + 1j, } }
从实数矩阵构造
Python
mat =[[0.77386158,0.29688609,0.74116861,0.67669958]
,[0.09515128,0.69212901,0.09219669,0.94389747]
,[0.72101242,0.41921056,0.55843845,0.92963435]
,[0.46651845,0.30803558,0.1703278,0.33562784]]
op = Hamiltonian(mat)
print("op:\n", op)
执行结果如下所示:
op:
{ qbit_total = 2, pauli_with_coef_s = { 'X0 ':0.373 + 0j, 'Y0 ':-0 -0.24026j, 'X0 Z1 ':-0.176981 + 0j, 'Y0 Z1 ':0 + 0.139393j, 'X1 ':0.678529 + 0j, 'Z0 X1 ':0.052562 + 0j, 'Y1 ':-0 -0.164005j, 'Z0 Y1 ':0 + 0.153926j, 'X0 X1 ':0.413656 + 0j, 'Y0 X1 ':-0 -0.134299j, 'X0 Y1 ':0 + 0.0292082j, 'Y0 Y1 ':-0.157953 + 0j, '':0.590014 + 0j, 'Z0 ':0.0761358 + 0j, 'Z1 ':0.142981 + 0j, 'Z0 Z1 ':-0.0352695 + 0j, } }
运算
支持加法、减法、乘法、标量数乘以及张量积运算。
说明
为了便于说明,这里约定了一些表述方式。这些表述方式与 PauliOperator
相同,将用于下边的运算规则说明。请参考 PauliOperator。
加法
API文档链接
使用规则
Hamiltonian + Hamiltonian
Python
H1 = Hamiltonian({"X0 Y1":1})
H2 = Hamiltonian({"X0 Y1": 2})
H3 = H1+H2
print("H3:\n",H3)
执行结果如下所示:
H3:
{ qbit_total = 2, pauli_with_coef_s = { 'X0 Y1 ':3 + 0j, } }
运算规则
- 相同项:相同项合并为一项,合并得到的项的系数由被合并项的系数相加得到。
- 不同项:保留不同项及其系数。
- 系数为 0 的项:删除该项。
减法
API文档链接
使用规则
Hamiltonian - Hamiltonian
Python
H1 = Hamiltonian({"X0 Y1":1})
H2 = Hamiltonian({"X0 Y1": 2})
H3 = H1-H2
print("H3:\n",H3)
执行结果如下所示:
H3:
{ qbit_total = 2, pauli_with_coef_s = { 'X0 Y1 ':3 + 0j, } }
运算规则
- 相同项:相同项合并为一项,合并得到的项的系数由被合并项的系数相减得到。
- 不同项:保留不同项及其系数。
- 系数为 0 的项:删除该项。
标量乘法
API文档链接
使用规则
Hamiltonian * Complex
Complex * Hamiltonian
Python
H1 = Hamiltonian({"X0 Y1":1})
H3 = (3.3+1.j) * H1
H4 = H1*(4.4+1.j)
print("H4:\n",H4)
执行结果如下所示:
H4:
{ qbit_total = 2, pauli_with_coef_s = { 'X0 Y1 ':4.4 + 1j, } }
运算规则
乘法
API文档链接
使用规则
Hamiltonian * Hamiltonian
Python
H1 = Hamiltonian({"X0 Z1":1})
H2 = Hamiltonian({"X0 Y1":1})
H3 = H1 * H2
print("H3:\n",H3)
执行结果如下所示:
H3:
{ qbit_total = 2, pauli_with_coef_s = { 'X1 ':0 -1j, } }
运算规则
单项与单项相乘
将仅包含单个项的 Hamiltonian
对象与另一个仅包含单个项的 Hamiltonian
对象相乘。
多项与多项相乘
多项 Hamiltonian
对象与多项 Hamiltonian
对象相乘。
遵循类似于多项式乘法的分配率。
Python
'''
H3 is equivalent to Hamiltonian({"X0": 1.1})*Hamiltonian({"Z0": 2.1})+Hamiltonian({"X0": 1.1})*Hamiltonian({"I0": 2.2})+Hamiltonian({"Y0": 1.2})*Hamiltonian({"Z0": 2.1})+Hamiltonian({"Y0": 1.2})*Hamiltonian({"I0": 2.2})
'''
H1 = Hamiltonian({"X0": 1.1,"Y0":1.2})
H2 = Hamiltonian({"Z0": 2.1,"I0":2.2})
H3=Hamiltonian({"Y0": -2.31j+2.64,"X0":2.52j+2.42})
print("H3:\n",H3)
执行结果如下所示:
H3:
{ qbit_total = 1, pauli_with_coef_s = { 'Y0 ':2.64 -2.31j, 'X0 ':2.42 + 2.52j, } }
- 应用加法规则、单项乘法规则和张量积运算规则。
张量积
API文档链接
使用规则
Hamiltonian tensor Hamiltonian
Python
H1 = Hamiltonian({"X0 Y1":1})
H2 = Hamiltonian({"X0 Y1":1})
H3 = H1.tensor(H2)
print("H3:\n",H3)
执行结果如下所示:
H3:
{ qbit_total = 4, pauli_with_coef_s = { 'X0 Y1 X2 Y3 ':1 + 0j, } }
运算规则
量子系统 Q1 对应哈密顿量 H1 的泡利算子字符串,其量子比特编号为 \(0...n-1\),其中 A1
作用于 Q1 的 k1
号量子比特。
量子系统 Q2 对应哈密顿量 H2 的泡利算子字符串,其量子比特编号为 \(0...m-1\),其中 A2
作用于 Q2 的 k2
号量子比特。
执行 H3 = H1.tensor(H2)
之后,量子系统 Q3 对应哈密顿量 H3,其量子比特编号为 \(0...(m+n-1)\),其中 A1
作用于 Q3 的 k1
号量子比特,A2
作用于 Q3 的 (n+k2)
号量子比特。
继续应用加法、乘法和标量数乘运算规则。
Python
H1=Hamiltonian({"Z0 Y3": 1})
H2=Hamiltonian({"Y2": 1})
H3=H1.tensor(H2)
print("H3:\n",H3)
执行结果如下所示:
H3:
{ qbit_total = 7, pauli_with_coef_s = { 'Z0 Y3 Y6 ':1 + 0j, } }
应用
计算期望值
在指定哈密顿量的量子系统上运行量子程序并计算期望值。
在 CPUQVM 上计算
在 CPUQVM 上计算量子程序在指定哈密顿量下的期望值。
Python
from pyqpanda3.core import CPUQVM, QProg, S, BARRIER,expval_hamiltonian
def test_expectation2():
qvm = CPUQVM()
prog = QProg()
prog << S(0)
prog << BARRIER([0, 1, 2])
"""The qubits used in qprog should be consistent with the qubits in the Pauli operator. The simulator determines the qubits that should be included in the quantum system based on the qbits in QProg, which are three quantum #bits in this case"""
Ham = Hamiltonian({"X0 Z1": 2, "X1 Y2": 3})
print("without I:\n", Ham)
try:
res = qvm.expval_hamiltonian(prog, Ham)
print("res:", res)
res2 = expval_hamiltonian(prog,Ham,backend='CPU')
print('res2:',res2)
except Exception as e:
print(e)
if __name__ == "__main__":
test_expectation2()
执行结果如下所示:
without I:
{ qbit_total = 3, pauli_with_coef_s = { 'X0 Z1 ':2 + 0j, 'X1 Y2 ':3 + 0j, } }
res: 0.0
res2: 0.0
Warrning! This interface will be deprecated in the future. Please use the new interface expval_hamiltonian (a global interface exported by pyqpanda3.core).
在 GPUQVM 上计算
在 GPUQVM 上计算量子程序在指定哈密顿量下的期望值。
Python
from pyqpanda3.core import CPUQVM, QProg, S, BARRIER,expval_hamiltonian
def test_expectation2():
qvm = CPUQVM()
prog = QProg()
prog << S(0)
prog << BARRIER([0, 1, 2])
"""The qubits used in qprog should be consistent with the qubits in the Pauli operator. The simulator determines the qubits that should be included in the quantum system based on the qbits in QProg, which are three quantum #bits in this case"""
Ham = Hamiltonian({"X0 Z1": 2, "X1 Y2": 3})
print("without I:\n", Ham)
try:
res2 = expval_hamiltonian(prog,Ham,backend='GPU')
print('res2:',res2)
except Exception as e:
print(e)
if __name__ == "__main__":
test_expectation2()
执行结果如下所示:
without I:
{ qbit_total = 3, pauli_with_coef_s = { 'X0 Z1 ':2 + 0j, 'X1 Y2 ':3 + 0j, } }
res2: 0.0
在 QPU 上计算
在 QPU 上计算量子程序在指定哈密顿量下的期望值。请参考量子云服务(Quantum Cloud Service)了解更多关于 originqc 云服务的信息。
API文档链接
Python
def test_qpu_hamiltonian_expectation()->bool:
prog = QProg()
prog << X(0)<<CNOT(0,1)<<SWAP(1,2)
Ha = Hamiltonian({"X0 Y1":1.1,"X0 Z1 I2":2.1,"I1 Z2":3.1})
test_url = "https://qcloud.originqc.com.cn/zh/computerServices"
api_key = "Your correct api key"
service = QCloudService(api_key, test_url)
backend = service.backend("origin_wukong")
try:
res = backend.expval_hamiltonian(prog,Ha,QCloudOptions())
print("res:", res)
except Exception as e:
print(e)
return True
if __name__ =="__main__":
test_qpu_hamiltonian_expectation()
执行结果如下所示:
- 注解
- 结果的数值可能会有波动。
在 originqc 云端的全振幅模拟器上计算
在 originqc 云端的全振幅模拟器上计算量子程序在指定哈密顿量下的期望值。请参考量子云服务(Quantum Cloud Service)了解更多关于 originqc 云服务的信息。
API文档链接
Python
def test_qpu_hamiltonian_expectation()->bool:
prog = QProg()
prog << X(0)<<CNOT(0,1)<<SWAP(1,2)
Ha = Hamiltonian({"X0 Y1":1.1,"X0 Z1 I2":2.1,"I1 Z2":3.1})
test_url = "https://qcloud.originqc.com.cn/zh/computerServices"
api_key = "Your correct api key"
service = QCloudService(api_key, test_url)
backend = service.backend("full_amplitude")
try:
res = backend.expval_hamiltonian(prog, Ha)
print("res:", res)
except Exception as e:
print(e)
return True
if __name__ =="__main__":
test_qpu_hamiltonian_expectation()
执行结果如下所示:
其它
获取对应的矩阵
API doc for pyqpanda3.hamiltonian.Hamiltonian.matrix
Python
op = Hamiltonian(pauli_with_coef_s = {
'X0 ':0.373 + 0j, 'Y0 ':-0 -0.24026j, 'X0 Z1 ':-0.176981 + 0j, 'Y0 Z1 ':0 + 0.139393j,
'X1 ':0.678529 + 0j, 'Z0 X1 ':0.052562 + 0j, 'Y1 ':-0 -0.164005j, 'Z0 Y1 ':0 + 0.153926j,
'X0 X1 ':0.413656 + 0j, 'Y0 X1 ':-0 -0.134299j, 'X0 Y1 ':0 + 0.0292082j, 'Y0 Y1 ':-0.157953 + 0j,
'':0.590014 + 0j, 'Z0 ':0.0761358 + 0j, 'Z1 ':0.142981 + 0j, 'Z0 Z1 ':-0.0352695 + 0j, }
)
mat = op.matrix()
print('mat:\n',mat)
执行结果如下所示:
mat:
[[0.7738613+0.j 0.296886 +0.j 0.74117 +0.j 0.6766998+0.j]
[0.095152 +0.j 0.6921287+0.j 0.0921958+0.j 0.943898 +0.j]
[0.721012 +0.j 0.4192102+0.j 0.5584383+0.j 0.929634 +0.j]
[0.4665182+0.j 0.308036 +0.j 0.170328 +0.j 0.3356277+0.j]]