Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BugFix: draw_mpl fails with adjointed CNOT #5149

Merged
merged 8 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,9 @@
[(#5125)](https://github.com/PennyLaneAI/pennylane/pull/5125/)
[(#5148)](https://github.com/PennyLaneAI/pennylane/pull/5148)

* `draw_mpl` no longer raises an error when drawing a circuit containing an adjoint of a controlled operation.
[(#5149)](https://github.com/PennyLaneAI/pennylane/pull/5149)

<h3>Contributors ✍️</h3>

This release contains contributions from (in alphabetical order):
Expand Down
23 changes: 16 additions & 7 deletions pennylane/drawer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"""
This module contains some useful utility functions for circuit drawing.
"""
import pennylane as qml
from pennylane.ops import Controlled, Conditional
from pennylane.measurements import MeasurementProcess, MidMeasureMP, MeasurementValue

Expand Down Expand Up @@ -95,6 +96,12 @@ def convert_wire_order(tape, wire_order=None, show_all_wires=False):
return {wire: ind for ind, wire in enumerate(wire_order)}


def _is_controlled(op):
"""Checks whether an operation is a controlled operation."""
# TODO: remove the other ones once they all inherit from Controlled [sc-37951]
return isinstance(op, (Controlled, qml.CCZ, qml.CSWAP, qml.CH, qml.CNOT, qml.Toffoli))


def unwrap_controls(op):
"""Unwraps nested controlled operations for drawing.

Expand All @@ -121,15 +128,17 @@ def unwrap_controls(op):
next_ctrl = op

while hasattr(next_ctrl, "base"):
base_control_wires = getattr(next_ctrl.base, "control_wires", [])
control_wires += base_control_wires

base_control_values = next_ctrl.base.hyperparameters.get(
"control_values", [True] * len(base_control_wires)
)
if _is_controlled(next_ctrl.base):
base_control_wires = getattr(next_ctrl.base, "control_wires", [])
control_wires += base_control_wires

base_control_values = next_ctrl.base.hyperparameters.get(
"control_values", [True] * len(base_control_wires)
)

if control_values is not None:
control_values.extend(base_control_values)
if control_values is not None:
control_values.extend(base_control_values)

next_ctrl = next_ctrl.base

Expand Down
15 changes: 15 additions & 0 deletions tests/drawer/test_draw_mpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,18 @@ def circuit():

_ = draw_qnode()
spy.assert_called_once()


def test_draw_mpl_with_control_in_adjoint():
def U(wires):
qml.adjoint(qml.CNOT)(wires=wires)

@qml.qnode(dev)
def circuit():
qml.ctrl(U, control=0)(wires=["a", 1.23])
return qml.state()

_, ax = qml.draw_mpl(circuit)()
assert len(ax.lines) == 4 # three wires, one control
assert len(ax.texts) == 4 # three wire labels, one gate label
assert ax.texts[-1].get_text() == "X†"
Loading