qml.pauli.PauliSentence

class PauliSentence[source]

Bases: dict

Dictionary representing a linear combination of Pauli words, with the keys as PauliWord instances and the values correspond to coefficients.

Note

An empty PauliSentence will be treated as the additive identity (i.e 0 * Identity()). Its matrix is the all-zero matrix (trivially the \(1\times 1\) zero matrix when no wire_order is passed to PauliSentence({}).to_mat()).

Examples

>>> ps = PauliSentence({
        PauliWord({0:'X', 1:'Y'}): 1.23,
        PauliWord({2:'Z', 0:'Y'}): -0.45j
    })
>>> ps
1.23 * X(0) @ Y(1)
+ (-0-0.45j) * Z(2) @ Y(0)

Combining Pauli words automatically results in Pauli sentences that can be used to construct more complicated operators.

>>> w1 = PauliWord({0:"X", 1:"Y"})
>>> w2 = PauliWord({1:"X", 2:"Z"})
>>> ps = 0.5 * w1 - 1.5 * w2 + 2
>>> ps + PauliWord({3:"Z"}) - 1
0.5 * X(0) @ Y(1)
+ -1.5 * X(1) @ Z(2)
+ 1 * I
+ 1.0 * Z(3)

Note that while the empty PauliWord PauliWord({}) respresents the identity, the empty PauliSentence represents 0

>>> PauliSentence({})
0 * I

We can compute commutators using the PauliSentence.commutator() method

>>> op1 = PauliWord({0:"X", 1:"X"})
>>> op2 = PauliWord({0:"Y"}) + PauliWord({1:"Y"})
>>> op1.commutator(op2)
2j * Z(0) @ X(1)
+ 2j * X(0) @ Z(1)

Or, alternatively, use commutator().

>>> qml.commutator(op1, op2, pauli=True)

Note that we need to specify pauli=True as commutator() returns PennyLane operators by default.

pauli_rep

Trivial pauli_rep

wires

Track wires of the PauliSentence.

pauli_rep

Trivial pauli_rep

wires

Track wires of the PauliSentence.

commutator(other)

Compute commutator between a PauliSentence \(P\) and other operator \(O\)

dot(vector[, wire_order])

Computes the matrix-vector product of the Pauli sentence with a state vector.

hamiltonian([wire_order])

Returns a native PennyLane Hamiltonian representing the PauliSentence.

map_wires(wire_map)

Return a new PauliSentence with the wires mapped.

operation([wire_order])

Returns a native PennyLane Operation representing the PauliSentence.

simplify([tol])

Remove any PauliWords in the PauliSentence with coefficients less than the threshold tolerance.

to_mat([wire_order, format, buffer_size])

Returns the matrix representation.

trace()

Return the normalized trace of the PauliSentence instance

commutator(other)[source]

Compute commutator between a PauliSentence \(P\) and other operator \(O\)

\[[P, O] = P O - O P\]

When the other operator is a PauliWord or PauliSentence, this method is faster than computing P @ O - O @ P. It is what is being used in commutator() when setting pauli=True.

Parameters

other (Union[Operator, PauliWord, PauliSentence]) – Second operator

Returns

The commutator result in form of a PauliSentence instances.

Return type

~PauliSentence

Examples

You can compute commutators between PauliSentence instances.

>>> pw1 = PauliWord({0:"X"})
>>> pw2 = PauliWord({1:"X"})
>>> ps1 = PauliSentence({pw1: 1., pw2: 2.})
>>> ps2 = PauliSentence({pw1: 0.5j, pw2: 1j})
>>> ps1.commutator(ps2)
0 * I

You can also compute the commutator with other operator types if they have a Pauli representation.

>>> ps1.commutator(qml.Y(0))
2j * Z(0)
dot(vector, wire_order=None)[source]

Computes the matrix-vector product of the Pauli sentence with a state vector. See pauli_sparse_matrices.md for the technical details.

hamiltonian(wire_order=None)[source]

Returns a native PennyLane Hamiltonian representing the PauliSentence.

Warning

hamiltonian() is deprecated. Instead, please use operation()

map_wires(wire_map)[source]

Return a new PauliSentence with the wires mapped.

operation(wire_order=None)[source]

Returns a native PennyLane Operation representing the PauliSentence.

simplify(tol=1e-08)[source]

Remove any PauliWords in the PauliSentence with coefficients less than the threshold tolerance.

to_mat(wire_order=None, format='dense', buffer_size=None)[source]

Returns the matrix representation.

Keyword Arguments
  • wire_order (iterable or None) – The order of qubits in the tensor product.

  • format (str) – The format of the matrix. It is “dense” by default. Use “csr” for sparse.

  • buffer_size (int or None) – The maximum allowed memory in bytes to store intermediate results in the calculation of sparse matrices. It defaults to 2 ** 30 bytes that make 1GB of memory. In general, larger buffers allow faster computations.

Returns

Matrix representation of the Pauli sentence.

Return type

(Union[NumpyArray, ScipySparseArray])

Raises

ValueError – Can’t get the matrix of an empty PauliSentence.

trace()[source]

Return the normalized trace of the PauliSentence instance

\[\frac{1}{2^n} \text{tr}\left( P \right).\]

The normalized trace does not scale with the number of qubits \(n\).

>>> PauliSentence({PauliWord({0:"I", 1:"I"}): 0.5}).trace()
0.5
>>> PauliSentence({PauliWord({}): 0.5}).trace()
0.5