Hamiltonians and Time Evolution

MIMIQ provides tools to define quantum Hamiltonians and simulate their time evolution using Trotterization methods. This page explains how to construct Hamiltonians, compute their expectation values, and apply Lie-Trotter, Suzuki-Trotter, and Yoshida decompositions.

This workflow allows you to:

Build realistic Hamiltonians from Pauli terms

Simulate time evolution efficiently using Trotter expansions

Measure physical observables like energy

Hamiltonian

In quantum computing, Hamiltonians play a central role in algorithms such as the Variational Quantum Eigensolver (VQE), Quantum Phase Estimation, and the Quantum Approximate Optimization Algorithm (QAOA). They are used to encode the energy landscape of a physical system, generate dynamics, or define cost functions for optimization.

A typical Hamiltonian is expressed as a sum of weighted Pauli strings:

\[H = \sum_j c_j \cdot P_j\]

Each term consists of a real coefficient \(c_j\) and a Pauli string \(P_j\), such as X, ZZ, or XYZ, which acts on a specific subset of qubits.

In quantum circuit frameworks, these Hamiltonians are often represented programmatically by associating each Pauli string with a set of target qubit indices and a coefficient, allowing users to construct and simulate physical models or optimization problems.

Simulating the Ising Model

A fundamental use case for Hamiltonians in quantum computing is to estimate physical quantities like the ground state energy of a system. This is at the heart of quantum algorithms such as the Variational Quantum Eigensolver (VQE), quantum simulation of materials, and quantum optimization.

Here, we demonstrate how to use MIMIQ to:

  • Build the Hamiltonian for the 1D transverse-field Ising model

  • Apply Trotterized time evolution (first and second order)

  • Measure the energy (expectation value of the Hamiltonian)

This provides a concrete template for simulating many-body quantum systems.

Ising Model

The 1D transverse-field Ising model is defined by the Hamiltonian:

\[H = -J \sum_{j=0}^{N-1} Z_j Z_{j+1} - h \sum_{j=0}^{N} X_j\]

This models a chain of spins with:

  • nearest-neighbor interaction (ZZ terms),

  • and a transverse magnetic field (X terms).

It’s widely used in condensed matter physics and quantum algorithm benchmarks.

Building the Hamiltonian

To construct this model in MIMIQ you can use Hamiltonian class to easily build your hamiltonian:

>>> N = 4  # number of qubits (spins)
>>> J = 1.0  # interaction strength
>>> h = 0.5  # field strength

>>> hamiltonian = Hamiltonian()

>>> for j in range(N - 1):
...     _ = hamiltonian.push(-J, PauliString("ZZ"), j, j + 1)


>>> for j in range(N):
...     _ = hamiltonian.push(-h, PauliString("X"), j)

>>> print(hamiltonian)
4-qubit Hamiltonian with 7 terms:
├── -1.0 * ZZ @ q[0,1]
├── -1.0 * ZZ @ q[1,2]
├── -1.0 * ZZ @ q[2,3]
├── -0.5 * X @ q[0]
├── -0.5 * X @ q[1]
├── -0.5 * X @ q[2]
└── -0.5 * X @ q[3]

Simulating Time Evolution

Suppose we want to apply \(e^{-iHt}\) to a quantum state. This is useful for preparing ground states via imaginary-time evolution (approximate cooling), or evolving an initial state in real time.

Because H has non-commuting terms, we use a Trotter approximation.

First-order Trotterization (Lie) (push_lietrotter())

>>> from mimiqcircuits.decompose import decompose_step
>>> c = Circuit()
>>> c.push_lietrotter(hamiltonian, tuple(range(N)), t = 0.1, steps = 1)
4-qubit circuit with 1 instruction:
└── trotter(0.1) @ q[0:3]


>>> c.decompose_step()
4-qubit circuit with 7 instructions:
├── RZZ(-0.2) @ q[0:1]
├── RZZ(-0.2) @ q[1:2]
├── RZZ(-0.2) @ q[2:3]
├── RX(-0.1) @ q[0]
├── RX(-0.1) @ q[1]
├── RX(-0.1) @ q[2]
└── RX(-0.1) @ q[3]

Second-order Trotterization (Suzuki) (push_suzukitrotter())

>>> c = Circuit()
>>> c.push_suzukitrotter(hamiltonian, tuple(range(N)), t = 0.1, steps = 1, order = 2)
4-qubit circuit with 1 instruction:
└── suzukitrotter_2(0.1) @ q[0:3]


