cqlib-core 0.1.0-beta.7

The high-performance core foundation for the CQLib quantum computing ecosystem.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
// This code is part of Cqlib.
//
// (C) Copyright China Telecom Quantum Group 2025-2026
//
// This code is licensed under the Apache License, Version 2.0. You may
// obtain a copy of this license in the LICENSE.txt file in the root directory
// of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
//
// Any modifications or derivative works of this code must retain this
// copyright notice, and modified files need to carry a notice indicating
// that they have been altered from the originals.

//! Dependency DAG used by the compiler SABRE implementation.
//!
//! The DAG is built from circuit operation order and logical-qubit overlap.
//! Nodes are intentionally coarser than single operations: consecutive
//! operations that share the same dependency boundary can be folded together so
//! routing sees the smallest set of scheduling barriers needed for progress.
//!
//! [`SabreNodeKind::TwoQ`] represents a two-logical-qubit interaction that
//! must be adjacent before it can be emitted. Dependencies are derived from a
//! per-wire frontier: each new operation depends on the latest node touching
//! any of its logical qubits, and then becomes the frontier for those qubits.
//! This lets SABRE reason about both interaction readiness and device-local
//! unary capabilities without crossing explicit dependency boundaries.
//!
//! [`SabreNodeKind::Unary`] represents a one-logical-qubit requirement. It is a
//! first-class routable node because device-local one-qubit capabilities may
//! require moving the logical token before the operation can be lowered.
//!
//! [`SabreNodeKind::Synchronize`] is used for zero-qubit operations, delays,
//! and directives. These operations preserve sequencing without adding a
//! device-placement requirement. An empty-qubit barrier is a global synchronization boundary: it
//! waits for every active wire and becomes a dependency of every subsequent
//! operation. Other initial synchronize operations that touch no mapped
//! frontier stay in [`SabreDag::initial`].
//!
//! Control-flow operations become recursive DAG nodes. The outer node preserves
//! the control-flow operation as a scheduling boundary, while each body is
//! decomposed into its own [`SabreDag`] so routing can restore layouts at block
//! boundaries.

use crate::circuit::{
    ClassicalControlOp, ClassicalExpr, ClassicalVar, Directive, Instruction, Operation,
};
use crate::compile::CompilerError;
use crate::device::LogicalQubit;
use rustworkx_core::petgraph::Direction;
use rustworkx_core::petgraph::graph::DiGraph;
use rustworkx_core::petgraph::prelude::NodeIndex;
use std::collections::{BTreeMap, BTreeSet};
use std::ops::Deref;
use std::sync::Arc;

#[derive(Debug, Clone)]
pub(crate) enum SabreNodeKind {
    Synchronize,
    Unary(LogicalQubit),
    TwoQ([LogicalQubit; 2]),
    ControlFlow(SabreControlFlow),
}

#[derive(Debug, Clone)]
pub(crate) enum SabreControlFlow {
    If {
        condition: ClassicalExpr,
        then_body: SabreDag,
        else_body: Option<SabreDag>,
    },
    While {
        condition: ClassicalExpr,
        body: SabreDag,
    },
    For {
        var: ClassicalVar,
        start: ClassicalExpr,
        stop: ClassicalExpr,
        step: ClassicalExpr,
        body: SabreDag,
    },
    Switch {
        target: ClassicalExpr,
        cases: Vec<SabreSwitchCase>,
        default: Option<SabreDag>,
    },
}

impl SabreControlFlow {
    /// Lists body DAGs in a canonical traversal order: then/else for `if`,
    /// the single body for loops, and cases followed by default for `switch`.
    ///
    /// Metadata preparation and recursive routing passes must agree on one
    /// order so per-body prepared data stays aligned with its body.
    pub(crate) fn bodies(&self) -> Vec<&SabreDag> {
        match self {
            SabreControlFlow::If {
                then_body,
                else_body,
                ..
            } => {
                let mut bodies = vec![then_body];
                bodies.extend(else_body.iter());
                bodies
            }
            SabreControlFlow::While { body, .. } | SabreControlFlow::For { body, .. } => {
                vec![body]
            }
            SabreControlFlow::Switch { cases, default, .. } => {
                let mut bodies = cases.iter().map(|case| &case.body).collect::<Vec<_>>();
                bodies.extend(default.iter());
                bodies
            }
        }
    }
}

