qvm-scheduler 0.1.0

High-performance quantum circuit scheduler for multi-job quantum computing with OpenQASM 3
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
//! Assignment data structures and management

use crate::{QvmError, Result, Qubit, ClassicalBit};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Assignment of a job to specific hardware resources
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Assignment {
    /// Job identifier
    pub job_id: usize,
    /// Assigned tile identifier
    pub tile_id: usize,
    /// Start execution time (microseconds since epoch)
    pub start_time: u64,
    /// Expected execution duration (microseconds)
    pub duration: u64,
    /// Mapping from logical to physical qubits
    pub qubit_mapping: Vec<usize>,
    /// Mapping from logical to physical classical bits
    pub classical_mapping: Vec<usize>,
    /// Additional resource allocations
    pub resource_allocation: ResourceAllocation,
}

/// Additional resource allocation information
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ResourceAllocation {
    /// Reserved buffer qubits
    pub buffer_qubits: Vec<usize>,
    /// Allocated classical memory
    pub classical_memory: usize,
    /// Special resource allocations
    pub special_resources: HashMap<String, String>,
    /// Quality of service parameters
    pub qos_params: QoSParameters,
}

/// Quality of Service parameters
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct QoSParameters {
    /// Expected fidelity
    pub expected_fidelity: f64,
    /// Maximum allowed crosstalk
    pub max_crosstalk: f64,
    /// Coherence time requirements
    pub coherence_requirements: (f64, f64), // (T1, T2)
    /// Error correction overhead
    pub error_correction_overhead: f64,
}

impl Assignment {
    /// Create a new assignment
    pub fn new(
        job_id: usize,
        tile_id: usize,
        start_time: u64,
        duration: u64,
    ) -> Self {
        Self {
            job_id,
            tile_id,
            start_time,
            duration,
            qubit_mapping: Vec::new(),
            classical_mapping: Vec::new(),
            resource_allocation: ResourceAllocation::default(),
        }
    }

    /// Set qubit mapping
    pub fn with_qubit_mapping(mut self, mapping: Vec<usize>) -> Self {
        self.qubit_mapping = mapping;
        self
    }

    /// Set classical mapping
    pub fn with_classical_mapping(mut self, mapping: Vec<usize>) -> Self {
        self.classical_mapping = mapping;
        self
    }

    /// Set resource allocation
    pub fn with_resource_allocation(mut self, allocation: ResourceAllocation) -> Self {
        self.resource_allocation = allocation;
        self
    }

    /// Get end time of the assignment
    pub fn end_time(&self) -> u64 {
        self.start_time + self.duration
    }

    /// Check if this assignment overlaps with another in time
    pub fn overlaps_in_time(&self, other: &Assignment) -> bool {
        !(self.end_time() <= other.start_time || other.end_time() <= self.start_time)
    }

    /// Check if this assignment conflicts with another (time + resources)
    pub fn conflicts_with(&self, other: &Assignment) -> bool {
        if !self.overlaps_in_time(other) {
            return false;
        }

        // Check qubit conflicts
        let self_qubits: std::collections::HashSet<_> = self.qubit_mapping.iter().collect();
        let other_qubits: std::collections::HashSet<_> = other.qubit_mapping.iter().collect();
        
        !self_qubits.is_disjoint(&other_qubits)
    }

    /// Get all physical qubits used (including buffers)
    pub fn all_used_qubits(&self) -> Vec<usize> {
        let mut qubits = self.qubit_mapping.clone();
        qubits.extend(&self.resource_allocation.buffer_qubits);
        qubits.sort();
        qubits.dedup();
        qubits
    }

    /// Validate the assignment
    pub fn validate(&self) -> Result<()> {
        // Check for duplicate qubit mappings
        let mut seen_qubits = std::collections::HashSet::new();
        for &qubit in &self.qubit_mapping {
            if !seen_qubits.insert(qubit) {
                return Err(QvmError::allocation_error(
                    format!("Duplicate qubit {} in assignment", qubit)
                ));
            }
        }

        // Check for duplicate classical mappings
        let mut seen_classical = std::collections::HashSet::new();
        for &classical in &self.classical_mapping {
            if !seen_classical.insert(classical) {
                return Err(QvmError::allocation_error(
                    format!("Duplicate classical bit {} in assignment", classical)
                ));
            }
        }

        // Validate timing
        if self.duration == 0 {
            return Err(QvmError::allocation_error("Assignment duration cannot be zero".to_string()));
        }

        Ok(())
    }

    /// Calculate resource utilization for this assignment
    pub fn resource_utilization(&self, total_qubits: usize) -> AssignmentUtilization {
        let qubit_usage = self.qubit_mapping.len() as f64 / total_qubits as f64;
        let buffer_overhead = self.resource_allocation.buffer_qubits.len() as f64 / total_qubits as f64;
        
        AssignmentUtilization {
            qubit_utilization: qubit_usage,
            buffer_overhead,
            efficiency: qubit_usage / (qubit_usage + buffer_overhead + f64::EPSILON),
            total_resource_usage: qubit_usage + buffer_overhead,
        }
    }
}