>>> c.decompose_step()
4-qubit circuit with 14 instructions:
├── RZZ(-0.1) @ q[0:1]
├── RZZ(-0.1) @ q[1:2]
├── RZZ(-0.1) @ q[2:3]
├── RX(-0.05) @ q[0]
├── RX(-0.05) @ q[1]
├── RX(-0.05) @ q[2]
├── RX(-0.05) @ q[3]
├── RX(-0.05) @ q[3]
├── RX(-0.05) @ q[2]
├── RX(-0.05) @ q[1]
├── RX(-0.05) @ q[0]
├── RZZ(-0.1) @ q[2:3]
├── RZZ(-0.1) @ q[1:2]
└── RZZ(-0.1) @ q[0:1]

Measuring the Energy

Once the circuit has prepared the desired quantum state such as by applying time evolution with Trotterization we can measure the energy by evaluating the expectation value (push_expval()) of the Hamiltonian.

>>> c.push_expval(hamiltonian, *range(N))
4-qubit, 7-zvar circuit with 16 instructions:
├── suzukitrotter_2(0.1) @ q[0:3]
├── ⟨ZZ⟩ @ q[0:1], z[0]
├── z[0] = -1.0 * z[0]
├── ⟨ZZ⟩ @ q[1:2], z[1]
├── z[1] = -1.0 * z[1]
├── ⟨ZZ⟩ @ q[2:3], z[2]
├── z[2] = -1.0 * z[2]
├── ⟨X⟩ @ q[0], z[3]
├── z[3] = -0.5 * z[3]
├── ⟨X⟩ @ q[1], z[4]
├── z[4] = -0.5 * z[4]
├── ⟨X⟩ @ q[2], z[5]
├── z[5] = -0.5 * z[5]
├── ⟨X⟩ @ q[3], z[6]
├── z[6] = -0.5 * z[6]
└── z[0] = 0.0 + z[0] + z[1] + z[2] + z[3] + z[4] + z[5] + z[6]

The result is stored in the Z-register — the first index contains the total expectation value (energy). You can access it from the simulation result.

Reference

class Hamiltonian(terms=None)[source]

Bases: object

A Hamiltonian written as a sum of Pauli strings.

The Hamiltonian is

\[H = \sum_j c_j P_j,\]

where each term \(c_j P_j\) is a HamiltonianTerm. Add terms with push() or add_terms(). Iteration, indexing and len act on the list of terms. Use it with push_expval() to measure its expectation value, and with push_lietrotter(), push_suzukitrotter() or push_yoshidatrotter() to simulate the time evolution \(e^{-iHt}\). These are available as methods of Circuit.

Parameters:

terms (iterable, optional) – The HamiltonianTerm objects to start with. Defaults to no terms.

Examples

>>> from mimiqcircuits import *
>>> h = Hamiltonian()
>>> h.push(1.0, PauliString("XX"), 0, 1)
2-qubit Hamiltonian with 1 terms:
└── 1.0 * XX @ q[0,1]
>>> h.push(1.0, PauliString("YY"), 0, 1)
2-qubit Hamiltonian with 2 terms:
├── 1.0 * XX @ q[0,1]
└── 1.0 * YY @ q[0,1]
>>> print(h)
2-qubit Hamiltonian with 2 terms:
├── 1.0 * XX @ q[0,1]
└── 1.0 * YY @ q[0,1]
add_terms(term)[source]

Append a term to the Hamiltonian.

Despite its name, it takes a single term.

Parameters:

term (HamiltonianTerm) – The term to append.

Returns:

This Hamiltonian, modified in place.

Return type:

Hamiltonian

push(coefficient, pauli, *qubits)[source]

Append the term coefficient * pauli acting on qubits.

Parameters:
  • coefficient (float) – The coefficient of the term, a real number or a symbolic expression.

  • pauli (PauliString) – The Pauli string, one letter per qubit.

  • *qubits (int) – The qubit indices, counted from 0.

Returns:

This Hamiltonian, modified in place.

Return type:

Hamiltonian

Raises:

ValueError – If a qubit index is negative or appears twice.

Examples

>>> from mimiqcircuits import *
>>> h = Hamiltonian()
>>> # -J Z_0 Z_1 with J = 1, then a field on qubit 1
>>> h.push(-1.0, PauliString("ZZ"), 0, 1)
2-qubit Hamiltonian with 1 terms:
└── -1.0 * ZZ @ q[0,1]
>>> h.push(0.5, PauliString("X"), 1)
2-qubit Hamiltonian with 2 terms:
├── -1.0 * ZZ @ q[0,1]
└── 0.5 * X @ q[1]
num_terms()[source]

Return the number of terms, the same as len(h).

Returns:

The number of terms.

Return type:

int

num_qubits()[source]

Return the number of qubits the Hamiltonian acts on.

This is one more than the largest qubit index of any term, so it counts unused qubits below that index.

Returns:

