To use the circuit drawer tool, install the visualization extra:

pip install 'pyqasm[visualization]'

Draw a quantum circuit directly from any OpenQASM string:

from pyqasm import draw

qasm = """
OPENQASM 3.0;
include "stdgates.inc";

qubit[3] q;
bit[3] b;

h q[0];
z q[1];
rz(pi/1.1) q[0];
cx q[0], q[1];
swap q[0], q[1];
ccx q[0], q[1], q[2];
b = measure q;
"""

draw(qasm, output='mpl')
Currently, only the mpl (matplotlib) output format is supported.

Input Formats

All draw methods accepts either a str (QASM source) or a pyqasm.QasmModule object. The following are equivalent:

from pyqasm import loads, draw
from pyqasm.printer import mpl_draw

module = loads(qasm_str)

draw(module, output='mpl')
draw(qasm_str, output='mpl')

mpl_draw(module)
mpl_draw(qasm_str)

Exclude Idle Wires

Use the idle_wires=False option to exclude idle qubits (i.e. wires with no circuit elements) in the output circuit diagram:

from pyqasm import draw

qasm = """
OPENQASM 3.0;
include "stdgates.inc";

qubit[3] q;
bit[2] b;

h q[0];
h q[1];
cx q[0], q[1];
b[0] = measure q[0];
b[1] = measure q[1];
"""

draw(qasm, idle_wires=False, output='mpl')

Save Diagram

Save the circuit diagram to a file by specifying output='mpl' and a filename:

draw(qasm, output='mpl', filename='/path/to/circuit.png')