Skip to content

Commit 6c7ee8f

Browse files
mtreinishElePTjakelishman
authored
Remove condition/c_if, duration, and unit from instructions (#13506)
* Remove condition/c_if, duration, and unit from instructions This commit removes the per instruction attributes for condition, duration, and unit. These attributes were deprecated in 1.3.0. Besides the label these attributes were the only mutable state for singleton instructions and removing them simplifies what needs to be tracked for instructions in both Python and rust. The associated methods and classes that were previously dependent on these attributes are also removed as they no longer serve a purpose. The removal of condition in particular removes a lot of logic from Qiskit especially from the transpiler because it was a something that needed to be checked outside of the normal data model for every operation. This PR simplifies the representation of this extra mutable state in Rust by removing the `ExtraInstructionAttributes` struct and replacing it with a `Option<Box<String>>`. The `Option<Box<String>>` is used instead of the simpler `Option<String>` because this this reduces the size of the label field from 24 bytes for `Option<String>` to 8 bytes for `Option<Box<String>>` with an extra layer of pointer indirection and a second heap allocation. This will have runtime overhead when labels are set, but because the vast majority of operations don't set labels the tradeoff for optimizing for the None case makes more sense. Another option would have been to use `Box<str>` here which is the equivalent of `Box<[u8]>` (where `String` is the equivalent to `Vec<u8>`) and from a runtime memory tradeoff would be a better choice for this application if labels were commonly used as labels aren't growable. But that requires 16 bytes instead of 8 and we'd be wasting an additional 8 bytes for each instruction in the circuit which isn't worth the cost. * Update random_circuit * Update more tests * Fix new scheduling pass implementation * Fix clippy::redundant_closure * Add release note for new args * Remove unused ExtraAttributes struct * Remove unused propagate_condition argument * Update legacy scheduling pass * Remove AlignMeasures which was deprecated and can not work without per gate duration * Fix lint * Remove deleted pass from init modules * Update qasm2 import to use IfElse instead of c_if * Use if_test instead of c_if for backwards compat qpy tests * Update more tests * More test updates * Fix handling of lack of dt * More test upgrades * Update more tests * Fix DAGCircuit::compose() panic * Fix more tests * Fix lint * Fix clippy * Fix MARS pass tests * Fix timeline drawer core * Update last c_if usage in tests * Adjust qpy tests to use c_if in old versions * Fix typo in qpy backwards compat test changes * Fix register capture test * Fix the worst test to debug in the world * Update release notes * Fix qpy hard failures The tests fail because of a circuit equality issue, form experience it's differing bit lists in the if state blocks. * Fix rust tests * Fix rust tests * Fix scheduled circuit tests * Fix lint * Remove visual tests that are no longer valid * More test fixes * Time unit conversion only applies to delay now With the removal of per instruction durations the time unit conversion pass only should work with delays, because all the instructions have fixed unit durations in the target (seconds). * Fix lint * Update reference images with removed c_if * Try to fix equality failure in qpy tests * Fix small oversight in qpy tests * More qpy test tweaks * Add missing arg to qpy tests * Remove c_if references in docs * Remove unused imports * Update docs * Add target to legacy dd pass docs * Restore propagate_condition arg for compatibility * More docs updates * Add back test lift legacy condition * Remove unnecessary controlflow builder method * Code simplification * Add warning to qpy load about condition in payload becoming if_else * Remove unused import * Apply suggestions from code review Co-authored-by: Elena Peña Tapia <[email protected]> * Handle delay duration in legacy scheduler * Document no control-flow basic simulator * Apply suggestions from code review Co-authored-by: Elena Peña Tapia <[email protected]> * Fix errant commas * Remove dead condition check * Clarify reason for exception suppression * Simplify time-unit conversions * Restore overly eager test removal from test_circuit_operations * Restore dag dep drawer test * Restore text drawer tests * Restore removed tests in test_circuit_drawer * Control fold in restored test for variable terminal width in CI * Remove bugged tests in backend v1 primitives implementation Two primitives tests were returning incorrect results after being rewritten to use IfElseOp instead of c_if. This is likely pointing to a bug in the primitive implementation or an aer bug. But since those classes are deprecated and the tests will be removed as part of the larger backend v1 primitive implementation removal in #13877 anyway this commit opts to just delete these tests now. --------- Co-authored-by: Elena Peña Tapia <[email protected]> Co-authored-by: Jake Lishman <[email protected]>
1 parent 67c7406 commit 6c7ee8f

File tree

212 files changed

+1746
-10800
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

212 files changed

+1746
-10800
lines changed

crates/accelerate/src/barrier_before_final_measurement.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use pyo3::prelude::*;
1414
use rayon::prelude::*;
1515
use rustworkx_core::petgraph::stable_graph::NodeIndex;
1616

17-
use qiskit_circuit::circuit_instruction::ExtraInstructionAttributes;
1817
use qiskit_circuit::dag_circuit::{DAGCircuit, NodeType};
1918
use qiskit_circuit::operations::{OperationRef, StandardInstruction};
2019
use qiskit_circuit::packed_instruction::{PackedInstruction, PackedOperation};
@@ -150,7 +149,7 @@ pub fn barrier_before_final_measurements(
150149
qargs.as_slice(),
151150
&[],
152151
None,
153-
ExtraInstructionAttributes::new(label, None, None, None),
152+
label,
154153
#[cfg(feature = "cache_pygates")]
155154
None,
156155
)?;

crates/accelerate/src/basis/basis_translator/compose_transforms.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub(super) fn compose_transforms<'a>(
7575
} else {
7676
Some(gate_obj.params)
7777
},
78-
gate_obj.extra_attrs,
78+
gate_obj.label.map(|x| *x),
7979
#[cfg(feature = "cache_pygates")]
8080
Some(gate.into()),
8181
)?;
@@ -121,7 +121,7 @@ pub(super) fn compose_transforms<'a>(
121121
op_node.bind(py),
122122
&replace_dag,
123123
None,
124-
true,
124+
None,
125125
)?;
126126
}
127127
}

