qml.sum

sum(*summands, do_queue=True, 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
  • do_queue (bool) – determines if the sum operator will be queued (currently not supported). Default is True.

  • 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.op_sum(qml.RX(np.array([1, 2, 3]), wires=0), qml.PauliX(1))
>>> op.matrix().shape
(3, 4, 4)

But it doesn’t support batching of operators:

>>> op = qml.op_sum(np.array([qml.RX(0.4, 0), qml.RZ(0.3, 0)]), qml.PauliZ(0))
AttributeError: 'numpy.ndarray' object has no attribute 'wires'

See also

Sum

Example

>>> summed_op = qml.sum(qml.PauliX(0), qml.PauliZ(0))
>>> summed_op
PauliX(wires=[0]) + PauliZ(wires=[0])
>>> summed_op.matrix()
array([[ 1,  1],
       [ 1, -1]])