Skip to content

Commit

Permalink
Fix issue with deepcopy and scope API.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinhartman committed Jan 29, 2024
1 parent b5aaffb commit 3b2c239
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion qiskit/circuit/quantumcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,9 +562,11 @@ def __deepcopy__(self, memo=None):
# copy.deepcopy(memo).
cls = self.__class__
result = cls.__new__(cls)
for k in self.__dict__.keys() - {"_data"}:
for k in self.__dict__.keys() - {"_data", "_builder_api"}:
setattr(result, k, copy.deepcopy(self.__dict__[k], memo))

result._builder_api = _OuterCircuitScopeInterface(result)

# Avoids pulling self._data into a Python list
# like we would when pickling.
result._data = self._data.copy()
Expand Down
15 changes: 15 additions & 0 deletions test/python/circuit/test_circuit_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@


"""Test Qiskit's QuantumCircuit class."""
import copy

import numpy as np
from ddt import data, ddt
Expand Down Expand Up @@ -638,6 +639,20 @@ def test_measure_all(self):

self.assertEqual(expected, circuit)

def test_measure_all_after_deepcopy(self):
"""
Test measure_all on a circuit that has been deep-copied.
"""
qc = QuantumCircuit(2)
qc.h(1)

qc2 = copy.deepcopy(qc)

qc.measure_all()
qc2.measure_all()

self.assertEqual(qc, qc2)

def test_measure_all_not_add_bits_equal(self):
"""Test measure_all applies measurements to all qubits.
Does not create a new ClassicalRegister if the existing one is big enough.
Expand Down

0 comments on commit 3b2c239

Please sign in to comment.