qml.labs.dla.batched_pauli_decompose

batched_pauli_decompose(H, tol=None, pauli=False)[source]

Decomposes a Hermitian matrix or a batch of matrices into a linear combination of Pauli operators.

Parameters:
  • H (tensor_like[complex]) – a Hermitian matrix of dimension (2**n, 2**n) or a collection of Hermitian matrices of dimension (batch, 2**n, 2**n).

  • tol (float) – Tolerance below which Pauli coefficients are discarded.

  • pauli (bool) – Whether to format the output as PauliSentence.

Returns:

the matrix (matrices) decomposed as a linear combination of Pauli operators, returned either as a Hamiltonian or PauliSentence instance.

Return type:

Union[Hamiltonian, PauliSentence]

Examples

Consider the Hamiltonian \(H=\frac{1}{4} X_0 + \frac{2}{5} Z_0 X_1\). We can compute its matrix and get back the Pauli representation via batched_pauli_decompose.

>>> from pennylane.labs.dla import batched_pauli_decompose
>>> H = 1 / 4 * qml.X(0) + 2 / 5 * qml.Z(0) @ qml.X(1)
>>> mat = H.matrix()
>>> op = batched_pauli_decompose(mat)
>>> op
0.25 * X(1) + 0.4 * Z(1)
>>> type(op)
pennylane.ops.op_math.sum.Sum

We can choose to receive a PauliSentence instead as output instead, by setting pauli=True:

>>> op = batched_pauli_decompose(mat, pauli=True)
>>> type(op)
pennylane.pauli.pauli_arithmetic.PauliSentence

This function supports batching and will return a list of operations for a batched input:

>>> ops = [1 / 4 * qml.X(0), 1 / 2 * qml.Z(0) + 1e-7 * qml.Y(0)]
>>> batch = np.stack([op.matrix() for op in ops])
>>> batched_pauli_decompose(batch)
[0.25 * X(0), 1e-07 * Y(0) + 0.5 * Z(0)]

Small contributions can be removed by specifying the tol parameter, which defaults to 1e-10, accordingly:

>>> batched_pauli_decompose(batch, tol=1e-6)
[0.25 * X(0), 0.5 * Z(0)]