qml.sum¶
- sum(*summands, grouping_type=None, method='lf', id=None, lazy=True)[source]¶
Construct an operator which is the sum of the given operators.
- Parameters
*summands (tuple[Operator]) – the operators we want to sum together.
- Keyword Arguments
id (str or None) – id for the Sum operator. Default is None.
lazy=True (bool) – If
lazy=False
, a simplification will be performed such that when any of the operators is already a sum operator, its operands (summands) will be used instead.grouping_type (str) – The type of binary relation between Pauli words used to compute the grouping. Can be
'qwc'
,'commuting'
, or'anticommuting'
.method (str) – The graph colouring heuristic to use in solving minimum clique cover for grouping. It can be
'lf'
(Largest First),'rlf'
(Recursive Largest First),'dsatur'
(Degree of Saturation), or'gis'
(Greedy Independent Set). This keyword argument is ignored ifgrouping_type
isNone
.
- Returns
The operator representing the sum of summands.
- Return type
Note
This operator supports batched operands:
>>> op = qml.sum(qml.RX(np.array([1, 2, 3]), wires=0), qml.X(1)) >>> op.matrix().shape (3, 4, 4)
But it doesn’t support batching of operators:
>>> op = qml.sum(np.array([qml.RX(0.4, 0), qml.RZ(0.3, 0)]), qml.Z(0)) AttributeError: 'numpy.ndarray' object has no attribute 'wires'
Note
If grouping is requested, the computed groupings are stored as a list of list of indices in
Sum.grouping_indices
. The indices refer to the operators and coefficients returned bySum.terms()
, notSum.operands
, as these are not guaranteed to be equivalent.See also
Example
>>> summed_op = qml.sum(qml.X(0), qml.Z(0)) >>> summed_op X(0) + Z(0) >>> summed_op.matrix() array([[ 1, 1], [ 1, -1]])
Grouping
Grouping information can be collected during construction using the
grouping_type
andmethod
keyword arguments. For example:import pennylane as qml a = qml.s_prod(1.0, qml.X(0)) b = qml.s_prod(2.0, qml.prod(qml.X(0), qml.X(1))) c = qml.s_prod(3.0, qml.Z(0)) op = qml.sum(a, b, c, grouping_type="qwc")
>>> op.grouping_indices ((2,), (0, 1))
grouping_type
can be"qwc"
(qubit-wise commuting),"commuting"
, or"anticommuting"
, andmethod
can be'lf'
(Largest First),'rlf'
(Recursive Largest First),'dsatur'
(Degree of Saturation), or'gis'
(Greedy Independent Set). To see more details about how these affect grouping, see Pauli Graph Colouring andcompute_partition_indices()
.