qml.utils.decompose_hamiltonian¶
-
decompose_hamiltonian
(H, hide_identity=False, wire_order=None)[source]¶ Decomposes a Hermitian matrix into a linear combination of Pauli operators.
- Parameters
H (array[complex]) – a Hermitian matrix of dimension \(2^n\times 2^n\)
hide_identity (bool) – does not include the
Identity
observable within the tensor products of the decomposition ifTrue
- Returns
a list of coefficients and a list of corresponding tensor products of Pauli observables that decompose the Hamiltonian.
- Return type
tuple[list[float], list[Observable]]
Example:
We can use this function to compute the Pauli operator decomposition of an arbitrary Hermitian matrix:
>>> A = np.array( ... [[-2, -2+1j, -2, -2], [-2-1j, 0, 0, -1], [-2, 0, -2, -1], [-2, -1, -1, 0]]) >>> coeffs, obs_list = decompose_hamiltonian(A) >>> coeffs [-1.0, -1.5, -0.5, -1.0, -1.5, -1.0, -0.5, 1.0, -0.5, -0.5]
We can use the output coefficients and tensor Pauli terms to construct a
Hamiltonian
:>>> H = qml.Hamiltonian(coeffs, obs_list) >>> print(H) (-1.0) [I0 I1] + (-1.5) [X1] + (-0.5) [Y1] + (-1.0) [Z1] + (-1.5) [X0] + (-1.0) [X0 X1] + (-0.5) [X0 Z1] + (1.0) [Y0 Y1] + (-0.5) [Z0 X1] + (-0.5) [Z0 Y1]
This Hamiltonian can then be used in defining VQE problems using
ExpvalCost
.
code/api/pennylane.utils.decompose_hamiltonian
Download Python script
Download Notebook
View on GitHub