Repository: PennyLaneAI/pennylane
Pull request #5071 (2024-01-16 19:30:04)

legacy device: check if object is measurement before checking stopping_condition
**Context:**
Many plugins are failing because of the deprecation of `MeasurementProcess.name` (#5047). The old device API checks if an object meets the stopping requirement _or_ if it's a measurement (in that order). Due to this ordering, we reference `mp.name` which is now deprecated.

**Description of the Change:**
Swap the order of conditions in `_local_tape_expand` to avoid hitting this deprecation warning

**Benefits:**
Plugins will work again!

**Possible Drawbacks:**
N/A

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/PennyLaneAI/pennylane/pull/5071?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Comparison is base [(`c01cb4b`)](https://app.codecov.io/gh/PennyLaneAI/pennylane/commit/c01cb4b96f758a9f1a0c4e422115829b80e113cb?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) 99.67% compared to head [(`cbadb09`)](https://app.codecov.io/gh/PennyLaneAI/pennylane/pull/5071?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) 99.66%.
> Report is 17 commits behind head on master.


<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##           master    #5071      +/-   ##
==========================================
- Coverage   99.67%   99.66%   -0.01%     
==========================================
  Files         394      394              
  Lines       35670    35428     -242     
==========================================
- Hits        35554    35310     -244     
- Misses        116      118       +2     
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/PennyLaneAI/pennylane/pull/5071?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI).


Patch:
diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md
index 96ee5ab67..c8c6bb091 100644
--- a/doc/releases/changelog-dev.md
+++ b/doc/releases/changelog-dev.md
@@ -75,6 +75,7 @@
 * `MeasurementProcess.name` and `MeasurementProcess.data` are now deprecated, as they contain dummy
   values that are no longer needed.
   [(#5047)](https://github.com/PennyLaneAI/pennylane/pull/5047)
+  [(#5071)](https://github.com/PennyLaneAI/pennylane/pull/5071)
 
 * Calling `qml.matrix` without providing a `wire_order` on objects where the wire order could be
   ambiguous now raises a warning. In the future, the `wire_order` argument will be required in
diff --git a/pennylane/_device.py b/pennylane/_device.py
index 0402b7a91..5aabc9a1c 100644
--- a/pennylane/_device.py
+++ b/pennylane/_device.py
@@ -75,7 +75,7 @@ def _local_tape_expand(tape, depth, stop_at):
         (tape.measurements, new_measurements),
     ]:
         for obj in queue:
-            if stop_at(obj) or isinstance(obj, qml.measurements.MeasurementProcess):
+            if isinstance(obj, MeasurementProcess) or stop_at(obj):
                 new_queue.append(obj)
                 continue
 


Test patch:
diff --git a/tests/test_device.py b/tests/test_device.py
index 4ca0d21a5..1cd774725 100644
--- a/tests/test_device.py
+++ b/tests/test_device.py
@@ -290,7 +290,7 @@ class TestDeviceSupportedLogic:
             dev.supports_observable(operation)
 
 
-class TestInternalFunctions:
+class TestInternalFunctions:  # pylint:disable=too-many-public-methods
     """Test the internal functions of the abstract Device class"""
 
     # pylint: disable=unnecessary-dunder-call
@@ -613,6 +613,15 @@ class TestInternalFunctions:
         assert new_tape.batch_size == tape.batch_size
         assert new_tape.output_dim == tape.output_dim
 
+    def test_default_expand_fn_with_invalid_op(self, mock_device_with_operations, recwarn):
+        """Test that default_expand_fn works with an invalid op and some measurement."""
+        invalid_tape = qml.tape.QuantumScript([qml.S(0)], [qml.expval(qml.PauliZ(0))])
+        expected_tape = qml.tape.QuantumScript([qml.RZ(np.pi / 2, 0)], [qml.expval(qml.PauliZ(0))])
+        dev = mock_device_with_operations(wires=1)
+        expanded_tape = dev.expand_fn(invalid_tape, max_expansion=3)
+        assert qml.equal(expanded_tape, expected_tape)
+        assert len(recwarn) == 0
+
 
 # pylint: disable=too-few-public-methods
 class TestClassmethods: