def test_standard_use(): """Tests standard usage produces expected figure and axes""" # not constructed before calling fig, ax = qml.draw_mpl(circuit1)(1.23, 2.34) assert isinstance(fig, mpl.figure.Figure) assert isinstance(ax, mpl.axes._axes.Axes) # proxy for whether correct things were drawn assert len(ax.patches ) == 7 # two boxes, 2 circles for CNOT, 3 patches for measure assert len(ax.lines) == 6 # three wires, three lines for CNOT assert len(ax.texts) == 5 # three wire labels, 2 box labels assert ax.texts[0].get_text() == "0" assert ax.texts[1].get_text() == "a" assert ax.texts[2].get_text() == "1.23" # gates in same layer can be in any order texts = [t.get_text() for t in ax.texts[3:]] assert "RX" in texts assert "RY" in texts plt.close()
def test_decimals(): """Test decimals changes operation labelling""" _, ax = qml.draw_mpl(circuit1, decimals=2)(1.23, 2.34) texts = [t.get_text() for t in ax.texts[3:]] assert "RX\n(1.23)" in texts assert "RY\n(2.34)" in texts plt.close()
def wires_labels(circuit): fig, ax = draw_mpl(circuit, wire_options={ 'color': 'black', 'linewidth': 5 }, label_options={'size': 20})(1.2345, 1.2345) plt.savefig(folder / "wires_labels.png") plt.close()
def make_imag(circuit, style): qml.drawer.use_style(style) fig, ax = qml.draw_mpl(circuit)(1.2345,1.2345) fig.suptitle(style, fontsize='xx-large') plt.savefig(folder / (style + "_style.png")) plt.close() qml.drawer.use_style('default')
def use_style(circuit): qml.drawer.use_style('black_white') fig, ax = qml.draw_mpl(circuit)(1.2345,1.2345) plt.savefig(folder / "black_white_style.png") plt.close() plt.style.use('default')
def test_empty_wires(self): """Test empty wires do not appear by default""" _, ax = qml.draw_mpl(circuit2)(1.23) assert len(ax.lines) == 1 # one wire assert len(ax.texts) == 2 # one wire label and one gate label assert ax.texts[0].get_text() == "0" assert ax.texts[1].get_text() == "RX" plt.close()
def decimals(dev): @qml.qnode(dev) def circuit2(x, y): qml.RX(x, wires=0) qml.Rot(*y, wires=0) return qml.expval(qml.PauliZ(0)) fig, ax = draw_mpl(circuit2, decimals=2)(1.23456, [1.2345, 2.3456, 3.456]) plt.savefig(folder / "decimals.png")
def test_wire_order(self): """Test wire_order changes order of wires""" _, ax = qml.draw_mpl(circuit1, wire_order=(1.23, "a"))(1.23, 2.34) assert len(ax.texts) == 5 assert ax.texts[0].get_text() == "1.23" assert ax.texts[1].get_text() == "a" assert ax.texts[2].get_text() == "0" plt.close()
def test_show_all_wires(self): """Test show_all_wires=True displays empty wires.""" _, ax = qml.draw_mpl(circuit2, show_all_wires=True)(1.23) assert len(ax.lines) == 3 # three wires assert len(ax.texts) == 4 # three wire labels and one gate label assert ax.texts[0].get_text() == "0" assert ax.texts[1].get_text() == "a" assert ax.texts[2].get_text() == "1.23" plt.close()
def test_active_wire_notches(self, notches, n_patches): """Test active wire notches can be toggled by keyword.""" @qml.qnode(dev) def temp_circ(): qml.QFT(wires=(0, 1.23)) return qml.probs(0) _, ax = qml.draw_mpl(temp_circ, show_all_wires=True, active_wire_notches=notches)() assert len(ax.patches) == n_patches
def rcparams(circuit): plt.rcParams['patch.facecolor'] = 'white' plt.rcParams['patch.edgecolor'] = 'black' plt.rcParams['patch.linewidth'] = 2 plt.rcParams['patch.force_edgecolor'] = True plt.rcParams['lines.color'] = 'black' fig, ax = draw_mpl(circuit)(1.2345, 1.2345) plt.savefig(folder / "rcparams.png") plt.close() plt.style.use('default')
def postprocessing(circuit): fig, ax = draw_mpl(circuit)(1.2345,1.2345) fig.suptitle("My Circuit", fontsize="xx-large") options = {'facecolor': "white", 'edgecolor': "#f57e7e", "linewidth": 6, "zorder": -1} box1 = plt.Rectangle((-0.5, -0.5), width=3.0, height=4.0, **options) ax.add_patch(box1) ax.annotate("CSWAP", xy=(3, 2.5), xycoords='data', xytext=(3.8,1.5), textcoords='data', arrowprops={'facecolor': 'black'}, fontsize=14) plt.savefig(folder / "postprocessing.png") plt.close()
def test_label_options(): """Test label options modifies label style.""" _, ax = qml.draw_mpl(circuit1, label_options={ "color": "purple", "fontsize": 20 })(1.23, 2.34) for l in ax.texts[0:3]: # three labels assert l.get_color() == "purple" assert l.get_fontsize() == 20 plt.close()
def test_wire_options(self): """Test wire options modifies wire styling""" _, ax = qml.draw_mpl(circuit1, wire_options={ "color": "black", "linewidth": 4 })(1.23, 2.34) for w in ax.lines[0:3]: # three wires assert w.get_color() == "black" assert w.get_linewidth() == 4 plt.close()
def test_wire_order_not_on_device(self): """Test when ``wire_order`` priority by requesting ``show_all_wires`` with the ``wire_order`` containing wires not on the device. The output should have three wires, one active and two empty wires from the wire order.""" _, ax = qml.draw_mpl(circuit2, wire_order=[2, "a"], show_all_wires=True)(1.23) assert len(ax.lines) == 3 # three wires assert len(ax.texts) == 4 # three wire labels and one gate label assert ax.texts[0].get_text() == "2" assert ax.texts[1].get_text() == "a" assert ax.texts[2].get_text() == "0"
def rcparams(circuit): 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' fig, ax = qml.draw_mpl(circuit)(1.2345,1.2345) plt.savefig(folder / "rcparams.png") plt.close() plt.style.use('default')
def test_style(self): """Test matplotlib styles impact figure styling.""" plt.style.use("fivethirtyeight") _, ax = qml.draw_mpl(circuit1)(1.23, 2.34) expected_facecolor = mpl.colors.to_rgba( plt.rcParams["patch.facecolor"]) assert ax.patches[0].get_facecolor() == expected_facecolor assert ax.patches[1].get_facecolor() == expected_facecolor expected_linecolor = mpl.colors.to_rgba(plt.rcParams["lines.color"]) for l in ax.lines[0:-1]: # final is fancy arrow, has different styling assert mpl.colors.to_rgba(l.get_color()) == expected_linecolor plt.style.use("default") plt.close()
def test_rcparams(self): """Test setting rcParams modifies style.""" rgba_red = (1, 0, 0, 1) rgba_green = (0, 1, 0, 1) plt.rcParams["patch.facecolor"] = rgba_red plt.rcParams["lines.color"] = rgba_green _, ax = qml.draw_mpl(circuit1)(1.23, 2.34) assert ax.patches[0].get_facecolor() == rgba_red assert ax.patches[1].get_facecolor() == rgba_red for l in ax.lines[0:-1]: # final is fancy arrow, has different styling assert l.get_color() == rgba_green plt.style.use("default") plt.close()
def show_all_wires(circuit): fig, ax = draw_mpl(circuit, wire_order=["aux"], show_all_wires=True)(1.2345, 1.2345) plt.savefig(folder / "show_all_wires.png") plt.close()
def wire_order(circuit): fig, ax = draw_mpl(circuit, wire_order=[3, 2, 1, 0])(1.2345, 1.2345) plt.savefig(folder / "wire_order.png") plt.close()
def test_fontsize(self): """Test fontsize set by keyword argument.""" _, ax = qml.draw_mpl(circuit1, fontsize=20)(1.234, 1.234) for t in ax.texts: assert t.get_fontsize() == 20
def main_example(circuit): fig, ax = draw_mpl(circuit)(1.2345, 1.2345) plt.savefig(folder / "main_example.png") plt.close()
def Solarize_Light2(circuit): with plt.style.context("Solarize_Light2"): fig, ax = draw_mpl(circuit)(1.2345, 1.2345) plt.savefig(folder / "Solarize_Light2.png") plt.close()