The number of qubits, 0 for an empty Hamiltonian.

Return type:

int

saveproto(file)[source]

Save the Hamiltonian to a file in protobuf format.

Parameters:

file – A path, or a binary file object with a write method.

Returns:

The number of bytes written.

Return type:

int

Raises:

ValueError – If writing to a path fails.

Examples

>>> from mimiqcircuits import *
>>> import io
>>> h = Hamiltonian()
>>> h.push(0.5, PauliString("ZZ"), 0, 1)
2-qubit Hamiltonian with 1 terms:
└── 0.5 * ZZ @ q[0,1]
>>> # write to an in-memory file, then read it back
>>> buf = io.BytesIO()
>>> _ = h.saveproto(buf)
>>> _ = buf.seek(0)
>>> Hamiltonian.loadproto(buf)
2-qubit Hamiltonian with 1 terms:
└── 0.5 * ZZ @ q[0,1]
static loadproto(file)[source]

Load a Hamiltonian from a file in protobuf format.

Parameters:

file – A path, or a binary file object with a read method, holding a Hamiltonian written by saveproto().

Returns:

The loaded Hamiltonian.

Return type:

Hamiltonian

Raises:

ValueError – If the file cannot be read or parsed.

matrix()[source]

Return the matrix of the Hamiltonian.

The matrix is \(2^n \times 2^n\) for num_qubits() qubits \(n\), with qubit 0 as the leftmost factor of the Kronecker product, that is the most significant bit of the row index.

Returns:

The matrix, symbolic if any coefficient is.

Return type:

symengine.DenseMatrix

Examples

>>> from mimiqcircuits import *
>>> h = Hamiltonian()
>>> h.push(1.0, PauliString("Z"), 0)
1-qubit Hamiltonian with 1 terms:
└── 1.0 * Z @ q[0]
>>> h.matrix()
[1.0, 0]
[0, -1.0]
evaluate(d)[source]

Substitute values into the symbolic coefficients.

Coefficients that are plain numbers are kept as they are. The Hamiltonian itself is not modified.

Parameters:

d (dict) – A dictionary from symbols, or their names, to values.

Returns:

A new Hamiltonian with the substituted coefficients, numeric or still partly symbolic.

Return type:

Hamiltonian

Examples

>>> from mimiqcircuits import *
>>> from symengine import Symbol
>>> J = Symbol("J")
>>> h = Hamiltonian()
>>> h.push(J, PauliString("ZZ"), 0, 1)
2-qubit Hamiltonian with 1 terms:
└── J * ZZ @ q[0,1]
>>> # set the coupling J to 0.25
>>> h.evaluate({J: 0.25})
2-qubit Hamiltonian with 1 terms:
└── 0.25 * ZZ @ q[0,1]
push_lietrotter(self, h, qubits, t, steps)

Append the first-order (Lie-Trotter) time evolution under a Hamiltonian.

Called as a method, circuit.push_lietrotter(h, qubits, t, steps). It approximates \(e^{-iHt}\), for \(H = \sum_{j=1}^m c_j P_j\), by

\[e^{-i H t} \approx \left[ \prod_{j=1}^m e^{-i c_j P_j \Delta t} \right]^n,\]

with \(n\) = steps and \(\Delta t = t / n\). The error per step is of order \(\Delta t^2\) and vanishes when the terms commute.

One step is declared once as a gate named trotter with parameter \(\Delta t\), holding one Pauli rotation per term (GateRX, GateRZZ, RPauli, …). The circuit gets steps calls to it.

Parameters:
  • h (Hamiltonian) – The Hamiltonian.

  • qubits (tuple) – The circuit qubits that the Hamiltonian qubits 0, 1, ... map to. There must be exactly h.num_qubits() of them.

  • t (float) – The total evolution time.

  • steps (int) – The number of Trotter steps \(n\).

Returns:

The circuit, modified in place.

Return type:

Circuit

Raises:

ValueError – If the number of qubits does not match the Hamiltonian.

See also

push_suzukitrotter(), push_yoshidatrotter(), GateDecl

Examples

>>> from mimiqcircuits import *
>>> c = Circuit()
>>> h = Hamiltonian()
>>> h.push(1.0, PauliString("ZZ"), 0, 1)
2-qubit Hamiltonian with 1 terms:
└── 1.0 * ZZ @ q[0,1]
>>> # evolve for t = 1 in 3 steps of 1/3
>>> c.push_lietrotter(h, (0, 1), t=1.0, steps=3)
2-qubit circuit with 3 instructions:
├── trotter(0.3333333333333333) @ q[0:1]
├── trotter(0.3333333333333333) @ q[0:1]
└── trotter(0.3333333333333333) @ q[0:1]
push_suzukitrotter(self, h, qubits, t, steps, order=2)

