-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
circuit_to_dagdependency.py
51 lines (38 loc) · 1.7 KB
/
circuit_to_dagdependency.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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, create_preds_and_succs=True):
"""Build a ``DAGDependency`` object from a :class:`~.QuantumCircuit`.
Args:
circuit (QuantumCircuit): the input circuit.
create_preds_and_succs (bool): whether to construct lists of
predecessors and successors for every node.
Return:
DAGDependency: the DAG representing the input circuit as a dag dependency.
"""
dagdependency = DAGDependency()
dagdependency.name = circuit.name
dagdependency.metadata = circuit.metadata
dagdependency.add_qubits(circuit.qubits)
dagdependency.add_clbits(circuit.clbits)
for register in circuit.qregs:
dagdependency.add_qreg(register)
for register in circuit.cregs:
dagdependency.add_creg(register)
for instruction in circuit.data:
dagdependency.add_op_node(instruction.operation, instruction.qubits, instruction.clbits)
if create_preds_and_succs:
dagdependency._add_predecessors()
dagdependency._add_successors()
dagdependency._calibrations = circuit._calibrations_prop
return dagdependency