mimiqcircuits.hamiltonian¶
Pauli Hamiltonians, their expectation values and Trotterized time evolution.
Functions
|
Append the computation of the expectation value of a Hamiltonian. |
|
Append the first-order (Lie-Trotter) time evolution under a Hamiltonian. |
|
Append the time evolution under a Hamiltonian with a Suzuki-Trotter formula. |
|
Append the time evolution under a Hamiltonian with a Yoshida product formula. |
Classes
|
A Hamiltonian written as a sum of Pauli strings. |
|
One term of a Hamiltonian: a coefficient times a Pauli string. |
- class HamiltonianTerm(coefficient, pauli, qubits)[source]¶
Bases:
objectOne term of a Hamiltonian: a coefficient times a Pauli string.
The term is
\[H_j = c_j P_j = c_j \left(P^{(1)} \otimes P^{(2)} \otimes \dots\right),\]where \(c_j\) is a real coefficient and \(P_j\) a Pauli string such as
XorYZ, whose \(k\)-th letter acts on the \(k\)-th qubit ofqubits. Terms are usually created withHamiltonian.push().- Parameters:
coefficient (float | int) – The real coefficient \(c_j\). A symbolic expression is accepted and can be substituted later with
Hamiltonian.evaluate().pauli (PauliString) – The Pauli string \(P_j\). Its length should equal the number of qubits; this is not checked.
qubits (tuple) – The indices of the qubits the term acts on, counted from 0.
- Raises:
ValueError – If a qubit index is negative or appears twice.
Examples
>>> from mimiqcircuits import * >>> # 0.5 X_0 Z_1 >>> t = HamiltonianTerm(0.5, PauliString("XZ"), (0, 1)) >>> t 0.5 * XZ @ q[0,1] >>> t.get_coefficient(), t.get_operation(), t.get_qubits() (0.5, XZ, (0, 1))
- get_operation()[source]¶
Return the Pauli string of the term.
- Returns:
The Pauli string \(P_j\).
- Return type:
- class Hamiltonian(terms=None)[source]¶
Bases:
objectA 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 withpush()oradd_terms(). Iteration, indexing andlenact on the list of terms. Use it withpush_expval()to measure its expectation value, and withpush_lietrotter(),push_suzukitrotter()orpush_yoshidatrotter()to simulate the time evolution \(e^{-iHt}\). These are available as methods ofCircuit.- Parameters:
terms (iterable, optional) – The
HamiltonianTermobjects 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:
- push(coefficient, pauli, *qubits)[source]¶
Append the term
coefficient * pauliacting onqubits.- 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:
- 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:
- 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,
0for an empty Hamiltonian.- Return type:
- saveproto(file)[source]¶
Save the Hamiltonian to a file in protobuf format.
- Parameters:
file – A path, or a binary file object with a
writemethod.- Returns:
The number of bytes written.
- Return type:
- 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
readmethod, holding a Hamiltonian written bysaveproto().- Returns:
The loaded Hamiltonian.
- Return type:
- 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:
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_expval(self, hamiltonian, *qubits, firstzvar=None)[source]¶
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 anExpectationValueof \(P_j\) into z-variablefirstzvar + jand scales it by \(c_j\) withMultiply, then sums them all withAdd, so that z-variablefirstzvarends 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 exactlyhamiltonian.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:
- 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
- push_lietrotter(self, h, qubits, t, steps)[source]¶
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\) =
stepsand \(\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
trotterwith parameter \(\Delta t\), holding one Pauli rotation per term (GateRX,GateRZZ,RPauli, …). The circuit getsstepscalls to it.- Parameters:
h (Hamiltonian) – The Hamiltonian.
qubits (tuple) – The circuit qubits that the Hamiltonian qubits
0, 1, ...map to. There must be exactlyh.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:
- Raises:
ValueError – If the number of qubits does not match the Hamiltonian.
See also
push_suzukitrotter(),push_yoshidatrotter(),GateDeclExamples
>>> 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)[source]¶
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 evenorder, 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 getsstepscalls 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 exactlyh.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:
- Raises:
ValueError – If the number of qubits does not match the Hamiltonian, or
orderis not an even integer of at least 2.
See also
push_lietrotter(),push_yoshidatrotter(),GateDeclReferences
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_yoshidatrotter(self, h, qubits, t, steps, order=4)[source]¶
Append the time evolution under a Hamiltonian with a Yoshida product formula.
Called as a method,
circuit.push_yoshidatrotter(h, qubits, t, steps, order). It approximates \(e^{-iHt}\) by composing second-order Trotter formulas symmetrically, which cancels the error terms up to the given evenorder. Each order makes three calls to the order below, against five forpush_suzukitrotter().The order \(2(k+1)\) formula is built from the order \(2k\) one as
\[S_{2(k+1)}(t) = S_{2k}(w_1 \cdot t) \cdot S_{2k}(w_2 \cdot t) \cdot S_{2k}(w_1 \cdot t)\]where the weights are:
\[w_1 = \frac{1}{2 - 2^{1/(2k+1)}}, \quad w_2 = -\frac{2^{1/(2k+1)}}{2 - 2^{1/(2k+1)}}\]and the base case is the standard second-order Strang splitting:
\[S_2(\Delta t) = \prod_{j=1}^{m} e^{-i \Delta t H_j / 2} \prod_{j=m}^{1} e^{-i \Delta t H_j / 2}\]Each order is declared once as a gate named
yoshida_<order>. The circuit getsstepscalls 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 exactlyh.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
4).
- Returns:
The circuit, modified in place.
- Return type:
- Raises:
ValueError – If the number of qubits does not match the Hamiltonian, or
orderis not an even integer of at least 2.
See also
push_suzukitrotter(),push_lietrotter(),GateDeclReferences
H. Yoshida, “Construction of higher order symplectic integrators”, Phys. Lett. A 150, 262 (1990).
Examples
>>> from mimiqcircuits import * >>> c = Circuit() >>> h = Hamiltonian() >>> h.push(1.0, PauliString("XY"), 0, 1) 2-qubit Hamiltonian with 1 terms: └── 1.0 * XY @ q[0,1] >>> h.push(0.5, PauliString("Z"), 0) 2-qubit Hamiltonian with 2 terms: ├── 1.0 * XY @ q[0,1] └── 0.5 * Z @ q[0] >>> c.push_yoshidatrotter(h, (0, 1), t=1.0, steps=3, order=4) 2-qubit circuit with 3 instructions: ├── yoshida_4(0.3333333333333333) @ q[0:1] ├── yoshida_4(0.3333333333333333) @ q[0:1] └── yoshida_4(0.3333333333333333) @ q[0:1]