The latest version of this notebook is available on https://github.com/qiskit/qiskit-tutorial.
When building a quantum circuit it often helps to draw the circuit. This is supported natively by a QuantumCircuit
object. You can either just call print()
on the circuit or call the draw()
method on the object. This will render a ASCII art version of the circuit diagram.
NOTE THIS ONLY WORKS WITH QISKIT 0.7
In [1]:
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
In [2]:
# Build a quantum circuit
n = 3 # number of qubits
q = QuantumRegister(n)
c = ClassicalRegister(n)
circuit = QuantumCircuit(q, c)
circuit.x(q[1])
circuit.h(q)
circuit.cx(q[0], q[1])
circuit.measure(q, c);
In [3]:
print(circuit)
In [4]:
circuit.draw()
Out[4]:
While a text output is useful for quickly seeing the output while developing a circuit it doesn't provide the most flexibility in it's output. There are 2 other alternative output renderers for the quantum circuit. One uses matplotlib and the other uses LaTex leveraging the qcircuit package. These can be specified by using mpl
and latex
values for the output
kwarg on the draw() method.
In [5]:
# Matplotlib Drawing
circuit.draw(output='mpl')
Out[5]:
In [6]:
# Latex Drawing
circuit.draw(output='latex')
Out[6]:
By default the draw method returns the rendered image as an object and does not output anything. The exact class returned depends on the output specified: 'text'
(the default returns a TextDrawer
object, 'mpl'
returns a matplotlib.Figure
object, and latex
returns a PIL.Image
object. Having the return types enables modifying or directly interacting with the rendered output from the drawers. Jupyter notebooks understand these return types and render it for us in this tutorial, but when running outside of jupyter you do not have this feature automatically. However, the draw()
method has optional arguments to display or save the output. When specified the filename
kwarg takes a path to save the rendered output to. Or if you're using the mpl
or latex
outputs you can leverage the interactive
kwarg to open the image in a new window (this will not always work from within a notebook but will be demonstrated anyway).
Depending on the output there are also options to customize the circuit diagram rendered by the circuit.
The first two options are shared between all 3 backends and they let you configure both the bit orders and whether or not you draw barriers. These can be set by the reverse_bits
kwarg and plot_barriers
kwarg respectively. The examples below will work with any output backend, only latex
is used for brevity.
In [7]:
# Draw a new circuit with barriers and more registers
q_a = QuantumRegister(3, name='qa')
q_b = QuantumRegister(5, name='qb')
c_a = ClassicalRegister(3)
c_b = ClassicalRegister(5)
circuit = QuantumCircuit(q_a, q_b, c_a, c_b)
circuit.x(q_a[1])
circuit.x(q_b[1])
circuit.x(q_b[2])
circuit.x(q_b[4])
circuit.barrier()
circuit.h(q_a)
circuit.barrier(q_a)
circuit.h(q_b)
circuit.cswap(q_b[0], q_b[1], q_b[2])
circuit.cswap(q_b[2], q_b[3], q_b[4])
circuit.cswap(q_b[3], q_b[4], q_b[0])
circuit.barrier(q_b)
circuit.measure(q_a, c_a)
circuit.measure(q_b, c_b);
In [8]:
# Draw the circuit
circuit.draw(output='latex')
Out[8]:
In [9]:
# Draw the circuit with reversed bit order
circuit.draw(output='latex', reverse_bits=True)
Out[9]:
In [10]:
# Draw the circuit without barriers
circuit.draw(output='latex', plot_barriers=False)
Out[10]:
In [11]:
# Draw the circuit without barriers and reverse bit order
circuit.draw(output='latex', plot_barriers=False, reverse_bits=True)
Out[11]:
There are also some options available to customize the output diagram which only work for a specific backend. The line_length
kwarg for the text
backend which can be used to set a maximum width for the output. When a diagram is wider than that it will wrap the diagram below. The mpl
backend has the style
kwarg which is used to customize the output. The scale
option is used by the mpl
and latex
backends to adjust the size of the output image, it's a multiplicative adjustment factor used to scale the output size. The style
kwarg takes in a dict with many different options in it. It provides a high level of flexibility and enables things like changing colors, changing rendered text for different types of gates, different line styles, etc. The list of available options for this are:
`'#000000'`
'#000000'
'#000000'
'#778899'
'#000000'
'#ffffff'
'#bdbdbd'
'#ffffff'
'id': 'id',
'u0': 'U_0',
'u1': 'U_1',
'u2': 'U_2',
'u3': 'U_3',
'x': 'X',
'y': 'Y',
'z': 'Z',
'h': 'H',
's': 'S',
'sdg': 'S^\\dagger',
't': 'T',
'tdg': 'T^\\dagger',
'rx': 'R_x',
'ry': 'R_y',
'rz': 'R_z',
'reset': '\\left|0\\right\\rangle'
}
You must specify all the necessary values if using this. There is
no provision for passing an incomplete dict in.gatefacecolor
and
the keys are the same as displaytext
. Also, just like
displaytext
there is no provision for an incomplete dict passed
in.latex
output modes.'solid'
, 'doublet'
, or any valid matplotlib
linestyle
kwarg value. Defaults to doublet
In [12]:
# Set line length to 80 for above circuit
circuit.draw(output='text', line_length=80)
Out[12]:
In [13]:
# Change the background color in mpl
style = {'backgroundcolor': 'lightgreen'}
circuit.draw(output='mpl', style=style)
Out[13]:
In [14]:
# Scale the mpl output to 1/2 the normal size
circuit.draw(output='mpl', scale=0.5)
Out[14]:
In [15]:
# Scale the latex output to 1/2 the normal size
circuit.draw(output='latex', scale=0.5)
Out[15]:
One additional option available with the latex output type is to return the raw LaTex source code instead of rendering an image for it. This enables easy integration in a seperate LaTex document. To use this you can just set the output
kwarg to 'latex_source'
. You can also use the filename
kwarg to write this output directly to a file (and still return the string) instead of returning just a string.
In [16]:
# Print the latex source for the visualization
print(circuit.draw(output='latex_source'))
In [17]:
# Save the latex source to a file
circuit.draw(output='latex_source', filename='/tmp/circuit.tex');
If you have an application where you prefer to draw a circuit with a self contained function instead of as a method of a circuit object you can directly use the circuit_drawer()
function, which is part of the public stable interface from qiskit.tools.visualization
. The function behaves identically to the circuit.draw()
method except that it takes in a circuit object as required argument.
In [18]:
from qiskit.tools.visualization import circuit_drawer
In [19]:
circuit_drawer(circuit, output='mpl', plot_barriers=False)
Out[19]: