Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DAGDependency #3581

Merged
merged 35 commits into from
May 24, 2020
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
53a8adf
Add DAGcanonical form and the converter circuit to canonical
Dec 3, 2019
cb3d3fc
Updated version of dag canonical and its converters, also include a …
Dec 9, 2019
27fc7b9
Updated dag_visualization.py to include DAG canonical and updated docs
Dec 9, 2019
d9267c0
Release notes and raise error for type in dag_visualization
Dec 9, 2019
cfcd380
Update with linting
Dec 9, 2019
16e3bca
Linting
Dec 9, 2019
a18d020
Linting
Dec 9, 2019
b3eaef2
Merge branch 'master' into canonical
rmoyard Dec 9, 2019
cad0c1e
Update release note
rmoyard Dec 10, 2019
eb4d616
Merge
rmoyard Dec 10, 2019
0fce65d
More linting for dagcanonical and dag_visualization
rmoyard Dec 10, 2019
3cd2463
Linting for dagcanonical
rmoyard Dec 10, 2019
ec1240d
Less returns in the commute function
rmoyard Dec 10, 2019
e21e6af
Update due to cyclic importation
rmoyard Dec 10, 2019
1c44fd2
Update with two converters from DAGcircuit() to DAGcanonical() and DA…
rmoyard Jan 27, 2020
bc9c22c
Merge branch 'master' into canonical
rmoyard Jan 31, 2020
56108c1
Merge branch 'master' into canonical
rmoyard Apr 18, 2020
c233913
Adaptation to retworkx, change of name (dagdependency) and add tests
rmoyard Apr 30, 2020
2eeb76f
Delete wrong file
rmoyard Apr 30, 2020
345dafb
Correction test
rmoyard Apr 30, 2020
a2adf20
Linting
rmoyard Apr 30, 2020
630487c
Correction method Add_successors()
rmoyard May 4, 2020
dc859be
some leftover typos and doc cleanup
ajavadia May 18, 2020
35890d0
doc
ajavadia May 18, 2020
ef39cef
snapshot should be like barrier
ajavadia May 18, 2020
a4cd90a
draw method update
ajavadia May 18, 2020
5b055fc
tweak
ajavadia May 18, 2020
4b9485d
Add a DagDepNode, change _update_edge and _add_successors.
rmoyard May 20, 2020
9a6ad24
Add qindices and cindices attributes to the DAGRepNode
rmoyard May 22, 2020
85f82f1
Merge remote-tracking branch 'origin/master' into canonical
rmoyard May 22, 2020
540ef9b
Merge branch 'master' into canonical
mergify[bot] May 23, 2020
7ddd5d1
Merge branch 'master' into canonical
mergify[bot] May 23, 2020
6c40cf4
Add qindices and cindices attributes initialization in _add_op_node f…
rmoyard May 23, 2020
deea53a
Merge branch 'canonical' of https://github.com/rmoyard/qiskit-terra i…
rmoyard May 23, 2020
afc0331
Update __init__ for DAGDepNode
rmoyard May 23, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions qiskit/converters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,21 @@
circuit_to_instruction
circuit_to_gate
ast_to_dag
dagdependency_to_circuit
circuit_to_dagdependency
dag_to_dagdependency
dagdependency_to_dag
"""

from .circuit_to_dag import circuit_to_dag
from .dag_to_circuit import dag_to_circuit
from .circuit_to_instruction import circuit_to_instruction
from .circuit_to_gate import circuit_to_gate
from .ast_to_dag import ast_to_dag
from .circuit_to_dagdependency import circuit_to_dagdependency
from .dagdependency_to_circuit import dagdependency_to_circuit
from .dag_to_dagdependency import dag_to_dagdependency
from .dagdependency_to_dag import dagdependency_to_dag

ajavadia marked this conversation as resolved.
Show resolved Hide resolved

def isinstanceint(obj):
Expand Down
44 changes: 44 additions & 0 deletions qiskit/converters/circuit_to_dagdependency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-

# This code is part of Qiskit.
#
# (C) Copyright IBM 2020.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

'''Helper function for converting a circuit to a dag dependency'''

from qiskit.dagcircuit.dagdependency import DAGDependency


def circuit_to_dagdependency(circuit):
"""Build a ``DAGDependency`` object from a ``QuantumCircuit``.

