From c43b520c3cb3d4c208ff0493c8e278291dcfaa2e Mon Sep 17 00:00:00 2001 From: chensgit169 Date: Thu, 24 Aug 2023 10:39:06 +0800 Subject: [PATCH 01/12] delete used lines --- src/quafu/qfasm/qfasm_convertor.py | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/quafu/qfasm/qfasm_convertor.py b/src/quafu/qfasm/qfasm_convertor.py index f441419..be0e121 100644 --- a/src/quafu/qfasm/qfasm_convertor.py +++ b/src/quafu/qfasm/qfasm_convertor.py @@ -2,7 +2,6 @@ from quafu.dagcircuits.circuit_dag import node_to_gate from quafu.dagcircuits.instruction_node import InstructionNode from quafu.circuits import QuantumCircuit, QuantumRegister -from quafu.elements.quantum_element import Instruction def qasm_to_circuit(qasm): @@ -173,15 +172,3 @@ def qasm2_to_quafu_qc(qc: QuantumCircuit, openqasm: str): print( "Warning: All operations after measurement will be removed for executing on experiment" ) - - -if __name__ == '__main__': - import re - - pattern = r"[a-z]" - - text = "Hello, world! This is a test." - - matches = re.findall(pattern, text) - - print(matches) From 35874ae269bf102e13ba7af4cbba042b5703c205 Mon Sep 17 00:00:00 2001 From: chensgit169 Date: Tue, 12 Sep 2023 13:51:40 +0800 Subject: [PATCH 02/12] specify typing of oracle gate_structure --- quafu/elements/quantum_element/quantum_gate.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/quafu/elements/quantum_element/quantum_gate.py b/quafu/elements/quantum_element/quantum_gate.py index e41cf22..b6e0e6d 100644 --- a/quafu/elements/quantum_element/quantum_gate.py +++ b/quafu/elements/quantum_element/quantum_gate.py @@ -253,13 +253,13 @@ def __init__(cls, name, bases, attrs): def customize_gate(cls_name: str, - gate_structure: list, + gate_structure: list[Instruction], qubit_num: int, ): """ helper function to create customized gate class :param cls_name: - :param gate_structure: + :param gate_structure: a list of instruction INSTANCES :param qubit_num: :return: """ From 74c4bb270d0a292b175f319610734110c60e65cd Mon Sep 17 00:00:00 2001 From: chensgit169 Date: Tue, 12 Sep 2023 22:51:33 +0800 Subject: [PATCH 03/12] add draw_dag.py --- quafu/visualisation/draw_dag.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 quafu/visualisation/draw_dag.py diff --git a/quafu/visualisation/draw_dag.py b/quafu/visualisation/draw_dag.py new file mode 100644 index 0000000..031786f --- /dev/null +++ b/quafu/visualisation/draw_dag.py @@ -0,0 +1,31 @@ +import graphviz + +from quafu import QuantumCircuit +from quafu.dagcircuits.circuit_dag import circuit_to_dag +from quafu.dagcircuits.instruction_node import InstructionNode + + +def _extract_node_info(node): + if isinstance(node, InstructionNode): + name, label = str(id(node)), node.name + else: + assert node == -1 or node == float("inf") + name, label = str(node), str(node) + return name, label + + +def draw_dag(qc: QuantumCircuit): + dag = circuit_to_dag(qc) + dot = graphviz.Digraph() + + for node in dag.nodes: + name, label = _extract_node_info(node) + dot.node(name, label=label) + + for edge in dag.edges(data=True): + node1, node2, link = edge + name1, label1 = _extract_node_info(node1) + name2, label2 = _extract_node_info(node2) + dot.edge(name1, name2, label=link['label']) + + dot.view(cleanup=True) From 902bd0810ca228c6a8e13084806a7eb017e0cd18 Mon Sep 17 00:00:00 2001 From: chensgit169 Date: Tue, 12 Sep 2023 22:52:04 +0800 Subject: [PATCH 04/12] delete ``pprint`` in task by mistake --- quafu/tasks/tasks.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/quafu/tasks/tasks.py b/quafu/tasks/tasks.py index df9b07d..7b3e31e 100644 --- a/quafu/tasks/tasks.py +++ b/quafu/tasks/tasks.py @@ -253,9 +253,7 @@ def send(self, raise UserError() else: res_dict = response.json() - import pprint - pprint.pprint(res_dict) if response.status_code in [201, 205]: raise UserError(res_dict["message"]) elif response.status_code == 5001: From 1a3046405c878cc8fc28dd98170fcca3f72553ab Mon Sep 17 00:00:00 2001 From: chensgit169 Date: Wed, 13 Sep 2023 10:02:41 +0800 Subject: [PATCH 05/12] move function of draw_dag() from ``dagcircuit`` into ``visualization`` Decouple from IPython, pygraphviz --- quafu/dagcircuits/circuit_dag.py | 76 ++++---------------------------- quafu/dagcircuits/dag_circuit.py | 6 +-- quafu/visualisation/draw_dag.py | 36 ++++++++++++++- 3 files changed, 47 insertions(+), 71 deletions(-) diff --git a/quafu/dagcircuits/circuit_dag.py b/quafu/dagcircuits/circuit_dag.py index 7e9fe1c..cdde958 100644 --- a/quafu/dagcircuits/circuit_dag.py +++ b/quafu/dagcircuits/circuit_dag.py @@ -1,24 +1,18 @@ -import numpy as np -from quafu import QuantumCircuit - -from quafu.elements.element_gates import * -from quafu.elements.quantum_element import Barrier, Delay, Measure, XYResonance -from quafu.elements.quantum_element.pulses.quantum_pulse import GaussianPulse, RectPulse, FlattopPulse +import copy +from typing import Any, List import networkx as nx -from typing import Dict, Any, List, Union -import copy +from quafu import QuantumCircuit +from quafu.dagcircuits.dag_circuit import ( + DAGCircuit, +) # dag_circuit.py in the same folder as circuit_dag.py now from quafu.dagcircuits.instruction_node import ( InstructionNode, ) # instruction_node.py in the same folder as circuit_dag.py now -from quafu.dagcircuits.dag_circuit import ( - DAGCircuit, -) # dag_circuit.py in the same folder as circuit_dag.py now - -# import pygraphviz as pgv -from networkx.drawing.nx_pydot import write_dot -from IPython.display import Image, SVG +from quafu.elements.element_gates import * +from quafu.elements.quantum_element import Barrier, Delay, Measure, XYResonance +from quafu.elements.quantum_element.pulses.quantum_pulse import GaussianPulse, RectPulse, FlattopPulse # transform a gate in quantumcircuit of quafu(not include measure_gate), @@ -340,58 +334,6 @@ def dag_to_circuit(dep_graph, n: int): return qcircuit -# Helper function to visualize the DAG,check the example in the docstring -def draw_dag(dep_g, output_format="png"): - """ - Helper function to visualize the DAG - - Args: - dep_g (DAG): DAG with Hashable Gates - output_format (str): output format, "png" or "svg" - - Returns: - img (Image or SVG): show the image of DAG, which is Image(filename="dag.png") or SVG(filename="dag.svg") - - example: - .. jupyter-execute:: - ex1: - # directly draw PNG picture - draw_dag(dep_g, output_format="png") # save a png picture "dag.png" and show it in jupyter notebook - - # directly draw SVG picture - draw_dag(dep_g, output_format="svg") # save a svg picture "dag.svg" and show it in jupyter notebook - - ex2: - # generate PNG picture - img_png = draw_dag(dep_g, output_format="png") - - # generate SVG picture - img_svg = draw_dag(dep_g, output_format="svg") - - # show PNG picture - img_png - - # show SVG picture - img_svg - - - """ - import pygraphviz - - write_dot(dep_g, "dag.dot") - G = pygraphviz.AGraph("dag.dot") - G.layout(prog="dot") - - if output_format == "png": - G.draw("dag.png") - return Image(filename="dag.png") - elif output_format == "svg": - G.draw("dag.svg") - return SVG(filename="dag.svg") - else: - raise ValueError("Unsupported output format: choose either 'png' or 'svg'") - - def nodelist_to_dag(op_nodes: List[Any]) -> DAGCircuit: # Starting Label Index i = 0 diff --git a/quafu/dagcircuits/dag_circuit.py b/quafu/dagcircuits/dag_circuit.py index d109461..b3f9aec 100644 --- a/quafu/dagcircuits/dag_circuit.py +++ b/quafu/dagcircuits/dag_circuit.py @@ -1,10 +1,10 @@ +from typing import Dict + import networkx as nx -from typing import Dict, Any, List +from networkx.classes.multidigraph import MultiDiGraph from quafu.dagcircuits.instruction_node import InstructionNode -from networkx.classes.multidigraph import MultiDiGraph - class DAGCircuit(MultiDiGraph): def __init__( diff --git a/quafu/visualisation/draw_dag.py b/quafu/visualisation/draw_dag.py index 031786f..031f645 100644 --- a/quafu/visualisation/draw_dag.py +++ b/quafu/visualisation/draw_dag.py @@ -7,7 +7,7 @@ def _extract_node_info(node): if isinstance(node, InstructionNode): - name, label = str(id(node)), node.name + name, label = str(id(node)), str(node) else: assert node == -1 or node == float("inf") name, label = str(node), str(node) @@ -15,6 +15,40 @@ def _extract_node_info(node): def draw_dag(qc: QuantumCircuit): + """ + Helper function to visualize the DAG + + Args: + dep_g (DAG): DAG with Hashable Gates + output_format (str): output format, "png" or "svg" + + Returns: + img (Image or SVG): show the image of DAG, which is Image(filename="dag.png") or SVG(filename="dag.svg") + + example: + .. jupyter-execute:: + ex1: + # directly draw PNG picture + draw_dag(dep_g, output_format="png") # save a png picture "dag.png" and show it in jupyter notebook + + # directly draw SVG picture + draw_dag(dep_g, output_format="svg") # save a svg picture "dag.svg" and show it in jupyter notebook + + ex2: + # generate PNG picture + img_png = draw_dag(dep_g, output_format="png") + + # generate SVG picture + img_svg = draw_dag(dep_g, output_format="svg") + + # show PNG picture + img_png + + # show SVG picture + img_svg + + + """ dag = circuit_to_dag(qc) dot = graphviz.Digraph() From efd9ca82e41a9a72336425513377bc9435a5e21d Mon Sep 17 00:00:00 2001 From: chensgit169 Date: Wed, 13 Sep 2023 10:09:40 +0800 Subject: [PATCH 06/12] remove dependence of pygraphviz, use graphviz instead --- requirements.txt | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index 3d7789a..e7bbf6d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,6 +8,6 @@ setuptools>=58.0.4 sparse>=0.13.0 scikit-build>=0.16.1 pybind11>=2.10.3 -pygraphviz>=1.11 +graphviz>=0.14.2 ply~=3.11 Pillow~=10.0.0 diff --git a/setup.py b/setup.py index 0918ba8..fa42d6d 100644 --- a/setup.py +++ b/setup.py @@ -28,7 +28,7 @@ "sparse>=0.13.0", "scikit-build>=0.16.1", "pybind11>=2.10.3", - "pygraphviz>=1.11", + "graphviz>=1.11", "ply~=3.11", "Pillow~=10.0.0" ] From 91319d6fc8dc6f6244b76a0b59a86a9c6d6d352a Mon Sep 17 00:00:00 2001 From: chensgit169 Date: Wed, 13 Sep 2023 10:21:52 +0800 Subject: [PATCH 07/12] add TODO in draw_dag() --- quafu/visualisation/draw_dag.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/quafu/visualisation/draw_dag.py b/quafu/visualisation/draw_dag.py index 031f645..bda597f 100644 --- a/quafu/visualisation/draw_dag.py +++ b/quafu/visualisation/draw_dag.py @@ -16,6 +16,8 @@ def _extract_node_info(node): def draw_dag(qc: QuantumCircuit): """ + # TODO: complete docstring, test supports for notebook, and format supports + Helper function to visualize the DAG Args: From e067d56a021be3d5c59cb1d8162a9e27c0706fe1 Mon Sep 17 00:00:00 2001 From: chensgit169 Date: Wed, 13 Sep 2023 10:31:50 +0800 Subject: [PATCH 08/12] update explanation for ``graphviz`` in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9627cdc..bc64de6 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ pip install -r requirements.txt python setup.py install ``` -Note that pygraphviz requires graphviz being installed on your system. Refer to [pygraphviz documentation](https://pygraphviz.github.io/documentation/stable/install.html) for detailed installation steps. +Note that we visualize DAG(directed acyclic graph) through python package ``graphviz``. And if you need it, make sure [Graphviz software](https://graphviz.org/) being installed on your system. Refer to [graphviz ยท PyPI](https://pypi.org/project/graphviz/#description) for installation guidance. ## GPU support To install PyQuafu with GPU-based circuit simulator, you need build from the source and make sure that [CUDA Toolkit](https://developer.nvidia.com/cuda-downloads) is installed. You can run From 8207b7cc9d03e1ae7757e90cca86bfc5daac3ef0 Mon Sep 17 00:00:00 2001 From: chensgit169 Date: Wed, 13 Sep 2023 10:33:15 +0800 Subject: [PATCH 09/12] correct graphviz version requirement Though the newest version is 0.20.1, 0.14.1 works fine already. --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index fa42d6d..442772b 100644 --- a/setup.py +++ b/setup.py @@ -28,7 +28,7 @@ "sparse>=0.13.0", "scikit-build>=0.16.1", "pybind11>=2.10.3", - "graphviz>=1.11", + "graphviz>=0.14.2", "ply~=3.11", "Pillow~=10.0.0" ] From a5162f0f9f186ac9d11e0d1825aa0e6e3cecf5e4 Mon Sep 17 00:00:00 2001 From: chensgit169 Date: Wed, 13 Sep 2023 10:55:53 +0800 Subject: [PATCH 10/12] update format and filename setting supports --- quafu/visualisation/draw_dag.py | 50 +++++++++++++-------------------- 1 file changed, 19 insertions(+), 31 deletions(-) diff --git a/quafu/visualisation/draw_dag.py b/quafu/visualisation/draw_dag.py index bda597f..8ea8b1e 100644 --- a/quafu/visualisation/draw_dag.py +++ b/quafu/visualisation/draw_dag.py @@ -4,6 +4,8 @@ from quafu.dagcircuits.circuit_dag import circuit_to_dag from quafu.dagcircuits.instruction_node import InstructionNode +from typing import Union, Any + def _extract_node_info(node): if isinstance(node, InstructionNode): @@ -14,45 +16,30 @@ def _extract_node_info(node): return name, label -def draw_dag(qc: QuantumCircuit): +def draw_dag(qc: Union[QuantumCircuit, None], + dag: Any = None, + output_format: str = 'pdf', + output_filename: str = 'DAG'): """ - # TODO: complete docstring, test supports for notebook, and format supports + TODO: complete docstring, test supports for notebook Helper function to visualize the DAG Args: - dep_g (DAG): DAG with Hashable Gates - output_format (str): output format, "png" or "svg" + qc (QuantumCircuit): pyquafu quantum circuit if provided + dag (DAG): DAG object with nodes and edges, built from qc if not provided + output_format (str): output format, including "png", "svg", "pdf"... + output_filename (str): file name of generated image Returns: - img (Image or SVG): show the image of DAG, which is Image(filename="dag.png") or SVG(filename="dag.svg") - - example: - .. jupyter-execute:: - ex1: - # directly draw PNG picture - draw_dag(dep_g, output_format="png") # save a png picture "dag.png" and show it in jupyter notebook - - # directly draw SVG picture - draw_dag(dep_g, output_format="svg") # save a svg picture "dag.svg" and show it in jupyter notebook - - ex2: - # generate PNG picture - img_png = draw_dag(dep_g, output_format="png") - - # generate SVG picture - img_svg = draw_dag(dep_g, output_format="svg") - - # show PNG picture - img_png - - # show SVG picture - img_svg - + dot: graphviz.Digraph object """ - dag = circuit_to_dag(qc) - dot = graphviz.Digraph() + if dag is None: + assert qc is not None + dag = circuit_to_dag(qc) + + dot = graphviz.Digraph(filename=output_filename) for node in dag.nodes: name, label = _extract_node_info(node) @@ -64,4 +51,5 @@ def draw_dag(qc: QuantumCircuit): name2, label2 = _extract_node_info(node2) dot.edge(name1, name2, label=link['label']) - dot.view(cleanup=True) + dot.render(format=output_format, cleanup=True) + return dot From 9da64fcb440133f82d23ef88e550e40ac9b2dc0c Mon Sep 17 00:00:00 2001 From: chensgit169 Date: Wed, 13 Sep 2023 11:26:21 +0800 Subject: [PATCH 11/12] update docstring --- quafu/visualisation/draw_dag.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/quafu/visualisation/draw_dag.py b/quafu/visualisation/draw_dag.py index 8ea8b1e..bf1e270 100644 --- a/quafu/visualisation/draw_dag.py +++ b/quafu/visualisation/draw_dag.py @@ -23,18 +23,17 @@ def draw_dag(qc: Union[QuantumCircuit, None], """ TODO: complete docstring, test supports for notebook - Helper function to visualize the DAG + Helper function to visualize the DAG - Args: - qc (QuantumCircuit): pyquafu quantum circuit if provided - dag (DAG): DAG object with nodes and edges, built from qc if not provided - output_format (str): output format, including "png", "svg", "pdf"... - output_filename (str): file name of generated image + Args: + qc (QuantumCircuit): pyquafu quantum circuit if provided + dag (DAG): DAG object with nodes and edges, built from qc if not provided + output_format (str): output format, including "png", "svg", "pdf"... + output_filename (str): file name of generated image - Returns: - dot: graphviz.Digraph object - - """ + Returns: + dot: graphviz.Digraph object + """ if dag is None: assert qc is not None dag = circuit_to_dag(qc) From f2f85f6bf11d633405e810648b43cdf3c0891ec6 Mon Sep 17 00:00:00 2001 From: chen_wei <109205133+chensgit169@users.noreply.github.com> Date: Wed, 13 Sep 2023 11:36:42 +0800 Subject: [PATCH 12/12] delete ``verbose`` in send() --- quafu/tasks/tasks.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/quafu/tasks/tasks.py b/quafu/tasks/tasks.py index 7b3e31e..47972e4 100644 --- a/quafu/tasks/tasks.py +++ b/quafu/tasks/tasks.py @@ -187,8 +187,7 @@ def send(self, qc: QuantumCircuit, name: str = "", group: str = "", - wait: bool = True, - verbose: bool = False, + wait: bool = True ) -> ExecResult: """ Run the circuit on experimental device. @@ -198,7 +197,6 @@ def send(self, name: Task name. group: The task belong which group. wait: Whether wait until the execution return. - verbose: Returns: ExecResult object that contain the dict return from quantum device. """