Qiskit-terra ver 0.5.5 intoduces matplotlib_circuit_drawer
, which is a drop-in replacement of latex_circuit_drawer
. If LaTeX is installed, circuit_drawer
draws circuits by latex_circuit_drawer
. Otherwise, it draws them by matplotlib_circuit_drawer
. We explain the details of matplotlib_circuit_drawer
in this notebook.
Takashi Imamichi, Naoki Kanazawa
In [1]:
from math import pi
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit.tools.visualization import matplotlib_circuit_drawer as drawer, qx_color_scheme
# We recommend the following options for Jupter notebook
%matplotlib inline
In [2]:
# Create a Quantum Register called "q" with 3 qubits
qr = QuantumRegister(3, 'q')
# Create a Classical Register called "c" with 3 bits
cr = ClassicalRegister(3, 'c')
In [3]:
# Create a Quantum Circuit called involving "qr" and "cr"
circuit = QuantumCircuit(qr, cr)
circuit.x(qr[0]).c_if(cr, 3)
circuit.z(qr[0])
circuit.u2(pi/2, 2*pi/3, qr[1])
circuit.cu1(pi, qr[0], qr[1])
# Barrier to seperator the input from the circuit
circuit.barrier(qr[0])
circuit.barrier(qr[1])
circuit.barrier(qr[2])
# Toffoli gate from qubit 0,1 to qubit 2
circuit.ccx(qr[0], qr[1], qr[2])
# CNOT (Controlled-NOT) gate from qubit 0 to qubit 1
circuit.cx(qr[0], qr[1])
circuit.swap(qr[0], qr[2])
# measure gate from qr to cr
circuit.measure(qr, cr)
Out[3]:
In [4]:
QASM_source = circuit.qasm()
print(QASM_source)
In [5]:
drawer(circuit)
In [6]:
drawer(circuit, basis='u1,u2,u3,id,cx', scale=1.0)
In [7]:
my_style = {'plotbarrier': True}
drawer(circuit, style=my_style)
In [8]:
my_style = {'cregbundle': True}
drawer(circuit, style=my_style)
In [9]:
my_style = {'showindex': True}
drawer(circuit, style=my_style)
In [10]:
my_style = {'compress': True}
drawer(circuit, style=my_style)
In [11]:
my_style = {'fold': 6}
drawer(circuit, style=my_style)
In [12]:
my_style = {'usepiformat': True}
drawer(circuit, style=my_style)
In [13]:
qr = QuantumRegister(1, 'q')
circuit_xyz = QuantumCircuit(qr)
circuit_xyz.x(qr[0])
circuit_xyz.y(qr[0])
circuit_xyz.z(qr[0])
drawer(circuit_xyz)
In [14]:
my_style = {'displaytext': {'x': '😺', 'y': '\Sigma', 'z': '✈'}}
drawer(circuit_xyz, style=my_style)
In [15]:
qr = QuantumRegister(2, 'q')
circuit_cucz = QuantumCircuit(qr)
circuit_cucz.cz(qr[0], qr[1])
circuit_cucz.cu1(pi, qr[0], qr[1])
drawer(circuit_cucz)
In [16]:
my_style = {'latexdrawerstyle': False}
drawer(circuit_cucz, style=my_style)
In [17]:
qr = QuantumRegister(3, 'q')
cr = ClassicalRegister(3, 'c')
circuit_all = QuantumCircuit(qr, cr)
circuit_all.x(qr[0])
circuit_all.y(qr[0])
circuit_all.z(qr[0])
circuit_all.barrier(qr[0])
circuit_all.barrier(qr[1])
circuit_all.barrier(qr[2])
circuit_all.h(qr[0])
circuit_all.s(qr[0])
circuit_all.sdg(qr[0])
circuit_all.t(qr[0])
circuit_all.tdg(qr[0])
circuit_all.iden(qr[0])
circuit_all.reset(qr[0])
circuit_all.rx(pi, qr[0])
circuit_all.ry(pi, qr[0])
circuit_all.rz(pi, qr[0])
circuit_all.u0(pi, qr[0])
circuit_all.u1(pi, qr[0])
circuit_all.u2(pi, pi, qr[0])
circuit_all.u3(pi, pi, pi, qr[0])
circuit_all.swap(qr[0], qr[1])
circuit_all.cx(qr[0], qr[1])
circuit_all.cy(qr[0], qr[1])
circuit_all.cz(qr[0], qr[1])
circuit_all.ch(qr[0], qr[1])
circuit_all.cu1(pi, qr[0], qr[1])
circuit_all.cu3(pi, pi, pi, qr[0], qr[1])
circuit_all.crz(pi, qr[0], qr[1])
circuit_all.ccx(qr[0], qr[1], qr[2])
circuit_all.cswap(qr[0], qr[1], qr[2])
circuit_all.measure(qr, cr)
Out[17]:
In [18]:
drawer(circuit_all)
You can configure the color scheme. Composer style sheet is prepared as qx_color_scheme
.
In [19]:
cmp_style = qx_color_scheme()
cmp_style
Out[19]:
In [20]:
drawer(circuit_all, style=cmp_style)
In [21]:
cmp_style.update({
'usepiformat': True,
'showindex': True,
'cregbundle': True,
'compress': True,
'fold': 17
})
drawer(circuit_all, filename='circuit.pdf', style=cmp_style)
In [ ]: