Skip to content

Commit

Permalink
Expose CircuitData interners and registers to the qiskit-circuit
Browse files Browse the repository at this point in the history
…crate (Qiskit#13006)

* Initial: Expose `CircuitData` interners and registers to the `qiskit-circuit` crate.
- Make the `CircuitData` iter method be an exact-size iterator.

* FIx: Expose immutable views of interners, registers and global phase.
- Revert the changes making the interners and registers visible to the crate `qiskit-circuit`.
- Create methods to expose immutable borrowed views of the interners, registers and global_phase to prevent from mutating the DAGCircuit.
- Add `get_qargs` and `get_cargs` to unpack interned qargs ans cargs.
- Other tweaks and fixes.

* Format: Fix typo in comment

Co-authored-by: Eli Arbel <[email protected]>

* Refactor: Use naming convention for getters.

* Docs: Apply suggestions from code review

- Correct incorrect docstrings for `qubits()` and `clbits()`

Co-authored-by: Eli Arbel <[email protected]>

---------

Co-authored-by: Eli Arbel <[email protected]>
  • Loading branch information
raynelfss and eliarbel authored Aug 26, 2024
1 parent f537602 commit cc87318
Showing 1 changed file with 40 additions and 5 deletions.
45 changes: 40 additions & 5 deletions crates/circuit/src/circuit_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::cell::OnceCell;
use crate::bit_data::BitData;
use crate::circuit_instruction::{CircuitInstruction, OperationFromPython};
use crate::imports::{ANNOTATED_OPERATION, CLBIT, QUANTUM_CIRCUIT, QUBIT};
use crate::interner::{IndexedInterner, Interner};
use crate::interner::{Index, IndexedInterner, Interner};
use crate::operations::{Operation, OperationRef, Param, StandardGate};
use crate::packed_instruction::{PackedInstruction, PackedOperation};
use crate::parameter_table::{ParameterTable, ParameterTableError, ParameterUse, ParameterUuid};
Expand Down Expand Up @@ -399,8 +399,8 @@ impl CircuitData {
///
/// Returns:
/// list(:class:`.Qubit`): The current sequence of registered qubits.
#[getter]
pub fn qubits(&self, py: Python<'_>) -> Py<PyList> {
#[getter("qubits")]
pub fn py_qubits(&self, py: Python<'_>) -> Py<PyList> {
self.qubits.cached().clone_ref(py)
}

Expand All @@ -424,8 +424,8 @@ impl CircuitData {
///
/// Returns:
/// list(:class:`.Clbit`): The current sequence of registered clbits.
#[getter]
pub fn clbits(&self, py: Python<'_>) -> Py<PyList> {
#[getter("clbits")]
pub fn py_clbits(&self, py: Python<'_>) -> Py<PyList> {
self.clbits.cached().clone_ref(py)
}

Expand Down Expand Up @@ -1137,6 +1137,41 @@ impl CircuitData {
self.data.iter()
}

/// Returns an immutable view of the Interner used for Qargs
pub fn qargs_interner(&self) -> &IndexedInterner<Vec<Qubit>> {
&self.qargs_interner
}

/// Returns an immutable view of the Interner used for Cargs
pub fn cargs_interner(&self) -> &IndexedInterner<Vec<Clbit>> {
&self.cargs_interner
}

/// Returns an immutable view of the Global Phase `Param` of the circuit
pub fn global_phase(&self) -> &Param {
&self.global_phase
}

/// Returns an immutable view of the Qubits registered in the circuit
pub fn qubits(&self) -> &BitData<Qubit> {
&self.qubits
}

/// Returns an immutable view of the Classical bits registered in the circuit
pub fn clbits(&self) -> &BitData<Clbit> {
&self.clbits
}

/// Unpacks from InternerIndex to `[Qubit]`
pub fn get_qargs(&self, index: Index) -> &[Qubit] {
self.qargs_interner().intern(index)
}

/// Unpacks from InternerIndex to `[Clbit]`
pub fn get_cargs(&self, index: Index) -> &[Clbit] {
self.cargs_interner().intern(index)
}

fn assign_parameters_inner<I>(&mut self, py: Python, iter: I) -> PyResult<()>
where
I: IntoIterator<Item = (Py<PyAny>, Param, HashSet<ParameterUse>)>,
Expand Down

0 comments on commit cc87318

Please sign in to comment.