-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR introduces a graphical DAG visualizer to ease the task of debugging the transpilation procedure.
- Loading branch information
1 parent
5de7f86
commit 9c8f35e
Showing
5 changed files
with
202 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
# Copyright 2018, IBM. | ||
# | ||
# This source code is licensed under the Apache License, Version 2.0 found in | ||
# the LICENSE.txt file in the root directory of this source tree. | ||
|
||
# pylint: disable=invalid-name | ||
|
||
""" | ||
Visualization function for DAG circuit representation. | ||
""" | ||
|
||
import sys | ||
import copy | ||
import nxpd | ||
from ._error import VisualizationError | ||
|
||
|
||
def dag_drawer(dag, scale=0.7, filename=None, style='color'): | ||
"""Plot the directed acyclic graph (dag) to represent operation dependencies | ||
in a quantum circuit. | ||
Args: | ||
dag (DAGCircuit): The dag to draw. | ||
scale (float): scaling factor | ||
filename (str): file path to save image to (format inferred from name) | ||
style (str): 'plain': B&W graph | ||
'color' (default): color input/output/op nodes | ||
Returns: | ||
Ipython.display.Image: if in Jupyter notebook and not saving to file, | ||
otherwise None. | ||
Raises: | ||
VisualizationError: when style is not recognized. | ||
""" | ||
G = copy.deepcopy(dag.multi_graph) # don't modify the original graph attributes | ||
G.graph['dpi'] = 100 * scale | ||
|
||
if style == 'plain': | ||
pass | ||
elif style == 'color': | ||
for node in G.nodes: | ||
n = G.nodes[node] | ||
if n['type'] == 'op': | ||
n['label'] = str(n['name']) | ||
n['color'] = 'blue' | ||
n['style'] = 'filled' | ||
n['fillcolor'] = 'lightblue' | ||
if n['type'] == 'in': | ||
n['label'] = n['name'][0] + '[' + str(n['name'][1]) + ']' | ||
n['color'] = 'black' | ||
n['style'] = 'filled' | ||
n['fillcolor'] = 'green' | ||
if n['type'] == 'out': | ||
n['label'] = n['name'][0] + '[' + str(n['name'][1]) + ']' | ||
n['color'] = 'black' | ||
n['style'] = 'filled' | ||
n['fillcolor'] = 'red' | ||
for e in G.edges(data=True): | ||
e[2]['label'] = e[2]['name'][0] + "[" + str(e[2]['name'][1]) + "]" | ||
else: | ||
raise VisualizationError("Unrecognized style for the dag_drawer.") | ||
|
||
show = nxpd.nxpdParams['show'] | ||
if filename: | ||
show = False | ||
elif ('ipykernel' in sys.modules) and ('spyder' not in sys.modules): | ||
show = 'ipynb' | ||
else: | ||
show = True | ||
|
||
return nxpd.draw(G, filename=filename, show=show) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ scipy>=0.19,!=0.19.1 | |
sympy>=1.0 | ||
pillow>=4.2.1 | ||
psutil>=5 | ||
nxpd>=0.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters