qvm_scheduler/
lib.rs

1//! # Quantum Virtual Machine Scheduler
2//!
3//! A backend-agnostic quantum circuit scheduler with OpenQASM 3 support.
4//! Provides circuit parsing, topology-aware scheduling, and composite circuit generation.
5
6#![cfg_attr(not(feature = "std"), no_std)]
7
8// WASM setup
9#[cfg(feature = "wasm")]
10use wasm_bindgen::prelude::*;
11
12#[cfg(all(feature = "wasm", target_arch = "wasm32"))]
13#[global_allocator]
14static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
15
16#[cfg(feature = "wasm")]
17#[wasm_bindgen]
18pub fn init_wasm() {
19    #[cfg(feature = "wasm")]
20    console_error_panic_hook::set_once();
21}
22
23// Core modules
24pub mod error;
25pub mod circuit_ir;
26pub mod topology;
27pub mod scheduler;
28pub mod composer;
29
30// API modules
31pub mod api;
32
33// WASM module
34#[cfg(feature = "wasm")]
35pub mod wasm;
36
37// Re-exports for convenience
38pub use error::{QvmError, Result};
39pub use circuit_ir::{QuantumCircuit, Operation, Qubit, ClassicalBit, CircuitBuilder};
40pub use topology::{Topology, Tile, Position, TopologyBuilder};
41pub use scheduler::{Job, Schedule, Scheduler};
42pub use composer::{CompositeCircuit, CircuitComposer};
43
44/// QVM version
45pub const VERSION: &str = env!("CARGO_PKG_VERSION");
46
47/// High-level QVM scheduler interface
48#[derive(Debug, Clone)]
49pub struct QvmScheduler {
50    topology: Topology,
51    scheduler: Scheduler,
52    composer: CircuitComposer,
53}
54
55impl QvmScheduler {
56    /// Create a new QVM scheduler with the given topology
57    pub fn new(topology: Topology) -> Self {
58        Self {
59            topology: topology.clone(),
60            scheduler: Scheduler::new(topology.clone()),
61            composer: CircuitComposer::new(topology),
62        }
63    }
64
65    /// Schedule multiple circuits and generate composite output
66    pub async fn schedule_circuits(
67        &mut self,
68        circuits: Vec<QuantumCircuit>,
69    ) -> Result<CompositeCircuit> {
70        let jobs: Vec<Job> = circuits
71            .into_iter()
72            .enumerate()
73            .map(|(id, circuit)| Job::new(id, circuit))
74            .collect();
75
76        let schedule = self.scheduler.schedule(jobs).await?;
77        self.composer.compose(schedule).await
78    }
79
80    /// Schedule multiple circuits (convenience method)
81    pub async fn schedule(
82        &self,
83        circuits: &[QuantumCircuit],
84    ) -> Result<CompositeCircuit> {
85        let jobs: Vec<Job> = circuits
86            .iter()
87            .enumerate()
88            .map(|(id, circuit)| Job::new(id, circuit.clone()))
89            .collect();
90
91        let mut scheduler = self.scheduler.clone();
92        let schedule = scheduler.schedule(jobs).await?;
93        
94        let mut composer = self.composer.clone();
95        composer.compose(schedule).await
96    }
97
98    /// Get the underlying topology
99    pub fn topology(&self) -> &Topology {
100        &self.topology
101    }
102}
103
104#[cfg(test)]
105mod tests {
106    use super::*;
107    use crate::topology::TopologyBuilder;
108
109    #[test]
110    fn test_qvm_scheduler_creation() {
111        let topology = TopologyBuilder::grid(3, 3);
112        let scheduler = QvmScheduler::new(topology);
113        assert_eq!(scheduler.topology().qubit_count(), 9);
114    }
115
116    #[cfg(feature = "std")]
117    #[tokio::test]
118    async fn test_empty_schedule() {
119        let topology = TopologyBuilder::grid(2, 2);
120        let mut scheduler = QvmScheduler::new(topology);
121        
122        let result = scheduler.schedule_circuits(vec![]).await;
123        assert!(result.is_ok());
124        
125        let composite = result.unwrap();
126        assert!(composite.circuits().is_empty());
127    }
128}