crates/accelerate/src/basis/basis_translator/mod.rs

+6-21
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ fn apply_translation(
522522
} else {
523523
Some(new_op.params)
524524
},
525-
new_op.extra_attrs,
525+
new_op.label.map(|x| *x),
526526
#[cfg(feature = "cache_pygates")]
527527
None,
528528
)?;
@@ -543,7 +543,7 @@ fn apply_translation(
543543
.collect(),
544544
)
545545
},
546-
node_obj.extra_attrs.clone(),
546+
node_obj.label.as_ref().map(|x| x.as_ref().clone()),
547547
#[cfg(feature = "cache_pygates")]
548548
None,
549549
)?;
@@ -571,7 +571,7 @@ fn apply_translation(
571571
.collect(),
572572
)
573573
},
574-
node_obj.extra_attrs.clone(),
574+
node_obj.label.as_ref().map(|x| x.as_ref().clone()),
575575
#[cfg(feature = "cache_pygates")]
576576
None,
577577
)?;
@@ -595,7 +595,7 @@ fn apply_translation(
595595
.collect(),
596596
)
597597
},
598-
node_obj.extra_attrs.clone(),
598+
node_obj.label.as_ref().map(|x| x.as_ref().clone()),
599599
#[cfg(feature = "cache_pygates")]
600600
None,
601601
)?;
@@ -667,27 +667,12 @@ fn replace_node(
667667
} else {
668668
inner_node.op.clone()
669669
};
670-
if node.condition().is_some() {
671-
match new_op.view() {
672-
OperationRef::Gate(gate) => {
673-
gate.gate.setattr(py, "condition", node.condition())?
674-
}
675-
OperationRef::Instruction(inst) => {
676-
inst.instruction
677-
.setattr(py, "condition", node.condition())?
678-
}
679-
OperationRef::Operation(oper) => {
680-
oper.operation.setattr(py, "condition", node.condition())?
681-
}
682-
_ => (),
683-
}
684-
}
685670
let new_params: SmallVec<[Param; 3]> = inner_node
686671
.params_view()
687672
.iter()
688673
.map(|param| param.clone_ref(py))
689674
.collect();
690-
let new_extra_props = node.extra_attrs.clone();
675+
let new_extra_props = node.label.as_ref().map(|x| x.as_ref().clone());
691676
dag.apply_operation_back(
692677
py,
693678
new_op,
@@ -802,7 +787,7 @@ fn replace_node(
802787
} else {
803788
Some(new_params)
804789
},
805-
inner_node.extra_attrs.clone(),
790+
inner_node.label.as_ref().map(|x| x.as_ref().clone()),
806791
#[cfg(feature = "cache_pygates")]
807792
None,
808793
)?;

