Hamiltonian
The Hamiltonian class represents a symbolic Hamiltonian as a sum of weighted Pauli operators. You can create Hamiltonians using the built-in Pauli operators and combine them with standard arithmetic operations.
Constructing
To constuct a Hamiltonian with a single Pauli, you can use the constructors X(i), Y(i), Z(i), I(i).
From these single-qubit operators, you can build multi-qubit Hamiltonians using arithmetic operations.
The operations follow Python syntax, for example: 2 * Z(0) + Z(1) and Z(0) * Z(1) build multi-qubit Hamiltonian.
List of Operations
Arithmetic operations:
Addition:
H1 + H2Scalar multiplication:
5 * Hmultiplication:
H0 * H1Subtraction:
H1 - H2Division by scalar:
H / 5Negation:
-H
Extra Symbolic Operators:
commutator:
H1.commutator(H2)anticommutator:
H1.anticommutator(H2)vector_norm:
H.vector_norm()frobenius_norm:
H.frobenius_norm()trace:
H.trace()
Exporting Hamiltonians:
to matrix:
H.to_matrix(nqubits)to qtensor:
H.to_qtensor(nqubits)
Importing Hamiltonians:
from qtensor:
Hamiltonian.from_qtensor(qtensor)from string:
Hamiltonian.parse(hamiltonian_string)
Example: Ising Hamiltonian
To define an Ising Hamiltonian of the form:
you can use the Pauli Z operators from the library:
from qilisdk.analog import Z
nqubits = 3
J = {(0, 1): 1, (0, 2): 2, (1, 2): 4}
h = {0: 1, 1: 2, 2: 3}
coupling = sum(weight * Z(i) * Z(j) for (i, j), weight in J.items())
fields = sum(weight * Z(i) for i, weight in h.items())
H = -(coupling + fields)
print(H)
Output:
- Z(0) Z(1) - 2 Z(0) Z(2) - 4 Z(1) Z(2) - Z(0) - 2 Z(1) - 3 Z(2)