def test_CNOT(self): """Tests the CNOT method""" drawer = MPLDrawer(1, 3) drawer.CNOT(0, (0, 1)) ctrl_line = drawer.ax.lines[3] assert ctrl_line.get_data() == ((0, 0), (0, 1)) center_line = drawer.ax.lines[4] assert center_line.get_data() == ((0, 0), (0.7, 1.3)) ctrl_circle = drawer.ax.patches[0] target_circle = drawer.ax.patches[1] assert ctrl_circle.center == (0, 0) assert ctrl_circle.width == 0.2 assert target_circle.center == (0, 1) assert target_circle.width == 0.6 assert target_circle.get_facecolor() == to_rgba( plt.rcParams["axes.facecolor"]) assert to_rgba(plt.rcParams["lines.color"]) == to_rgba( target_circle.get_edgecolor()) plt.close()
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_measure_formatted(self): """Tests you can color the measure box""" drawer = MPLDrawer(1, 1) rgba_red = (1.0, 0, 0, 1.0) rgba_green = (0, 1, 0, 1) box_options = {"facecolor": rgba_red, "edgecolor": rgba_green} lines_options = {"color": rgba_green, "linewidth": 0.5} drawer.measure(0, 0, box_options=box_options, lines_options=lines_options) box = drawer.ax.patches[0] assert box.get_facecolor() == rgba_red assert box.get_edgecolor() == rgba_green arc = drawer.ax.patches[1] assert arc.get_edgecolor() == rgba_green assert arc.get_linewidth() == 0.5 arrow = drawer.ax.patches[2] assert arrow.get_edgecolor() == rgba_green assert arrow.get_linewidth() == 0.5 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_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_customfigsize(self): """Tests a custom figsize alters the size""" drawer = MPLDrawer(1, 1, figsize=(5, 5)) assert drawer.fig.get_figwidth() == 5 assert drawer.fig.get_figheight() == 5 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 CNOT(savefile="cnot.png"): drawer = MPLDrawer(n_wires=2, n_layers=2) drawer.CNOT(0, (0, 1)) options = {"color": "indigo", "linewidth": 4} drawer.CNOT(1, (1, 0), options=options) plt.savefig(folder / savefile) plt.close()
def test_figsize_wires(self, n_wires, n_layers): """Tests the figure is sized correctly.""" drawer = MPLDrawer(n_wires=n_wires, n_layers=n_layers) assert drawer.fig.get_figwidth() == (n_layers + 3) assert drawer.fig.get_figheight() == (n_wires + 1) drawer = MPLDrawer(n_wires=n_wires, n_layers=n_layers) lines = drawer.ax.lines assert len(lines) == n_wires for wire, line in enumerate(lines): assert line.get_xdata() == (-1, n_layers) assert line.get_ydata() == (wire, wire) 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 SWAP(savefile="SWAP.png"): drawer = MPLDrawer(n_wires=2, n_layers=2) drawer.SWAP(0, (0, 1)) swap_options = {"linewidth": 2, "color": "indigo"} drawer.SWAP(1, (0, 1), options=swap_options) plt.savefig(folder / savefile) plt.close()
def test_ctrl_control_values_error(self): """Tests a ValueError is raised if different number of wires and control_values.""" drawer = MPLDrawer(1, 2) with pytest.raises(ValueError, match="`control_values` must be the same length"): drawer.ctrl(0, (0, 1), control_values=True) plt.close()
def measure(savefile="measure.png"): drawer = MPLDrawer(n_wires=2, n_layers=1) drawer.measure(layer=0, wires=0) measure_box = {'facecolor': 'white', 'edgecolor': 'indigo'} measure_lines = {'edgecolor': 'indigo', 'facecolor': 'plum', 'linewidth': 2} drawer.measure(layer=0, wires=1, box_options=measure_box, lines_options=measure_lines) plt.savefig(folder / savefile) plt.close()
def test_ctrlo_circ(self): """Test only the ``ctrlo_circ`` private method.""" drawer = MPLDrawer(1, 1) drawer._ctrlo_circ(0, 0) circ = drawer.ax.patches[0] assert circ.get_facecolor() == to_rgba(plt.rcParams["axes.facecolor"]) assert circ.get_edgecolor() == to_rgba(plt.rcParams["lines.color"]) assert circ.get_linewidth() == plt.rcParams["lines.linewidth"]
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 ctrl(savefile="ctrl.png"): drawer = MPLDrawer(n_wires=2, n_layers=3) drawer.ctrl(layer=0, wires=0, wires_target=1) drawer.ctrl(layer=1, wires=(0, 1), control_values=[0, 1]) options = {"color": "indigo", "linewidth": 4} drawer.ctrl(layer=2, wires=(0, 1), control_values=[1, 0], options=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_fontsize(self): """Test fontsize can be get and set via property.""" drawer = MPLDrawer(1, 1) assert drawer._fontsize == drawer.fontsize assert drawer.fontsize == 14 drawer.fontsize = 10 assert drawer._fontsize == 10 assert drawer.fontsize == 10 plt.close()
def test_wires_formatting(self): """Tests wires formatting with options""" rgba_red = (1, 0, 0, 1) options = {"linewidth": 3, "color": rgba_red} drawer = MPLDrawer(n_wires=2, n_layers=2, wire_options=options) for line in drawer.ax.lines: assert line.get_linewidth() == 3 assert line.get_color() == rgba_red 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_swap_x(self): """Tests the ``_swap_x`` private method.""" drawer = MPLDrawer(1, 1) drawer._swap_x(0, 0) l1 = drawer.ax.lines[1] l2 = drawer.ax.lines[2] assert l1.get_data() == ((-0.2, 0.2), (-0.2, 0.2)) assert l2.get_data() == ((-0.2, 0.2), (0.2, -0.2)) 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_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 test_SWAP_options(self): """Tests that SWAP can be colored.""" drawer = MPLDrawer(1, 3) rgba_red = (1, 0, 0, 1) options = {"color": rgba_red, "linewidth": 3} drawer.SWAP(0, (0, 2), options=options) for line in drawer.ax.lines[3:]: assert line.get_color() == rgba_red assert line.get_linewidth() == 3 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_ctrl_circ(self): """Test only the ``_ctrl_circ`` private method.""" drawer = MPLDrawer(1, 1) drawer._ctrl_circ(0, 0) circ = drawer.ax.patches[0] assert circ.get_facecolor() == to_rgba(plt.rcParams["lines.color"]) assert circ.center == (0, 0) assert circ.width == 0.2 plt.close()
def test_SWAP(self): """Tests the SWAP method.""" drawer = MPLDrawer(1, 3) drawer.SWAP(0, (0, 2)) connecting_line = drawer.ax.lines[3] assert connecting_line.get_data() == ((0, 0), (0, 2)) x_lines = drawer.ax.lines[4:] assert x_lines[0].get_data() == ((-0.2, 0.2), (-0.2, 0.2)) assert x_lines[1].get_data() == ((-0.2, 0.2), (0.2, -0.2)) assert x_lines[2].get_data() == ((-0.2, 0.2), (1.8, 2.2)) assert x_lines[3].get_data() == ((-0.2, 0.2), (2.2, 1.8)) plt.close()
def test_ctrl_on_zero(self): """Tests a control on zero circle is open""" drawer = MPLDrawer(1, 1) drawer.ctrl(0, 0, control_values=False) circ = drawer.ax.patches[0] assert circ.get_facecolor() == to_rgba(plt.rcParams["axes.facecolor"]) assert circ.get_edgecolor() == to_rgba(plt.rcParams["lines.color"]) assert circ.get_linewidth() == plt.rcParams["lines.linewidth"] assert circ.center == (0, 0) assert circ.width == 0.2