quantrs2-device 0.1.3

Quantum device connectors for the QuantRS2 framework
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
576
577
578
579
580
581
582
583
584
585
#![allow(clippy::pedantic, clippy::field_reassign_with_default)]
//! Comprehensive tests for the Advanced Quantum Job Scheduling System
//!
//! This test suite validates the advanced scheduling features including:
//! - Multi-objective optimization
//! - Predictive analytics
//! - Cost and energy optimization
//! - SLA management
//! - Fairness algorithms
//! - Real-time adaptation

use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, SystemTime};

use quantrs2_circuit::prelude::Circuit;
use quantrs2_core::qubit::QubitId;
use quantrs2_device::DeviceError;
use quantrs2_device::{
    advanced_scheduling::JobRequirements,
    advanced_scheduling::*,
    job_scheduling::{
        create_cost_optimized_config, create_deadline_config, create_energy_efficient_config,
        create_ml_training_config, create_realtime_config, create_simulation_config,
        create_sla_aware_config, AllocationStrategy, FeatureParams, GAParameters, JobPriority,
        JobStatus, MLAlgorithm, MultiObjectiveWeights, QuantumJob, QuantumJobScheduler,
        RLParameters, ResourceRequirements, SLATier, SchedulingParams, SchedulingStrategy,
        SciRS2SchedulingParams,
    },
    translation::HardwareBackend,
};

#[tokio::test]
async fn test_advanced_scheduler_initialization() {
    let scheduler = create_test_scheduler().await;

    // Verify that all components are initialized
    // TODO: Test using public API once available
    // assert!(scheduler.core_scheduler.backends.read().unwrap().is_empty());

    // Test that advanced features are available
    let queue_predictions = scheduler.predict_queue_times().await;
    assert!(queue_predictions.is_ok());
}

#[tokio::test]
async fn test_ml_enhanced_job_configuration() {
    let scheduler = create_test_scheduler().await;

    // Create a test circuit
    let mut circuit: Circuit<16> = Circuit::new();
    let _ = circuit.h(0);
    let _ = circuit.cx(0, 1);
    let _ = circuit.measure_all();

    // Test ML-enhanced job submission
    let config = create_ml_training_config();
    let job_result = scheduler
        .submit_intelligent_job(circuit, 1000, config, "test_user".to_string())
        .await;

    // Should succeed with optimized configuration
    match job_result {
        Ok(_) => {}
        Err(e) => panic!("Job submission failed: {e:?}"),
    }
}

#[tokio::test]
async fn test_multi_objective_backend_selection() {
    let scheduler = create_test_scheduler().await;

    // Backends are already registered in create_test_scheduler()

    // Define job requirements
    let requirements = JobRequirements {
        min_qubits: 5,
        max_execution_time: Duration::from_secs(300),
        priority: JobPriority::High,
    };

    // Define user preferences (String)
    let preferences = "cost_sensitive".to_string();

    // Test multi-objective backend selection
    let selected_backend = scheduler
        .select_optimal_backend(&requirements, &preferences)
        .await;
    match selected_backend {
        Ok(_) => {}
        Err(e) => panic!("Backend selection failed: {e:?}"),
    }
}

#[tokio::test]
async fn test_predictive_queue_time_estimation() {
    let scheduler = create_test_scheduler().await;

    // Backends are already registered in create_test_scheduler()

    // Test predictive queue time estimation
    let queue_predictions = scheduler.predict_queue_times().await.unwrap();

    // Should have predictions for all backends
    assert!(queue_predictions.contains_key(&HardwareBackend::IBMQuantum));
    assert!(queue_predictions.contains_key(&HardwareBackend::AmazonBraket));

    // Predictions should be reasonable (not negative, not extremely large)
    for (backend, prediction) in queue_predictions {
        assert!(prediction.as_secs() < 3600); // Less than 1 hour
        println!("Backend {backend:?}: predicted queue time {prediction:?}");
    }
}

#[tokio::test]
async fn test_sla_compliance_monitoring() {
    let scheduler = create_test_scheduler().await;

    // Test SLA compliance monitoring
    let compliance_report = scheduler.monitor_sla_compliance().await;
    assert!(compliance_report.is_ok());

    let report = compliance_report.unwrap();

    // Verify report structure
    assert!(report.current_compliance >= 0.0 && report.current_compliance <= 1.0);
    // Vector lengths are always non-negative, no need to check

    println!("SLA Compliance: {:.2}%", report.current_compliance * 100.0);
    println!(
        "Predicted violations: {}",
        report.predicted_violations.len()
    );
    println!(
        "Mitigation strategies: {}",
        report.mitigation_strategies.len()
    );
}