crates/accelerate/src/commutation_analysis.rs

-2
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,10 @@ pub(crate) fn analyze_commutations_inner(
9595
py,
9696
&op1,
9797
params1,
98-
&packed_inst0.extra_attrs,
9998
qargs1,
10099
cargs1,
101100
&op2,
102101
params2,
103-
&packed_inst1.extra_attrs,
104102
qargs2,
105103
cargs2,
106104
MAX_NUM_QUBITS,

crates/accelerate/src/commutation_checker.rs

+2-16
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use pyo3::types::{PyBool, PyDict, PySequence, PyTuple};
2525
use pyo3::BoundObject;
2626

2727
use qiskit_circuit::bit_data::BitData;
28-
use qiskit_circuit::circuit_instruction::{ExtraInstructionAttributes, OperationFromPython};
28+
use qiskit_circuit::circuit_instruction::OperationFromPython;
2929
use qiskit_circuit::dag_node::DAGOpNode;
3030
use qiskit_circuit::imports::QI_OPERATOR;
3131
use qiskit_circuit::operations::OperationRef::{Gate as PyGateType, Operation as PyOperationType};
@@ -178,12 +178,10 @@ impl CommutationChecker {
178178
py,
179179
&op1.instruction.operation.view(),
180180
&op1.instruction.params,
181-
&op1.instruction.extra_attrs,
182181
&qargs1,
183182
&cargs1,
184183
&op2.instruction.operation.view(),
185184
&op2.instruction.params,
186-
&op2.instruction.extra_attrs,
187185
&qargs2,
188186
&cargs2,
189187
max_num_qubits,
@@ -215,12 +213,10 @@ impl CommutationChecker {
215213
py,
216214
&op1.operation.view(),
217215
&op1.params,
218-
&op1.extra_attrs,
219216
&qargs1,
220217
&cargs1,
221218
&op2.operation.view(),
222219
&op2.params,
223-
&op2.extra_attrs,
224220
&qargs2,
225221
&cargs2,
226222
max_num_qubits,
@@ -285,12 +281,10 @@ impl CommutationChecker {
285281
py: Python,
286282
op1: &OperationRef,
287283
params1: &[Param],
288-
attrs1: &ExtraInstructionAttributes,
289284
qargs1: &[Qubit],
290285
cargs1: &[Clbit],
291286
op2: &OperationRef,
292287
params2: &[Param],
293-
attrs2: &ExtraInstructionAttributes,
294288
qargs2: &[Qubit],
295289
cargs2: &[Clbit],
296290
max_num_qubits: u32,
@@ -321,12 +315,10 @@ impl CommutationChecker {
321315
let commutation: Option<bool> = commutation_precheck(
322316
op1,
323317
params1,
324-
attrs1,
325318
qargs1,
326319
cargs1,
327320
op2,
328321
params2,
329-
attrs2,
330322
qargs2,
331323
cargs2,
332324
max_num_qubits,
@@ -576,21 +568,15 @@ impl CommutationChecker {
576568
fn commutation_precheck(
577569
op1: &OperationRef,
578570
params1: &[Param],
579-
attrs1: &ExtraInstructionAttributes,
580571
qargs1: &[Qubit],
581572
cargs1: &[Clbit],
582573
op2: &OperationRef,
583574
params2: &[Param],
584-
attrs2: &ExtraInstructionAttributes,
585575
qargs2: &[Qubit],
586576
cargs2: &[Clbit],
587577
max_num_qubits: u32,
588578
) -> Option<bool> {
589-
if op1.control_flow()
590-
|| op2.control_flow()
591-
|| attrs1.condition().is_some()
592-
|| attrs2.condition().is_some()
593-
{
579+
if op1.control_flow() || op2.control_flow() {
594580
return Some(false);
595581
}
596582

crates/accelerate/src/consolidate_blocks.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use rustworkx_core::petgraph::stable_graph::NodeIndex;
2121
use smallvec::smallvec;
2222

2323
use qiskit_circuit::circuit_data::CircuitData;
24-
use qiskit_circuit::circuit_instruction::ExtraInstructionAttributes;
2524
use qiskit_circuit::dag_circuit::DAGCircuit;
2625
use qiskit_circuit::gate_matrix::{ONE_QUBIT_IDENTITY, TWO_QUBIT_IDENTITY};
2726
use qiskit_circuit::imports::{QI_OPERATOR, QUANTUM_CIRCUIT};
@@ -125,7 +124,7 @@ pub(crate) fn consolidate_blocks(
125124
inst_node,
126125
PackedOperation::from_unitary(Box::new(unitary_gate)),
127126
smallvec![],
128-
ExtraInstructionAttributes::default(),
127+
None,
129128
)?;
130129
continue;
131130
}
@@ -199,7 +198,7 @@ pub(crate) fn consolidate_blocks(
199198
&block,
200199
PackedOperation::from_unitary(Box::new(unitary_gate)),
201200
smallvec![],
202-
ExtraInstructionAttributes::default(),
201+
None,
203202
false,
204203
&block_index_map,
205204
&clbit_pos_map,
@@ -238,7 +237,7 @@ pub(crate) fn consolidate_blocks(
238237
&block,
239238
PackedOperation::from_unitary(Box::new(unitary_gate)),
240239
smallvec![],
241-
ExtraInstructionAttributes::default(),
240+
None,
242241
false,
243242
&qubit_pos_map,
244243
&clbit_pos_map,
@@ -276,7 +275,7 @@ pub(crate) fn consolidate_blocks(
276275
first_inst_node,
277276
PackedOperation::from_unitary(Box::new(unitary_gate)),
278277
smallvec![],
279-
ExtraInstructionAttributes::default(),
278+
None,
280279
)?;
281280
continue;
282281
}
@@ -320,7 +319,7 @@ pub(crate) fn consolidate_blocks(
320319
&run,
321320
PackedOperation::from_unitary(Box::new(unitary_gate)),
322321
smallvec![],
323-
ExtraInstructionAttributes::default(),
322+
None,
324323
false,
325324
&block_index_map,
326325
&clbit_pos_map,

crates/accelerate/src/elide_permutations.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ fn run(py: Python, dag: &mut DAGCircuit) -> PyResult<Option<(DAGCircuit, Vec<usi
4040
let mut new_dag = dag.copy_empty_like(py, "alike")?;
4141
for node_index in dag.topological_op_nodes()? {
4242
if let NodeType::Operation(inst) = &dag[node_index] {
43-
match (inst.op.name(), inst.condition()) {
44-
("swap", None) => {
43+
match inst.op.name() {
44+
"swap" => {
4545
let qargs = dag.get_qargs(inst.qubits);
4646
let index0 = qargs[0].index();
4747
let index1 = qargs[1].index();
4848
mapping.swap(index0, index1);
4949
}
50-
("permutation", None) => {
50+
"permutation" => {
5151
if let Param::Obj(ref pyobj) = inst.params.as_ref().unwrap()[0] {
5252
let pyarray: PyReadonlyArray1<i32> = pyobj.extract(py)?;
5353
let pattern = pyarray.as_array();
@@ -88,7 +88,7 @@ fn run(py: Python, dag: &mut DAGCircuit) -> PyResult<Option<(DAGCircuit, Vec<usi
8888
&mapped_qargs,
8989
cargs,
9090
inst.params.as_deref().cloned(),
91-
inst.extra_attrs.clone(),
91+
inst.label.as_ref().map(|x| x.as_ref().clone()),
9292
#[cfg(feature = "cache_pygates")]
9393
inst.py_op.get().map(|x| x.clone_ref(py)),
9494
)?;

crates/accelerate/src/gate_direction.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use qiskit_circuit::operations::OperationRef;
2121
use qiskit_circuit::packed_instruction::PackedOperation;
2222
use qiskit_circuit::{
2323
circuit_instruction::CircuitInstruction,
24-
circuit_instruction::ExtraInstructionAttributes,
2524
converters::{circuit_to_dag, QuantumCircuitData},
2625
dag_circuit::DAGCircuit,
2726
dag_node::{DAGNode, DAGOpNode},
@@ -360,7 +359,7 @@ where
360359
.bind(py)
361360
.call_method1("replace_blocks", (op_blocks,))?;
362361

363-
dag.py_substitute_node(dag.get_node(py, node)?.bind(py), &new_op, false, false)?;
362+
dag.py_substitute_node(py, dag.get_node(py, node)?.bind(py), &new_op, false, None)?;
364363
}
365364

366365
for (node, replacemanet_dag) in nodes_to_replace {
@@ -369,7 +368,7 @@ where
369368
dag.get_node(py, node)?.bind(py),
370369
&replacemanet_dag,
371370
None,
372-
true,
371+
None,
373372
)?;
374373
}
375374

@@ -394,7 +393,7 @@ fn has_calibration_for_op_node(
394393
qubits: py_args.unbind(),
395394
clbits: PyTuple::empty(py).unbind(),
396395
params: packed_inst.params_view().iter().cloned().collect(),
397-
extra_attrs: packed_inst.extra_attrs.clone(),
396+
label: packed_inst.label.clone(),
398397
#[cfg(feature = "cache_pygates")]
399398
py_op: packed_inst.py_op.clone(),
400399
},
@@ -467,7 +466,7 @@ fn apply_operation_back(
467466
qargs,
468467
&[],
469468
param,
470-
ExtraInstructionAttributes::default(),
469+
None,
471470
#[cfg(feature = "cache_pygates")]
472471
None,
473472
)?;

crates/accelerate/src/split_2q_unitaries.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use pyo3::prelude::*;
1818
use rustworkx_core::petgraph::stable_graph::NodeIndex;
1919
use smallvec::{smallvec, SmallVec};
2020

21-
use qiskit_circuit::circuit_instruction::ExtraInstructionAttributes;
2221
use qiskit_circuit::dag_circuit::{DAGCircuit, NodeType, Wire};
2322
use qiskit_circuit::operations::{ArrayType, Operation, OperationRef, Param, UnitaryGate};
2423
use qiskit_circuit::packed_instruction::PackedOperation;
@@ -148,7 +147,7 @@ pub fn split_2q_unitaries(
148147
&[Qubit::new(mapping[index0])],
149148
&[],
150149
None,
151-
ExtraInstructionAttributes::default(),
150+
None,
152151
#[cfg(feature = "cache_pygates")]
153152
None,
154153
)?;
@@ -158,7 +157,7 @@ pub fn split_2q_unitaries(
158157
&[Qubit::new(mapping[index1])],
159158
&[],
160159
None,
161-
ExtraInstructionAttributes::default(),
160+
None,
162161
#[cfg(feature = "cache_pygates")]
163162
None,
164163
)?;
@@ -180,7 +179,7 @@ pub fn split_2q_unitaries(
180179
&mapped_qargs,
181180
cargs,
182181
inst.params.as_deref().cloned(),
183-
inst.extra_attrs.clone(),
182+
inst.label.as_ref().map(|x| x.to_string()),
184183
#[cfg(feature = "cache_pygates")]
185184
inst.py_op.get().map(|x| x.clone_ref(py)),
186185
)?;

crates/accelerate/src/target_transpiler/mod.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use pyo3::{
3232
IntoPyObjectExt,
3333
};
3434

35-
use qiskit_circuit::circuit_instruction::{ExtraInstructionAttributes, OperationFromPython};
35+
use qiskit_circuit::circuit_instruction::OperationFromPython;
3636
use qiskit_circuit::operations::{Operation, OperationRef, Param};
3737
use qiskit_circuit::packed_instruction::PackedOperation;
3838
use smallvec::SmallVec;
@@ -775,17 +775,15 @@ impl Target {
775775
let out_inst = match inst {
776776
TargetOperation::Normal(op) => match op.operation.view() {
777777
OperationRef::StandardGate(standard) => standard
778-
.create_py_op(py, Some(&op.params), &ExtraInstructionAttributes::default())?
778+
.create_py_op(py, Some(&op.params), None)?
779779
.into_any(),
780780
OperationRef::StandardInstruction(standard) => standard
781-
.create_py_op(py, Some(&op.params), &ExtraInstructionAttributes::default())?
781+
.create_py_op(py, Some(&op.params), None)?
782782
.into_any(),
783783
OperationRef::Gate(gate) => gate.gate.clone_ref(py),
784784
OperationRef::Instruction(instruction) => instruction.instruction.clone_ref(py),
785785
OperationRef::Operation(operation) => operation.operation.clone_ref(py),
786-
OperationRef::Unitary(unitary) => unitary
787-
.create_py_op(py, &ExtraInstructionAttributes::default())?
788-
.into_any(),
786+
OperationRef::Unitary(unitary) => unitary.create_py_op(py, None)?.into_any(),
789787
},
790788
TargetOperation::Variadic(op_cls) => op_cls.clone_ref(py),
791789
};

0 commit comments

Comments
 (0)