qml.estimator.compact_hamiltonian.PauliHamiltonian¶
- class PauliHamiltonian(num_qubits, pauli_terms, one_norm=None)[source]¶
Bases:
objectStores 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[str, int] | Iterable[dict]) – A dictionary representing the Hamiltonian terms where the keys are Pauli strings, e.g
"XY", and the values are integers denoting how frequently a Pauli string 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 for more information.one_norm (float | int | None) – the one-norm of the Hamiltonian
- Raises:
TypeError – if
pauli_termsis not a dictionaryValueError – if
one_normis provided but is not a non-negative float or integerValueError – if
pauli_termscontains invalid keys (not Pauli strings) or values (not integers)
See also
TrotterPauli,SelectPauliExample
A
PauliHamiltonianis 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
Usage Details
The terms of the Hamiltonian can also be separated into groups such that all operators in the group commute. Users can instantiate the
PauliHamiltonianby 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
Attributes
The number of qubits the Hamiltonian acts on
The total number of Pauli words in the Hamiltonian
The one-norm of the Hamiltonian
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