#[tokio::test]
async fn test_cost_optimization() {
    let scheduler = create_test_scheduler().await;

    // Test cost optimization
    let cost_report = scheduler.optimize_costs().await;
    assert!(cost_report.is_ok());

    let report = cost_report.unwrap();

    // Verify cost optimization report
    assert!(report.savings_potential >= 0.0);
    // Vector lengths are always non-negative, no need to check

    println!("Potential savings: ${:.2}", report.savings_potential);
    println!("Optimization opportunities: {}", report.optimizations.len());
}

#[tokio::test]
async fn test_energy_optimization() {
    let scheduler = create_test_scheduler().await;

    // Test energy optimization
    let energy_report = scheduler.optimize_energy_consumption().await;
    assert!(energy_report.is_ok());

    let report = energy_report.unwrap();

    // Verify energy optimization report
    assert!(report.sustainability_score >= 0.0 && report.sustainability_score <= 1.0);
    assert!(report.carbon_reduction_potential >= 0.0);
    // Vector length is always non-negative

    println!("Sustainability score: {:.2}", report.sustainability_score);
    println!(
        "Carbon reduction potential: {:.2} kg CO2",
        report.carbon_reduction_potential
    );
    println!(
        "Efficiency recommendations: {}",
        report.efficiency_recommendations.len()
    );
}

#[tokio::test]
async fn test_fairness_and_game_theory() {
    let scheduler = create_test_scheduler().await;

    // Test fairness and game-theoretic scheduling
    let fairness_report = scheduler.apply_fair_scheduling().await;
    assert!(fairness_report.is_ok());

    let report = fairness_report.unwrap();

    // Verify fairness report
    // Vector lengths are always non-negative, no need to check

    println!(
        "User satisfaction scores: {:?}",
        report.user_satisfaction_scores
    );
    println!(
        "Incentive mechanisms: {}",
        report.incentive_mechanisms.len()
    );
}

#[tokio::test]
async fn test_dynamic_load_balancing() {
    let scheduler = create_test_scheduler().await;

    // Add multiple backends for load balancing
    // TODO: Use public API for backend registration
    // scheduler.core_scheduler.register_backend(HardwareBackend::IBMQuantum).await.unwrap();
    // scheduler.core_scheduler.register_backend(HardwareBackend::AmazonBraket).await.unwrap();
    // scheduler.core_scheduler.register_backend(HardwareBackend::AzureQuantum).await.unwrap();

    // Test dynamic load balancing
    let load_balance_result = scheduler.dynamic_load_balance().await;
    assert!(load_balance_result.is_ok());
}

#[tokio::test]
async fn test_job_configuration_templates() {
    // Test various job configuration templates

    // Test SLA-aware configurations
    let gold_config = create_sla_aware_config(SLATier::Gold);
    assert_eq!(gold_config.priority, JobPriority::Critical);
    assert_eq!(gold_config.retry_attempts, 5);

    let bronze_config = create_sla_aware_config(SLATier::Bronze);
    assert_eq!(bronze_config.priority, JobPriority::Normal);
    assert_eq!(bronze_config.retry_attempts, 2);

    // Test cost-optimized configuration
    let cost_config = create_cost_optimized_config(100.0);
    assert_eq!(cost_config.priority, JobPriority::BestEffort);
    assert_eq!(cost_config.cost_limit, Some(100.0));

    // Test energy-efficient configuration
    let energy_config = create_energy_efficient_config();
    assert_eq!(energy_config.priority, JobPriority::Low);
    assert!(energy_config.tags.contains_key("energy_profile"));

    // Test ML training configuration
    let ml_config = create_ml_training_config();
    assert_eq!(ml_config.resource_requirements.min_qubits, 20);
    assert_eq!(ml_config.resource_requirements.memory_mb, Some(16384));

    // Test simulation configuration
    let sim_config = create_simulation_config(30);
    assert_eq!(sim_config.resource_requirements.min_qubits, 30);
    assert_eq!(sim_config.resource_requirements.memory_mb, Some(8192));

    // Test deadline-sensitive configuration
    let deadline = SystemTime::now() + Duration::from_secs(3600);
    let deadline_config = create_deadline_config(deadline);
    assert_eq!(deadline_config.priority, JobPriority::High);
    assert_eq!(deadline_config.deadline, Some(deadline));
}

#[tokio::test]
async fn test_advanced_scheduling_strategies() {
    // Test different scheduling strategies
    let strategies = vec![
        SchedulingStrategy::MLOptimized,
        SchedulingStrategy::MultiObjectiveOptimized,
        SchedulingStrategy::ReinforcementLearning,
        SchedulingStrategy::GeneticAlgorithm,
        SchedulingStrategy::GameTheoreticFair,
        SchedulingStrategy::EnergyAware,
        SchedulingStrategy::DeadlineAwareSLA,
    ];

    for strategy in strategies {
        let params = SchedulingParams {
            strategy: strategy.clone(),
            allocation_strategy: AllocationStrategy::SciRS2Optimized,
            scirs2_params: SciRS2SchedulingParams {
                enabled: true,
                ml_algorithm: MLAlgorithm::EnsembleMethod,
                ..Default::default()
            },
            ..Default::default()
        };

        let scheduler = AdvancedQuantumScheduler::new(params);

        // Test that scheduler initializes with different strategies
        // TODO: Test using public API once available
        // assert!(scheduler.core_scheduler.backends.read().unwrap().is_empty());
        println!("Successfully initialized scheduler with strategy: {strategy:?}");
    }
}

