qml.drawer.tape_mpl

tape_mpl(tape, wire_order=None, show_all_wires=False, decimals=None, style=None, *, fig=None, **kwargs)[source]

Produces a matplotlib graphic from a tape.

Parameters

tape (QuantumTape) – the operations and measurements to draw

Keyword Arguments
  • wire_order (Sequence[Any]) – the order (from top to bottom) to print the wires of the circuit

  • show_all_wires (bool) – If True, all wires, including empty wires, are printed.

  • decimals (int) – How many decimal points to include when formatting operation parameters. Default None will omit parameters from operation labels.

  • style (str) – visual style of plot. Valid strings are {'black_white', 'black_white_dark', 'sketch', 'sketch_dark', 'solarized_light', 'solarized_dark', 'default'}. If no style is specified, the global style set with use_style() will be used, and the initial default is ‘black_white’. If you would like to use your environment’s current rcParams, set style to “rcParams”. Setting style does not modify matplotlib global plotting settings.

  • fontsize (float or str) – fontsize for text. Valid strings are {'xx-small', 'x-small', 'small', 'medium', large', 'x-large', 'xx-large'}. Default is 14.

  • wire_options (dict) – matplotlib formatting options for the wire lines

  • label_options (dict) – matplotlib formatting options for the wire labels

  • active_wire_notches (bool) – whether or not to add notches indicating active wires. Defaults to True.

  • fig (None or matplotlib Figure) – Matplotlib figure to plot onto. If None, then create a new figure.

Returns

The key elements for matplotlib’s object oriented interface.

Return type

matplotlib.figure.Figure, matplotlib.axes._axes.Axes

Example:

ops = [
    qml.QFT(wires=(0,1,2,3)),
    qml.IsingXX(1.234, wires=(0,2)),
    qml.Toffoli(wires=(0,1,2)),
    qml.CSWAP(wires=(0,2,3)),
    qml.RX(1.2345, wires=0),
    qml.CRZ(1.2345, wires=(3,0))
]
measurements = [qml.expval(qml.Z(0))]
tape = qml.tape.QuantumTape(ops, measurements)

fig, ax = tape_mpl(tape)
fig.show()
../../_images/default.png

Decimals:

The keyword decimals controls how many decimal points to include when labelling the operations. The default value None omits parameters for brevity.

ops = [qml.RX(1.23456, wires=0), qml.Rot(1.2345,2.3456, 3.456, wires=0)]
measurements = [qml.expval(qml.Z(0))]
tape2 = qml.tape.QuantumTape(ops, measurements)

fig, ax = tape_mpl(tape2, decimals=2)
../../_images/decimals1.png

Wires:

The keywords wire_order and show_all_wires control the location of wires from top to bottom.

fig, ax = tape_mpl(tape, wire_order=[3,2,1,0])
../../_images/wire_order1.png

If a wire is in wire_order, but not in the tape, it will be omitted by default. Only by selecting show_all_wires=True will empty wires be diplayed.

fig, ax = tape_mpl(tape, wire_order=["aux"], show_all_wires=True)
../../_images/show_all_wires1.png

Integration with matplotlib:

This function returns matplotlib figure and axes objects. Using these objects, users can perform further customization of the graphic.

fig, ax = tape_mpl(tape)
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)
../../_images/postprocessing1.png

Formatting:

PennyLane has inbuilt styles for controlling the appearance of the circuit drawings. All available styles can be determined by evaluating qml.drawer.available_styles(). Any available string can then be passed via the kwarg style to change the settings for that plot. This will not affect style settings for subsequent matplotlib plots.

fig, ax = tape_mpl(tape, style='sketch')
../../_images/sketch_style1.png

You can also control the appearance with matplotlib’s provided tools, see the matplotlib docs . For example, we can customize plt.rcParams. To use a customized appearance based on matplotlib’s plt.rcParams, qml.drawer.tape_mpl must be run with style="rcParams":

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 = tape_mpl(tape, style="rcParams")
../../_images/rcparams1.png

The wires and wire labels can be manually formatted by passing in dictionaries of keyword-value pairs of matplotlib options. wire_options accepts options for lines, and label_options accepts text options.

fig, ax = tape_mpl(tape, wire_options={'color':'teal', 'linewidth': 5},
            label_options={'size': 20})
../../_images/wires_labels1.png