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

Draw qubit reset and postselection with matplotlib #4832

Merged
merged 20 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from 10 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
Binary file modified doc/_static/draw_mpl/decimals.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/_static/draw_mpl/main_example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/_static/draw_mpl/mid_measure.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/_static/draw_mpl/postprocessing.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/_static/draw_mpl/rcparams.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/_static/draw_mpl/show_all_wires.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/_static/draw_mpl/sketch_style.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/_static/draw_mpl/wire_order.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/_static/draw_mpl/wires_labels.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/_static/drawer/SWAP.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/_static/drawer/black_white_style.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/_static/drawer/box_gates.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/_static/drawer/box_gates_autosized.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/_static/drawer/box_gates_formatted.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/_static/drawer/cnot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/_static/drawer/cond.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/_static/drawer/ctrl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/_static/drawer/example_basic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/_static/drawer/example_formatted.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/_static/drawer/example_rcParams.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/_static/drawer/float_layer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/_static/drawer/labels_formatted.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/_static/drawer/labels_test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/_static/drawer/measure.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
* `AmplitudeEmbedding` now also supports batching when used with Tensorflow.
[(#4818)](https://github.com/PennyLaneAI/pennylane/pull/4818)

* `qml.draw` now supports drawing mid-circuit measurements.
* `qml.draw` and `qml.draw_mpl` now support drawing mid-circuit measurements.
[(#4775)](https://github.com/PennyLaneAI/pennylane/pull/4775)
albi3ro marked this conversation as resolved.
Show resolved Hide resolved

* Autograd and torch can now use vjps provided by the device from the new device API. If a device provides
Expand Down Expand Up @@ -106,6 +106,8 @@
* Specifying `control_values` passed to `qml.ctrl` as a string is no longer supported.
[(#4816)](https://github.com/PennyLaneAI/pennylane/pull/4816)

* The `Conditional` operator is moved from `transforms` to `ops.op_math` to prevent circular import issues.
albi3ro marked this conversation as resolved.
Show resolved Hide resolved

albi3ro marked this conversation as resolved.
Show resolved Hide resolved
<h3>Deprecations 👋</h3>

* `qml.grad` and `qml.jacobian` now explicitly raise errors if trainable parameters are integers.
Expand Down
33 changes: 29 additions & 4 deletions pennylane/drawer/mpldrawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,26 @@ def label(self, labels, text_options=None):
for wire, ii_label in enumerate(labels):
self._ax.text(-1.5, wire, ii_label, **text_options)

def erase_wire(self, layer: int, wire: int, length: int) -> None:
"""Erases a portion of a wire by adding a rectangle that matches the background.

Args:
layer (int): starting x coordinate for erasing the wire
wire (int): y location to to erase the wire from
albi3ro marked this conversation as resolved.
Show resolved Hide resolved
length (float, int): horizontal distance from ``layer`` to erase the background.

"""

rect = patches.Rectangle(
(layer, wire - 0.1),
length,
0.2,
facecolor=plt.rcParams["figure.facecolor"],
edgecolor=plt.rcParams["figure.facecolor"],
zorder=1.1,
)
self.ax.add_patch(rect)

def box_gate(self, layer, wires, text="", box_options=None, text_options=None, **kwargs):
"""Draws a box and adds label text to its center.

Expand Down Expand Up @@ -780,14 +800,15 @@ def _swap_x(self, layer, wire, options=None):
self._ax.add_line(l1)
self._ax.add_line(l2)

def measure(self, layer, wires, box_options=None, lines_options=None):
def measure(self, layer, wires, text=None, box_options=None, lines_options=None):
"""Draw a Measurement graphic at designated layer, wire combination.

Args:
layer (int): layer to draw on
wires (int): wire to draw on

Keyword Args:
text=None (str): an annotation for the lower right corner.
box_options=None (dict): dictionary to format a matplotlib rectangle
lines_options=None (dict): dictionary to format matplotlib arc and arrow

Expand Down Expand Up @@ -834,7 +855,7 @@ def measure(self, layer, wires, box_options=None, lines_options=None):
self._ax.add_patch(box)

arc = patches.Arc(
(layer, wires + self._box_length / 16),
(layer, wires + 0.15 * self._box_length),
0.6 * self._box_length,
0.55 * self._box_length,
theta1=180,
Expand All @@ -844,8 +865,8 @@ def measure(self, layer, wires, box_options=None, lines_options=None):
self._ax.add_patch(arc)

# can experiment with the specific numbers to make it look decent
arrow_start_x = layer - 0.165 * self._box_length
arrow_start_y = wires + 0.25 * self._box_length
arrow_start_x = layer - 0.15 * self._box_length
arrow_start_y = wires + 0.3 * self._box_length
arrow_width = 0.3 * self._box_length
arrow_height = -0.5 * self._box_length

Expand All @@ -858,6 +879,10 @@ def measure(self, layer, wires, box_options=None, lines_options=None):
head_width=self._box_length / 8.0,
**lines_options,
)
if text:
self._ax.text(
layer + 0.05 * self._box_length, wires + 0.2, text, fontsize=self.fontsize
)

def cond(self, layer, measured_layer, wires, wires_target, options=None):
"""Add classical communication double-lines for conditional operations
Expand Down
Loading
Loading