qml.pauli.PauliWord¶
- class PauliWord(mapping)[source]¶
Bases:
dictImmutable dictionary used to represent a Pauli Word, associating wires with their respective operators. Can be constructed from a standard dictionary.
Note
An empty
PauliWordwill 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 nowire_orderis passed toPauliWord({}).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
PauliSentencewith the resultingPauliWordas a key and the corresponding coefficient as its value.>>> w1 = PauliWord({0:"X", 1:"Y"}) >>> w2 = PauliWord({1:"X", 2:"Z"}) >>> w1 @ w2 -1j * X(0) @ Z(1) @ Z(2)
We can multiply scalars to Pauli words or add/subtract them, resulting in a
PauliSentenceinstance.>>> 0.5 * w1 - 1.5 * w2 + 2 0.5 * X(0) @ Y(1) + -1.5 * X(1) @ Z(2) + 2 * I
Attributes
Trivial pauli_rep
Track wires in a PauliWord.
- pauli_rep¶
Trivial pauli_rep
- wires¶
Track wires in a PauliWord.
Methods
commutator(other)Compute commutator between a
PauliWord\(P\) and other operator \(O\)commutes_with(other)Fast check if two PauliWords commute with each other
map_wires(wire_map)Return a new PauliWord with the wires mapped.
operation([wire_order])Returns a native PennyLane
Operationrepresenting 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
PauliWordorPauliSentence, this method is faster than computingP @ O - O @ P. It is what is being used incommutator()when settingpauli=True.- Parameters:
other (Union[Operator, PauliWord, PauliSentence]) – Second operator
- Returns:
The commutator result in form of a
PauliSentenceinstances.- Return type:
~PauliSentence
Examples
You can compute commutators between
PauliWordinstances.>>> 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)
- 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.