qml.draw_mpl

draw_mpl(qnode, wire_order=None, show_all_wires=False, decimals=None, expansion_strategy=None, **kwargs)[source]

Draw a qnode with matplotlib

Parameters

qnode (QNode) – the input QNode that is to be drawn.

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.

  • 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.

  • expansion_strategy (str) –

    The strategy to use when circuit expansions or decompositions are required.

    • gradient: The QNode will attempt to decompose the internal circuit such that all circuit operations are supported by the gradient method.

    • device: The QNode will attempt to decompose the internal circuit such that all circuit operations are natively supported by the device.

Returns

A function that has the same argument signature as qnode. When called, the function will draw the QNode as a tuple of (matplotlib.figure.Figure, matplotlib.axes._axes.Axes)

Example:

dev = qml.device('lightning.qubit', wires=(0,1,2,3))

@qml.qnode(dev)
def circuit(x, z):
    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(x, wires=0)
    qml.CRZ(z, wires=(3,0))
    return qml.expval(qml.PauliZ(0))


fig, ax = qml.draw_mpl(circuit)(1.2345,1.2345)
fig.show()
../../_images/main_example.png

Decimals:

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

@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 = qml.draw_mpl(circuit2, decimals=2)(1.23456, [1.2345,2.3456,3.456])
fig.show()
../../_images/decimals.png

Wires:

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

fig, ax = qml.draw_mpl(circuit, wire_order=[3,2,1,0])(1.2345,1.2345)
fig.show()
../../_images/wire_order.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 = qml.draw_mpl(circuit, wire_order=["aux"], show_all_wires=True)(1.2345,1.2345)
fig.show()
../../_images/show_all_wires.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 = qml.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)
fig.show()
../../_images/postprocessing.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 to qml.drawer.use_style.

qml.drawer.use_style('black_white')
fig, ax = qml.draw_mpl(circuit)(1.2345,1.2345)
fig.show()
../../_images/black_white_style.png

You can also control the appearance with matplotlib’s provided tools, see the matplotlib docs . For example, we can customize plt.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 = qml.draw_mpl(circuit)(1.2345,1.2345)
fig.show()
../../_images/rcparams.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 = qml.draw_mpl(circuit, wire_options={'color':'black', 'linewidth': 5},
            label_options={'size': 20})(1.2345,1.2345)
fig.show()
../../_images/wires_labels.png

Contents

Using PennyLane

Development

API