def integration_formatted(savefile="example_formatted.png"): wire_options = {"color": "indigo", "linewidth": 4} drawer = MPLDrawer(n_wires=2, n_layers=4, wire_options=wire_options) label_options = {"fontsize": "x-large", "color": "indigo"} drawer.label(["0", "a"], text_options=label_options) box_options = {"facecolor": "lightcoral", "edgecolor": "maroon", "linewidth": 5} text_options = {"fontsize": "xx-large", "color": "maroon"} drawer.box_gate(layer=0, wires=0, text="Z", box_options=box_options, text_options=text_options) swap_options = {"linewidth": 4, "color": "darkgreen"} drawer.SWAP(layer=1, wires=(0, 1), options=swap_options) ctrl_options = {"linewidth": 4, "color": "teal"} drawer.CNOT(layer=2, wires=(0, 1), options=ctrl_options) drawer.ctrl(layer=3, wires=(0, 1), options=ctrl_options) measure_box = {"facecolor": "white", "edgecolor": "indigo"} measure_lines = {"edgecolor": "indigo", "facecolor": "plum", "linewidth": 2} for wire in range(2): drawer.measure(layer=4, wires=wire, box_options=measure_box, lines_options=measure_lines) drawer.fig.suptitle("My Circuit", fontsize="xx-large") plt.savefig(folder / savefile) plt.close()
def test_no_active_wire_notches(self): """Test active wire notches deactivated by keyword.""" drawer = MPLDrawer(n_layers=1, n_wires=3) drawer.box_gate(layer=0, wires=(0, 2), active_wire_notches=False) # only one patch for big box, no patches for notches assert len(drawer.ax.patches) == 1 plt.close()
def test_active_wire_notches_number(self, wires, n_notches): """Tests the number of active wires drawn is the expected amount.""" drawer = MPLDrawer(n_layers=1, n_wires=4) drawer.box_gate(layer=0, wires=wires) assert len(drawer.ax.patches) == (n_notches + 1) plt.close()
def float_layer(savefile="float_layer.png"): drawer = MPLDrawer(2, 2) drawer.box_gate(layer=0.5, wires=0, text="Big Gate", extra_width=0.5) drawer.box_gate(layer=0, wires=1, text="X") drawer.box_gate(layer=1, wires=1, text="Y") plt.savefig(folder / savefile) plt.close()
def box_gates_formatted(savefile="box_gates_formatted.png"): box_options = {"facecolor": "lightcoral", "edgecolor": "maroon", "linewidth": 5} text_options = {"fontsize": "xx-large", "color": "maroon"} drawer = MPLDrawer(n_wires=2, n_layers=1) drawer.box_gate( layer=0, wires=(0, 1), text="CY", box_options=box_options, text_options=text_options ) plt.savefig(folder / savefile) plt.close()
def test_autosize_false(self): """Test that the text is unchanged if autosize is set to False.""" drawer = MPLDrawer(n_layers=1, n_wires=2) drawer.box_gate(0, (0, 1), text="very very long text", autosize=False) t = drawer.ax.texts[0] assert t.get_rotation() == 0 assert t.get_fontsize() == drawer._fontsize plt.close()
def test_wide_multline_text_multiwires(self): """Test case where the box is on multiple wires and text is fat, tall, and fatter than it is tall. It should be rotated.""" drawer = MPLDrawer(n_layers=1, n_wires=2) drawer.box_gate(0, (0, 1), text="very very long text\nall\nthe\nlines\nyep", autosize=True) assert self.text_in_box(drawer) plt.close()
def test_text_formatting(self): """Tests rotated text""" drawer = MPLDrawer(1, 1) rgba_red = (1, 0, 0, 1) options = {"color": rgba_red, "rotation": "vertical"} drawer.box_gate(0, 0, text="X", text_options=options) text = drawer.ax.texts[0] assert text.get_rotation() == 90.0 assert text.get_color() == rgba_red plt.close()
def test_autosize_multiwires(self): """Test case where the box is on multiple wires. The text should be rotated 90deg, and still inside the box.""" drawer = MPLDrawer(n_layers=1, n_wires=2) drawer.box_gate(0, (0, 1), text="very very long text", autosize=True) t = drawer.ax.texts[0] assert t.get_rotation() == 90.0 assert self.text_in_box(drawer) plt.close()
def test_autosize_one_wire(self): """Test case where the box is on only one wire. The text should still be inside the box, but not rotated.""" drawer = MPLDrawer(n_layers=1, n_wires=1) drawer.box_gate(0, 0, text="very very long text", autosize=True) t = drawer.ax.texts[0] assert t.get_rotation() == 0 assert self.text_in_box(drawer) plt.close()
def integration(style="default", savefile="example_basic.png"): use_style(style) drawer = MPLDrawer(n_wires=5, n_layers=6) drawer.label(["0", "a", r"$|\Psi\rangle$", r"$|\theta\rangle$", "aux"]) drawer.box_gate(0, [0, 1, 2, 3, 4], "Entangling Layers") drawer.box_gate(1, [0, 2, 3], "U(θ)") drawer.box_gate(1, 4, "Z") drawer.SWAP(2, (3,4)) drawer.CNOT(2, (0, 2)) drawer.ctrl(3, [1, 3], control_values=[True, False]) drawer.box_gate( layer=3, wires=2, text="H", box_options={"zorder": 4}, text_options={"zorder": 5} ) drawer.ctrl(4, [1, 2]) drawer.measure(5, 0) drawer.fig.suptitle("My Circuit", fontsize="xx-large") plt.savefig(folder / savefile) plt.style.use("default") plt.close()
def test_multiline_text_single_wire(self): """Test case where the box is on one wire and the text is skinny and tall. If the text is too tall, it should still be shrunk to fit inside the box.""" drawer = MPLDrawer(n_layers=1, n_wires=1) drawer.box_gate(0, 0, text="text\nwith\nall\nthe\nlines\nyep", autosize=True) t = drawer.ax.texts[0] assert t.get_rotation() == 0.0 assert self.text_in_box(drawer) plt.close()
def test_notch_standard_styling(self): """Test notch styling is correct""" drawer = MPLDrawer(n_layers=1, n_wires=3) drawer.box_gate(0, (0, 2)) xs = [-0.415, 0.375, -0.415, 0.375] ys = [-0.125, -0.125, 1.875, 1.875] # first patch is big box for x, y, notch in zip(xs, ys, drawer.ax.patches[1:]): assert notch.get_x() == x assert notch.get_y() == y assert notch.get_width() == drawer._notch_width assert notch.get_height() == drawer._notch_height assert notch.get_zorder() == 1 assert notch.get_boxstyle().pad == 0.05 plt.close()
def test_box_formatting(self): """Tests that box_options influences the rectangle""" drawer = MPLDrawer(1, 3) rgba_red = (1, 0, 0, 1) rgba_green = (0, 1, 0, 1) options = {"facecolor": rgba_red, "edgecolor": rgba_green, "zorder": 5} drawer.box_gate(0, (0, 2), text="X", box_options=options) # notches get same styling options as box for p in drawer.ax.patches: assert p.get_facecolor() == rgba_red assert p.get_edgecolor() == rgba_green # except for zorder assert drawer.ax.patches[0].get_zorder() == 5 for n in drawer.ax.patches[1:]: assert n.get_zorder() == 4 plt.close()
def text_tall_multitline_text_multiwires(self): """Test case where the box is on mutiple wires and the text is skinny and tall. If the text is just too tall and the width fits inside the box, the text should not be rotated.""" drawer = MPLDrawer(n_layers=1, n_wires=2) drawer.box_gate( 0, (0, 1), text= "text\nwith\nall\nthe\nlines\nyep\ntoo\nmany\nlines\nway\ntoo\nmany", autosize=True, ) t = drawer.ax.texts[0] assert t.get_rotation() == 0.0 assert self.text_in_box(drawer) plt.close()
def autosize(savefile="box_gates_autosized.png"): drawer = MPLDrawer(n_layers=4, n_wires=2) drawer.box_gate(layer=0, wires=0, text="A longer label") drawer.box_gate(layer=0, wires=1, text="Label") drawer.box_gate(layer=1, wires=(0, 1), text="long multigate label") drawer.box_gate(layer=3, wires=(0, 1), text="Not autosized label", autosize=False) plt.savefig(folder / savefile) plt.close()
def test_multiwire_box(self): """tests a gate spanning multiple wires.""" drawer = MPLDrawer(1, 3) drawer.box_gate(0, (0, 2), text="Tall Gate") box = drawer.ax.patches[0] assert box.get_x() == -drawer._box_length / 2.0 + drawer._pad assert box.get_y() == -drawer._box_length / 2.0 + drawer._pad assert box.get_width() == drawer._box_length - 2 * drawer._pad assert box.get_height() == 2 + drawer._box_length - 2 * drawer._pad assert box.get_zorder() == 2 text = drawer.ax.texts[0] assert text.get_text() == "Tall Gate" assert text.get_position() == (0, 1.0) assert text.get_zorder() == 3 plt.close()
def test_simple_box(self): """tests basic functionality of box_gate.""" drawer = MPLDrawer(1, 1) drawer.box_gate(0, 0, "X") box = drawer.ax.patches[0] assert box.get_x() == -drawer._box_length / 2.0 + drawer._pad assert box.get_y() == -drawer._box_length / 2.0 + drawer._pad assert box.get_width() == drawer._box_length - 2 * drawer._pad assert box.get_height() == drawer._box_length - 2 * drawer._pad assert box.get_zorder() == 2 text = drawer.ax.texts[0] assert text.get_text() == "X" assert text.get_position() == (0, 0) assert text.get_zorder() == 3 plt.close()
def test_extra_width(self): """tests a box with added width.""" drawer = MPLDrawer(1, 3) drawer.box_gate(0, (0, 2), text="Wide Gate", extra_width=0.4) box = drawer.ax.patches[0] assert box.get_x() == -(drawer._box_length + 0.4) / 2.0 + drawer._pad assert box.get_y() == -drawer._box_length / 2.0 + drawer._pad assert box.get_width() == drawer._box_length + 0.4 - 2 * drawer._pad assert box.get_height() == 2 + drawer._box_length - 2 * drawer._pad text = drawer.ax.texts[0] assert text.get_text() == "Wide Gate" assert text.get_position() == (0, 1.0) xs = [-0.615, 0.575, -0.615, 0.575] for x, notch in zip(xs, drawer.ax.patches[1:]): assert notch.get_x() == x plt.close()
def integration_rcParams(savefile="example_rcParams.png"): plt.rcParams['patch.facecolor'] = 'mistyrose' plt.rcParams['patch.edgecolor'] = 'maroon' plt.rcParams['text.color'] = 'maroon' plt.rcParams['font.weight'] = 'bold' plt.rcParams['patch.linewidth'] = 4 plt.rcParams['patch.force_edgecolor'] = True plt.rcParams['lines.color'] = 'indigo' plt.rcParams['lines.linewidth'] = 5 plt.rcParams['figure.facecolor'] = 'ghostwhite' drawer = MPLDrawer(n_wires=5, n_layers=5) drawer = MPLDrawer(n_wires=5, n_layers=6) drawer.label(["0", "a", r"$|\Psi\rangle$", r"$|\theta\rangle$", "aux"]) drawer.box_gate(0, [0, 1, 2, 3, 4], "Entangling Layers") drawer.box_gate(1, [0, 2, 3], "U(θ)") drawer.box_gate(1, 4, "Z") drawer.SWAP(2, (3,4)) drawer.CNOT(2, (0, 2)) drawer.ctrl(3, [1, 3], control_values=[True, False]) drawer.box_gate( layer=3, wires=2, text="H", box_options={"zorder": 4}, text_options={"zorder": 5} ) drawer.ctrl(4, [1, 2]) drawer.measure(5, 0) drawer.fig.suptitle("My Circuit", fontsize="xx-large") plt.savefig(folder / savefile) plt.style.use("default") plt.close()
def box_gates(savefile="box_gates.png"): drawer = MPLDrawer(n_wires=2, n_layers=1) drawer.box_gate(layer=0, wires=(0, 1), text="CY") plt.savefig(folder / savefile) plt.close()