Repository: PennyLaneAI/pennylane
Pull request #3509 (2022-12-09 21:26:08)

do not nested-queue QNodes
**Context:**
With the introduction of `AnnotatedQueue`, we stopped supporting queue-by-default behaviour for nested tapes/scripts/queues/whatever. However, I manually preserved it in #3454 (see [this commit](https://github.com/PennyLaneAI/pennylane/pull/3454/commits/051ce049c391d3b02ab0581389a1dcbcaa497752)) for QNodes just to be safe.

**Description of the Change:**
No longer support queue-by-default of the qscript generated within a QNode to any surrounding context.

**Benefits:**
Consistent queuing standards throughout the codebase. Users can still manually queue `my_qnode.tape` if they so desire, but if they do not set a `_queuing_category` manually it'll get processed away. In other words, only an advanced tape-aware user would ever do this.

**Possible Drawbacks:**
Someone might have expected this to keep happening, which is why this is marked as a breaking change. You can see I updated one set of tests which made that assumption.

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/3509?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) Report
> Merging [#3509](https://codecov.io/gh/PennyLaneAI/pennylane/pull/3509?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) (6914892) into [master](https://codecov.io/gh/PennyLaneAI/pennylane/commit/0ecb1f11a7c185868d80d7a4a8cb8926993db17d?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) (0ecb1f1) will **decrease** coverage by `0.00%`.
> The diff coverage is `n/a`.

```diff
@@            Coverage Diff             @@
##           master    #3509      +/-   ##
==========================================
- Coverage   99.86%   99.86%   -0.01%     
==========================================
  Files         309      309              
  Lines       27542    27540       -2     
==========================================
- Hits        27506    27504       -2     
  Misses         36       36              
```


| [Impacted Files](https://codecov.io/gh/PennyLaneAI/pennylane/pull/3509?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) | Coverage Δ | |
|---|---|---|
| [pennylane/qnode.py](https://codecov.io/gh/PennyLaneAI/pennylane/pull/3509/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI#diff-cGVubnlsYW5lL3Fub2RlLnB5) | `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/releases/changelog-dev.md b/doc/releases/changelog-dev.md
index 5952eadf4..f3ec6d421 100644
--- a/doc/releases/changelog-dev.md
+++ b/doc/releases/changelog-dev.md
@@ -120,6 +120,9 @@
 
  <h3>Breaking changes</h3>
 
+* The tape constructed by a QNode is no longer queued to surrounding contexts.
+  [(#3509)](https://github.com/PennyLaneAI/pennylane/pull/3509)
+
 <h3>Deprecations</h3>
 
 <h3>Documentation</h3>
diff --git a/pennylane/qnode.py b/pennylane/qnode.py
index 54e683237..0814b764d 100644
--- a/pennylane/qnode.py
+++ b/pennylane/qnode.py
@@ -722,9 +722,7 @@ class QNode:
         """Call the quantum function with a tape context, ensuring the operations get queued."""
 
         self._tape = make_qscript(self.func)(*args, **kwargs)
-        self._tape._queue_category = "_ops"
         self._qfunc_output = self.tape._qfunc_output
-        qml.QueuingManager.append(self.tape)
 
         params = self.tape.get_parameters(trainable_only=False)
         self.tape.trainable_params = qml.math.get_trainable_indices(params)


Test patch:
diff --git a/tests/numpy/test_numpy_wrapper.py b/tests/numpy/test_numpy_wrapper.py
index e100fce61..e0425117a 100644
--- a/tests/numpy/test_numpy_wrapper.py
+++ b/tests/numpy/test_numpy_wrapper.py
@@ -535,13 +535,14 @@ class TestNumpyConversion:
 
         phi = np.tensor([[0.04439891, 0.14490549, 3.29725643, 2.51240058]])
 
-        with qml.tape.OperationRecorder() as rec:
-            circuit(phi=phi)
+        circuit(phi=phi)
 
-        for i in range(phi.shape[1]):
+        ops = circuit.tape.operations
+        assert len(ops) == 4
+        for op, p in zip(ops, phi[0]):
             # Test each rotation applied
-            assert rec.queue[0].name == "RX"
-            assert len(rec.queue[0].parameters) == 1
+            assert op.name == "RX"
+            assert op.parameters == [p]
 
     def test_multiple_gate_parameter(self):
         """Test that when supplied a PennyLane tensor, a QNode passes arguments
@@ -556,9 +557,10 @@ class TestNumpyConversion:
 
         phi = np.tensor([[0.04439891, 0.14490549, 3.29725643]])
 
-        with qml.tape.OperationRecorder() as rec:
-            circuit(phi=phi)
+        circuit(phi=phi)
 
         # Test the rotation applied
-        assert rec.queue[0].name == "Rot"
-        assert len(rec.queue[0].parameters) == 3
+        ops = circuit.tape.operations
+        assert len(ops) == 1
+        assert ops[0].name == "Rot"
+        assert np.array_equal(ops[0].parameters, phi[0])
diff --git a/tests/test_qnode.py b/tests/test_qnode.py
index 3f8a7a213..c93e4dbbf 100644
--- a/tests/test_qnode.py
+++ b/tests/test_qnode.py
@@ -1132,6 +1132,21 @@ class TestIntegration:
         with pytest.raises(ValueError, match="wires have been measured already: {1}"):
             qnode()
 
+    def test_qnode_does_not_support_nested_queuing(self):
+        """Test that operators in QNodes are not queued to surrounding contexts."""
+        dev = qml.device("default.qubit", wires=1)
+
+        @qml.qnode(dev)
+        def qnode():
+            qml.PauliX(0)
+            return qml.expval(qml.PauliZ(0))
+
+        with qml.queuing.AnnotatedQueue() as q:
+            qnode()
+
+        assert q.queue == []
+        assert len(qnode.tape.operations) == 1
+
 
 class TestShots:
     """Unit tests for specifying shots per call."""