qml.sum

sum(*summands, 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.

Returns

The operator representing the sum of summands.

Return type

Sum

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'

See also

Sum

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]])