Skip to content

Commit

Permalink
Move Option<Box<_>> into ExtraInstructionAttributes. (#13127)
Browse files Browse the repository at this point in the history
* Move Option<Box<_>> into ExtraInstructionAttributes.

Previously, CircuitInstruction and PackedInstruction held
the ExtraInstructionAttributes struct within an Option<Box<_>>.
By putting the Option<Box<_>> inside ExtraInstructionAttributes,
we can use the struct itself to manage its memory and provide
access to the attributes behind a nicer interface.

The size of ExtraInstructionAttributes should be the same size
as the old Option<Box<ExtraInstructionAttributes>>, so this
should not have memory implications.

* Address review comments.

- Use tuple struct.
- Use 'Attributes' over 'Attrs'.
- Add doc comment for internal 'ExtraAttributes' struct.
- Add doc comments for methods.
- Add setters for unit and duration.

* Fix performance regression from unnecessary dict creation.
  • Loading branch information
kevinhartman authored Sep 12, 2024
1 parent 4c88a71 commit c655acb
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 221 deletions.
4 changes: 2 additions & 2 deletions crates/accelerate/src/commutation_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ pub(crate) fn analyze_commutations_inner(
py,
&op1,
params1,
packed_inst0.extra_attrs.as_deref(),
&packed_inst0.extra_attrs,
qargs1,
cargs1,
&op2,
params2,
packed_inst1.extra_attrs.as_deref(),
&packed_inst1.extra_attrs,
qargs2,
cargs2,
MAX_NUM_QUBITS,
Expand Down
20 changes: 10 additions & 10 deletions crates/accelerate/src/commutation_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ impl CommutationChecker {
py,
&op1.instruction.operation.view(),
&op1.instruction.params,
op1.instruction.extra_attrs.as_deref(),
&op1.instruction.extra_attrs,
&qargs1,
&cargs1,
&op2.instruction.operation.view(),
&op2.instruction.params,
op2.instruction.extra_attrs.as_deref(),
&op2.instruction.extra_attrs,
&qargs2,
&cargs2,
max_num_qubits,
Expand Down Expand Up @@ -162,12 +162,12 @@ impl CommutationChecker {
py,
&op1.operation.view(),
&op1.params,
op1.extra_attrs.as_deref(),
&op1.extra_attrs,
&qargs1,
&cargs1,
&op2.operation.view(),
&op2.params,
op2.extra_attrs.as_deref(),
&op2.extra_attrs,
&qargs2,
&cargs2,
max_num_qubits,
Expand Down Expand Up @@ -232,12 +232,12 @@ impl CommutationChecker {
py: Python,
op1: &OperationRef,
params1: &[Param],
attrs1: Option<&ExtraInstructionAttributes>,
attrs1: &ExtraInstructionAttributes,
qargs1: &[Qubit],
cargs1: &[Clbit],
op2: &OperationRef,
params2: &[Param],
attrs2: Option<&ExtraInstructionAttributes>,
attrs2: &ExtraInstructionAttributes,
qargs2: &[Qubit],
cargs2: &[Clbit],
max_num_qubits: u32,
Expand Down Expand Up @@ -494,20 +494,20 @@ impl CommutationChecker {
fn commutation_precheck(
op1: &OperationRef,
params1: &[Param],
attrs1: Option<&ExtraInstructionAttributes>,
attrs1: &ExtraInstructionAttributes,
qargs1: &[Qubit],
cargs1: &[Clbit],
op2: &OperationRef,
params2: &[Param],
attrs2: Option<&ExtraInstructionAttributes>,
attrs2: &ExtraInstructionAttributes,
qargs2: &[Qubit],
cargs2: &[Clbit],
max_num_qubits: u32,
) -> Option<bool> {
if op1.control_flow()
|| op2.control_flow()
|| attrs1.is_some_and(|attr| attr.condition.is_some())
|| attrs2.is_some_and(|attr| attr.condition.is_some())
|| attrs1.condition().is_some()
|| attrs2.condition().is_some()
{
return Some(false);
}
Expand Down
17 changes: 7 additions & 10 deletions crates/circuit/src/circuit_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
use std::cell::OnceCell;

use crate::bit_data::BitData;
use crate::circuit_instruction::{CircuitInstruction, OperationFromPython};
use crate::circuit_instruction::{
CircuitInstruction, ExtraInstructionAttributes, OperationFromPython,
};
use crate::imports::{ANNOTATED_OPERATION, CLBIT, QUANTUM_CIRCUIT, QUBIT};
use crate::interner::{Interned, Interner};
use crate::operations::{Operation, OperationRef, Param, StandardGate};
Expand Down Expand Up @@ -157,7 +159,7 @@ impl CircuitData {
qubits,
clbits,
params,
extra_attrs: None,
extra_attrs: ExtraInstructionAttributes::default(),
#[cfg(feature = "cache_pygates")]
py_op: OnceCell::new(),
});
Expand Down Expand Up @@ -266,7 +268,7 @@ impl CircuitData {
qubits,
clbits: no_clbit_index,
params,
extra_attrs: None,
extra_attrs: ExtraInstructionAttributes::default(),
#[cfg(feature = "cache_pygates")]
py_op: OnceCell::new(),
});
Expand Down Expand Up @@ -324,7 +326,7 @@ impl CircuitData {
qubits,
clbits: no_clbit_index,
params,
extra_attrs: None,
extra_attrs: ExtraInstructionAttributes::default(),
#[cfg(feature = "cache_pygates")]
py_op: OnceCell::new(),
});
Expand Down Expand Up @@ -683,12 +685,7 @@ impl CircuitData {
#[pyo3(signature = (func))]
pub fn map_nonstandard_ops(&mut self, py: Python<'_>, func: &Bound<PyAny>) -> PyResult<()> {
for inst in self.data.iter_mut() {
if inst.op.try_standard_gate().is_some()
&& !inst
.extra_attrs
.as_ref()
.is_some_and(|attrs| attrs.condition.is_some())
{
if inst.op.try_standard_gate().is_some() && inst.extra_attrs.condition().is_none() {
continue;
}
let py_op = func.call1((inst.unpack_py_op(py)?,))?;
Expand Down
Loading

0 comments on commit c655acb

Please sign in to comment.