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 option to skip deepcopy on circuit_to_dag #9848

Merged
merged 1 commit into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion qiskit/circuit/quantumcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,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.