Skip to content

Commit

Permalink
Fix: Remove interner copying from copy_empty_like()
Browse files Browse the repository at this point in the history
- Revert changes to `QuantumCircuit.copy()`
  • Loading branch information
raynelfss committed Feb 10, 2025
1 parent 30423ff commit dc555c8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
17 changes: 9 additions & 8 deletions crates/circuit/src/circuit_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,12 @@ impl CircuitData {
/// CircuitData: The shallow copy.
#[pyo3(signature = (copy_instructions=true, deepcopy=false))]
pub fn copy(&self, py: Python<'_>, copy_instructions: bool, deepcopy: bool) -> PyResult<Self> {
let mut res = self.copy_empty_like(py, Some(self.data().len()))?;
let mut res = self.copy_empty_like(py)?;
res.qargs_interner = self.qargs_interner.clone();
res.cargs_interner = self.cargs_interner.clone();
res.reserve(py, self.data().len());
res.param_table.clone_from(&self.param_table);

if deepcopy {
let memo = PyDict::new(py);
for inst in &self.data {
Expand Down Expand Up @@ -317,22 +321,19 @@ impl CircuitData {
Ok(res)
}

/// Performs an empty-like shallow copy.
/// Performs a copy with no instruction.
///
/// Returns:
/// CircuitData: The shallow copy.
#[pyo3(signature = (reserve = None,))]
pub fn copy_empty_like(&self, py: Python<'_>, reserve: Option<usize>) -> PyResult<Self> {
let mut res = CircuitData::new(
pub fn copy_empty_like(&self, py: Python<'_>) -> PyResult<Self> {
let res = CircuitData::new(
py,
Some(self.qubits.cached().bind(py)),
Some(self.clbits.cached().bind(py)),
None,
reserve.unwrap_or_default(),
0,
self.global_phase.clone(),
)?;
res.qargs_interner = self.qargs_interner.clone();
res.cargs_interner = self.cargs_interner.clone();

Ok(res)
}
Expand Down
9 changes: 1 addition & 8 deletions qiskit/circuit/quantumcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3698,14 +3698,7 @@ def copy(self, name: str | None = None) -> typing.Self:
Returns:
QuantumCircuit: a deepcopy of the current circuit, with the specified name
"""
if not (name is None or isinstance(name, str)):
raise TypeError(
f"invalid name for a circuit: '{name}'. The name must be a string or 'None'."
)
cpy = _copy.copy(self)

_copy_metadata(self, cpy, "alike")

cpy = self.copy_empty_like(name)
cpy._data = self._data.copy()
return cpy

Expand Down

0 comments on commit dc555c8

Please sign in to comment.