Represents operator defined by linear combination of PauliStrings.
cirq.PauliSum(
linear_dict: Optional[value.LinearDict[UnitPauliStringT]] = None
)
Since cirq.PauliString
s store their own coefficients, this class
does not implement the cirq.LinearDict
interface. Instead, you can
add and subtract terms and then iterate over the resulting
(simplified) expression.
Under the hood, this class is backed by a LinearDict with coefficient-less
PauliStrings as keys. PauliStrings are reconstructed on-the-fly during
iteration.
PauliSums can be constructed explicitly:
a, b = cirq.GridQubit.rect(1, 2)
psum = cirq.PauliSum.from_pauli_strings([
cirq.PauliString(-1, cirq.X(a), cirq.Y(b)),
cirq.PauliString(2, cirq.Z(a), cirq.Z(b)),
cirq.PauliString(0.5, cirq.Y(a), cirq.Y(b))
])
print(psum)
-1.000*X(q(0, 0))*Y(q(0, 1))+2.000*Z(q(0, 0))*Z(q(0, 1))+0.500*Y(q(0, 0))*Y(q(0, 1))
or implicitly:
a, b = cirq.GridQubit.rect(1, 2)
psum = cirq.X(a) * cirq.X(b) + 3.0 * cirq.Y(a)
print(psum)
1.000*X(q(0, 0))*X(q(0, 1))+3.000*Y(q(0, 0))
basic arithmetic and expectation operations are supported as well:
a, b = cirq.GridQubit.rect(1, 2)
psum = cirq.X(a) * cirq.X(b) + 3.0 * cirq.Y(a)
two_psum = 2 * psum
four_psum = two_psum + two_psum
print(four_psum)
4.000*X(q(0, 0))*X(q(0, 1))+12.000*Y(q(0, 0))
expectation = four_psum.expectation_from_state_vector(
np.array([0.707106, 0, 0, 0.707106], dtype=complex),
qubit_map={a: 0, b: 1}
)
print(f'{expectation:.1f}')
4.0+0.0j
Raises |
ValueError
|
If structure of linear_dict contains tuples other than the
form (cirq.Qid , cirq.Pauli ).
|
Attributes |
qubits
|
The sorted list of qubits used in this PauliSum.
|
Methods
copy
View source
copy() -> 'PauliSum'
Return a copy of this PauliSum.
Returns: A copy of this PauliSum.
expectation_from_density_matrix
View source
expectation_from_density_matrix(
state: np.ndarray,
qubit_map: Mapping[cirq.Qid
, int],
*,
atol: float = 1e-07,
check_preconditions: bool = True
) -> float
Evaluate the expectation of this PauliSum given a density matrix.
See PauliString.expectation_from_density_matrix
.
Args |
state
|
An array representing a valid density matrix.
|
qubit_map
|
A map from all qubits used in this PauliSum to the
indices of the qubits that state is defined over.
|
atol
|
Absolute numerical tolerance.
|
check_preconditions
|
Whether to check that state represents a
valid density matrix.
|
Returns |
The expectation value of the input state.
|
Raises |
NotImplementedError
|
If any of the coefficients are imaginary,
so that this is not Hermitian.
|
TypeError
|
If the input state is not a complex type.
|
ValueError
|
If the input vector is not the correct size or shape.
|
expectation_from_state_vector
View source
expectation_from_state_vector(
state_vector: np.ndarray,
qubit_map: Mapping[cirq.Qid
, int],
*,
atol: float = 1e-07,
check_preconditions: bool = True
) -> float
Evaluate the expectation of this PauliSum given a state vector.
See PauliString.expectation_from_state_vector
.
Args |
state_vector
|
An array representing a valid state vector.
|
qubit_map
|
A map from all qubits used in this PauliSum to the
indices of the qubits that state_vector is defined over.
|
atol
|
Absolute numerical tolerance.
|
check_preconditions
|
Whether to check that state_vector represents
a valid state vector.
|
Returns |
The expectation value of the input state.
|
Raises |
NotImplementedError
|
If any of the coefficients are imaginary,
so that this is not Hermitian.
|
TypeError
|
If the input state is not a complex type.
|
ValueError
|
If the input vector is not the correct size or shape.
|
from_boolean_expression
View source
@classmethod
from_boolean_expression(
boolean_expr: Expr, qubit_map: Dict[str, 'cirq.Qid']
) -> 'PauliSum'
Builds the Hamiltonian representation of a Boolean expression.
This is based on "On the representation of Boolean and real functions as Hamiltonians for
quantum computing" by Stuart Hadfield, https://arxiv.org/abs/1804.09130
Args |
boolean_expr
|
A Sympy expression containing symbols and Boolean operations
|
qubit_map
|
map of string (boolean variable name) to qubit.
|
Return |
The PauliSum that represents the Boolean expression.
|
Raises |
ValueError
|
If boolean_expr is of an unsupported type.
|
from_pauli_strings
View source
@classmethod
from_pauli_strings(
terms: Union[PauliString, List[PauliString]]
) -> 'PauliSum'
Returns a PauliSum by combining cirq.PauliString
terms.
Returns |
PauliSum object representing the addition of all the cirq.PauliString
terms in terms .
|
matrix
View source
matrix(
qubits: Optional[Iterable[raw_types.Qid]] = None
) -> np.ndarray
Returns the matrix of this PauliSum in computational basis of qubits.
Args |
qubits
|
Ordered collection of qubits that determine the subspace
in which the matrix representation of the Pauli sum is to
be computed. If none is provided the default ordering of
self.qubits is used. Qubits present in qubits but absent from
self.qubits are acted on by the identity.
|
Returns |
np.ndarray representing the matrix of this PauliSum expression.
|
Raises |
TypeError
|
if any of the gates in self does not provide a unitary.
|
with_qubits
View source
with_qubits(
*new_qubits
) -> 'PauliSum'
Return a new PauliSum on new_qubits
.
Args |
*new_qubits
|
cirq.Qid objects to replace existing
qubit objects in this PauliSum.
|
Returns |
PauliSum with new_qubits replacing the previous
qubits.
|
Raises |
ValueError
|
If len(new_qubits) != len(self.qubits).
|
wrap
View source
@staticmethod
wrap(
val: cirq.PauliSumLike
) -> 'PauliSum'
Convert a cirq.PauliSumLike
object to a PauliSum
Attempts to convert an existing int, float, complex, cirq.PauliString
,
cirq.PauliSum
or cirq.SingleQubitPauliStringGateOperation
into
a cirq.PauliSum
object. For example:
my_psum = cirq.PauliSum.wrap(2.345)
my_psum
cirq.PauliSum(cirq.LinearDict({frozenset(): (2.345+0j)}))
Returns |
PauliSum representation of val .
|
__add__
View source
__add__(
other
)
__bool__
View source
__bool__() -> bool
__eq__
View source
__eq__(
other: _SupportsValueEquality
) -> bool
__iter__
View source
__iter__()
__len__
View source
__len__() -> int
__mul__
View source
__mul__(
other: cirq.PauliSumLike
)
__ne__
View source
__ne__(
other: _SupportsValueEquality
) -> bool
__neg__
View source
__neg__()
__pow__
View source
__pow__(
exponent: int
)
__radd__
View source
__radd__(
other
)
__rmul__
View source
__rmul__(
other: cirq.PauliSumLike
)
__rsub__
View source
__rsub__(
other
)
__sub__
View source
__sub__(
other
)
__truediv__
View source
__truediv__(
a: value.Scalar
)