Prev Tutorial: Quantum Information
Next Tutorial: Quantum Profiling
Introduction
The Hamiltonian, denoted as H, is a fundamental concept in quantum mechanics and classical mechanics. It represents the total energy of a system, and it is a function of the coordinates and momenta (or velocities) of the particles in the system.
In classical mechanics, the Hamiltonian is derived from the Lagrangian function through a Legendre transformation, and it is used to describe the dynamics of the system through the Hamilton's equations of motion. These equations provide a more general and powerful description of the motion of particles than Newton's laws of motion.
In quantum mechanics, the Hamiltonian corresponds to the energy operator, and its eigenvalues represent the possible energy levels of the system. The Schrödinger equation, which describes the evolution of a quantum system in time, can be derived from the Hamiltonian.
Simulate
Simulate the Hamiltonian using Pauli operators
Here is API doc
From a dict like { 'X0 Z1 ':2 + 0j, 'X1 Y2 ':3 + 0j, }
Constructs a Hamiltonian from a dict containing operators and coefficients.
Python
Ham = Hamiltonian({"X0 Z1":2,"X1 Y2":3})
print("Ham\n",Ham)
Output
Ham
{ qbit_total = 3, pauli_with_coef_s = { 'X0 Z1 ':2 + 0j, 'X1 Y2 ':3 + 0j, } }
From a list like [("XXZ", [0, 1, 4], 1 + 2j), ("ZZ", [1, 2], -1 + 1j)]
Constructs a Hamiltonian from a list containing operators, indices, and coefficients.
Python
Ham = Hamiltonian([("XXZ", [0, 1, 4], 1 + 2j), ("ZZ", [1, 2], -1 + 1j)] )
print("Ham:\n",Ham)
Output
Ham:
{ qbit_total = 5, pauli_with_coef_s = { 'X0 X1 Z4 ':1 + 2j, 'Z1 Z2 ':-1 + 1j, } }
From a PauliOperator object
Constructs a Hamiltonian from a PauliOperator
Python
op = PauliOperator([("XXZ", [0, 1, 4], 1 + 2j), ("ZZ", [1, 2], -1 + 1j)] )
Ham = Hamiltonian(op)
print("Ham:\n",Ham)
Output
Ham:
{ qbit_total = 5, pauli_with_coef_s = { 'X0 X1 Z4 ':1 + 2j, 'Z1 Z2 ':-1 + 1j, } }
From a real matrix
Generate a Hamiltonian object from a real matrix.
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)
Output
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, } }
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. These expressions are same with PauliOperator's. Please see PauliOperator.
Addition
Here is API doc
Using rules
Hamiltonian + Hamiltonian
Python
H1 = Hamiltonian({"X0 Y1":1})
H2 = Hamiltonian({"X0 Y1": 2})
H3 = H1+H2
print("H3:\n",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
Hamiltonian - Hamiltonian
Python
H1 = Hamiltonian({"X0 Y1":1})
H2 = Hamiltonian({"X0 Y1": 2})
H3 = H1-H2
print("H3:\n",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 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 = Hamiltonian({"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
Hamiltonian * Hamiltonian
Python
H1 = Hamiltonian({"X0 Z1":1})
H2 = Hamiltonian({"X0 Y1":1})
H3 = H1 * H2
print("H3:\n",H3)
Output
H3:
{ qbit_total = 2, pauli_with_coef_s = { 'X1 ':0 -1j, } }
Rules of operation
Single-term and Single-term
Multiply a Hamiltonian object containing only a single term with another Hamiltonian object containing only a single term
(1) Consolidated into 1 item:
Python
H1 = Hamiltonian({"X0 Y1":1})
H2 = Hamiltonian({"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 = Hamiltonian({"X0 Y1": 1})
H2 = Hamiltonian({"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 Hamiltonian object multiply Multi-term Hamiltonian object
(1) Apply the allocation rate in a manner akin to polynomial multiplication
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)
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
Hamiltonian tensor Hamiltonian
Python
H1 = Hamiltonian({"X0 Y1":1})
H2 = Hamiltonian({"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=Hamiltonian({"Z0 Y3": 1})
H2=Hamiltonian({"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 the expectation
Calculate the expectation of running quantum programs on quantum systems with Hamiltonian
On CPUQVM
Calculate the expectation of running quantum programs on CPUQVM with Hamiltonian
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()
Output
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).
On GPUQVM
Calculate the expectation of running quantum programs on GPUQVM with Hamiltonian
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()
Output
without I:
{ qbit_total = 3, pauli_with_coef_s = { 'X0 Z1 ':2 + 0j, 'X1 Y2 ':3 + 0j, } }
res2: 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_hamiltonian
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()
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_hamiltonian
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()
Output
Other
Obtain the corresponding matrix.
API doc for pyqpanda3.hamiltonian.Hamiltonian.matrix
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)
Output is
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]]