qml.pauli.PauliWord

class PauliWord(mapping)[source]

Bases: dict

Immutable dictionary used to represent a Pauli Word, associating wires with their respective operators. Can be constructed from a standard dictionary.

Note

An empty PauliWord will be treated as the multiplicative identity (i.e identity on all wires). Its matrix is the identity matrix (trivially the \(1\times 1\) one matrix when no wire_order is passed to PauliWord({}).to_mat()).

Examples

Initializing a Pauli word:

>>> w = PauliWord({"a": 'X', 2: 'Y', 3: 'Z'})
>>> w
X(a) @ Y(2) @ Z(3)

When multiplying Pauli words together, we obtain a PauliSentence with the resulting PauliWord as a key and the corresponding coefficient as its value.

>>> w1 = PauliWord({0:"X", 1:"Y"})
>>> w2 = PauliWord({1:"X", 2:"Z"})
>>> w1 @ w2
-1j * Z(1) @ Z(2) @ X(0)

We can multiply scalars to Pauli words or add/subtract them, resulting in a PauliSentence instance.

>>> 0.5 * w1 - 1.5 * w2 + 2
0.5 * X(0) @ Y(1)
+ -1.5 * X(1) @ Z(2)
+ 2 * I

pauli_rep

Trivial pauli_rep

wires

Track wires in a PauliWord.

pauli_rep

Trivial pauli_rep

wires

Track wires in a PauliWord.

commutator(other)

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

commutes_with(other)

Fast check if two PauliWords commute with each other

hamiltonian([wire_order])

Return Hamiltonian representing the PauliWord.

map_wires(wire_map)

Return a new PauliWord with the wires mapped.

operation([wire_order, get_as_tensor])

Returns a native PennyLane Operation representing the PauliWord.

to_mat([wire_order, format, coeff])

Returns the matrix representation.

update(_PauliWord__m, **kwargs)

Restrict updating PW after instantiation.

commutator(other)[source]

Compute commutator between a PauliWord \(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 PauliWord instances.

>>> pw = PauliWord({0:"X"})
>>> pw.commutator(PauliWord({0:"Y"}))
2j * Z(0)

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

>>> pw.commutator(qml.Y(0))
2j * Z(0)
commutes_with(other)[source]

Fast check if two PauliWords commute with each other

hamiltonian(wire_order=None)[source]

Return Hamiltonian representing the PauliWord.

map_wires(wire_map)[source]

Return a new PauliWord with the wires mapped.

operation(wire_order=None, get_as_tensor=False)[source]

Returns a native PennyLane Operation representing the PauliWord.

to_mat(wire_order=None, format='dense', coeff=1.0)[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.

  • coeff (float) – Coefficient multiplying the resulting matrix.

Returns

Matrix representation of the Pauli word.

Return type

(Union[NumpyArray, ScipySparseArray])

Raises

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

update(_PauliWord__m, **kwargs)[source]

Restrict updating PW after instantiation.