qml.pauli.PauliSentence¶
- class PauliSentence[source]¶
Bases:
dictDictionary representing a linear combination of Pauli words, with the keys as
PauliWordinstances and the values correspond to coefficients.Note
An empty
PauliSentencewill be treated as the additive identity (i.e0 * Identity()). Its matrix is the all-zero matrix (trivially the \(1\times 1\) zero matrix when nowire_orderis passed toPauliSentence({}).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
PauliWordPauliWord({})respresents the identity, the emptyPauliSentencerepresents 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) 2j * Z(0) @ X(1) + 2j * X(0) @ Z(1)
Note that we need to specify
pauli=Trueascommutator()returns PennyLane operators by default.Attributes
Trivial pauli_rep
Track wires of the PauliSentence.
- pauli_rep¶
Trivial pauli_rep
- wires¶
Track wires of the PauliSentence.
Methods
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.
map_wires(wire_map)Return a new PauliSentence with the wires mapped.
operation([wire_order])Returns a native PennyLane
Operationrepresenting 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
PauliSentenceinstance- 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
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
PauliSentenceinstances.>>> 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.
- operation(wire_order=())[source]¶
Returns a native PennyLane
Operationrepresenting 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 ** 30bytes 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
PauliSentenceinstance\[\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