Skip to content

Commit 6877913

Browse files
authored
Refactor BitData to ObjectRegistry. (Qiskit#13985)
* s * Rename BitData to ObjectRegistry. * s * Update tests for new error string. This is only really a user-facing error message when working with DAGCircuit, since QuantumCircuit first checks if the bits being added to it are duplicates. And, in the case of DAGCircuit, the previous error message was already unfriendly: ValueError: Existing bit ShareableQubit(Owned { register: OwningRegisterInfo { name: "q16", size: 2, subclass: QUBIT }, index: 0 }) cannot be re-added in strict mode. * Fix comment in commutation checker. * Fix lint. * Address review comments.
1 parent 6f317f9 commit 6877913

File tree

6 files changed

+221
-192
lines changed

6 files changed

+221
-192
lines changed

crates/accelerate/src/commutation_checker.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use ndarray::Array2;
1616
use num_complex::Complex64;
1717
use num_complex::ComplexFloat;
1818
use once_cell::sync::Lazy;
19-
use qiskit_circuit::bit_data::VarAsKey;
19+
use qiskit_circuit::object_registry::PyObjectAsKey;
2020
use smallvec::SmallVec;
2121
use std::fmt::Debug;
2222

@@ -27,10 +27,10 @@ use pyo3::prelude::*;
2727
use pyo3::types::{PyBool, PyDict, PySequence, PyTuple};
2828
use pyo3::BoundObject;
2929

30-
use qiskit_circuit::bit_data::BitData;
3130
use qiskit_circuit::circuit_instruction::OperationFromPython;
3231
use qiskit_circuit::dag_node::DAGOpNode;
3332
use qiskit_circuit::imports::QI_OPERATOR;
33+
use qiskit_circuit::object_registry::ObjectRegistry;
3434
use qiskit_circuit::operations::OperationRef::{Gate as PyGateType, Operation as PyOperationType};
3535
use qiskit_circuit::operations::{
3636
get_standard_gate_names, Operation, OperationRef, Param, StandardGate,
@@ -90,20 +90,20 @@ where
9090
T: From<BitType> + Copy,
9191
BitType: From<T>,
9292
{
93-
// Using `VarAsKey` here is a total hack, but this is a short-term workaround before a
93+
// Using `PyObjectAsKey` here is a total hack, but this is a short-term workaround before a
9494
// larger refactor of the commutation checker.
95-
let mut bitdata: BitData<T, VarAsKey> = BitData::new();
95+
let mut registry: ObjectRegistry<T, PyObjectAsKey> = ObjectRegistry::new();
9696

9797
for bit in bits1.iter().chain(bits2.iter()) {
98-
bitdata.add(bit.into(), false)?;
98+
registry.add(bit.into(), false)?;
9999
}
100100

101101
Ok((
102-
bitdata
103-
.map_bits(bits1.iter().map(|bit| bit.into()))?
102+
registry
103+
.map_objects(bits1.iter().map(|bit| bit.into()))?
104104
.collect(),
105-
bitdata
106-
.map_bits(bits2.iter().map(|bit| bit.into()))?
105+
registry
106+
.map_objects(bits2.iter().map(|bit| bit.into()))?
107107
.collect(),
108108
))
109109
}

crates/circuit/src/circuit_data.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ use crate::bit::{
1919
BitLocations, ClassicalRegister, PyBit, QuantumRegister, Register, ShareableClbit,
2020
ShareableQubit,
2121
};
22-
use crate::bit_data::BitData;
2322
use crate::bit_locator::BitLocator;
2423
use crate::circuit_instruction::{CircuitInstruction, OperationFromPython};
2524
use crate::dag_circuit::add_global_phase;
2625
use crate::imports::{ANNOTATED_OPERATION, QUANTUM_CIRCUIT};
2726
use crate::interner::{Interned, Interner};
27+
use crate::object_registry::ObjectRegistry;
2828
use crate::operations::{Operation, OperationRef, Param, StandardGate};
2929
use crate::packed_instruction::{PackedInstruction, PackedOperation};
3030
use crate::parameter_table::{ParameterTable, ParameterTableError, ParameterUse, ParameterUuid};
@@ -116,9 +116,9 @@ pub struct CircuitData {
116116
/// The cache used to intern instruction bits.
117117
cargs_interner: Interner<[Clbit]>,
118118
/// Qubits registered in the circuit.
119-
qubits: BitData<Qubit, ShareableQubit>,
119+
qubits: ObjectRegistry<Qubit, ShareableQubit>,
120120
/// Clbits registered in the circuit.
121-
clbits: BitData<Clbit, ShareableClbit>,
121+
clbits: ObjectRegistry<Clbit, ShareableClbit>,
122122
/// QuantumRegisters stored in the circuit
123123
qregs: RegisterData<QuantumRegister>,
124124
/// ClassicalRegisters stored in the circuit
@@ -150,8 +150,8 @@ impl CircuitData {
150150
data: Vec::new(),
151151
qargs_interner: Interner::new(),
152152
cargs_interner: Interner::new(),
153-
qubits: BitData::new(),
154-
clbits: BitData::new(),
153+
qubits: ObjectRegistry::new(),
154+
clbits: ObjectRegistry::new(),
155155
param_table: ParameterTable::new(),
156156
global_phase: Param::Float(0.),
157157
qregs: RegisterData::new(),
@@ -182,8 +182,8 @@ impl CircuitData {
182182
let args = {
183183
let self_ = self_.borrow();
184184
(
185-
(!self_.qubits.is_empty()).then_some(self_.qubits.bits().clone()),
186-
(!self_.clbits.is_empty()).then_some(self_.clbits.bits().clone()),
185+
(!self_.qubits.is_empty()).then_some(self_.qubits.objects().clone()),
186+
(!self_.clbits.is_empty()).then_some(self_.clbits.objects().clone()),
187187
None::<()>,
188188
self_.data.len(),
189189
self_.global_phase.clone(),
@@ -247,7 +247,7 @@ impl CircuitData {
247247
self.add_qreg(register, true)?;
248248
}
249249

250-
for (index, qubit) in self.qubits.bits().iter().enumerate() {
250+
for (index, qubit) in self.qubits.objects().iter().enumerate() {
251251
if !self.qubit_indices.contains_key(qubit) {
252252
self.qubit_indices
253253
.insert(qubit.clone(), BitLocations::new(index as u32, []));
@@ -308,7 +308,7 @@ impl CircuitData {
308308
self.add_creg(register, true)?;
309309
}
310310

311-
for (index, clbit) in self.clbits.bits().iter().enumerate() {
311+
for (index, clbit) in self.clbits.objects().iter().enumerate() {
312312
if !self.clbit_indices.contains_key(clbit) {
313313
self.clbit_indices
314314
.insert(clbit.clone(), BitLocations::new(index as u32, []));
@@ -533,8 +533,8 @@ impl CircuitData {
533533
pub fn copy_empty_like(&self, py: Python<'_>) -> PyResult<Self> {
534534
let mut res = CircuitData::new(
535535
py,
536-
Some(self.qubits.bits().clone()),
537-
Some(self.clbits.bits().clone()),
536+
Some(self.qubits.objects().clone()),
537+
Some(self.clbits.objects().clone()),
538538
None,
539539
0,
540540
self.global_phase.clone(),
@@ -1159,7 +1159,7 @@ impl CircuitData {
11591159
) -> PyResult<Vec<ShareableQubit>> {
11601160
bit_argument_conversion(
11611161
&qubit_representation,
1162-
self.qubits.bits(),
1162+
self.qubits.objects(),
11631163
&self.qubit_indices,
11641164
)
11651165
}
@@ -1178,7 +1178,7 @@ impl CircuitData {
11781178
) -> PyResult<Vec<ShareableClbit>> {
11791179
bit_argument_conversion(
11801180
&clbit_representation,
1181-
self.clbits.bits(),
1181+
self.clbits.objects(),
11821182
&self.clbit_indices,
11831183
)
11841184
}
@@ -1294,8 +1294,8 @@ impl CircuitData {
12941294
#[allow(clippy::too_many_arguments)]
12951295
pub fn from_packed_instructions<I>(
12961296
py: Python,
1297-
qubits: BitData<Qubit, ShareableQubit>,
1298-
clbits: BitData<Clbit, ShareableClbit>,
1297+
qubits: ObjectRegistry<Qubit, ShareableQubit>,
1298+
clbits: ObjectRegistry<Clbit, ShareableClbit>,
12991299
qargs_interner: Interner<[Qubit]>,
13001300
cargs_interner: Interner<[Clbit]>,
13011301
qregs: RegisterData<QuantumRegister>,
@@ -1399,8 +1399,8 @@ impl CircuitData {
13991399
data: Vec::with_capacity(instruction_capacity),
14001400
qargs_interner: Interner::new(),
14011401
cargs_interner: Interner::new(),
1402-
qubits: BitData::with_capacity(num_qubits as usize),
1403-
clbits: BitData::with_capacity(num_clbits as usize),
1402+
qubits: ObjectRegistry::with_capacity(num_qubits as usize),
1403+
clbits: ObjectRegistry::with_capacity(num_clbits as usize),
14041404
param_table: ParameterTable::new(),
14051405
global_phase: Param::Float(0.0),
14061406
qregs: RegisterData::new(),
@@ -1553,12 +1553,12 @@ impl CircuitData {
15531553
fn pack(&mut self, py: Python, inst: &CircuitInstruction) -> PyResult<PackedInstruction> {
15541554
let qubits = self.qargs_interner.insert_owned(
15551555
self.qubits
1556-
.map_bits(inst.qubits.extract::<Vec<ShareableQubit>>(py)?.into_iter())?
1556+
.map_objects(inst.qubits.extract::<Vec<ShareableQubit>>(py)?.into_iter())?
15571557
.collect(),
15581558
);
15591559
let clbits = self.cargs_interner.insert_owned(
15601560
self.clbits
1561-
.map_bits(inst.clbits.extract::<Vec<ShareableClbit>>(py)?.into_iter())?
1561+
.map_objects(inst.clbits.extract::<Vec<ShareableClbit>>(py)?.into_iter())?
15621562
.collect(),
15631563
);
15641564
Ok(PackedInstruction {
@@ -1637,12 +1637,12 @@ impl CircuitData {
16371637
}
16381638

16391639
/// Returns an immutable view of the Qubits registered in the circuit
1640-
pub fn qubits(&self) -> &BitData<Qubit, ShareableQubit> {
1640+
pub fn qubits(&self) -> &ObjectRegistry<Qubit, ShareableQubit> {
16411641
&self.qubits
16421642
}
16431643

16441644
/// Returns an immutable view of the Classical bits registered in the circuit
1645-
pub fn clbits(&self) -> &BitData<Clbit, ShareableClbit> {
1645+
pub fn clbits(&self) -> &ObjectRegistry<Clbit, ShareableClbit> {
16461646
&self.clbits
16471647
}
16481648

0 commit comments

Comments
 (0)