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 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_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 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
def test_ctrl_no_target(self): """Tests a single control with no target""" drawer = MPLDrawer(1, 1) drawer.ctrl(0, 0) ctrl_line = drawer.ax.lines[1] assert ctrl_line.get_data() == ((0, 0), (0, 0)) assert len(drawer.ax.patches) == 1 circle = drawer.ax.patches[0] assert circle.width == 0.2 assert circle.center == (0, 0) plt.close()
def test_ctrl_target(self): """Tests target impacts line extent""" drawer = MPLDrawer(1, 3) drawer.ctrl(0, 0, 2) ctrl_line = drawer.ax.lines[3] assert ctrl_line.get_data() == ((0, 0), (0, 2)) circles = drawer.ax.patches assert len(circles) == 1 circle = drawer.ax.patches[0] assert circle.width == 0.2 assert circle.center == (0, 0) plt.close()
def test_ctrl_multi_wires(self): """Tests two control wires with no target.""" drawer = MPLDrawer(1, 3) ctrl_wires = (0, 1) drawer.ctrl(0, ctrl_wires) ctrl_line = drawer.ax.lines[3] assert ctrl_line.get_data() == ((0, 0), ctrl_wires) circles = drawer.ax.patches assert len(circles) == 2 for wire, circle in zip(ctrl_wires, circles): assert circle.width == 0.2 assert circle.center == (0, wire) 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_ctrl_formatting(self): """Tests two control wires with no target.""" drawer = MPLDrawer(1, 3) ctrl_wires = (0, 1) rgba_red = (1, 0, 0, 1) options = {"color": rgba_red, "linewidth": 4} drawer.ctrl(0, ctrl_wires, control_values=[1, 0], options=options) ctrl_line = drawer.ax.lines[3] assert ctrl_line.get_color() == rgba_red assert ctrl_line.get_linewidth() == 4 closed_circ = drawer.ax.patches[0] assert closed_circ.get_facecolor() == rgba_red open_circ = drawer.ax.patches[1] assert open_circ.get_edgecolor() == rgba_red assert open_circ.get_facecolor() == to_rgba( plt.rcParams["axes.facecolor"]) assert open_circ.get_linewidth() == 4 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()