Repository: PennyLaneAI/pennylane
Pull request #1504 (2021-08-08 05:53:39)

Updated resolution of qml.CY when drawn
**Context:** 
Current draw of qml.CY uses "CY" in both the control and target wire representation. Similar gates CNOT and CZ use only a C on the control wire and X or Z on the target wire. 

**Description of the Change:** 
Added CY entries to resolver dicts to define CY gate resolution when drawn. 

**Benefits:** 
Consistent representation across similar control gates. 

**Possible Drawbacks:** 
None identified. 

**Related GitHub Issues:** 
[#1443](https://github.com/PennyLaneAI/pennylane/issues/1443)


codecov[bot]: # [Codecov](https://codecov.io/gh/PennyLaneAI/pennylane/pull/1504?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) Report
> Merging [#1504](https://codecov.io/gh/PennyLaneAI/pennylane/pull/1504?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) (77a5815) into [master](https://codecov.io/gh/PennyLaneAI/pennylane/commit/0fe4507c153f071250389ac56ab44d9fea07f57b?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) (0fe4507) will **not change** coverage.
> The diff coverage is `n/a`.

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

```diff
@@           Coverage Diff           @@
##           master    #1504   +/-   ##
=======================================
  Coverage   98.22%   98.22%           
=======================================
  Files         184      184           
  Lines       13203    13203           
=======================================
  Hits        12968    12968           
  Misses        235      235           
```


| [Impacted Files](https://codecov.io/gh/PennyLaneAI/pennylane/pull/1504?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/1504/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% <ø> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/PennyLaneAI/pennylane/pull/1504?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/1504?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI). Last update [0fe4507...77a5815](https://codecov.io/gh/PennyLaneAI/pennylane/pull/1504?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).

josh146: Thanks for this contribution @zer0points! Someone from the team will be on hand to provide a review shortly 🙂 

In the meantime, it looks like the changelog has not been updated. Could you add a new bullet point to `.github/CHANGELOG.md` in the "Bug fixes" section, detailing the change and adding a link back to this PR? And don't forget to add your name to the contributors section!
antalszava: Hi @zer0points, thank you for the contribution! :slightly_smiling_face: It is looking great. :tada:  It might be worth adding a test case too, to verify that the output is correct.

This could be done by:
a) Appending `CY` entries to the parametrized input of a corresponding test case, as is the case for [`CZ` here](https://github.com/PennyLaneAI/pennylane/blob/459b5cd9be57c228c115c9e534aa23e564f582d2/tests/circuit_drawer/test_circuit_drawer.py#L299).
b) Updating the visual [representation of the drawn circuit here](https://github.com/PennyLaneAI/pennylane/blob/459b5cd9be57c228c115c9e534aa23e564f582d2/tests/circuit_drawer/test_circuit_drawer.py#L328). Note, that this step will likely require a bit of back and forth with the string representation. Using the `-vv` option with `pytest` might be helpful to double-check which part of the output still needs some work.

Let us know if you'd have any questions!
zer0points: Thanks for the feedback! 
I'll take a look and work on it 😄 
antalszava: Hi @zer0points, just a small check-in, any ways we could assist here? :slightly_smiling_face: 
zer0points: Thanks for the check-in 😃 , been a little swamped with my day job, so this has slipped a bit. 
It is still on my mind though! Relatively new to pytest so I have been doing some reading. Will definitely be making time for this before the end of next week and update the PR. Let me know if there is any rush on this I can reprioritize as needed. 
antalszava: No worries at all, that timeline should work fine! :slightly_smiling_face: Feel free to drop any questions you might have related to `pytest` too, there can definitely be some quirks. :smile: 
zer0points: Hi @antalszava, I have updated the tests accordingly.

I did have a question as I was working through it though. 

This might be due to my inexperience with pytest, so sorry if it's a naïve question, but I noticed that for the ["_with_variable_names"](https://github.com/PennyLaneAI/pennylane/blob/459b5cd9be57c228c115c9e534aa23e564f582d2/tests/circuit_drawer/test_circuit_drawer.py#L328) fixtures, there is no corresponding test cases I can see. In local testing, I was able to modify those fixtures however I wanted and it did not affect the unit test results. Only the "_with_values" fixtures seem to have test cases. Are there tests somewhere I am not seeing, or is that intentional?

Patch:
diff --git a/.github/CHANGELOG.md b/.github/CHANGELOG.md
index d1bcdbad4..b15468aca 100644
--- a/.github/CHANGELOG.md
+++ b/.github/CHANGELOG.md
@@ -407,6 +407,9 @@ and requirements-ci.txt (unpinned). This latter would be used by the CI.
 
 <h3>Bug fixes</h3>
 
+* Fixed the circuit representation of CY gates to align with CNOT and CZ gates when calling the circuit drawer.
+  [(#1504)](https://github.com/PennyLaneAI/pennylane/issues/1504)
+
 * Dask and CVXPY dependent tests are skipped if those packages are not installed.
 [(#1617)](https://github.com/PennyLaneAI/pennylane/pull/1617)
 
@@ -439,9 +442,9 @@ and requirements-ci.txt (unpinned). This latter would be used by the CI.
 
 This release contains contributions from (in alphabetical order):
 
-Vishnu Ajith, Akash Narayanan B, Thomas Bromley, Tanya Garg, Josh Izaac, Prateek Jain, Ankit Khandelwal,
-Christina Lee, Johannes Jakob Meyer, Romain Moyard, Esteban Payares, Pratul Saini, Maria Schuld,
-Arshpreet Singh, Ingrid Strandberg, Slimane Thabet, David Wierichs, Vincent Wong.
+Vishnu Ajith, Akash Narayanan B, Thomas Bromley, Sahaj Dhamija, Tanya Garg, Josh Izaac, Prateek Jain,
+Ankit Khandelwal, Christina Lee, Johannes Jakob Meyer, Romain Moyard, Esteban Payares, Pratul Saini,
+Maria Schuld, Arshpreet Singh, Ingrid Strandberg, Slimane Thabet, David Wierichs, Vincent Wong.
 
 # Release 0.17.0 (current release)
 
@@ -998,7 +1001,7 @@ Arshpreet Singh, Ingrid Strandberg, Slimane Thabet, David Wierichs, Vincent Wong
 * Fixed a bug in the initialization of `QubitUnitary` where the size of
   the matrix was not checked against the number of wires.
   [(#1439)](https://github.com/PennyLaneAI/pennylane/pull/1439)
-
+  
 <h3>Documentation</h3>
 
 * Improved Contribution Guide and Pull Requests Guide.
diff --git a/pennylane/circuit_drawer/representation_resolver.py b/pennylane/circuit_drawer/representation_resolver.py
index c489897f9..6a1a5bae2 100644
--- a/pennylane/circuit_drawer/representation_resolver.py
+++ b/pennylane/circuit_drawer/representation_resolver.py
@@ -42,6 +42,7 @@ class RepresentationResolver:
         "CSWAP": "SWAP",
         "PauliY": "Y",
         "PauliZ": "Z",
+        "CY": "Y",
         "CZ": "Z",
         "Identity": "I",
         "Hadamard": "H",
@@ -76,6 +77,7 @@ class RepresentationResolver:
         "CRY": [0],
         "CRZ": [0],
         "CRot": [0],
+        "CY": [0],
         "CZ": [0],
         "ControlledAddition": [0],
         "ControlledPhase": [0],


Test patch:
diff --git a/tests/circuit_drawer/test_circuit_drawer.py b/tests/circuit_drawer/test_circuit_drawer.py
index f96e0afd5..472be9f76 100644
--- a/tests/circuit_drawer/test_circuit_drawer.py
+++ b/tests/circuit_drawer/test_circuit_drawer.py
@@ -298,6 +298,8 @@ def parameterized_qubit_qnode():
         qml.PauliZ(wires=[1]).inv()
         qml.CZ(wires=[0, 1])
         qml.CZ(wires=[0, 2]).inv()
+        qml.CY(wires=[1, 2])
+        qml.CY(wires=[2, 0]).inv()
         qml.CNOT(wires=[2, 1])
         qml.CNOT(wires=[0, 2])
         qml.SWAP(wires=[0, 2]).inv()
@@ -325,11 +327,11 @@ def parameterized_qubit_qnode():
 def drawn_parameterized_qubit_circuit_with_variable_names():
     """The rendered circuit representation of the above qubit circuit with variable names."""
     return (
-        " 0: ──RX(a)────────────────────────╭C─────RX(angles[0])──────────────────────────────────────────────╭C─────╭C───────╭C──╭C────────────╭C──╭SWAP⁻¹──╭SWAP───┤ ⟨Y⟩       \n"
-        + " 1: ──RX(b)───────Z────────────────╰X⁻¹──╭RY(b)──────────RX(4*angles[1])──╭RY(0.359)⁻¹──╭SWAP⁻¹──────├X──Z──│───Z⁻¹──╰Z──│─────╭X──╭C──│───│────────├SWAP───┤ Var[H]    \n"
-        + " 2: ──Rϕ(1.89*c)──RX(angles[2])⁻¹────────│────────────────────────────────│─────────────├SWAP⁻¹──U0──╰C─────╰X───────────╰Z⁻¹──╰C──│───╰X──╰SWAP⁻¹──│───────┤ Sample[X] \n"
-        + " 3: ─────────────────────────────────────╰C──────────────RZ(b)────────────╰C────────────│──────────────────────────────────────────╰X───────RZ(b)───│──────╭┤ ⟨H0⟩      \n"
-        + " 4: ────────────────────────────────────────────────────────────────────────────────────╰C──────────────────────────────────────────────────────────╰C─────╰┤ ⟨H0⟩      \n"
+        " 0: ──RX(a)────────────────────────╭C─────RX(angles[0])──────────────────────────────────────────────╭C─────╭C───────╭C──╭C────────╭Y⁻¹──────────╭C──╭SWAP⁻¹──╭SWAP───┤ ⟨Y⟩       \n"
+        + " 1: ──RX(b)───────Z────────────────╰X⁻¹──╭RY(b)──────────RX(4*angles[1])──╭RY(0.359)⁻¹──╭SWAP⁻¹──────├X──Z──│───Z⁻¹──╰Z──│─────╭C──│─────╭X──╭C──│───│────────├SWAP───┤ Var[H]    \n"
+        + " 2: ──Rϕ(1.89*c)──RX(angles[2])⁻¹────────│────────────────────────────────│─────────────├SWAP⁻¹──U0──╰C─────╰X───────────╰Z⁻¹──╰Y──╰C────╰C──│───╰X──╰SWAP⁻¹──│───────┤ Sample[X] \n"
+        + " 3: ─────────────────────────────────────╰C──────────────RZ(b)────────────╰C────────────│────────────────────────────────────────────────────╰X───────RZ(b)───│──────╭┤ ⟨H0⟩      \n"
+        + " 4: ────────────────────────────────────────────────────────────────────────────────────╰C────────────────────────────────────────────────────────────────────╰C─────╰┤ ⟨H0⟩      \n"
         + "U0 =\n"
         + "[[1. 0.]\n"
         + " [0. 1.]]\n"
@@ -345,11 +347,11 @@ def drawn_parameterized_qubit_circuit_with_variable_names():
 def drawn_parameterized_qubit_circuit_with_values():
     """The rendered circuit representation of the above qubit circuit with variable values."""
     return (
-        " 0: ──RX(0.1)───────────────╭C─────RX(0.4)──────────────────────────────────────╭C───╭C──╭C─────╭C───────╭C──╭C────────────╭C──╭SWAP⁻¹───╭SWAP───┤ ⟨Y⟩       \n"
-        + " 1: ──RX(0.2)────Z──────────╰X⁻¹──╭RY(0.2)──RX(2)────╭RY(0.359)⁻¹──╭SWAP⁻¹──────├C───├C──├X──Z──│───Z⁻¹──╰Z──│─────╭X──╭C──│───│─────────├SWAP───┤ Var[H]    \n"
-        + " 2: ──Rϕ(0.567)──RX(0.6)⁻¹────────│──────────────────│─────────────├SWAP⁻¹──U0──╰U0──├C──╰C─────╰X───────────╰Z⁻¹──╰C──│───╰X──╰SWAP⁻¹───│───────┤ Sample[X] \n"
-        + " 3: ──────────────────────────────╰C────────RZ(0.2)──╰C────────────│─────────────────╰X────────────────────────────────╰X───────RZ(0.2)──│──────╭┤ ⟨H0⟩      \n"
-        + " 4: ───────────────────────────────────────────────────────────────╰C────────────────────────────────────────────────────────────────────╰C─────╰┤ ⟨H0⟩      \n"
+        " 0: ──RX(0.1)───────────────╭C─────RX(0.4)──────────────────────────────────────╭C───╭C──╭C─────╭C───────╭C──╭C────────╭Y⁻¹──────────╭C──╭SWAP⁻¹───╭SWAP───┤ ⟨Y⟩       \n"
+        + " 1: ──RX(0.2)────Z──────────╰X⁻¹──╭RY(0.2)──RX(2)────╭RY(0.359)⁻¹──╭SWAP⁻¹──────├C───├C──├X──Z──│───Z⁻¹──╰Z──│─────╭C──│─────╭X──╭C──│───│─────────├SWAP───┤ Var[H]    \n"
+        + " 2: ──Rϕ(0.567)──RX(0.6)⁻¹────────│──────────────────│─────────────├SWAP⁻¹──U0──╰U0──├C──╰C─────╰X───────────╰Z⁻¹──╰Y──╰C────╰C──│───╰X──╰SWAP⁻¹───│───────┤ Sample[X] \n"
+        + " 3: ──────────────────────────────╰C────────RZ(0.2)──╰C────────────│─────────────────╰X──────────────────────────────────────────╰X───────RZ(0.2)──│──────╭┤ ⟨H0⟩      \n"
+        + " 4: ───────────────────────────────────────────────────────────────╰C──────────────────────────────────────────────────────────────────────────────╰C─────╰┤ ⟨H0⟩      \n"
         + "U0 =\n"
         + "[[1. 0.]\n"
         + " [0. 1.]]\n"