Пример #1
0
    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()
Пример #2
0
    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()
Пример #3
0
def measure_formatted(savefile="measure_formatted.png"):
    drawer = MPLDrawer(n_wires=1, n_layers=1)

    measure_box = {"facecolor": "white", "edgecolor": "indigo"}
    measure_lines = {"edgecolor": "indigo", "facecolor": "plum", "linewidth": 2}
    drawer.measure(0, 0, box_options=measure_box, lines_options=measure_lines)
    plt.savefig(folder / savefile)
    plt.close()
Пример #4
0
def SWAP_formatted(savefile="SWAP_formatted.png"):
    swap_keywords = {"linewidth": 2, "color": "indigo"}

    drawer = MPLDrawer(n_wires=2, n_layers=1)

    drawer.SWAP(0, (0, 1), options=swap_keywords)
    plt.savefig(folder / savefile)
    plt.close()
Пример #5
0
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()
Пример #6
0
    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"]
Пример #7
0
    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()
Пример #8
0
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()
Пример #9
0
    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()
Пример #10
0
    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()
Пример #11
0
    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()
Пример #12
0
    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()
Пример #13
0
    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()
Пример #14
0
    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()
Пример #15
0
    def test_box_formatting(self):
        """Tests that box_options influences the rectangle"""

        drawer = MPLDrawer(1, 1)
        rgba_red = (1, 0, 0, 1)
        rgba_green = (0, 1, 0, 1)
        options = {"facecolor": rgba_red, "edgecolor": rgba_green}
        drawer.box_gate(0, 0, text="X", box_options=options)

        rect = drawer.ax.patches[0]
        assert rect.get_facecolor() == rgba_red
        assert rect.get_edgecolor() == rgba_green
        plt.close()
Пример #16
0
    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()
Пример #17
0
    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()
Пример #18
0
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()
Пример #19
0
    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()
Пример #20
0
    def test_labels_formatting(self):
        """Test labels are formatted with text options."""

        drawer = MPLDrawer(1, 3)

        rgba_red = (1, 0, 0, 1)
        labels = (0, 1, 2)
        options = {"fontsize": 10, "color": rgba_red}
        drawer.label(labels, text_options=options)

        for text in drawer.ax.texts:
            assert text.get_fontsize() == 10
            assert text.get_color() == rgba_red

        plt.close()
Пример #21
0
    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
Пример #22
0
    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()
Пример #23
0
    def test_extra_width(self):
        """tests a box with added width."""

        drawer = MPLDrawer(1, 1)
        drawer.box_gate(0, 0, text="Wide Gate", extra_width=0.4)

        rect = drawer.ax.patches[0]

        assert allclose(rect.get_xy(), (-0.6, -0.4))
        assert rect.get_height() == 0.8
        assert allclose(rect.get_width(), 1.2)

        text = drawer.ax.texts[0]

        assert text.get_text() == "Wide Gate"
        assert text.get_position() == (0, 0)
        plt.close()
Пример #24
0
    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()
Пример #25
0
    def test_multiwire_box(self):
        """tests a gate spanning multiple wires."""

        drawer = MPLDrawer(1, 3)
        drawer.box_gate(0, (0, 2), text="Tall Gate")

        rect = drawer.ax.patches[0]

        assert rect.get_xy() == (-0.4, -0.4)
        assert rect.get_width() == 0.8
        assert rect.get_height() == 2.8

        text = drawer.ax.texts[0]

        assert text.get_text() == "Tall Gate"
        assert text.get_position() == (0, 1.0)
        plt.close()
Пример #26
0
    def test_CNOT_color(self):
        drawer = MPLDrawer(1, 3)
        rgba_red = (1, 0, 0, 1)
        drawer.CNOT(0, (0, 1), options={"color": rgba_red})

        ctrl_line = drawer.ax.lines[3]
        assert ctrl_line.get_color() == rgba_red

        center_line = drawer.ax.lines[4]
        assert center_line.get_color() == rgba_red

        ctrl_circle = drawer.ax.patches[0]
        assert ctrl_circle.get_facecolor() == rgba_red

        target_circle = drawer.ax.patches[1]
        assert target_circle.get_edgecolor() == rgba_red

        plt.close()
Пример #27
0
    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()
Пример #28
0
    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()
Пример #29
0
    def test_labels(self):
        """Tests labels are added"""

        drawer = MPLDrawer(1, 3)

        labels = ("a", "b", "c")
        drawer.label(labels)

        drawn_labels = drawer.ax.texts

        for wire, expected_label, actual_label in zip(range(3), labels,
                                                      drawn_labels):

            assert actual_label.get_text() == expected_label

            assert actual_label.get_position() == (-1.5, wire)

        plt.close()
Пример #30
0
    def test_simple_box(self):
        """tests basic functionality of box_gate."""

        drawer = MPLDrawer(1, 1)

        drawer.box_gate(0, 0, "X")

        rect = drawer.ax.patches[0]

        assert rect.get_xy() == (-0.4, -0.4)
        assert rect.get_width() == 0.8
        assert rect.get_height() == 0.8

        text = drawer.ax.texts[0]

        assert text.get_text() == "X"
        assert text.get_position() == (0, 0)
        plt.close()