Append the time evolution under a Hamiltonian with a Suzuki-Trotter formula.

Called as a method, circuit.push_suzukitrotter(h, qubits, t, steps, order). It approximates \(e^{-iHt}\) with the Suzuki product formula of the given even order, whose error per step is of order \(\Delta t^{\text{order}+1}\).

The second-order formula is

\[e^{-i H t} \approx \left[ \prod_{j=1}^{m} e^{-i \frac{\Delta t}{2} H_j} \prod_{j=m}^{1} e^{-i \frac{\Delta t}{2} H_j} \right]^n\]

where the Hamiltonian \(H\) is a sum of terms:

\[H = \sum_{j=1}^{m} H_j\]

and the Trotter step size is:

\[\Delta t = \frac{t}{n}\]

and each higher order is built from the one below by the Suzuki recursion

\[S_{2k}(\lambda) = [S_{2k-2}(p_k \lambda)]^2 \cdot S_{2k-2}((1 - 4p_k)\lambda) \cdot [S_{2k-2}(p_k \lambda)]^2\]

with:

\[p_k = \left(4 - 4^{1/(2k - 1)}\right)^{-1}.\]

Each order is declared once as a gate named suzukitrotter_<order>, the higher ones calling the lower ones five times. The circuit gets steps calls to the gate of the requested order.

Parameters:
  • h (Hamiltonian) – The Hamiltonian.

  • qubits (tuple) – The circuit qubits that the Hamiltonian qubits 0, 1, ... map to. There must be exactly h.num_qubits() of them.

  • t (float) – The total evolution time.

  • steps (int) – The number of Trotter steps \(n\).

  • order (int) – The order of the formula, an even integer of at least 2 (default 2).

Returns:

The circuit, modified in place.

Return type:

Circuit

Raises:

ValueError – If the number of qubits does not match the Hamiltonian, or order is not an even integer of at least 2.

See also

push_lietrotter(), push_yoshidatrotter(), GateDecl

References

  • M. Suzuki, “General theory of fractal path integrals with applications to many-body theories and statistical physics”, J. Math. Phys. 32, 400 (1991).

Examples

>>> from mimiqcircuits import *
>>> c = Circuit()
>>> h = Hamiltonian()
>>> h.push(1.0, PauliString("XX"), 0, 1)
2-qubit Hamiltonian with 1 terms:
└── 1.0 * XX @ q[0,1]
>>> c.push_suzukitrotter(h, (0, 1), t=1.0, steps=5, order=2)
2-qubit circuit with 5 instructions:
├── suzukitrotter_2(0.2) @ q[0:1]
├── suzukitrotter_2(0.2) @ q[0:1]
├── suzukitrotter_2(0.2) @ q[0:1]
├── suzukitrotter_2(0.2) @ q[0:1]
└── suzukitrotter_2(0.2) @ q[0:1]
push_expval(self, hamiltonian, *qubits, firstzvar=None)

Append the computation of the expectation value of a Hamiltonian.

Called as a method, circuit.push_expval(hamiltonian, *qubits). For each term \(c_j P_j\) it appends an ExpectationValue of \(P_j\) into z-variable firstzvar + j and scales it by \(c_j\) with Multiply, then sums them all with Add, so that z-variable firstzvar ends up holding

\[\langle \psi | H | \psi \rangle = \sum_j c_j \langle \psi | P_j | \psi \rangle.\]

The other z-variables keep the value of each scaled term. The state is not changed.

Parameters:
  • hamiltonian (Hamiltonian) – The Hamiltonian to evaluate.

  • *qubits (int) – The circuit qubits that the Hamiltonian qubits 0, 1, ... map to. There must be exactly hamiltonian.num_qubits() of them.

  • firstzvar (int, optional) – First z-variable to use. Defaults to the first unused one, circuit.num_zvars().

Returns:

The circuit, modified in place.

Return type:

Circuit

Raises:

ValueError – If the number of qubits does not match the Hamiltonian.

Examples

>>> from mimiqcircuits import *
>>> c = Circuit()
>>> h = Hamiltonian()
>>> h.push(1.0, PauliString("ZZ"), 0, 1)
2-qubit Hamiltonian with 1 terms:
└── 1.0 * ZZ @ q[0,1]
>>> # Hamiltonian qubits 0 and 1 are circuit qubits 1 and 2
>>> c.push_expval(h, 1, 2)
3-qubit, 1-zvar circuit with 3 instructions:
├── ⟨ZZ⟩ @ q[1:2], z[0]
├── z[0] = 1.0 * z[0]
└── z[0] = 0.0 + z[0]

See also

ExpectationValue, Multiply, Add