Repository: PennyLaneAI/pennylane
Pull request #3417 (2022-11-24 15:58:12)

Deprecate transforms.measurement_grouping
**Description of the Change:**
Deprecate `qml.transforms.measurement_grouping` in favour of `qml.transforms.hamiltonian_expand`

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.
codecov[bot]: # [Codecov](https://codecov.io/gh/PennyLaneAI/pennylane/pull/3417?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) Report
> Merging [#3417](https://codecov.io/gh/PennyLaneAI/pennylane/pull/3417?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) (9e40571) into [master](https://codecov.io/gh/PennyLaneAI/pennylane/commit/0fe04b8e20845b96bf8cc998f9be7704835994db?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) (0fe04b8) will **increase** coverage by `0.00%`.
> The diff coverage is `100.00%`.

```diff
@@           Coverage Diff           @@
##           master    #3417   +/-   ##
=======================================
  Coverage   99.69%   99.69%           
=======================================
  Files         299      299           
  Lines       26519    26521    +2     
=======================================
+ Hits        26439    26441    +2     
  Misses         80       80           
```


| [Impacted Files](https://codecov.io/gh/PennyLaneAI/pennylane/pull/3417?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) | Coverage Δ | |
|---|---|---|
| [pennylane/transforms/measurement\_grouping.py](https://codecov.io/gh/PennyLaneAI/pennylane/pull/3417/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI#diff-cGVubnlsYW5lL3RyYW5zZm9ybXMvbWVhc3VyZW1lbnRfZ3JvdXBpbmcucHk=) | `100.00% <100.00%> (ø)` | |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI)


Patch:
diff --git a/doc/development/deprecations.rst b/doc/development/deprecations.rst
index c38612c3b..1b8d0c5db 100644
--- a/doc/development/deprecations.rst
+++ b/doc/development/deprecations.rst
@@ -54,6 +54,39 @@ Pending deprecations
         some_qfunc(params)
         return qml.expval(Hamiltonian)
 
+* ``qml.transforms.measurement_grouping`` has been deprecated, and usage will now raise a warning.
+
+  - Deprecated in v0.28
+  - Will be removed in v0.29
+
+  Don't use:
+
+  .. code-block:: python
+
+    with qml.tape.QuantumTape() as tape:
+      qml.RX(0.1, wires=0)
+      qml.RX(0.2, wires=1)
+
+    obs = [qml.PauliZ(0), qml.PauliX(1)]
+    coeffs = [2.0, 1.0]
+
+    tapes, fn = qml.transforms.measurement_grouping(tape, obs, coeffs)
+
+  Instead, use:
+
+  .. code-block:: python
+
+    obs = [qml.PauliZ(0), qml.PauliX(1)]
+    coeffs = [2.0, 1.0]
+    H = qml.Hamiltonian(coeffs, obs)
+
+    with qml.tape.QuantumTape() as tape:
+      qml.RX(0.1, wires=0)
+      qml.RX(0.2, wires=1)
+      qml.expval(H)
+
+    tapes, fn = qml.transforms.hamiltonian_expand(tape)
+
 Completed deprecation cycles
 ----------------------------
 
diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md
index fd196be83..f2cd1292a 100644
--- a/doc/releases/changelog-dev.md
+++ b/doc/releases/changelog-dev.md
@@ -212,6 +212,9 @@ Deprecations cycles are tracked at [doc/developement/deprecations.rst](https://d
   * `qml.tape.QuantumTape.stop_recording()`: Use `qml.QueuingManager.stop_recording()`
   * `qml.QueuingContext` is now `qml.QueuingManager`
   * `QueuingManager.safe_update_info` and `AnnotatedQueue.safe_update_info`: Use plain `update_info`
+  
+* `qml.transforms.measurement_grouping` has been deprecated. Use `qml.transforms.hamiltonian_expand` instead.
+  [(#3417)](https://github.com/PennyLaneAI/pennylane/pull/3417)
 
 <h3>Documentation</h3>
 
diff --git a/pennylane/transforms/measurement_grouping.py b/pennylane/transforms/measurement_grouping.py
index 649ab6b74..45152c163 100644
--- a/pennylane/transforms/measurement_grouping.py
+++ b/pennylane/transforms/measurement_grouping.py
@@ -14,6 +14,7 @@
 """
 Contains the measurement grouping transform
 """
+import warnings
 import pennylane as qml
 
 
@@ -73,6 +74,12 @@ def measurement_grouping(tape, obs_list, coeffs_list):
     >>> print(res)
     2.0007186031172046
     """
+
+    warnings.warn(
+        "qml.transforms.measurement_grouping is deprecated. Instead, use qml.transforms.hamiltonian_expand",
+        UserWarning,
+    )
+
     obs_groupings, coeffs_groupings = qml.pauli.group_observables(obs_list, coeffs_list)
     tapes = []
 


Test patch:
diff --git a/tests/transforms/test_measurement_grouping.py b/tests/transforms/test_measurement_grouping.py
index 527b1695e..88b665c6e 100644
--- a/tests/transforms/test_measurement_grouping.py
+++ b/tests/transforms/test_measurement_grouping.py
@@ -35,3 +35,19 @@ def test_measurement_grouping():
     dev = qml.device("default.qubit", wires=3)
     res = fn(dev.batch_execute(tapes))
     assert np.isclose(res, 2.0007186)
+
+
+def test_deprecation_warning_measurement_grouping():
+    """Tests that a deprecation warning is raised when using measurement_grouping."""
+
+    with qml.tape.QuantumTape() as tape:
+        qml.RX(0.1, wires=0)
+        qml.RX(0.2, wires=1)
+        qml.CNOT(wires=[0, 1])
+        qml.CNOT(wires=[1, 2])
+
+    obs = [qml.PauliZ(0), qml.PauliX(0) @ qml.PauliZ(1), qml.PauliX(2)]
+    coeffs = [2.0, -0.54, 0.1]
+
+    with pytest.warns(UserWarning, match="is deprecated"):
+        _, _ = qml.transforms.measurement_grouping(tape, obs, coeffs)