Skip to content

Commit

Permalink
Merge pull request #1370 from sergiomtzlosa/matplotlib-circuit-drawer
Browse files Browse the repository at this point in the history
Matplotlib circuit drawer
  • Loading branch information
MatteoRobbiati authored Sep 4, 2024
2 parents f20f433 + 232c455 commit 47cb206
Show file tree
Hide file tree
Showing 7 changed files with 1,387 additions and 0 deletions.
45 changes: 45 additions & 0 deletions doc/source/code-examples/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -327,3 +327,48 @@ For example
q2: ──────o──|──|────o──|──|──H─U1─U1────────|─|─
q3: ─────────o──|───────o──|────o──|──H─U1───|─x─
q4: ────────────o──────────o───────o────o──H─x───

How to visualize a circuit with style?
--------------------------------------

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
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.
The built-in styles are: ``garnacha``, ``fardelejo``, ``quantumspain``, ``color-blind``, ``cachirulo`` or custom dictionary.

For example, we can draw the QFT circuit for 5-qubits:

.. testcode::

import matplotlib.pyplot as plt
import qibo
from qibo import gates, models
from qibo.models import QFT

# new plot function based on matplotlib
from qibo.ui import plot_circuit

%matplotlib inline

# create a 5-qubits QFT circuit
c = QFT(5)
c.add(gates.M(qubit) for qubit in range(2))

# print circuit with default options (default black & white style, scale factor of 0.6 and clustered gates)
plot_circuit(c);

# print the circuit with built-int style "garnacha", clustering gates and a custom scale factor
# built-in styles: "garnacha", "fardelejo", "quantumspain", "color-blind", "cachirulo" or custom dictionary
plot_circuit(c, scale = 0.8, cluster_gates = True, style="garnacha");

# plot the Qibo circuit with a custom style
custom_style = {
"facecolor" : "#6497bf",
"edgecolor" : "#01016f",
"linecolor" : "#01016f",
"textcolor" : "#01016f",
"fillcolor" : "#ffb9b9",
"gatecolor" : "#d8031c",
"controlcolor" : "#360000"
}

plot_circuit(c, scale = 0.8, cluster_gates = True, style=custom_style);
470 changes: 470 additions & 0 deletions examples/circuit-draw-mpl/qibo-draw-circuit-matplotlib.ipynb

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/qibo/ui/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from qibo.ui.mpldrawer import plot_circuit
42 changes: 42 additions & 0 deletions src/qibo/ui/drawer_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from qibo.gates.abstract import Gate


class FusedStartGateBarrier(Gate):
"""
:class:`qibo.ui.drawer_utils.FusedStartGateBarrier` gives room to fused group of gates.
Inherit from ``qibo.gates.abstract.Gate``. A special gate barrier gate to pin the starting point of fused gates.
"""

def __init__(self, q_ctrl, q_trgt, nfused, equal_qbits=False):

super().__init__()
self.name = (
"FusedStartGateBarrier"
+ str(nfused)
+ ("" if not equal_qbits else "@EQUAL")
)
self.draw_label = ""
self.control_qubits = (q_ctrl,)
self.target_qubits = (q_trgt,) if q_ctrl != q_trgt else ()
self.init_args = [q_trgt, q_ctrl] if q_ctrl != q_trgt else [q_ctrl]
self.unitary = False
self.is_controlled_by = False
self.nfused = nfused


class FusedEndGateBarrier(Gate):
"""
:class:`qibo.ui.drawer_utils.FusedEndGateBarrier` gives room to fused group of gates.
Inherit from ``qibo.gates.abstract.Gate``. A special gate barrier gate to pin the ending point of fused gates.
"""

def __init__(self, q_ctrl, q_trgt):

super().__init__()
self.name = "FusedEndGateBarrier"
self.draw_label = ""
self.control_qubits = (q_ctrl,)
self.target_qubits = (q_trgt,) if q_ctrl != q_trgt else ()
self.init_args = [q_trgt, q_ctrl] if q_ctrl != q_trgt else [q_ctrl]
self.unitary = False
self.is_controlled_by = False
Loading

0 comments on commit 47cb206

Please sign in to comment.