示例#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 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()
示例#4
0
    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()
示例#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 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()
示例#8
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()
示例#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 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()
示例#11
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()
示例#12
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()
示例#13
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()
示例#14
0
    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()
示例#15
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()
示例#16
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()
示例#17
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()
示例#18
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()
示例#19
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()
示例#20
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()
示例#21
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()
示例#22
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
示例#23
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()
示例#24
0
    def test_CNOT_control_values(self):
        """Tests the ``control_values`` keyword for CNOT."""

        drawer = MPLDrawer(1, 3)

        drawer.CNOT(0, (0, 1, 2), control_values=[True, False])

        ctrl_circ1 = drawer.ax.patches[0]
        ctrl_circ2 = drawer.ax.patches[1]

        # first should be a closed in circle
        assert ctrl_circ1.get_facecolor() == to_rgba(
            plt.rcParams["lines.color"])
        # second facecolor should match the background
        assert ctrl_circ2.get_facecolor() == to_rgba(
            plt.rcParams["axes.facecolor"])
示例#25
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()
示例#26
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()
示例#27
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()
示例#28
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()
示例#29
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()
示例#30
0
    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()