#[derive(Debug, Clone)]
pub(crate) struct SabreSwitchCase {
    pub(crate) value: u128,
    pub(crate) body: SabreDag,
}

#[derive(Debug, Clone)]
pub(crate) struct SabreNode {
    pub(crate) operations: Vec<SabreOperation>,
    pub(crate) kind: SabreNodeKind,
}

#[derive(Debug, Clone)]
pub(crate) struct SabreOperation {
    pub(crate) operation: Arc<Operation>,
    pub(crate) source_order: usize,
}

impl Deref for SabreOperation {
    type Target = Operation;

    fn deref(&self) -> &Self::Target {
        &self.operation
    }
}

#[derive(Debug, Clone)]
pub(crate) struct SabreDag {
    pub(crate) initial: Vec<SabreOperation>,
    pub(crate) graph: DiGraph<SabreNode, ()>,
    pub(crate) first_layer: Vec<NodeIndex>,
}

impl SabreDag {
    pub(crate) fn from_operations(operations: &[Operation]) -> Result<Self, CompilerError> {
        let mut initial = Vec::new();
        let mut graph = DiGraph::new();
        let mut wire_pos: BTreeMap<LogicalQubit, NodeIndex> = BTreeMap::new();
        let mut first_layer = Vec::new();
        let mut global_barrier: Option<NodeIndex> = None;
        let mut parent_marks = Vec::<u32>::new();
        let mut parent_generation = 0_u32;

        for (source_order, operation) in operations.iter().enumerate() {
            let kind = kind_from_operation(operation)?;
            let is_global_barrier = matches!(
                operation.instruction,
                Instruction::Directive(Directive::Barrier)
            ) && operation.qubits.is_empty();
            let ordering_barrier = matches!(
                operation.instruction,
                Instruction::ClassicalData(_) | Instruction::ClassicalControl(_)
            ) || is_global_barrier;
            let qubits = operation
                .qubits
                .iter()
                .copied()
                .map(LogicalQubit::from_qubit)
                .collect::<Vec<_>>();

            parent_generation = parent_generation.wrapping_add(1);
            if parent_generation == 0 {
                parent_marks.fill(0);
                parent_generation = 1;
            }
            parent_marks.resize(graph.node_count(), 0);
            let mut parents = Vec::new();
            if let Some(parent) = global_barrier {
                parent_marks[parent.index()] = parent_generation;
                parents.push(parent);
            }
            if ordering_barrier {
                for parent in wire_pos.values().copied() {
                    if parent_marks[parent.index()] != parent_generation {
                        parent_marks[parent.index()] = parent_generation;
                        parents.push(parent);
                    }
                }
            } else {
                for logical in &qubits {
                    if let Some(parent) = wire_pos.get(logical).copied()
                        && parent_marks[parent.index()] != parent_generation
                    {
                        parent_marks[parent.index()] = parent_generation;
                        parents.push(parent);
                    }
                }
            }
            let predecessors = match parents.as_slice() {
                [] => Predecessors::AllUnmapped,
                [parent] => Predecessors::Single(*parent),
                _ => Predecessors::Multiple(parents),
            };
            let mut created_node = None;
            let operation = SabreOperation {
                operation: Arc::new(operation.clone()),
                source_order,
            };
            match predecessors {
                Predecessors::AllUnmapped => match kind {
                    SabreNodeKind::Synchronize if !ordering_barrier => {
                        initial.push(operation.clone())
                    }
                    kind => {
                        let node = graph.add_node(SabreNode {
                            operations: vec![operation.clone()],
                            kind,
                        });
                        first_layer.push(node);
                        created_node = Some(node);
                        for logical in qubits {
                            wire_pos.insert(logical, node);
                        }
                    }
                },
                Predecessors::Single(previous) => {
                    // Only requirements with the same ordered logical operands
                    // share one routable node. Synchronization boundaries and
                    // unary/pair transitions remain explicit DAG nodes.
                    let fold_into_previous = !ordering_barrier
                        && match (&graph[previous].kind, &kind) {
                            (SabreNodeKind::Unary(previous), SabreNodeKind::Unary(current)) => {
                                previous == current
                            }
                            (SabreNodeKind::TwoQ(previous), SabreNodeKind::TwoQ(current)) => {
                                previous == current
                            }
                            _ => false,
                        };
                    if fold_into_previous {
                        graph[previous].operations.push(operation.clone());
                        for logical in qubits {
                            wire_pos.insert(logical, previous);
                        }
                    } else {
                        let node = graph.add_node(SabreNode {
                            operations: vec![operation.clone()],
                            kind,
                        });
                        graph.add_edge(previous, node, ());
                        created_node = Some(node);
                        for logical in qubits {
                            wire_pos.insert(logical, node);
                        }
                    }
                }
                Predecessors::Multiple(parents) => {
                    let node = graph.add_node(SabreNode {
                        operations: vec![operation.clone()],
                        kind,
                    });
                    created_node = Some(node);
                    for parent in parents {
                        // `parents` is deduplicated while it is assembled and
                        // `node` was just created, so no edge to it can exist.
                        // Adding directly avoids scanning a high-outdegree
                        // parent's adjacency list for every new dependency.
                        graph.add_edge(parent, node, ());
                    }
                    for logical in qubits {
                        wire_pos.insert(logical, node);
                    }
                }
            }
            if ordering_barrier {
                global_barrier = created_node;
            }
        }

        Ok(Self {
            initial,
            graph,
            first_layer,
        })
    }

