Prev Tutorial: Quantum Visualization
Next Tutorial: Variational Quantum Circuit
Introduction
Pauli operator is an important concept in quantum mechanics, with a wide range of application prospects. The mathematical form of the Pauli operator is a matrix, usually represented as S = (Sx, Sy, Sz), where Sx, Sy, Sz represent the components of spin in the x, y, and z directions, respectively. These four components, including a unit vector, can be easily applied to calculations and derivations in quantum mechanics. Specifically, the Pauli operators are a set of three 2×2 unitary matrices The Hermitian matrix is also known as the unitary matrix. These four components correspond to the quantum logic gate X (x-direction component), the quantum logic gate Y (y-direction component), the quantum logic gate Z (z-direction component), and the quantum logic gate I unit vector.
To distinguish between the X, Y, Z gates and the combination of these four gates, we refer to these four gates as the basic Pauli operators, and the combination of these four gates as the Pauli operator combination. The combination of Pauli operators can express complex physical concepts. For example, the Hamiltonian can be simulated using a combination of basic Pauli operators.
The lower part uses \(\sigma_X\) \(\sigma_Y\) \(\sigma_Z\) \(\sigma_f\) to represent the unitary matrices corresponding to X, Y, Z, and I, respectively.
Unitary matrix
The unitary matrix of the Pauli operator in the basis
\[
\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}
\]
Matrix multiplication
Matrix multiplication operation between basic Pauli operators
The \(=\) below represents the equal sign in mathematical formulas
XX,YY,ZZ
The Pauli operator multiplied by itself yields the identity matrix
\[
\sigma_X\sigma_X=\sigma_I
\]
\[
\sigma_Y\sigma_Y=\sigma_I
\]
\[
\sigma_Z\sigma_Z=\sigma_I
\]
X/Y/Z and I
The value of the Pauli operator multiplied by the identity matrix remains unchanged, whether it is multiplied by the left or right
\[
\sigma_X\sigma_I=\sigma_I\sigma_X=\sigma_I
\]
\[
\sigma_Y\sigma_I=\sigma_I\sigma_X=\sigma_I
\]
\[
\sigma_Z\sigma_I=\sigma_I\sigma_X=\sigma_I
\]
X/Y/Z and X/Y/Z
Multiplication between 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
\]
Simulation of Pauli Operator
QPanda3 uses the PauliOperator class to simulate basic Pauli operators and their combinations. To simulate the way Pauli operators act on quantum systems, the PauliOperator class assigns a qubit index to each of the basic Pauli operators. Each PauliOperator object can be expressed using a quantum circuit such as QCircuit in QPanda3.
For X/Y/Z/I
Simulation of the Pauli operator for the foundation
Here is API doc
Simulation of Pauli X
Python
op = PauliOperator("X")
print("op:\n",op)
Output
op:
{ qbit_total = 1, pauli_with_coef_s = { 'X0 ':1 + 0j, } }
Simulation of Pauli Y
Python
op = PauliOperator("Y")
print("op:\n",op)
Output
op:
{ qbit_total = 1, pauli_with_coef_s = { 'Y0 ':1 + 0j, } }
Simulation of Pauli Z
Python
op = PauliOperator("Z")
print("op:\n",op)
Output
op:
{ qbit_total = 1, pauli_with_coef_s = { 'Z0 ':1 + 0j, } }
Simulation of Pauli I
Python
op = PauliOperator("I")
print("op:\n",op.str_with_I(True))
Output
op:
{ qbit_totql = 1, paulis = [ 'I', ], coefs = [ 1 + 0j, ] }
For combination
Simulation of the combination of Pauli operators
Here is API doc
From vectors
Constructs a PauliOperator from vectors of operations and coefficients.
Python
op = PauliOperator({"X0 Z1":2,"X1 Y2":3})
print("op:\n",op.str_with_I(False))
Output
op:
{ qbit_totql = 3, paulis = [ 'XZI', 'IXY', ], coefs = [ 2 + 0j, 3 + 0j, ] }
From tuples
Constructs a PauliOperator from a vector of tuples.
Python
op = PauliOperator([("XXZ", [0, 1, 4], 1 + 2j), ("ZZ", [1, 2], -1 + 1j)] )
print("op:\n",op.str_with_I(False))
Output
op:
{ qbit_totql = 5, paulis = [ 'XXIIZ', 'IZZII', ], coefs = [ 1 + 2j, -1 + 1j, ] }
Operations
Addition, subtraction, multiplication, division, and tensor product operations
Describing
For the convenience of description, some expressions are agreed upon here. These expressions will be used for explanation in the following.
Terms of Pauli Operator
The "X0 Z1" and "X1 Y2" in PauliOperator({"X0 Z1":2+1.j,"X1 Y2":3+1.j}) are called terms of the PauliOperator object;
Equivalence
The equivalence of Pauli operator terms
- Essential condition 1: The quantum systems corresponding to the two Pauli operator terms are identical, which is manifested as the qbits in the quantum system being completely identical
- Essential condition 2: The two Pauli operator terms have the same effect on the quantum system
For examples
- "X0 Z1" is equal to "X0 Z1" provided that the corresponding quantum systems are the same, both containing qbits with indices 0 and 1, and that the other qbits are also the same
- "X1 Y2" is equal to "I0 X1 Y2" if the corresponding quantum systems are the same, both containing qbits with indices 0, 1, and 2, and other qbits are also the same
Coefficient
Coefficients of Pauli operator terms
For PauliOperator({“X0 Z1”:2+1.j,”X1 Y2”:3+1.j}) , we call 2+1.j the coefficient of term “X0 Z1” and 3+1.j the coefficient of term “X1 Y2” .
Addition
Here is API doc
Using rules
PauliOperator + PauliOperator
Python
H1 = PauliOperator({"X0 Y1":1})
H2 = PauliOperator({"X0 Y1": 2})
H3 = H1+H2
print("H3:",H3)
Output
H3: { qbit_total = 2, pauli_with_coef_s = { 'X0 Y1 ':3 + 0j, } }
Rules of operation
- If the items are the same, the coefficients of the corresponding items are added
- Different items, different items and their coefficients are retained
- If the coefficient of an item is 0, delete the item
Subtraction
Here is API doc
Using rules
PauliOperator - PauliOperator
Python
H1 = PauliOperator({"X0 Y1":1})
H2 = PauliOperator({"X0 Y1": 2})
H3 = H1-H2
print("H3:",H3)
Output
H3: { qbit_total = 2, pauli_with_coef_s = { 'X0 Y1 ':-1 + 0j, } }
Rules of operation
- If the items are the same, the coefficients of the corresponding items are subtracted
- If the items are different, different items and their coefficients will be retained
- If the coefficient of an item is 0, delete the item
Scalar multiplication
Here is API doc
Using rules
Python
H1 = PauliOperator({"X0 Y1":1})
H3 = (3.3+1.j) * H1
H4 = H1*(4.4+1.j)
print("H4:\n",H4)
Output
H4:
{ qbit_total = 2, pauli_with_coef_s = { 'X0 Y1 ':4.4 + 1j, } }
Rules of operation
- Multiply each coefficient by a complex number
- If the coefficient of an item is 0, delete the item
Multiplication
Here is API doc
Using rules
PauliOperator * PauliOperator
Python
H1 = PauliOperator({"X0 Y1":1})
H2 = PauliOperator({"X0 Y1":1})
H3 = H1 * H2
print("H3:\n",H3)
Output
H3:
{ qbit_total = 2, pauli_with_coef_s = { '':1 + 0j, } }
Rules of operation
Single-term and single-term
Multiply a PauliOperator object containing only a single term with another PauliOperator object containing only a single term
(1) Consolidated into 1 item:
Python
H1 = PauliOperator({"X0 Y1":1})
H2 = PauliOperator({"X2 Y3":1})
H3 = H1 * H2
print("H3:\n",H3)
Output
H3:
{ qbit_total = 4, pauli_with_coef_s = { 'X0 Y1 X2 Y3 ':1 + 0j, } }
(2) Pauli operators on the same qubit can evolve and the coefficients of the terms may be changed during the evolution
Python
H1 = PauliOperator({"X0 Y1": 1})
H2 = PauliOperator({"Z0 I1": 1})
H3 = H1 * H2
print("H3:\n",H3)
Output
H3:
{ qbit_total = 2, pauli_with_coef_s = { 'Y0 Y1 ':0 -1j, } }
(3) If the coefficient of an item is 0, delete the item
Multi-term and Multi-term
Multi-term PauliOperator object multiply Multi-term PauliOperator object
(1) Apply the allocation rate in a manner akin to polynomial multiplication
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)
Output
H3:
{ qbit_total = 1, pauli_with_coef_s = { 'Y0 ':2.64 -2.31j, 'X0 ':2.42 + 2.52j, } }
(2) Apply the rules of addition, multiplication of single terms, and tensor product operations
Tensor product
Here is API doc
Using rules
PauliOperator tensor PauliOperator
Python
H1 = PauliOperator({"X0 Y1":1})
H2 = PauliOperator({"X0 Y1":1})
H3 = H1.tensor(H2)
print("H3:\n",H3)
Output
H3:
{ qbit_total = 4, pauli_with_coef_s = { 'X0 Y1 X2 Y3 ':1 + 0j, } }
Rules of operation
The qbit numbers of the quantum system Q1 corresponding to the Pauli operator string H1 are 0...n-1; There is a Pauli operator A1 acting on the k1 qbit of Q1.
The qbit numbers of the Q2 quantum system corresponding to the Pauli string H2 are 0...m-1; There is a Pauli operator A2 acting on the k2 qbit of Q2.
After the execution of H3=H1.tensor(H2), the qbit number of the quantum system Q3 corresponding to H3 is 0...(m+n-1),where A1 acts on qbit k1 of Q3 and A2 acts on the (n+k2) qbit of Q3
Continue to apply the rules of addition, multiplication, and division
Python
H1=PauliOperator({"Z0 Y3": 1})
H2=PauliOperator({"Y2": 1})
H3=H1.tensor(H2)
print("H3:\n",H3)
Output
H3:
{ qbit_total = 7, pauli_with_coef_s = { 'Z0 Y3 Y6 ':1 + 0j, } }
Application
Calculate expectation
On CPUQVM
Calculate the expectation of running quantum programs on quantum systems with Pauli operators
Python
def test_expectation2():
qvm = CPUQVM()
prog = QProg()
prog.append(S(0))
prog.append(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)
except Exception as e:
print(e)
if __name__ == "__main__":
test_expectation2()
Output
without I:
{ qbit_total = 3, pauli_with_coef_s = { 'X0 Z1 ':2 + 0j, 'X1 Y2 ':3 + 0j, } }
res: 0.0
On QPU
Calculate the expectation of running quantum programs on QPU with Hamiltonian
Please watch Quantum Cloud Service to learn more ablout originqc's cloud service
API doc for QCloudBackend.expval_pauli_operator
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()
Output
- Note
- The numerical values of the results will fluctuate.
On Full Amplitude Simulator in originqc's cloud
Calculate the expectation of running quantum programs on QPU with Hamiltonian
Please watch Quantum Cloud Service to learn more ablout originqc's cloud service
API doc for QCloudBackend.expval_pauli_operator
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, [0, 1, 2])
print("res:", res)
except Exception as e:
print(e)
return True
if __name__ =="__main__":
test_qpu_pauli_operator_expectation()
Output