qml.Hamiltonian¶
-
class
Hamiltonian
(coeffs, observables, simplify=False)[source]¶ Bases:
object
Lightweight class for representing Hamiltonians for Variational Quantum Eigensolver problems.
Hamiltonians can be expressed as linear combinations of observables, e.g., \(\sum_{k=0}^{N-1} c_k O_k\).
This class keeps track of the terms (coefficients and observables) separately.
- Parameters
coeffs (Iterable[float]) – coefficients of the Hamiltonian expression
observables (Iterable[Observable]) – observables in the Hamiltonian expression
simplify (bool) – Specifies whether the Hamiltonian is simplified upon initialization (like-terms are combined). The default value is False.
See also
Example:
A Hamiltonian can be created by simply passing the list of coefficients as well as the list of observables:
>>> coeffs = [0.2, -0.543] >>> obs = [qml.PauliX(0) @ qml.PauliZ(1), qml.PauliZ(0) @ qml.Hadamard(2)] >>> H = qml.Hamiltonian(coeffs, obs) >>> print(H) (-0.543) [Z0 H2] + (0.2) [X0 Z1]
The user can also provide custom observables:
>>> obs_matrix = np.array([[0.5, 1.0j, 0.0, -3j], [-1.0j, -1.1, 0.0, -0.1], [0.0, 0.0, -0.9, 12.0], [3j, -0.1, 12.0, 0.0]]) >>> obs = qml.Hermitian(obs_matrix, wires=[0, 1]) >>> H = qml.Hamiltonian((0.8, ), (obs, )) >>> print(H) (0.8) [Hermitian0'1]
Alternatively, the
molecular_hamiltonian()
function from the Quantum Chemistry module can be used to generate a molecular Hamiltonian.Attributes
Return the coefficients defining the Hamiltonian.
Return the operators defining the Hamiltonian.
The terms of the Hamiltonian expression \(\sum_{k=0}^{N-1} c_k O_k\)
The sorted union of wires from all operators.
-
coeffs
¶ Return the coefficients defining the Hamiltonian.
- Returns
coefficients in the Hamiltonian expression
- Return type
Iterable[float])
-
ops
¶ Return the operators defining the Hamiltonian.
- Returns
observables in the Hamiltonian expression
- Return type
Iterable[Observable])
-
terms
¶ The terms of the Hamiltonian expression \(\sum_{k=0}^{N-1} c_k O_k\)
- Returns
tuples of coefficients and operations, each of length N
- Return type
(tuple, tuple)
Methods
compare
(H)Compares with another
Hamiltonian
,Observable
, orTensor
, to determine if they are equivalent.simplify
()Simplifies the Hamiltonian by combining like-terms.
-
compare
(H)[source]¶ Compares with another
Hamiltonian
,Observable
, orTensor
, to determine if they are equivalent.Hamiltonians/observables are equivalent if they represent the same operator (their matrix representations are equal), and they are defined on the same wires.
Warning
The compare method does not check if the matrix representation of a
Hermitian
observable is equal to an equivalent observable expressed in terms of Pauli matrices, or as a linear combination of Hermitians. To do so would require the matrix form of Hamiltonians and Tensors be calculated, which would drastically increase runtime.- Returns
True if equivalent.
- Return type
(bool)
Examples
>>> A = np.array([[1, 0], [0, -1]]) >>> H = qml.Hamiltonian( ... [0.5, 0.5], ... [qml.Hermitian(A, 0) @ qml.PauliY(1), qml.PauliY(1) @ qml.Hermitian(A, 0) @ qml.Identity("a")] ... ) >>> obs = qml.Hermitian(A, 0) @ qml.PauliY(1) >>> print(H.compare(obs)) True
>>> H1 = qml.Hamiltonian([1, 1], [qml.PauliX(0), qml.PauliZ(1)]) >>> H2 = qml.Hamiltonian([1, 1], [qml.PauliZ(0), qml.PauliX(1)]) >>> H1.compare(H2) False
>>> ob1 = qml.Hamiltonian([1], [qml.PauliX(0)]) >>> ob2 = qml.Hermitian(np.array([[0, 1], [1, 0]]), 0) >>> ob1.compare(ob2) False
Contents
Using PennyLane
Development
API
Downloads