Args:
circuit (QuantumCircuit): the input circuits.

Return:
DAGDependency: the DAG representing the input circuit as a dag dependency.
"""
dagdependency = DAGDependency()
dagdependency.name = circuit.name

for register in circuit.qregs:
dagdependency.add_qreg(register)

for register in circuit.cregs:
dagdependency.add_creg(register)

for operation, qargs, cargs in circuit.data:
dagdependency.add_op_node(operation, qargs, cargs)
dagdependency.add_edge()

dagdependency.add_successors()

return dagdependency
50 changes: 50 additions & 0 deletions qiskit/converters/dag_to_dagdependency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# -*- coding: utf-8 -*-

# This code is part of Qiskit.
#
# (C) Copyright IBM 2020.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""Helper function for converting a dag circuit to a dag dependency"""
from qiskit.dagcircuit.dagdependency import DAGDependency


def dag_to_dagdependency(dag):
"""Build a ``DAGDependency`` object from a ``DAGCircuit``.

Args:
dag (DAGCircuit): the input dag.

Return:
DAGDependency: the DAG representing the input circuit as a dag dependency.
"""

dagdependency = DAGDependency()
DAGDependency.name = dag.name

qregs = list(dag.qregs.values())
cregs = list(dag.cregs.values())

for register in qregs:
dagdependency.add_qreg(register)

for register in cregs:
dagdependency.add_creg(register)

for node in dag.topological_op_nodes():
# Get arguments for classical control (if any)
inst = node.op.copy()
inst.condition = node.condition
dagdependency.add_op_node(inst, node.qargs, node.cargs)
dagdependency.add_edge()

dagdependency.add_successors()

return dagdependency
40 changes: 40 additions & 0 deletions qiskit/converters/dagdependency_to_circuit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# -*- coding: utf-8 -*-

# This code is part of Qiskit.
#
# (C) Copyright IBM 2020.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""Helper function for converting a dag dependency to a circuit"""
from qiskit.circuit import QuantumCircuit


def dagdependency_to_circuit(dagdependency):
"""Build a ``QuantumCircuit`` object from a ``DAGDependency``.

Args:
dagdependency (DAGDependency): the input dag.

Return:
QuantumCircuit: the circuit representing the input dag dependency.
"""

name = dagdependency.name or None
circuit = QuantumCircuit(*dagdependency.qregs.values(), *dagdependency.cregs.values(),
name=name)

for node in dagdependency.get_nodes():
node_op = node['operation']
# Get arguments for classical control (if any)
inst = node_op.op.copy()
inst.condition = node_op.condition
circuit._append(inst, node_op.qargs, node_op.cargs)

return circuit
49 changes: 49 additions & 0 deletions qiskit/converters/dagdependency_to_dag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# -*- coding: utf-8 -*-

# This code is part of Qiskit.
#
# (C) Copyright IBM 2020.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""Helper function for converting a dag dependency to a dag circuit"""
from qiskit.dagcircuit.dagcircuit import DAGCircuit


def dagdependency_to_dag(dagdependency):
"""Build a ``DAGCircuit`` object from a ``DAGDependency``.

Args:
dag dependency (DAGDependency): the input dag.

Return:
DAGCircuit: the DAG representing the input circuit.
"""

dagcircuit = DAGCircuit()
dagcircuit.name = dagdependency.name

qregs = list(dagdependency.qregs.values())
cregs = list(dagdependency.cregs.values())

for register in qregs:
dagcircuit.add_qreg(register)

for register in cregs:
dagcircuit.add_creg(register)

for node in dagdependency.get_nodes():
node_op = node['operation']
# Get arguments for classical control (if any)
inst = node_op.op.copy()
inst.condition = node_op.condition

dagcircuit.apply_operation_back(inst, node_op.qargs, node_op.cargs, inst.condition)

return dagcircuit
2 changes: 2 additions & 0 deletions qiskit/dagcircuit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

DAGCircuit
DAGNode
DAGDependency

Exceptions
==========
Expand All @@ -39,3 +40,4 @@
from .dagcircuit import DAGCircuit
from .dagnode import DAGNode
from .exceptions import DAGCircuitError
from .dagdependency import DAGDependency
Loading