Skip to content

Commit

Permalink
Addition of operators and scalars + extra dunder methods (#2849)
Browse files Browse the repository at this point in the history
* feat (Operator): ✨ Add sum with scalar.

* chore (TODO): ✏️ Add TODO comment.

* chore (changelog): ✏️ Add feature to changelog.

* chore (changelog): ✏️ Small fix.

* feat (Operator): ✨ Add dunder methods.

* fix (Hamiltonian): 🐛 Call super().__add__().

* test (Operation): 🧪 Add dunder test.

* fix (Operator): 🐛 Raise error if other is of incorrect type.

* refactor (Operation): ♻️ Make use of the SProd class.

* style (operation): 🎨 Import Sum and SProd.

* style (operation): 🎨 Revert import.

* chore (changelog): ✏️ Add feature to changelog.

* test (Operation): 🧪 Modify dunder test.

* test (Operation): 🧪 Modify dunder test.

* docs: 📝 Add TODO comment.
  • Loading branch information
AlbertMitjans authored Jul 26, 2022
1 parent cad746f commit 4837dd7
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 11 deletions.
20 changes: 19 additions & 1 deletion doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,25 @@
RZ**2(1.0, wires=[0])
```

* A `SProd` symbolic class is added that allows users to represent the scalar product
* Added support for addition of operators and scalars. [(#2849)](https://github.com/PennyLaneAI/pennylane/pull/2849)

```pycon
>>> sum_op = 5 + qml.PauliX(0)
>>> sum_op.matrix()
array([[5., 1.],
[1., 5.]])
```

Added `__neg__` and `__sub__` dunder methods to the `qml.operation.Operator` class so that users
can negate and substract operators more naturally.

```pycon
>>> -(-qml.PauliZ(0) + qml.PauliX(0)).matrix()
array([[ 1, -1],
[-1, -1]])
```

* A `SProd` symbolic class is added that allows users to represent the scalar product
of operators. [(#2622)](https://github.com/PennyLaneAI/pennylane/pull/2622)

We can get the matrix, eigenvalues, terms, diagonalizing gates and more.
Expand Down
34 changes: 28 additions & 6 deletions pennylane/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1206,15 +1206,36 @@ def expand(self):
return tape

def __add__(self, other):
r"""The addition operation between Operator objects."""
if isinstance(other, numbers.Number) and other == 0:
return self
"""The addition operation of Operator-Operator objects and Operator-scalar."""
if isinstance(other, numbers.Number):
if other == 0:
return self
return qml.ops.Sum( # pylint: disable=no-member
self,
qml.ops.SProd( # pylint: disable=no-member
scalar=other, base=qml.Identity(wires=self.wires)
),
)
if isinstance(other, Operator):
return qml.ops.Sum(self, other) # pylint: disable=no-member
raise ValueError(f"Cannot add Operator and {type(other)}")

__radd__ = __add__

def __sub__(self, other):
"""The substraction operation of Operator-Operator objects and Operator-scalar."""
if isinstance(other, (Operator, numbers.Number)):
return self + (-other)
raise ValueError(f"Cannot substract {type(other)} from Operator.")

def __rsub__(self, other):
"""The reverse substraction operation of Operator-Operator objects and Operator-scalar."""
return -self + other

def __neg__(self):
"""The negation operation of an Operator object."""
return qml.ops.SProd(scalar=-1, base=self) # pylint: disable=no-member

def __pow__(self, other):
r"""The power operation of an Operator object."""
if isinstance(other, numbers.Number):
Expand Down Expand Up @@ -1678,8 +1699,6 @@ def compare(self, other):

def __add__(self, other):
r"""The addition operation between Observables/Tensors/qml.Hamiltonian objects."""
if isinstance(other, numbers.Number) and other == 0:
return self
if isinstance(other, qml.Hamiltonian):
return other + self
if isinstance(other, (Observable, Tensor)):
Expand All @@ -1705,7 +1724,10 @@ def __sub__(self, other):
r"""The subtraction operation between Observables/Tensors/qml.Hamiltonian objects."""
if isinstance(other, (Observable, Tensor, qml.Hamiltonian)):
return self.__add__(other.__mul__(-1))
raise ValueError(f"Cannot subtract {type(other)} from Observable")
try:
return super().__sub__(other=other)
except ValueError as e:
raise ValueError(f"Cannot subtract {type(other)} from Observable") from e


class Tensor(Observable):
Expand Down
11 changes: 7 additions & 4 deletions pennylane/ops/qubit/hamiltonian.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,9 @@ def simplify(self):
self._grouping_indices = None

def __str__(self):
# Lambda function that formats the wires
wires_print = lambda ob: ",".join(map(str, ob.wires.tolist()))
def wires_print(ob: Observable):
"""Function that formats the wires."""
return ",".join(map(str, ob.wires.tolist()))

list_of_coeffs = self.data # list of scalar tensors
paired_coeff_obs = list(zip(list_of_coeffs, self.ops))
Expand Down Expand Up @@ -589,8 +590,10 @@ def __add__(self, H):
)
ops.append(H)
return qml.Hamiltonian(coeffs, ops, simplify=True)

raise ValueError(f"Cannot add Hamiltonian and {type(H)}")
try:
return super().__add__(other=H)
except ValueError as e:
raise ValueError(f"Cannot add Hamiltonian and {type(H)}") from e

__radd__ = __add__

Expand Down
16 changes: 16 additions & 0 deletions tests/test_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,22 @@ class DummyOp(qml.operation.Operation):
with pytest.raises(ValueError, match="Cannot raise an Operator"):
_ = DummyOp(wires=[0]) ** DummyOp(wires=[0])

def test_sum_with_scalar(self):
"""Test the __sum__ dunder method with a scalar value."""
sum_op = 5 + qml.PauliX(0)
final_op = qml.ops.Sum(qml.ops.s_prod(5, qml.Identity(0)), qml.PauliX(0))
# TODO: Use qml.equal when fixed.
assert np.allclose(sum_op.matrix(), final_op.matrix(), rtol=0)

def test_dunder_methods(self):
"""Test the __sub__, __rsub__ and __neg__ dunder methods."""
sum_op = qml.PauliX(0) - 5
sum_op_2 = -(5 - qml.PauliX(0))
assert np.allclose(a=sum_op.matrix(), b=np.array([[-5, 1], [1, -5]]), rtol=0)
assert np.allclose(a=sum_op.matrix(), b=sum_op_2.matrix(), rtol=0)
neg_op = -qml.PauliX(0)
assert np.allclose(a=neg_op.matrix(), b=np.array([[0, -1], [-1, 0]]), rtol=0)


class TestInverse:
"""Test inverse of operations"""
Expand Down

0 comments on commit 4837dd7

Please sign in to comment.