Skip to content

Commit

Permalink
add a setter to avoid breaking Observable.return_type (#4798)
Browse files Browse the repository at this point in the history
**Context:**
Some plugin tests failed because they were setting this property, which
I did not expect.

**Description of the Change:**
Add a setter to the `Observable.return_type` property to avoid breaking
things in this release. Also warn if someone uses it.

**Benefits:**
We don't break `Observable.return_type` in this release.

**Possible Drawbacks:**
People can still set it? idk seems like a safe call for now. I'm not
validating the type provided

**Related GitHub Issues:**
missed in #4762
  • Loading branch information
timmysilv authored Nov 8, 2023
1 parent 2c7c0c1 commit e5c42ea
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
* `Observable.return_type` is deprecated. Instead, you should inspect the type
of the surrounding measurement process.
[(#4762)](https://github.com/PennyLaneAI/pennylane/pull/4762)
[(#4798)](https://github.com/PennyLaneAI/pennylane/pull/4798)

<h3>Documentation 📝</h3>

Expand Down
10 changes: 10 additions & 0 deletions pennylane/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1887,6 +1887,16 @@ def return_type(self):
)
return self._return_type

@return_type.setter
def return_type(self, value):
"""Change the return type of an Observable. Note that this property is deprecated."""
warnings.warn(
"`Observable.return_type` is deprecated. Instead, you should "
"create a measurement process containing this Observable.",
UserWarning,
)
self._return_type = value

def __matmul__(self, other):
if active_new_opmath():
return super().__matmul__(other=other)
Expand Down
5 changes: 5 additions & 0 deletions tests/test_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,11 @@ class DummyObserv(qml.operation.Observable):
with pytest.warns(UserWarning, match="`Observable.return_type` is deprecated. Instead"):
assert DummyObserv(0, wires=[1]).return_type is None

obs = DummyObserv(0, wires=[1])
with pytest.warns(UserWarning, match="`Observable.return_type` is deprecated. Instead"):
# pylint:disable=attribute-defined-outside-init
obs.return_type = qml.measurements.Sample

def test_construction_with_wires_pos_arg(self):
"""Test that the wires can be given as a positional argument"""

Expand Down

0 comments on commit e5c42ea

Please sign in to comment.