    /// Builds the static interaction workload used by bidirectional layout
    /// refinement.
    ///
    /// Control-flow bodies are included once. Alternative branches remain
    /// parallel when they touch disjoint logical wires, while a stable branch
    /// order adds per-wire dependencies between branches that share a wire.
    /// This preserves the front-layer invariant that two ready interactions
    /// cannot occupy the same logical qubit.
    pub(crate) fn refinement_workload(operations: &[Operation]) -> Result<Self, CompilerError> {
        let mut builder = WorkloadBuilder::default();
        let mut state = WorkloadState::default();
        builder.add_operations(operations, &mut state)?;
        let first_layer = builder.graph.externals(Direction::Incoming).collect();
        Ok(Self {
            initial: Vec::new(),
            graph: builder.graph,
            first_layer,
        })
    }

    pub(crate) fn reverse_interactions(&self) -> Self {
        let mut graph = self.graph.clone();
        graph.reverse();
        let first_layer = graph.externals(Direction::Incoming).collect();
        Self {
            initial: Vec::new(),
            graph,
            first_layer,
        }
    }
}

#[derive(Default)]
struct WorkloadState {
    wire_frontier: BTreeMap<LogicalQubit, NodeIndex>,
    global_barrier: Option<NodeIndex>,
}

#[derive(Default)]
struct WorkloadBuilder {
    graph: DiGraph<SabreNode, ()>,
}