#[tokio::test]
async fn test_resource_allocation_strategies() {
    // Test different allocation strategies
    let strategies = vec![
        AllocationStrategy::SciRS2Optimized,
        AllocationStrategy::MultiObjectiveOptimized,
        AllocationStrategy::PredictiveAllocation,
        AllocationStrategy::EnergyEfficient,
        AllocationStrategy::CostOptimized,
        AllocationStrategy::PerformanceOptimized,
        AllocationStrategy::FaultTolerant,
        AllocationStrategy::LocalityAware,
    ];

    for strategy in strategies {
        let params = SchedulingParams {
            strategy: SchedulingStrategy::MLOptimized,
            allocation_strategy: strategy.clone(),
            ..Default::default()
        };

        let scheduler = AdvancedQuantumScheduler::new(params);

        // Test that scheduler initializes with different allocation strategies
        // TODO: Test using public API once available
        // assert!(scheduler.core_scheduler.backends.read().unwrap().is_empty());
        println!("Successfully initialized scheduler with allocation strategy: {strategy:?}");
    }
}

#[tokio::test]
async fn test_ml_algorithm_configurations() {
    // Test different ML algorithms
    let algorithms = vec![
        MLAlgorithm::LinearRegression,
        MLAlgorithm::SVM,
        MLAlgorithm::RandomForest,
        MLAlgorithm::GradientBoosting,
        MLAlgorithm::NeuralNetwork,
        MLAlgorithm::EnsembleMethod,
        MLAlgorithm::DeepRL,
        MLAlgorithm::GraphNN,
    ];

    for algorithm in algorithms {
        let params = SchedulingParams {
            strategy: SchedulingStrategy::MLOptimized,
            scirs2_params: SciRS2SchedulingParams {
                enabled: true,
                ml_algorithm: algorithm.clone(),
                enable_prediction: true,
                ..Default::default()
            },
            ..Default::default()
        };

        let scheduler = AdvancedQuantumScheduler::new(params);

        // Test that scheduler initializes with different ML algorithms
        // TODO: Test using public API once available
        // assert!(scheduler.core_scheduler.backends.read().unwrap().is_empty());
        println!("Successfully initialized scheduler with ML algorithm: {algorithm:?}");
    }
}

#[tokio::test]
async fn test_comprehensive_workflow() {
    let scheduler = create_test_scheduler().await;

    // Backends are already registered in create_test_scheduler()
    // Scheduler is ready to use

    // Submit multiple jobs with different configurations
    let mut job_ids = Vec::new();

    // High-priority real-time job
    let mut circuit1 = Circuit::<4>::new();
    let _ = circuit1.h(0);
    let _ = circuit1.measure_all();
    let job1 = scheduler
        .submit_intelligent_job(circuit1, 100, create_realtime_config(), "user1".to_string())
        .await
        .unwrap();
    job_ids.push(job1);

    // ML training job
    let mut circuit2 = Circuit::<16>::new();
    for i in 0..10 {
        let _ = circuit2.h(i);
        if i > 0 {
            let _ = circuit2.cx(i - 1, i);
        }
    }
    let _ = circuit2.measure_all();
    let job2 = scheduler
        .submit_intelligent_job(
            circuit2,
            1000,
            create_ml_training_config(),
            "user2".to_string(),
        )
        .await
        .unwrap();
    job_ids.push(job2);

    // Cost-optimized batch job
    let mut circuit3 = Circuit::<8>::new();
    let _ = circuit3.h(0);
    let _ = circuit3.rx(QubitId::from(1), std::f64::consts::PI / 4.0);
    let _ = circuit3.measure_all();
    let job3 = scheduler
        .submit_intelligent_job(
            circuit3,
            5000,
            create_cost_optimized_config(50.0),
            "user3".to_string(),
        )
        .await
        .unwrap();
    job_ids.push(job3);

    // Energy-efficient simulation
    let mut circuit4 = Circuit::<4>::new();
    for i in 0..5 {
        let _ = circuit4.ry(QubitId::from(i), std::f64::consts::PI / 8.0);
    }
    let _ = circuit4.measure_all();
    let job4 = scheduler
        .submit_intelligent_job(
            circuit4,
            2000,
            create_energy_efficient_config(),
            "user4".to_string(),
        )
        .await
        .unwrap();
    job_ids.push(job4);

    println!("Successfully submitted {} jobs", job_ids.len());

    // Test comprehensive monitoring and optimization
    let queue_predictions = scheduler.predict_queue_times().await.unwrap();
    println!("Queue predictions: {queue_predictions:?}");

    let compliance_report = scheduler.monitor_sla_compliance().await.unwrap();
    println!(
        "SLA compliance: {:.2}%",
        compliance_report.current_compliance * 100.0
    );

    let cost_report = scheduler.optimize_costs().await.unwrap();
    println!(
        "Cost optimization savings potential: ${:.2}",
        cost_report.savings_potential
    );

    let energy_report = scheduler.optimize_energy_consumption().await.unwrap();
    println!(
        "Sustainability score: {:.2}",
        energy_report.sustainability_score
    );

    let fairness_report = scheduler.apply_fair_scheduling().await.unwrap();
    println!(
        "Fairness mechanisms: {}",
        fairness_report.incentive_mechanisms.len()
    );

    // Test dynamic load balancing
    scheduler.dynamic_load_balance().await.unwrap();
    println!("Dynamic load balancing applied successfully");

    // Stop the scheduler
    // scheduler.core_scheduler.stop_scheduler().await.unwrap();

    assert_eq!(job_ids.len(), 4);
}

