Repository: PennyLaneAI/pennylane
Pull request #1917 (2021-11-19 11:09:46)

Bugfix: draw template with matrix parameter
Bugfix for Issue #1916 

Due to the new QNode expansion settings, the qnode tape has unexpanded templates, which the current text circuit drawer was unable to handle.  This bugfix generalizes the treatment of matrix parameters.

This code was previously raising an error:

```python
import pennylane as qml
from pennylane import numpy as np

dev = qml.device('default.qubit', wires = 1)

@qml.qnode(dev)
def circuit():
    qml.AmplitudeEmbedding(np.array([0,1]), wires=0)
    return qml.state()
                        
print(qml.draw(circuit)())
```

But now gives:
```
 0: ──AmplitudeEmbedding(M0)──┤ State 
M0 =
[0.+0.j 1.+0.j]
```

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

[![Impacted file tree graph](https://codecov.io/gh/PennyLaneAI/pennylane/pull/1917/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/1917?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI)

```diff
@@           Coverage Diff           @@
##           master    #1917   +/-   ##
=======================================
  Coverage   98.83%   98.83%           
=======================================
  Files         222      222           
  Lines       16979    16979           
=======================================
  Hits        16781    16781           
  Misses        198      198           
```


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

------

[Continue to review full report at Codecov](https://codecov.io/gh/PennyLaneAI/pennylane/pull/1917?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/1917?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI). Last update [c631726...64de6b4](https://codecov.io/gh/PennyLaneAI/pennylane/pull/1917?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/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md
index f0d7c15ac..4e68086e4 100644
--- a/doc/releases/changelog-dev.md
+++ b/doc/releases/changelog-dev.md
@@ -259,7 +259,8 @@
 
 <h3>Bug fixes</h3>
 
-* `qml.CSWAP` and `qml.CRot` now define `control_wires`, and `qml.SWAP`
+* `qml.draw` now supports arbitrary templates with matrix parameters.
+  [(#1917)](https://github.com/PennyLaneAI/pennylane/pull/1917)
 
 * `QuantumTape.trainable_params` now is a list instead of a set, making
   it more stable in very rare edge cases.
diff --git a/pennylane/circuit_drawer/representation_resolver.py b/pennylane/circuit_drawer/representation_resolver.py
index 6e50431fc..b7f131f4b 100644
--- a/pennylane/circuit_drawer/representation_resolver.py
+++ b/pennylane/circuit_drawer/representation_resolver.py
@@ -416,13 +416,7 @@ class RepresentationResolver:
             )
 
         # Operations that only have matrix arguments
-        elif base_name in {
-            "GaussianState",
-            "FockDensityMatrix",
-            "FockStateVector",
-            "QubitStateVector",
-            "InterferometerUnitary",
-        }:
+        elif len(qml.math.shape(op.data[0])) != 0:
             representation = name + RepresentationResolver._format_matrix_arguments(
                 op.data, "M", self.matrix_cache
             )


Test patch:
diff --git a/tests/transforms/test_draw.py b/tests/transforms/test_draw.py
index 42e3c1e14..8301b292f 100644
--- a/tests/transforms/test_draw.py
+++ b/tests/transforms/test_draw.py
@@ -401,6 +401,19 @@ def test_qubit_circuit_length_under_max_length_kwdarg():
         assert qml.draw(qnode, max_length=60)() == expected
 
 
+def test_matrix_parameter_template():
+    """Assert draw method handles templates with matrix valued parameters."""
+    dev = qml.device("default.qubit", wires=1)
+
+    @qml.qnode(dev)
+    def circuit():
+        qml.AmplitudeEmbedding(np.array([0, 1]), wires=0)
+        return qml.state()
+
+    expected = " 0: ──AmplitudeEmbedding(M0)──┤ State \nM0 =\n[0.+0.j 1.+0.j]\n"
+    assert qml.draw(circuit)() == expected
+
+
 class TestWireOrdering:
     """Tests for wire ordering functionality"""