Visualize circuits
Ang pahinang ito ay hindi pa naisalin. Nakikita mo ang orihinal na bersyon sa Ingles.
Package versions
The code on this page was developed using the following requirements. We recommend using these versions or newer.
qiskit[all]~=2.3.0
It's often useful to see the circuits you're creating. Use the following options to display Qiskit circuits.
from qiskit import QuantumCircuit
Draw a quantum circuit
The QuantumCircuit class supports drawing circuits through the draw() method, or by printing the circuit object. By default, both render an ASCII art version of the circuit diagram.
Note that print returns None but has the side effect of printing the diagram, whereas QuantumCircuit.draw returns the diagram with no side effects. Since Jupyter notebooks display the output of the last line of each cell, they appear to have the same effect.
# Build a quantum circuit
circuit = QuantumCircuit(3, 3)
circuit.x(1)
circuit.h(range(3))
circuit.cx(0, 1)
circuit.measure(range(3), range(3));
print(circuit)
┌───┐ ┌─┐
q_0: ┤ H ├───────■──┤M├───
├───┤┌───┐┌─┴─┐└╥┘┌─┐
q_1: ┤ X ├┤ H ├┤ X ├─╫─┤M├
├───┤└┬─┬┘└───┘ ║ └╥┘
q_2: ┤ H ├─┤M├───────╫──╫─
└───┘ └╥┘ ║ ║
c: 3/═ ══════╩════════╩══╩═
2 0 1
circuit.draw()
┌───┐ ┌─┐
q_0: ┤ H ├───────■──┤M├───
├───┤┌───┐┌─┴─┐└╥┘┌─┐
q_1: ┤ X ├┤ H ├┤ X ├─╫─┤M├
├───┤└┬─┬┘└───┘ ║ └╥┘
q_2: ┤ H ├─┤M├───────╫──╫─
└───┘ └╥┘ ║ ║
c: 3/═══════╩════════╩══╩═
2 0 1
Alternative renderers
A text output is useful for quickly seeing the output while developing a circuit, but it doesn't provide the most flexibility. There are two alternative output renderers for the quantum circuit. One uses Matplotlib and the other uses LaTeX. The LaTeX renderer requires the qcircuit package. Select these renderers by setting the "output" argument to the strings mpl and latex.
OSX users can get the required LaTeX packages through the mactex package.
# Matplotlib drawing
circuit.draw(output="mpl")
# Latex drawing
circuit.draw(output="latex")
Save output
Drawing a large-scale circuit inline in a Jupyter notebook can be slow or unreadable. You can save the diagram directly to a file, then open it in an image viewer and zoom in as needed.
# Save as an image using the Matplotlib drawer
circuit.draw(output="mpl", filename="circuit-mpl.jpeg")
# Or save a LaTeX rendering
circuit.draw(output="latex", filename="circuit-latex.pdf")
Control circuit drawings
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. Jupyter notebooks understand these return types and render them properly, but when running outside of Jupyter, images will not display automatically.
The draw() method has optional arguments to display or save the output. When specified, the filename kwarg takes a path to which it saves the rendered output. Alternatively, if you're using the mpl or latex outputs, you can use the interactive kwarg to open the image in a new window (this will not always work from within a notebook).
Customize the output
Depending on the output, there are also options to customize the circuit diagram.