Skip to content

Commit 13443f4

Browse files
authored
Remove unecessary nodes field in SabreDAG (Qiskit#14199)
The SabreDAG struct was carrying a nodes field which was a Vec of the topologically sorted node descriptions used to create the inner graph object. The format of this field mirrors what Python passes to Rust when initially creating the DAG. The reason this was being kept around was for reversing the graph when working with control flow ops. However, in practice this isn't necessary because all the data contained in the vec is also contained in the graph and we can just reverse the graph directly and save having to carry around the entire object. Also removing this field simplifies the step of moving the sabre dag construction into Rust which is required for porting the remainder of the pass to Rust.
1 parent 8ec4c9c commit 13443f4

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

crates/accelerate/src/sabre/layout.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -243,21 +243,14 @@ fn layout_trial(
243243
num_qubits: dag.num_qubits,
244244
num_clbits: dag.num_clbits,
245245
dag: dag.dag.clone(),
246-
nodes: dag.nodes.clone(),
247246
first_layer: dag.first_layer.clone(),
248247
node_blocks: dag
249248
.node_blocks
250249
.keys()
251250
.map(|index| (*index, Vec::new()))
252251
.collect(),
253252
};
254-
let dag_no_control_reverse = SabreDAG::new(
255-
dag_no_control_forward.num_qubits,
256-
dag_no_control_forward.num_clbits,
257-
dag_no_control_forward.nodes.iter().rev().cloned().collect(),
258-
dag_no_control_forward.node_blocks.clone(),
259-
)
260-
.unwrap();
253+
let dag_no_control_reverse = dag_no_control_forward.reverse_dag();
261254

262255
for _iter in 0..max_iterations {
263256
for dag in [&dag_no_control_forward, &dag_no_control_reverse] {

crates/accelerate/src/sabre/sabre_dag.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,28 @@ pub struct SabreDAG {
4242
pub num_clbits: usize,
4343
pub dag: DiGraph<DAGNode, ()>,
4444
pub first_layer: Vec<NodeIndex>,
45-
pub nodes: Vec<(usize, Vec<VirtualQubit>, HashSet<usize>, bool)>,
4645
pub node_blocks: HashMap<usize, Vec<SabreDAG>>,
4746
}
4847

48+
impl SabreDAG {
49+
pub fn reverse_dag(&self) -> Self {
50+
let mut out_dag = self.clone();
51+
out_dag.dag.reverse();
52+
out_dag.first_layer = out_dag
53+
.dag
54+
.node_indices()
55+
.filter(|idx| {
56+
out_dag
57+
.dag
58+
.neighbors_directed(*idx, Incoming)
59+
.next()
60+
.is_none()
61+
})
62+
.collect();
63+
out_dag
64+
}
65+
}
66+
4967
#[pymethods]
5068
impl SabreDAG {
5169
#[new]
@@ -105,7 +123,6 @@ impl SabreDAG {
105123
num_clbits,
106124
dag,
107125
first_layer,
108-
nodes,
109126
node_blocks,
110127
})
111128
}

0 commit comments

Comments
 (0)