Skip to content

Commit 47cb206

Browse files
Merge pull request #1370 from sergiomtzlosa/matplotlib-circuit-drawer
Matplotlib circuit drawer
2 parents f20f433 + 232c455 commit 47cb206

File tree

7 files changed

+1387
-0
lines changed

7 files changed

+1387
-0
lines changed

doc/source/code-examples/examples.rst

+45
Original file line numberDiff line numberDiff line change
@@ -327,3 +327,48 @@ For example
327327
q2: ──────o──|──|────o──|──|──H─U1─U1────────|─|─
328328
q3: ─────────o──|───────o──|────o──|──H─U1───|─x─
329329
q4: ────────────o──────────o───────o────o──H─x───
330+
331+
How to visualize a circuit with style?
332+
--------------------------------------
333+
334+
Qibo is able to draw a circuit using ``matplotlib`` library by calling the function ``plot_circuit``. It also have built-in styles ready to use
335+
and also it is possible to apply custom styles to the circuit. The function is able to cluster the gates to reduce the circuit depth.
336+
The built-in styles are: ``garnacha``, ``fardelejo``, ``quantumspain``, ``color-blind``, ``cachirulo`` or custom dictionary.
337+
338+
For example, we can draw the QFT circuit for 5-qubits:
339+
340+
.. testcode::
341+
342+
import matplotlib.pyplot as plt
343+
import qibo
344+
from qibo import gates, models
345+
from qibo.models import QFT
346+
347+
# new plot function based on matplotlib
348+
from qibo.ui import plot_circuit
349+
350+
%matplotlib inline
351+
352+
# create a 5-qubits QFT circuit
353+
c = QFT(5)
354+
c.add(gates.M(qubit) for qubit in range(2))
355+
356+
# print circuit with default options (default black & white style, scale factor of 0.6 and clustered gates)
357+
plot_circuit(c);
358+
359+
# print the circuit with built-int style "garnacha", clustering gates and a custom scale factor
360+
# built-in styles: "garnacha", "fardelejo", "quantumspain", "color-blind", "cachirulo" or custom dictionary
361+
plot_circuit(c, scale = 0.8, cluster_gates = True, style="garnacha");
362+
363+
# plot the Qibo circuit with a custom style
364+
custom_style = {
365+
"facecolor" : "#6497bf",
366+
"edgecolor" : "#01016f",
367+
"linecolor" : "#01016f",
368+
"textcolor" : "#01016f",
369+
"fillcolor" : "#ffb9b9",
370+
"gatecolor" : "#d8031c",
371+
"controlcolor" : "#360000"
372+
}
373+
374+
plot_circuit(c, scale = 0.8, cluster_gates = True, style=custom_style);

examples/circuit-draw-mpl/qibo-draw-circuit-matplotlib.ipynb

+470
Large diffs are not rendered by default.

src/qibo/ui/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from qibo.ui.mpldrawer import plot_circuit

src/qibo/ui/drawer_utils.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from qibo.gates.abstract import Gate
2+
3+
4+
class FusedStartGateBarrier(Gate):
5+
"""
6+
:class:`qibo.ui.drawer_utils.FusedStartGateBarrier` gives room to fused group of gates.
7+
Inherit from ``qibo.gates.abstract.Gate``. A special gate barrier gate to pin the starting point of fused gates.
8+
"""
9+
10+
def __init__(self, q_ctrl, q_trgt, nfused, equal_qbits=False):
11+
12+
super().__init__()
13+
self.name = (
14+
"FusedStartGateBarrier"
15+
+ str(nfused)
16+
+ ("" if not equal_qbits else "@EQUAL")
17+
)
18+
self.draw_label = ""
19+
self.control_qubits = (q_ctrl,)
20+
self.target_qubits = (q_trgt,) if q_ctrl != q_trgt else ()
21+
self.init_args = [q_trgt, q_ctrl] if q_ctrl != q_trgt else [q_ctrl]
22+
self.unitary = False
23+
self.is_controlled_by = False
24+
self.nfused = nfused
25+
26+
27+
class FusedEndGateBarrier(Gate):
28+
"""
29+
:class:`qibo.ui.drawer_utils.FusedEndGateBarrier` gives room to fused group of gates.
30+
Inherit from ``qibo.gates.abstract.Gate``. A special gate barrier gate to pin the ending point of fused gates.
31+
"""
32+
33+
def __init__(self, q_ctrl, q_trgt):
34+
35+
super().__init__()
36+
self.name = "FusedEndGateBarrier"
37+
self.draw_label = ""
38+
self.control_qubits = (q_ctrl,)
39+
self.target_qubits = (q_trgt,) if q_ctrl != q_trgt else ()
40+
self.init_args = [q_trgt, q_ctrl] if q_ctrl != q_trgt else [q_ctrl]
41+
self.unitary = False
42+
self.is_controlled_by = False

0 commit comments

Comments
 (0)