/// Assignment utilization metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AssignmentUtilization {
    /// Fraction of qubits used for computation
    pub qubit_utilization: f64,
    /// Fraction of qubits used for buffering
    pub buffer_overhead: f64,
    /// Efficiency (computation / total usage)
    pub efficiency: f64,
    /// Total resource usage
    pub total_resource_usage: f64,
}

/// Manager for tracking and optimizing assignments
#[derive(Debug, Clone)]
pub struct AssignmentManager {
    assignments: Vec<Assignment>,
    /// Index by time slot for quick lookups
    time_index: std::collections::BTreeMap<u64, Vec<usize>>,
}

impl AssignmentManager {
    /// Create a new assignment manager
    pub fn new() -> Self {
        Self {
            assignments: Vec::new(),
            time_index: std::collections::BTreeMap::new(),
        }
    }

    /// Add an assignment
    pub fn add_assignment(&mut self, assignment: Assignment) -> Result<()> {
        assignment.validate()?;

        // Check for conflicts with existing assignments
        for existing in &self.assignments {
            if assignment.conflicts_with(existing) {
                return Err(QvmError::allocation_error(
                    format!("Assignment conflicts with existing assignment for job {}", existing.job_id)
                ));
            }
        }

        let assignment_id = self.assignments.len();
        self.assignments.push(assignment.clone());

        // Update time index
        self.time_index
            .entry(assignment.start_time)
            .or_insert_with(Vec::new)
            .push(assignment_id);

        Ok(())
    }

    /// Remove an assignment by job ID
    pub fn remove_assignment(&mut self, job_id: usize) -> Option<Assignment> {
        if let Some(pos) = self.assignments.iter().position(|a| a.job_id == job_id) {
            let removed = self.assignments.remove(pos);
            
            // Update time index
            if let Some(time_assignments) = self.time_index.get_mut(&removed.start_time) {
                time_assignments.retain(|&idx| idx != pos);
                if time_assignments.is_empty() {
                    self.time_index.remove(&removed.start_time);
                }
            }

            // Update indices after removal
            for time_assignments in self.time_index.values_mut() {
                for idx in time_assignments.iter_mut() {
                    if *idx > pos {
                        *idx -= 1;
                    }
                }
            }

            Some(removed)
        } else {
            None
        }
    }

    /// Get assignments active at a specific time
    pub fn assignments_at_time(&self, time: u64) -> Vec<&Assignment> {
        self.assignments
            .iter()
            .filter(|a| a.start_time <= time && time < a.end_time())
            .collect()
    }

    /// Get assignments in a time range
    pub fn assignments_in_range(&self, start: u64, end: u64) -> Vec<&Assignment> {
        self.assignments
            .iter()
            .filter(|a| a.start_time < end && a.end_time() > start)
            .collect()
    }

    /// Find next available time slot for a job
    pub fn find_next_available_time(&self, duration: u64, required_qubits: &[usize]) -> u64 {
        let mut time = 0;
        let time_step = 1000; // 1ms granularity

        loop {
            let active_assignments = self.assignments_at_time(time);
            let used_qubits: std::collections::HashSet<usize> = active_assignments
                .iter()
                .flat_map(|a| a.all_used_qubits())
                .collect();

            // Check if all required qubits are available
            if required_qubits.iter().all(|&qubit| !used_qubits.contains(&qubit)) {
                return time;
            }

            time += time_step;
            
            // Safety check to avoid infinite loops
            if time > 1_000_000_000 { // 1000 seconds
                break;
            }
        }

        time
    }

    /// Optimize assignments to reduce conflicts and improve utilization
    pub fn optimize_assignments(&mut self) -> Result<()> {
        // Sort assignments by start time
        self.assignments.sort_by_key(|a| a.start_time);
        
        // Rebuild time index
        self.time_index.clear();
        for (i, assignment) in self.assignments.iter().enumerate() {
            self.time_index
                .entry(assignment.start_time)
                .or_insert_with(Vec::new)
                .push(i);
        }

        // Try to compress timeline by moving assignments earlier when possible
        for i in 0..self.assignments.len() {
            let current_assignment = &self.assignments[i];
            let required_qubits = current_assignment.all_used_qubits();
            
            let earliest_start = self.find_earliest_start_time(
                current_assignment.duration,
                &required_qubits,
                i
            );

            if earliest_start < current_assignment.start_time {
                // Move assignment to earlier time
                let mut updated_assignment = current_assignment.clone();
                updated_assignment.start_time = earliest_start;
                self.assignments[i] = updated_assignment;
            }
        }

        // Rebuild time index after optimization
        self.time_index.clear();
        for (i, assignment) in self.assignments.iter().enumerate() {
            self.time_index
                .entry(assignment.start_time)
                .or_insert_with(Vec::new)
                .push(i);
        }

        Ok(())
    }

