diff --git a/qiskit/quantum_info/operators/mixins/linear.py b/qiskit/quantum_info/operators/mixins/linear.py index a512df475a45..cfd3e8f3c446 100644 --- a/qiskit/quantum_info/operators/mixins/linear.py +++ b/qiskit/quantum_info/operators/mixins/linear.py @@ -38,13 +38,35 @@ class LinearMixin(MultiplyMixin, ABC): """ def __add__(self, other): + # enable easy use of sum(...) + if isinstance(other, int) and other == 0: + return self + + qargs = getattr(other, "qargs", None) + return self._add(other, qargs=qargs) + + def __radd__(self, other): + # enable easy use of sum(...) + if isinstance(other, int) and other == 0: + return self + qargs = getattr(other, "qargs", None) return self._add(other, qargs=qargs) def __sub__(self, other): + if isinstance(other, int) and other == 0: + return self + qargs = getattr(other, "qargs", None) return self._add(-other, qargs=qargs) + def __rsub__(self, other): + if isinstance(other, int) and other == 0: + return -self + + qargs = getattr(other, "qargs", None) + return (-self)._add(other, qargs=qargs) + @abstractmethod def _add(self, other, qargs=None): """Return the CLASS self + other. diff --git a/releasenotes/notes/base-operators-sums-d331e78a9fa4b5d8.yaml b/releasenotes/notes/base-operators-sums-d331e78a9fa4b5d8.yaml new file mode 100644 index 000000000000..26b691c4798f --- /dev/null +++ b/releasenotes/notes/base-operators-sums-d331e78a9fa4b5d8.yaml @@ -0,0 +1,4 @@ +--- +features: + - | + ``BaseOperator`` subclasses (like ScalarOp, SparsePauliOp and PauliList) can now be used with ``sum(...)`` diff --git a/test/python/quantum_info/operators/test_scalar_op.py b/test/python/quantum_info/operators/test_scalar_op.py index 45079f8d0cc6..16507948dd8a 100644 --- a/test/python/quantum_info/operators/test_scalar_op.py +++ b/test/python/quantum_info/operators/test_scalar_op.py @@ -168,6 +168,27 @@ def test_add(self, coeff1, coeff2): target = coeff1 + coeff2 self.assertScalarOp(val, dims, target) + @combine(coeff1=[0, 1, -3.1, 1 + 3j]) + def test_radd(self, coeff1): + """Test right-side addition with ScalarOp.""" + dims = (3, 2) + op1 = ScalarOp(dims, coeff=coeff1) + + val = op1 + 0 + self.assertScalarOp(val, dims, coeff1) + + @combine(coeff1=[0, 1, -3.1, 1 + 3j], coeff2=[-1, -5.1 - 2j]) + def test_sum(self, coeff1, coeff2): + """Test add operation with ScalarOp. ({coeff1} + {coeff2})""" + # Add two ScalarOps + dims = (3, 2) + op1 = ScalarOp(dims, coeff=coeff1) + op2 = ScalarOp(6, coeff=coeff2) + + val = sum([op1, op2]) + target = coeff1 + coeff2 + self.assertScalarOp(val, dims, target) + @combine(coeff1=[0, 1, -3.1, 1 + 3j], coeff2=[-1, -5.1 - 2j]) def test_subtract(self, coeff1, coeff2): """Test add operation with ScalarOp. ({coeff1} - {coeff2})""" @@ -179,6 +200,15 @@ def test_subtract(self, coeff1, coeff2): target = coeff1 - coeff2 self.assertScalarOp(val, dims, target) + @combine(coeff1=[0, 1, -3.1, 1 + 3j]) + def test_rsub(self, coeff1): + """Test right-side subtraction with ScalarOp.""" + dims = (3, 2) + op1 = ScalarOp(dims, coeff=coeff1) + + val = 0 - op1 + self.assertScalarOp(val, dims, -coeff1) + @combine( coeff1=[0, 1, -3.1, 1 + 3j], coeff2=[-1, -5.1 - 2j],