Skip to content

Commit c562df8

Browse files
committed
FIx: Create an internal path for custom prefixes in registers.
Prior implementations would replace the Register's prefix attribute inplace which is an unsafe operation. The following commits add a secure path for a provisional replacement of a register's prefix name to fix changed unsafe behavior from #13860.
1 parent abb0cf9 commit c562df8

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

crates/circuit/src/bit.rs

+19
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,25 @@ macro_rules! create_bit_object {
771771
})
772772
}
773773

774+
/// Allows for the creation of a new register with a provisional prefix and the
775+
/// same instance counter.
776+
#[pyo3(signature=(size=None, name=None, bits=None))]
777+
#[staticmethod]
778+
fn _new_with_prefix(
779+
py: Python,
780+
size: Option<isize>,
781+
name: Option<String>,
782+
bits: Option<Vec<$bit_struct>>,
783+
) -> PyResult<Py<Self>> {
784+
let name =
785+
format!(
786+
"{}{}",
787+
name.unwrap_or(Self::prefix().to_string()),
788+
$reg_struct::anonymous_instance_count().fetch_add(1, Ordering::Relaxed)
789+
);
790+
Py::new(py, Self::py_new(size, Some(name), bits)?)
791+
}
792+
774793
#[classattr]
775794
fn prefix() -> &'static str {
776795
$pyreg_prefix

qiskit/circuit/quantumcircuit.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3889,15 +3889,15 @@ def clear(self) -> None:
38893889
def _create_creg(self, length: int, name: str) -> ClassicalRegister:
38903890
"""Creates a creg, checking if ClassicalRegister with same name exists"""
38913891
if name in [creg.name for creg in self.cregs]:
3892-
new_creg = ClassicalRegister(length, name=f"{name}{ClassicalRegister.instance_count}")
3892+
new_creg = ClassicalRegister._new_with_prefix(length, name)
38933893
else:
38943894
new_creg = ClassicalRegister(length, name)
38953895
return new_creg
38963896

38973897
def _create_qreg(self, length: int, name: str) -> QuantumRegister:
38983898
"""Creates a qreg, checking if QuantumRegister with same name exists"""
38993899
if name in [qreg.name for qreg in self.qregs]:
3900-
new_qreg = QuantumRegister(length, name=f"{name}{QuantumRegister.instance_count}")
3900+
new_qreg = QuantumRegister._new_with_prefix(length, name)
39013901
else:
39023902
new_qreg = QuantumRegister(length, name)
39033903
return new_qreg

qiskit/transpiler/passes/layout/full_ancilla_allocation.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,8 @@ def run(self, dag):
9292

9393
if idle_physical_qubits:
9494
if self.ancilla_name in dag.qregs:
95-
qreg = QuantumRegister(
96-
len(idle_physical_qubits),
97-
name=f"{self.ancilla_name}{QuantumRegister.instances_count + 1}",
95+
qreg = QuantumRegister._new_with_prefix(
96+
len(idle_physical_qubits), self.ancilla_name
9897
)
9998
else:
10099
qreg = QuantumRegister(len(idle_physical_qubits), name=self.ancilla_name)

0 commit comments

Comments
 (0)