Repository: PennyLaneAI/pennylane
Pull request #1354 (2021-05-21 22:09:37)

Fix drawing a QNode with no operations
**Context**
QNodes with no operations raises an error when drawn.

This error boils down to the grid that the `CircuitDrawer` creates. There is a copying operation that is called on a `None` type object (due to having no operations).

**Changes**

Catches the case where the grid is `None` and no copying of the internal grid happens, instead, an empty grid is used.

**Ralated Issues**
#1326 

codecov[bot]: # [Codecov](https://codecov.io/gh/PennyLaneAI/pennylane/pull/1354?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) Report
> Merging [#1354](https://codecov.io/gh/PennyLaneAI/pennylane/pull/1354?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) (e20026f) into [master](https://codecov.io/gh/PennyLaneAI/pennylane/commit/0965bb0c97c4de2dc2408751ca3a61dd47074264?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) (0965bb0) will **not change** coverage.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/PennyLaneAI/pennylane/pull/1354/graphs/tree.svg?width=650&height=150&src=pr&token=9S3YzlUgGk&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI)](https://codecov.io/gh/PennyLaneAI/pennylane/pull/1354?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI)

```diff
@@           Coverage Diff           @@
##           master    #1354   +/-   ##
=======================================
  Coverage   98.25%   98.25%           
=======================================
  Files         146      146           
  Lines       11146    11146           
=======================================
  Hits        10952    10952           
  Misses        194      194           
```


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

------

[Continue to review full report at Codecov](https://codecov.io/gh/PennyLaneAI/pennylane/pull/1354?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/PennyLaneAI/pennylane/pull/1354?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI). Last update [0965bb0...e20026f](https://codecov.io/gh/PennyLaneAI/pennylane/pull/1354?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI).


Patch:
diff --git a/.github/CHANGELOG.md b/.github/CHANGELOG.md
index 0f14dbf6d..25ce7e5a0 100644
--- a/.github/CHANGELOG.md
+++ b/.github/CHANGELOG.md
@@ -268,6 +268,9 @@ random_mat2 = rng.standard_normal(3, requires_grad=False)
 
 <h3>Bug fixes</h3>
 
+* Fixes drawing QNodes with no operations.
+  [(#1354)](https://github.com/PennyLaneAI/pennylane/pull/1354)
+
 * Fixes incorrect wires in the decomposition of the `ControlledPhaseShift` operation.
   [(#1338)](https://github.com/PennyLaneAI/pennylane/pull/1338)
 
diff --git a/pennylane/circuit_drawer/grid.py b/pennylane/circuit_drawer/grid.py
index 177d9b69e..40312cb14 100644
--- a/pennylane/circuit_drawer/grid.py
+++ b/pennylane/circuit_drawer/grid.py
@@ -176,7 +176,7 @@ class Grid:
         Returns:
             Grid: A copy of the Grid
         """
-        return Grid(self.raw_grid.copy())
+        return Grid(self.raw_grid.copy()) if self.raw_grid is not None else Grid()
 
     def append_grid_by_layers(self, other_grid):
         """Append the layers of another Grid to this Grid.


Test patch:
diff --git a/tests/circuit_drawer/test_circuit_drawer.py b/tests/circuit_drawer/test_circuit_drawer.py
index c18742fc0..473d9c301 100644
--- a/tests/circuit_drawer/test_circuit_drawer.py
+++ b/tests/circuit_drawer/test_circuit_drawer.py
@@ -913,3 +913,21 @@ class TestWireOrdering:
 
         with pytest.raises(ValueError, match="contains wires not contained on the device"):
             res = circuit.draw(wire_order=["q2", 5])
+
+    def test_no_ops_draws(self):
+        """Test that a QNode with no operations still draws correctly"""
+        dev = qml.device("default.qubit", wires=3)
+
+        @qml.qnode(dev)
+        def qnode():
+            return qml.expval(qml.PauliX(wires=[0]) @ qml.PauliX(wires=[1]) @ qml.PauliX(wires=[2]))
+
+        qnode()
+        res = qnode.draw()
+        expected = [
+            " 0: ──╭┤ ⟨X ⊗ X ⊗ X⟩ \n",
+            " 1: ──├┤ ⟨X ⊗ X ⊗ X⟩ \n",
+            " 2: ──╰┤ ⟨X ⊗ X ⊗ X⟩ \n",
+        ]
+
+        assert res == "".join(expected)