Skip to content

Commit

Permalink
remove observable.return_type (#5044)
Browse files Browse the repository at this point in the history
completing the deprecation cycle. will break braket-*-latest as they
haven't moved yet.

[sc-51274]
  • Loading branch information
timmysilv committed Jan 12, 2024
1 parent 7291e44 commit 4ff93d6
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 50 deletions.
12 changes: 6 additions & 6 deletions doc/development/deprecations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ Pending deprecations
- Deprecated in v0.34
- Will be removed in v0.35

* ``Observable.return_type`` is deprecated. Instead, you should inspect the type
of the surrounding measurement process.

- Deprecated in v0.34
- Will be removed in v0.35

* ``single_tape_transform``, ``batch_transform``, ``qfunc_transform``, and ``op_transform`` are
deprecated. Instead switch to using the new ``qml.transform`` function. Please refer to
`the transform docs <https://docs.pennylane.ai/en/stable/code/qml_transforms.html#custom-transforms>`_
Expand Down Expand Up @@ -114,6 +108,12 @@ Completed deprecation cycles
- Deprecated in v0.33
- Removed in v0.35

* ``Observable.return_type`` has been removed. Instead, you should inspect the type
of the surrounding measurement process.

- Deprecated in v0.34
- Removed in v0.35

* Specifying ``control_values`` passed to ``qml.ctrl`` as a string is no longer supported.

- Deprecated in v0.25
Expand Down
4 changes: 4 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
of `functools.partial`.
[(#5046)](https://github.com/PennyLaneAI/pennylane/pull/5046)

* `Observable.return_type` has been removed. Instead, you should inspect the type
of the surrounding measurement process.
[(#5044)](https://github.com/PennyLaneAI/pennylane/pull/5044)

<h3>Deprecations 👋</h3>

* Matrix and tensor products between `PauliWord` and `PauliSentence` instances are done using the `@` operator, `*` will be used only for scalar multiplication.
Expand Down
25 changes: 0 additions & 25 deletions pennylane/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1859,8 +1859,6 @@ class Observable(Operator):
can be useful for some applications where the instance has to be identified
"""

_return_type = None

@property
def _queue_category(self):
"""Used for sorting objects into their respective lists in `QuantumTape` objects.
Expand All @@ -1883,26 +1881,6 @@ def is_hermitian(self):
"""All observables must be hermitian"""
return True

@property
def return_type(self):
"""None or ObservableReturnTypes: Measurement type that this observable is called with."""
warnings.warn(
"`Observable.return_type` is deprecated. Instead, you should "
"inspect the type of the surrounding measurement process.",
qml.PennyLaneDeprecationWarning,
)
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.",
qml.PennyLaneDeprecationWarning,
)
self._return_type = value

def __matmul__(self, other):
if active_new_opmath():
return super().__matmul__(other=other)
Expand Down Expand Up @@ -2489,8 +2467,6 @@ def prune(self):
"""Returns a pruned tensor product of observables by removing :class:`~.Identity` instances from
the observables building up the :class:`~.Tensor`.
The ``return_type`` attribute is preserved while pruning.
If the tensor product only contains one observable, then this observable instance is
returned.
Expand Down Expand Up @@ -2528,7 +2504,6 @@ def prune(self):
else:
obs = Tensor(*self.non_identity_obs)

obs._return_type = self._return_type
return obs

def map_wires(self, wire_map: dict):
Expand Down
1 change: 0 additions & 1 deletion pennylane/tape/qscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,6 @@ def observables(self) -> List[Union[MeasurementProcess, Observable]]:

for m in self.measurements:
if m.obs is not None:
m.obs._return_type = m.return_type
obs.append(m.obs)
else:
obs.append(m)
Expand Down
1 change: 0 additions & 1 deletion tests/ops/op_math/test_adjoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ class CustomObs(qml.operation.Observable):
assert isinstance(1.0 * ob @ ob, qml.Hamiltonian)

# check the dir
assert "return_type" in dir(ob)
assert "grad_recipe" not in dir(ob)

@pytest.mark.parametrize(
Expand Down
1 change: 0 additions & 1 deletion tests/ops/op_math/test_pow_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ class CustomObs(qml.operation.Observable):
assert isinstance(1.0 * ob @ ob, qml.Hamiltonian)

# check the dir
assert "return_type" in dir(ob)
assert "grad_recipe" not in dir(ob)


Expand Down
16 changes: 0 additions & 16 deletions tests/test_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -922,22 +922,6 @@ class DummyOp(qml.operation.Operation):
class TestObservableConstruction:
"""Test custom observables construction."""

def test_observable_return_type_none(self):
"""Check that the return_type of an observable is initially None"""

class DummyObserv(qml.operation.Observable):
r"""Dummy custom observable"""
num_wires = 1
grad_method = None

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 4ff93d6

Please sign in to comment.