qml.Hamiltonian¶

class Hamiltonian(coeffs, observables)[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

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.2) [X0 Z1] + (-0.543) [Z0 H2]

Alternatively, the generate_hamiltonian() function from the Quantum Chemistry module can be used to generate a molecular Hamiltonian.

 coeffs Return the coefficients defining the Hamiltonian. ops Return the operators defining the Hamiltonian. terms The terms of the Hamiltonian expression $$\sum_{k=0}^{N-1}$$ c_k O_k
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)