Skip to content

Commit

Permalink
Add option to skip deepcopy on circuit_to_dag (Qiskit#9848)
Browse files Browse the repository at this point in the history
Just as was done for Qiskit#9825 for dag_to_circuit(), this commit adds a new
option, copy_operations, which allows to disable the deepcopy on
circuit_to_dag(). Previously, the circuit to dag converter would always
deep copy every operation, however we don't need this extra overhead
in every case; where the input QuantumCircuit is discarded after
conversion. This new flag lets us avoid the copy overhead in these
situations, however in general the converter still needs to default to
copying.
  • Loading branch information
mtreinish authored and ElePT committed Apr 5, 2023
1 parent d1367d4 commit 37286c1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
4 changes: 3 additions & 1 deletion qiskit/circuit/quantumcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,9 @@ def __eq__(self, other) -> bool:
# TODO: remove the DAG from this function
from qiskit.converters import circuit_to_dag

return circuit_to_dag(self) == circuit_to_dag(other)
return circuit_to_dag(self, copy_operations=False) == circuit_to_dag(
other, copy_operations=False
)

@classmethod
def _increment_instances(cls):
Expand Down
16 changes: 12 additions & 4 deletions qiskit/converters/circuit_to_dag.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@
from qiskit.dagcircuit.dagcircuit import DAGCircuit


def circuit_to_dag(circuit):
def circuit_to_dag(circuit, copy_operations=True):
"""Build a ``DAGCircuit`` object from a ``QuantumCircuit``.
Args:
circuit (QuantumCircuit): the input circuit.
copy_operations (bool): Deep copy the operation objects
in the :class:`~.QuantumCircuit` for the output :class:`~.DAGCircuit`.
This should only be set to ``False`` if the input :class:`~.QuantumCircuit`
will not be used anymore as the operations in the output
:class:`~.DAGCircuit` will be shared instances and modifications to
operations in the :class:`~.DAGCircuit` will be reflected in the
:class:`~.QuantumCircuit` (and vice versa).
Return:
DAGCircuit: the DAG representing the input circuit.
Expand Down Expand Up @@ -57,9 +64,10 @@ def circuit_to_dag(circuit):
dagcircuit.add_creg(register)

for instruction in circuit.data:
dagcircuit.apply_operation_back(
copy.deepcopy(instruction.operation), instruction.qubits, instruction.clbits
)
op = instruction.operation
if copy_operations:
op = copy.deepcopy(op)
dagcircuit.apply_operation_back(op, instruction.qubits, instruction.clbits)

dagcircuit.duration = circuit.duration
dagcircuit.unit = circuit.unit
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
features:
- |
Added a new option, ``copy_operations``, to :func:`~.circuit_to_dag` to
enable optionally disabling deep copying the operations from the input
:class`~.QuantumCircuit` to the output :class:`~.QuantumCircuit`. In cases
where the input :class`~.QuantumCircuit` is not used anymore after
conversion this deep copying is unnecessary overhead as any shared
references wouldn't have any potential unwanted side effects if the input
:class`~.QuantumCircuit` is discarded.

0 comments on commit 37286c1

Please sign in to comment.