QPanda3
Supported by OriginQ
Loading...
Searching...
No Matches
Hamiltonian

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.

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.

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

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.

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

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

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

Hamiltonian * Complex
Complex * Hamiltonian

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

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:

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

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

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

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

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

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

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

Output

Note
The numerical values of the results will fluctuate.
res: -0.6217999999999999

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

Output

res: -3.1

Other

Obtain the corresponding matrix.

API doc for pyqpanda3.hamiltonian.Hamiltonian.matrix

from pyqpanda3.hamiltonian import Hamiltonian
# prepare Hamiltonian object
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, }
)
# Generate a matrix from a Hamiltonian object.
mat = op.matrix()
print('mat:\n',mat)
Definition __init__.py:1

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]]