Repository: PennyLaneAI/pennylane
Pull request #3433 (2022-11-29 08:55:45)

Deprecate `observables` argument in `QubitDevice.statistics`
Add a deprecation warning when using `QubitDevice.statistics` with a list of observables, or when using the `observables` argument:
```pycon
>>> dev = qml.device("default.qubit", wires=0)
>>> dev.statistics([])
/Users/albertmitjans/Developer/pennylane/pennylane/_qubit_device.py:724: UserWarning: The ``observables`` argument in ``QubitDevice.statistics`` is deprecated. Please use ``circuit`` instead.
  warnings.warn(
>>> dev.statistics(observables=[])
/Users/albertmitjans/Developer/pennylane/pennylane/_qubit_device.py:724: UserWarning: The ``observables`` argument in ``QubitDevice.statistics`` is deprecated. Please use ``circuit`` instead.
  warnings.warn(
```

github-actions[bot]: Hello. You may have forgotten to update the changelog!
Please edit [`doc/releases/changelog-dev.md`](/PennyLaneAI/pennylane/blob/master/doc/releases/changelog-dev.md) with:
* A one-to-two sentence description of the change. You may include a small working example for new features.
* A link back to this PR.
* Your name (or GitHub username) in the contributors section.
AlbertMitjans: @trbromley @josh146 waiting for your approval before merging

Patch:
diff --git a/doc/development/deprecations.rst b/doc/development/deprecations.rst
index 401494006..43b571e5d 100644
--- a/doc/development/deprecations.rst
+++ b/doc/development/deprecations.rst
@@ -6,6 +6,12 @@ Deprecations
 Pending deprecations
 --------------------
 
+* The ``observables`` argument in ``QubitDevice.statistics`` is deprecated. Please use ``circuit``
+  instead.
+
+  - Still accessible in v0.28
+  - Will be removed in v0.29
+
 * The ``seed_recipes`` argument in ``qml.classical_shadow`` and ``qml.shadow_expval`` is deprecated.
   A new argument ``seed`` has been added, which defaults to ``None`` and can contain an integer with the 
   wanted seed.
diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md
index 38ed45bd3..b33a30053 100644
--- a/doc/releases/changelog-dev.md
+++ b/doc/releases/changelog-dev.md
@@ -354,6 +354,10 @@ Deprecations cycles are tracked at [doc/developement/deprecations.rst](https://d
 * `qml.transforms.measurement_grouping` has been deprecated. Use `qml.transforms.hamiltonian_expand` instead.
   [(#3417)](https://github.com/PennyLaneAI/pennylane/pull/3417)
 
+* The ``observables`` argument in ``QubitDevice.statistics`` is deprecated. Please use ``circuit``
+  instead.
+  [(#3433)](https://github.com/PennyLaneAI/pennylane/pull/3433)
+
 * The `seed_recipes` argument in `qml.classical_shadow` and `qml.shadow_expval` is deprecated.
   A new argument `seed` has been added, which defaults to None and can contain an integer with the
   wanted seed.
diff --git a/pennylane/_qubit_device.py b/pennylane/_qubit_device.py
index 9f173b3fe..6d1cbcd05 100644
--- a/pennylane/_qubit_device.py
+++ b/pennylane/_qubit_device.py
@@ -669,20 +669,21 @@ class QubitDevice(Device):
 
         return Wires.all_wires(list_of_wires)
 
-    def statistics(self, circuit: QuantumScript, shot_range=None, bin_size=None):
+    def statistics(
+        self, circuit: QuantumScript = None, shot_range=None, bin_size=None, observables=None
+    ):
         """Process measurement results from circuit execution and return statistics.
 
         This includes returning expectation values, variance, samples, probabilities, states, and
         density matrices.
 
         Args:
-            observables (List[.Observable]): the observables to be measured
+            circuit (~.tape.QuantumScript): the quantum script currently being executed
             shot_range (tuple[int]): 2-tuple of integers specifying the range of samples
                 to use. If not specified, all samples are used.
             bin_size (int): Divides the shot range into bins of size ``bin_size``, and
                 returns the measurement statistic separately over each bin. If not
                 provided, the entire shot range is treated as a single bin.
-            circuit (~.tape.QuantumTape): the quantum tape currently being executed
 
         Raises:
             QuantumFunctionError: if the value of :attr:`~.Observable.return_type` is not supported
@@ -716,7 +717,17 @@ class QubitDevice(Device):
             * Finally, we repeat the measurement statistics for the final 100 shots,
               ``shot_range=[35, 135]``, ``bin_size=100``.
         """
-        observables = circuit.observables
+        if isinstance(circuit, QuantumScript):
+            observables = circuit.observables
+
+        else:
+            warnings.warn(
+                message="The ``observables`` argument in ``QubitDevice.statistics`` is deprecated. "
+                "Please use ``circuit`` instead.",
+                category=UserWarning,
+            )
+            observables = circuit if circuit is not None else observables
+
         results = []
 
         for obs in observables:
@@ -841,7 +852,7 @@ class QubitDevice(Device):
         density matrices.
 
         Args:
-            observables (List[.Observable]): the observables to be measured
+            circuit (~.tape.QuantumScript): the quantum script currently being executed
             shot_range (tuple[int]): 2-tuple of integers specifying the range of samples
                 to use. If not specified, all samples are used.
             bin_size (int): Divides the shot range into bins of size ``bin_size``, and


Test patch:
diff --git a/tests/test_qubit_device.py b/tests/test_qubit_device.py
index 1f3564e51..bd6f53c66 100644
--- a/tests/test_qubit_device.py
+++ b/tests/test_qubit_device.py
@@ -312,6 +312,21 @@ class TestParameters:
 class TestExtractStatistics:
     """Test the statistics method"""
 
+    def test_observables_deprecated(self, mock_qubit_device_extract_stats, monkeypatch):
+        """Test that using a list of observables as an argument is deprecated."""
+        with monkeypatch.context() as m:
+            dev = mock_qubit_device_extract_stats()
+            with pytest.warns(
+                UserWarning,
+                match="The ``observables`` argument in ``QubitDevice.statistics`` is deprecated. ",
+            ):
+                dev.statistics([])
+            with pytest.warns(
+                UserWarning,
+                match="The ``observables`` argument in ``QubitDevice.statistics`` is deprecated. ",
+            ):
+                dev.statistics(observables=[])
+
     @pytest.mark.parametrize("returntype", [Expectation, Variance, Sample, Probability, State])
     def test_results_created(self, mock_qubit_device_extract_stats, monkeypatch, returntype):
         """Tests that the statistics method simply builds a results list without any side-effects"""