impl WorkloadBuilder {
    fn add_operations(
        &mut self,
        operations: &[Operation],
        state: &mut WorkloadState,
    ) -> Result<BTreeSet<LogicalQubit>, CompilerError> {
        let mut touched = BTreeSet::new();
        for operation in operations {
            match &operation.instruction {
                Instruction::ClassicalControl(flow) => {
                    touched.extend(self.add_control_flow(flow, state)?);
                }
                Instruction::ClassicalData(_) => {
                    self.add_global_synchronize(state);
                }
                Instruction::Directive(Directive::Barrier) => {
                    if operation.qubits.is_empty() {
                        self.add_global_synchronize(state);
                    } else {
                        let qubits = operation
                            .qubits
                            .iter()
                            .copied()
                            .map(LogicalQubit::from_qubit)
                            .collect::<Vec<_>>();
                        self.add_wire_synchronize(&qubits, state);
                    }
                }
                Instruction::Directive(_) | Instruction::Delay => {}
                _ => match operation.qubits.len() {
                    0 | 1 => {}
                    2 => {
                        let pair = [
                            LogicalQubit::from_qubit(operation.qubits[0]),
                            LogicalQubit::from_qubit(operation.qubits[1]),
                        ];
                        self.add_two_qubit(pair, state);
                        touched.extend(pair);
                    }
                    arity => {
                        return Err(CompilerError::InvalidInput(format!(
                            "sabre requires operations with more than two qubits to be decomposed before routing; found {arity}-qubit operation {}",
                            operation.instruction
                        )));
                    }
                },
            }
        }
        Ok(touched)
    }

    fn add_two_qubit(&mut self, pair: [LogicalQubit; 2], state: &mut WorkloadState) {
        let mut parents = BTreeSet::new();
        parents.extend(state.global_barrier);
        parents.extend(
            pair.iter()
                .filter_map(|logical| state.wire_frontier.get(logical).copied()),
        );
        let node = self.graph.add_node(SabreNode {
            operations: Vec::new(),
            kind: SabreNodeKind::TwoQ(pair),
        });
        // `node` was just created and `parents` is a deduplicated
        // `BTreeSet`. No edge to `node` can exist yet, so adding
        // directly avoids scanning each parent's adjacency list for
        // every new dependency edge.
        for parent in parents {
            self.graph.add_edge(parent, node, ());
        }
        state.wire_frontier.insert(pair[0], node);
        state.wire_frontier.insert(pair[1], node);
    }

    fn add_global_synchronize(&mut self, state: &mut WorkloadState) -> NodeIndex {
        let mut parents = state
            .wire_frontier
            .values()
            .copied()
            .collect::<BTreeSet<_>>();
        parents.extend(state.global_barrier);
        let node = self.add_node(SabreNodeKind::Synchronize, parents);
        for frontier in state.wire_frontier.values_mut() {
            *frontier = node;
        }
        state.global_barrier = Some(node);
        node
    }

    fn add_wire_synchronize(
        &mut self,
        qubits: &[LogicalQubit],
        state: &mut WorkloadState,
    ) -> NodeIndex {
        let mut parents = BTreeSet::new();
        parents.extend(state.global_barrier);
        parents.extend(
            qubits
                .iter()
                .filter_map(|logical| state.wire_frontier.get(logical).copied()),
        );
        let node = self.add_node(SabreNodeKind::Synchronize, parents);
        for &logical in qubits {
            state.wire_frontier.insert(logical, node);
        }
        node
    }

    fn add_control_flow(
        &mut self,
        flow: &ClassicalControlOp,
        state: &mut WorkloadState,
    ) -> Result<BTreeSet<LogicalQubit>, CompilerError> {
        let fork = self.add_global_synchronize(state);
        let base_frontier = state.wire_frontier.clone();
        let mut cross_branch_frontier = base_frontier.clone();
        let mut all_touched = BTreeSet::new();
        let mut join_parents = BTreeSet::from([fork]);
        for body in flow.bodies() {
            let mut branch_state = WorkloadState {
                wire_frontier: cross_branch_frontier.clone(),
                global_barrier: Some(fork),
            };
            let branch_touched = self.add_operations(body.operations(), &mut branch_state)?;
            for &logical in &branch_touched {
                if let Some(frontier) = branch_state.wire_frontier.get(&logical).copied() {
                    cross_branch_frontier.insert(logical, frontier);
                    join_parents.insert(frontier);
                }
            }
            if let Some(barrier) = branch_state.global_barrier
                && barrier != fork
            {
                join_parents.insert(barrier);
            }
            all_touched.extend(branch_touched);
        }

        let join = self.add_node(SabreNodeKind::Synchronize, join_parents);
        for frontier in state.wire_frontier.values_mut() {
            *frontier = join;
        }
        for &logical in &all_touched {
            state.wire_frontier.insert(logical, join);
        }
        state.global_barrier = Some(join);
        Ok(all_touched)
    }