    /// Find earliest start time for an assignment, excluding a specific index
    fn find_earliest_start_time(
        &self,
        duration: u64,
        required_qubits: &[usize],
        exclude_idx: usize,
    ) -> u64 {
        let mut time = 0;
        let time_step = 1000;

        loop {
            let active_assignments: Vec<&Assignment> = self.assignments
                .iter()
                .enumerate()
                .filter(|(i, a)| *i != exclude_idx && a.start_time <= time && time < a.end_time())
                .map(|(_, a)| a)
                .collect();

            let used_qubits: std::collections::HashSet<usize> = active_assignments
                .iter()
                .flat_map(|a| a.all_used_qubits())
                .collect();

            if required_qubits.iter().all(|&qubit| !used_qubits.contains(&qubit)) {
                return time;
            }

            time += time_step;

            if time > 1_000_000_000 {
                break;
            }
        }

        time
    }

    /// Get all assignments
    pub fn assignments(&self) -> &[Assignment] {
        &self.assignments
    }

    /// Get assignment count
    pub fn count(&self) -> usize {
        self.assignments.len()
    }

    /// Clear all assignments
    pub fn clear(&mut self) {
        self.assignments.clear();
        self.time_index.clear();
    }

    /// Calculate overall utilization statistics
    pub fn utilization_stats(&self, total_qubits: usize) -> OverallUtilization {
        if self.assignments.is_empty() {
            return OverallUtilization::default();
        }

        let max_time = self.assignments
            .iter()
            .map(|a| a.end_time())
            .max()
            .unwrap_or(0);

        let mut total_qubit_time = 0u64;
        let mut total_buffer_time = 0u64;

        for assignment in &self.assignments {
            total_qubit_time += assignment.qubit_mapping.len() as u64 * assignment.duration;
            total_buffer_time += assignment.resource_allocation.buffer_qubits.len() as u64 * assignment.duration;
        }

        let total_possible_time = total_qubits as u64 * max_time;
        let utilization = if total_possible_time > 0 {
            total_qubit_time as f64 / total_possible_time as f64
        } else {
            0.0
        };

        let buffer_overhead = if total_possible_time > 0 {
            total_buffer_time as f64 / total_possible_time as f64
        } else {
            0.0
        };

        OverallUtilization {
            average_utilization: utilization,
            peak_utilization: 0.0, // Would need time-series analysis
            buffer_overhead,
            efficiency: utilization / (utilization + buffer_overhead + f64::EPSILON),
            total_assignments: self.assignments.len(),
        }
    }
}

impl Default for AssignmentManager {
    fn default() -> Self {
        Self::new()
    }
}

/// Overall utilization statistics
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct OverallUtilization {
    /// Average resource utilization
    pub average_utilization: f64,
    /// Peak resource utilization
    pub peak_utilization: f64,
    /// Buffer overhead
    pub buffer_overhead: f64,
    /// Overall efficiency
    pub efficiency: f64,
    /// Total number of assignments
    pub total_assignments: usize,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_assignment_creation() {
        let assignment = Assignment::new(0, 1, 1000, 5000)
            .with_qubit_mapping(vec![0, 1, 2])
            .with_classical_mapping(vec![0, 1]);

        assert_eq!(assignment.job_id, 0);
        assert_eq!(assignment.tile_id, 1);
        assert_eq!(assignment.end_time(), 6000);
        assert_eq!(assignment.qubit_mapping.len(), 3);
    }

    #[test]
    fn test_assignment_conflicts() {
        let assignment1 = Assignment::new(0, 1, 1000, 2000)
            .with_qubit_mapping(vec![0, 1]);
        let assignment2 = Assignment::new(1, 2, 1500, 2000)
            .with_qubit_mapping(vec![1, 2]);

        assert!(assignment1.conflicts_with(&assignment2));
    }

    #[test]
    fn test_assignment_manager() {
        let mut manager = AssignmentManager::new();
        
        let assignment = Assignment::new(0, 1, 1000, 2000)
            .with_qubit_mapping(vec![0, 1]);

        manager.add_assignment(assignment).unwrap();
        assert_eq!(manager.count(), 1);

        let active = manager.assignments_at_time(1500);
        assert_eq!(active.len(), 1);

        let removed = manager.remove_assignment(0);
        assert!(removed.is_some());
        assert_eq!(manager.count(), 0);
    }

    #[test]
    fn test_next_available_time() {
        let mut manager = AssignmentManager::new();
        
        let assignment = Assignment::new(0, 1, 1000, 2000)
            .with_qubit_mapping(vec![0, 1]);
        manager.add_assignment(assignment).unwrap();

        // Should find time after existing assignment
        let next_time = manager.find_next_available_time(1000, &[0]);
        assert!(next_time >= 3000);

        // Different qubits should be available immediately
        let next_time = manager.find_next_available_time(1000, &[2, 3]);
        assert_eq!(next_time, 0);
    }
}