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

Fix issue with deepcopy and builder API. #11669

Merged
merged 3 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -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
Loading