    fn add_node(&mut self, kind: SabreNodeKind, parents: BTreeSet<NodeIndex>) -> NodeIndex {
        let node = self.graph.add_node(SabreNode {
            operations: Vec::new(),
            kind,
        });
        // `node` was just created and `parents` is a deduplicated
        // `BTreeSet`. No edge to `node` can exist yet.
        for parent in parents {
            self.graph.add_edge(parent, node, ());
        }
        node
    }
}

enum Predecessors {
    AllUnmapped,
    Single(NodeIndex),
    Multiple(Vec<NodeIndex>),
}

fn kind_from_operation(operation: &Operation) -> Result<SabreNodeKind, CompilerError> {
    match &operation.instruction {
        Instruction::ClassicalControl(flow) => match flow {
            ClassicalControlOp::If(op) => Ok(SabreNodeKind::ControlFlow(SabreControlFlow::If {
                condition: op.condition().clone(),
                then_body: SabreDag::from_operations(op.then_body().operations())?,
                else_body: op
                    .else_body()
                    .map(|body| SabreDag::from_operations(body.operations()))
                    .transpose()?,
            })),
            ClassicalControlOp::While(op) => {
                Ok(SabreNodeKind::ControlFlow(SabreControlFlow::While {
                    condition: op.condition().clone(),
                    body: SabreDag::from_operations(op.body().operations())?,
                }))
            }
            ClassicalControlOp::For(op) => Ok(SabreNodeKind::ControlFlow(SabreControlFlow::For {
                var: op.var(),
                start: op.start().clone(),
                stop: op.stop().clone(),
                step: op.step().clone(),
                body: SabreDag::from_operations(op.body().operations())?,
            })),
            ClassicalControlOp::Switch(op) => {
                let cases = op
                    .cases()
                    .iter()
                    .map(|case| {
                        Ok(SabreSwitchCase {
                            value: case.value(),
                            body: SabreDag::from_operations(case.body().operations())?,
                        })
                    })
                    .collect::<Result<Vec<_>, CompilerError>>()?;
                let default = op
                    .default()
                    .map(|body| SabreDag::from_operations(body.operations()))
                    .transpose()?;
                Ok(SabreNodeKind::ControlFlow(SabreControlFlow::Switch {
                    target: op.target().clone(),
                    cases,
                    default,
                }))
            }
            ClassicalControlOp::Break | ClassicalControlOp::Continue => {
                Ok(SabreNodeKind::Synchronize)
            }
        },
        Instruction::ClassicalData(_) | Instruction::Directive(_) | Instruction::Delay => {
            Ok(SabreNodeKind::Synchronize)
        }
        _ => match operation.qubits.len() {
            0 => Ok(SabreNodeKind::Synchronize),
            1 => Ok(SabreNodeKind::Unary(LogicalQubit::from_qubit(
                operation.qubits[0],
            ))),
            2 => Ok(SabreNodeKind::TwoQ([
                LogicalQubit::from_qubit(operation.qubits[0]),
                LogicalQubit::from_qubit(operation.qubits[1]),
            ])),
            arity => Err(CompilerError::InvalidInput(format!(
                "sabre requires operations with more than two qubits to be decomposed before routing; found {arity}-qubit operation {}",
                operation.instruction
            ))),
        },
    }
}

#[cfg(test)]
#[path = "dag_test.rs"]
mod dag_test;