qml.estimator.compact_hamiltonian.PauliHamiltonian

class PauliHamiltonian(num_qubits, pauli_terms, one_norm=None)[source]

Bases: object

Stores the minimum necessary information required to estimate resources for a Hamiltonian expressed as a linear combination of tensor products of Pauli operators.

Parameters:
  • num_qubits (int) – total number of qubits the Hamiltonian acts on

  • pauli_terms (dict | Iterable(dict)) – A representation for all of the terms (Pauli words) of the Hamiltonian. The terms of the Hamiltonian can also be separated into groups such that all Pauli words in a group commute. When a single dictionary is provided, it should represent all the terms of the Hamiltonian where the dictionary keys are Pauli strings (e.g "XY" or "Z") and the values are integers corresponding to how frequently that Pauli word appears in the Hamiltonian. When a list of dictionaries is provided, each dictionary is interpreted as a commuting group of terms. See the Usage Details section below for more information.

  • one_norm (float | int | None) – the one-norm of the Hamiltonian

Returns:

An instance of PauliHamiltonian

Return type:

PauliHamiltonian

See also

TrotterPauli

Example

A PauliHamiltonian is a compact representation which can be used with compatible templates to obtain resource estimates. Consider for example the Hamiltonian:

\[\hat{H} = 0.1 \cdot \Sigma^{30}_{j=1} \hat{X}_{j} \hat{X}_{j+1} - 0.05 \cdot \Sigma^{30}_{k=1} \hat{Y}_{k} \hat{Y}_{k+1} + 0.25 \cdot \Sigma^{40}_{l=1} \hat{X}_{l}\]

This Hamiltonian is represented in a compact form using PauliHamiltonian:

>>> import pennylane.estimator as qre
>>> pauli_ham = qre.PauliHamiltonian(
...     num_qubits = 40,
...     pauli_terms = {"X":40, "XX":30, "YY":30},
...     one_norm = 14.5,  # (|0.1| * 30) + (|-0.05| * 30) + (|0.25| * 40)
... )
>>> pauli_ham
PauliHamiltonian(num_qubits=40, one_norm=14.5, pauli_terms={'X': 40, 'XX': 30, 'YY': 30})

The Hamiltonian can be used as input for other subroutines, like TrotterPauli:

>>> num_steps, order = (10, 2)
>>> res = qre.estimate(qre.TrotterPauli(pauli_ham, num_steps, order))
>>> print(res)
--- Resources: ---
 Total wires: 40
   algorithmic wires: 40
   allocated wires: 0
     zero state: 0
     any state: 0
 Total gates : 9.400E+4
   'T': 8.800E+4,
   'CNOT': 2.400E+3,
   'Z': 1.200E+3,
   'S': 2.400E+3

The terms of the Hamiltonian can also be separated into groups such that all operators in the group commute. Users can instantiate the PauliHamiltonian by specifying these groups of terms directly.

>>> import pennylane.estimator as qre
>>> commuting_groups = [
...     {"X": 40, "XX": 30}, # first commuting group
...     {"YY": 30}, # second commuting group
... ]
>>> pauli_ham = qre.PauliHamiltonian(
...     num_qubits = 40,
...     pauli_terms = commuting_groups,
...     one_norm = 14.5,  # (|0.1| * 30) + (|-0.05| * 30) + (|0.25| * 40)
... )
>>> pauli_ham
PauliHamiltonian(num_qubits=40, one_norm=14.5, pauli_terms=[{'X': 40, 'XX': 30}, {'YY': 30}])

Note that providing more information will generally lead to more accurate resource estimates.

>>> num_steps, order = (10, 2)
>>> res = qre.estimate(qre.TrotterPauli(pauli_ham, num_steps, order))
>>> print(res)
--- Resources: ---
 Total wires: 40
   algorithmic wires: 40
   allocated wires: 0
     zero state: 0
     any state: 0
 Total gates : 5.014E+4
   'T': 4.708E+4,
   'CNOT': 1.260E+3,
   'Z': 600,
   'S': 1.200E+3

num_qubits

The number of qubits the Hamiltonian acts on

num_terms

The total number of Pauli words in the Hamiltonian.

one_norm

The one-norm of the Hamiltonian.

pauli_terms

A dictionary representing the distribution of Pauli words in the Hamiltonian

num_qubits

The number of qubits the Hamiltonian acts on

num_terms

The total number of Pauli words in the Hamiltonian.

one_norm

The one-norm of the Hamiltonian.

pauli_terms

A dictionary representing the distribution of Pauli words in the Hamiltonian