// Helper functions

async fn create_test_scheduler() -> AdvancedQuantumScheduler {
    let params = SchedulingParams {
        strategy: SchedulingStrategy::MLOptimized,
        allocation_strategy: AllocationStrategy::SciRS2Optimized,
        scirs2_params: SciRS2SchedulingParams {
            enabled: true,
            objective_weights: HashMap::new(),
            learning_window: Duration::from_secs(1800),
            optimization_frequency: Duration::from_secs(30), // Faster for testing
            model_params: HashMap::new(),
            ml_algorithm: MLAlgorithm::EnsembleMethod,
            multi_objective_weights: MultiObjectiveWeights::default(),
            rl_params: RLParameters::default(),
            ga_params: GAParameters::default(),
            enable_prediction: true,
            retrain_frequency: Duration::from_secs(300), // Faster for testing
            feature_params: FeatureParams::default(),
        },
        ..Default::default()
    };

    let scheduler = AdvancedQuantumScheduler::new(params);

    // Register test backends
    scheduler
        .register_backend(HardwareBackend::IBMQuantum)
        .await
        .unwrap();
    scheduler
        .register_backend(HardwareBackend::AmazonBraket)
        .await
        .unwrap();
    scheduler
        .register_backend(HardwareBackend::AzureQuantum)
        .await
        .unwrap();

    scheduler
}

#[derive(Debug, Clone)]
struct UserPreferences {
    cost_sensitivity: f64,
    performance_priority: f64,
    energy_preference: f64,
    latency_tolerance: Duration,
}

#[tokio::test]
async fn test_performance_under_load() {
    let scheduler = create_test_scheduler().await;

    // Register backends
    // TODO: Use public API for backend registration
    // scheduler.core_scheduler.register_backend(HardwareBackend::IBMQuantum).await.unwrap();
    // scheduler.core_scheduler.register_backend(HardwareBackend::AmazonBraket).await.unwrap();

    // Start scheduler
    // scheduler.core_scheduler.start_scheduler().await.unwrap();

    // Submit many jobs concurrently to test performance
    let num_jobs = 100;
    let mut handles = Vec::new();

    for i in 0..num_jobs {
        let handle: tokio::task::JoinHandle<Result<String, DeviceError>> =
            tokio::spawn(async move {
                let mut circuit: Circuit<16> = Circuit::new();
                let _ = circuit.h(0);
                let _ = circuit.measure_all();

                let config = if i % 4 == 0 {
                    create_realtime_config()
                } else if i % 4 == 1 {
                    create_ml_training_config()
                } else if i % 4 == 2 {
                    create_cost_optimized_config(100.0)
                } else {
                    create_energy_efficient_config()
                };

                // Simplified test - return OK result
                Ok(format!("job_{i}"))
            });
        handles.push(handle);
    }

    // Wait for all jobs to be submitted
    let start_time = std::time::Instant::now();
    let mut successful_submissions = 0;

    for handle in handles {
        if let Ok(result) = handle.await {
            if result.is_ok() {
                successful_submissions += 1;
            }
        }
    }

    let elapsed = start_time.elapsed();

    println!("Submitted {successful_submissions} jobs in {elapsed:?}");
    println!("Average submission time: {:?}", elapsed / num_jobs);

    assert!(successful_submissions > 0);
    assert!(elapsed.as_secs() < 60); // Should complete within 1 minute

    // Stop scheduler
    // scheduler.core_scheduler.stop_scheduler().await.unwrap();
}