上一章: 量子可视化
下一章: 变分量子线路
简介
泡利算子是量子力学中的一个重要概念,具有广泛的应用前景。泡利算子的数学形式是一个矩阵,通常表示为 \(S = (Sx, Sy, Sz)\),其中 \(Sx\)、 \(Sy\)、 \(Sz\) 分别代表自旋在 \(x\)、 \(y\)、 \(z\) 方向上的分量。这四个分量(包括一个单位矩阵)可以方便地应用于量子力学中的计算和推导。
具体来说,泡利算子是一组 \(2×2\) 的酉矩阵,也是厄米矩阵(Hermitian matrices)。这四个分量分别对应以下量子逻辑门:
- X 门( \(x\) 方向分量)
- Y 门( \(y\) 方向分量)
- Z 门( \(z\) 方向分量)
- I(单位矩阵)
为了区分 X、Y、Z 门与这四个门的组合,我们将这四个门称为基本泡利算子,而将这四个门的组合泡利算子的组合。
泡利算子的组合可以用于表达复杂的物理概念,例如,哈密顿量可以使用基本泡利算子的组合进行模拟。
在下文中,我们使用 \(\sigma_X\) \(\sigma_Y\) \(\sigma_Z\) \(\sigma_f\) 分别表示 X、Y、Z 和 I 对应的矩阵。
酉矩阵
泡利算子在标准基下的酉矩阵表示如下:
\[\sigma_X=
\begin{bmatrix}
0 & 1 \\
1 & 0
\end{bmatrix}
\]
\[\sigma_Y=
\begin{bmatrix}
0 & -i \\
i & 0
\end{bmatrix}
\]
\[\sigma_Z=
\begin{bmatrix}
1 & 0 \\
0 & -1
\end{bmatrix}
\]
\[\sigma_I=
\begin{bmatrix}
1 & 0 \\
0 & 1
\end{bmatrix}
\]
矩阵乘法
基本泡利运算子之间的矩阵乘法操作。下面的 \( = \) 表示数学公式中的等号
自乘运算(XX、YY、ZZ)
泡利算子自乘的结果为单位矩阵:
\[\sigma_X\sigma_X=\sigma_I
\]
\[\sigma_Y\sigma_Y=\sigma_I
\]
\[\sigma_Z\sigma_Z=\sigma_I
\]
与单位矩阵相乘(X/Y/Z 和 I)
泡利算子与单位矩阵相乘时,其值不变:
\[\sigma_X\sigma_I=\sigma_I\sigma_X=\sigma_X
\]
\[\sigma_Y\sigma_I=\sigma_I\sigma_Y=\sigma_Y
\]
\[\sigma_Z\sigma_I=\sigma_I\sigma_Z=\sigma_Z
\]
泡利算子之间的乘法(X/Y/Z 之间的乘法)
\[\sigma_X\sigma_Y=i\sigma_Z
\]
\[\sigma_Y\sigma_X=-i\sigma_Z
\]
\[\sigma_X\sigma_Z=-i\sigma_Y
\]
\[\sigma_Z\sigma_X=i\sigma_Y
\]
\[\sigma_Y\sigma_Z=i\sigma_X
\]
\[\sigma_Z\sigma_Y=-i\sigma_X
\]
泡利算子的模拟
QPanda3 使用 PauliOperator
类来模拟基本泡利算子及其组合。为了模拟泡利算子作用于量子系统的方式,PauliOperator
类会为每个基本泡利算子分配一个量子比特索引。在 QPanda3 中,每个 PauliOperator
对象都可以通过 QCircuit
量子线路进行等效表达。
X/Y/Z/I 的模拟
使用 PauliOperator
模拟基本泡利算子。
API 文档链接
模拟泡利X
Python
op = PauliOperator("X")
print("op:\n",op)
执行结果如下所示:
op:
{ qbit_total = 1, pauli_with_coef_s = { 'X0 ':1 + 0j, } }
模拟泡利 Y
Python
op = PauliOperator("Y")
print("op:\n",op)
执行结果如下所示:
op:
{ qbit_total = 1, pauli_with_coef_s = { 'Y0 ':1 + 0j, } }
模拟泡利 Z
Python
op = PauliOperator("Z")
print("op:\n",op)
执行结果如下所示:
op:
{ qbit_total = 1, pauli_with_coef_s = { 'Z0 ':1 + 0j, } }
模拟单位矩阵 I
Python
op = PauliOperator("I")
print("op:\n",op.str_with_I(True))
执行结果如下所示:
op:
{ qbit_totql = 1, paulis = [ 'I', ], coefs = [ 1 + 0j, ] }
对泡利算子组合的模拟
对基本泡利算子的组合进行模拟
API文档链接
通过Python字典创建
从包含表示泡利算子的字符串和系数的字典构造 PauliOperator
对象。
例如 {"X0 Z1":2,"X1 Y2":3}
。
Python
op = PauliOperator({"X0 Z1":2,"X1 Y2":3})
print("op:\n",op.str_with_I(False))
执行结果如下所示:
op:
{ qbit_total = 3, paulis = [ 'XZI', 'IXY', ], coefs = [ 2 + 0j, 3 + 0j, ] }
通过Python列表创建
从表示泡利算子的字符串、索引和系数的列表构造 PauliOperator
对象。
例如 [("XXZ", [0, 1, 4], 1 + 2j), ("ZZ", [1, 2], -1 + 1j)]
。
Python
op = PauliOperator([("XXZ", [0, 1, 4], 1 + 2j), ("ZZ", [1, 2], -1 + 1j)] )
print("op:\n",op.str_with_I(False))
执行结果如下所示:
op:
{ qbit_total = 5, paulis = [ 'XXIIZ', 'IZZII', ], coefs = [ 1 + 2j, -1 + 1j, ] }
通过实数矩阵创建
从一个实数矩阵构造 PauliOperator
对象。
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 = PauliOperator(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({"X0 Z1":2+1.j,"X1 Y2":3+1.j})
语句中,"X0 Z1"
和 "X1 Y2"
被称为 PauliOperator
对象的项。
等价性
PauliOperator 项的等价性定义如下:
- 必要条件 1:两个 PauliOperator 项对应的量子系统必须相同,即量子系统中的比特必须完全一致。
- 必要条件 2:两个 PauliOperator 项对量子系统的作用必须相同。
示例:
"X0 Z1"
等价于 "X0 Z1"
,前提是它们对应的量子系统相同,均包含索引为 0 和 1 的比特,且其他比特也相同。
"X1 Y2"
等价于 "I0 X1 Y2"
,前提是它们对应的量子系统相同,均包含索引为 0、1 和 2 的比特,且其他比特也相同。
系数
PauliOperator项的系数。
在 PauliOperator({“X0 Z1”:2+1.j,”X1 Y2”:3+1.j})
中,2+1.j
被称为 “X0 Z1”
的系数,3+1.j
被称为 “X1 Y2”
的系数。
加法
API文档链接
使用规则
PauliOperator + PauliOperator
Python
H1 = PauliOperator({"X0 Y1":1})
H2 = PauliOperator({"X0 Y1": 2})
H3 = H1+H2
print("H3:",H3)
执行结果如下所示:
H3: { qbit_total = 2, pauli_with_coef_s = { 'X0 Y1 ':3 + 0j, } }
计算规则
- 如果项相同,则对应项的系数相加。
- 如果项不同,则保留不同的项及其系数。
- 如果某项的系数为 0,则删除该项。
减法
API文档链接
使用规则
PauliOperator - PauliOperator
Python
H1 = PauliOperator({"X0 Y1":1})
H2 = PauliOperator({"X0 Y1": 2})
H3 = H1-H2
print("H3:",H3)
执行结果如下所示:
H3: { qbit_total = 2, pauli_with_coef_s = { 'X0 Y1 ':-1 + 0j, } }
计算规则
- 如果项相同,则对应项的系数相减。
- 如果项不同,则保留不同的项及其系数。
- 如果某项的系数为 0,则删除该项。
标量数乘
API文档链接
使用规则
PauliOperator * Complex
Complex * PauliOperator
Python
H1 = PauliOperator({"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, } }
计算规则
- 每个系数都乘以一个复数。
- 如果某项的系数为 0,则删除该项。
乘法
API文档链接
使用规则
PauliOperator * PauliOperator
Python
H1 = PauliOperator({"X0 Y1":1})
H2 = PauliOperator({"X0 Y1":1})
H3 = H1 * H2
print("H3:\n",H3)
执行结果如下所示:
H3:
{ qbit_total = 2, pauli_with_coef_s = { '':1 + 0j, } }
计算规则
单项PauliOperator相乘
单项 PauliOperator 相乘是指两个仅包含单个项的 PauliOperator
对象相乘。
多项PauliOperator相乘
多项PauliOperator相乘是指两个多项 PauliOperator
对象相乘。
遵循类似于多项式乘法的分配率。
Python
'''
H3 is equivalent to PauliOperator({"X0": 1.1})*PauliOperator({"Z0": 2.1})+PauliOperator({"X0": 1.1})*PauliOperator({"I0": 2.2})+PauliOperator({"Y0": 1.2})*PauliOperator({"Z0": 2.1})+PauliOperator({"Y0": 1.2})*PauliOperator({"I0": 2.2})
'''
H1 = PauliOperator({"X0": 1.1,"Y0":1.2})
H2 = PauliOperator({"Z0": 2.1,"I0":2.2})
H3=H1*H2
print("H3:\n",H3)
执行结果如下所示:
H3:
{ qbit_total = 1, pauli_with_coef_s = { 'Y0 ':2.64 -2.31j, 'X0 ':2.42 + 2.52j, } }
- 依次应用加法、单项乘法及张量积运算的规则。
张量积
API文档链接
使用规则
PauliOperator tensor PauliOperator
Python
H1 = PauliOperator({"X0 Y1":1})
H2 = PauliOperator({"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 对应的 Pauli 算子字符串 H1 作用于量子比特 \(0...n-1\),其中算子 A1 作用于 Q1 的 k1 号比特。
量子系统 Q2 对应的 Pauli 算子字符串 H2 作用于量子比特 \(0...m-1\),其中算子 A2 作用于 Q2 的 k2 号比特。
执行 H3 = H1.tensor(H2)
后,量子系统 Q3 对应的比特编号为 \(0...(m+n-1)\),其中 A1 作用于 Q3 的 k1 号比特,A2 作用于 Q3 的 (n+k2) 号比特。
继续应用加法、乘法和标量数乘的运算规则。
Python
H1=PauliOperator({"Z0 Y3": 1})
H2=PauliOperator({"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_pauli_operator
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"""
op = PauliOperator({"X0 Z1": 2, "X1 Y2": 3})
print("without I:\n", op)
try:
res = qvm.expval_pauli_operator(prog, op)
print("res:", res)
res2 = expval_pauli_operator(prog,op,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_pauli_operator (a global interface exported by pyqpanda3.core).
在GPUQVM上计算
使用 GPUQVM 计算量子程序在指定泡利算子的量子系统上的期望值。
Python
from pyqpanda3.core import CPUQVM, QProg, S, BARRIER,expval_pauli_operator
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"""
op = PauliOperator({"X0 Z1": 2, "X1 Y2": 3})
print("without I:\n", op)
try:
res2 = expval_pauli_operator(prog,op,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计算量子程序在指定泡利算子的量子系统上的期望值。
请参考 量子云服务 了解 originqc
的云服务。
QCloudBackend.expval_pauli_operator API 文档
Python
def test_qpu_pauli_operator_expectation()->bool:
prog = QProg()
prog << X(0)<<CNOT(0,1)<<SWAP(1,2)
Ha = PauliOperator({"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_pauli_operator(prog,Ha,QCloudOptions())
print("res:", res)
except Exception as e:
print(e)
return True
if __name__ =="__main__":
test_qpu_pauli_operator_expectation()
执行结果如下所示:
- 注解
- 结果的数值会有所波动。
在 originqc 云的全振幅模拟器上计算
使用 originqc
云的全振幅模拟器计算量子程序在指定泡利算子的量子系统上的期望值。
请参考 量子云服务 了解 originqc
的云服务。
QCloudBackend.expval_pauli_operator API 文档
Python
def test_qpu_pauli_operator_expectation()->bool:
prog = QProg()
prog << X(0)<<CNOT(0,1)<<SWAP(1,2)
Ha = PauliOperator({"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_pauli_operator(prog, Ha)
print("res:", res)
except Exception as e:
print(e)
return True
if __name__ =="__main__":
test_qpu_pauli_operator_expectation()
执行结果如下所示:
其它
获取对应的矩阵
API doc for pyqpanda3.hamiltonian.PauliOperator.matrix
Python
op = PauliOperator(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]]