quantrs2-tytan 0.1.3

High-level quantum annealing interface inspired by Tytan 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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
//! Core testing framework implementation.
//!
//! This module provides the main TestingFramework struct and its implementation
//! for running tests, managing test suites, and generating reports.

use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::{Duration, Instant};

use crate::sampler::Sampler;

use super::config::{ReportFormat, TestConfig};
use super::generators::{
    default_generators, FinanceTestGenerator, LogisticsTestGenerator, ManufacturingTestGenerator,
};
use super::reports;
use super::results::{ConvergenceData, MemoryStats, PerformanceData, RuntimeStats, TestResults};
use super::types::{
    CIReport, CIStatus, Difficulty, FailureType, GeneratorConfig, ProblemType, RegressionIssue,
    RegressionReport, SamplerComparison, TestCase, TestCategory, TestComparison, TestEnvironment,
    TestFailure, TestGenerator, TestResult, TestSuite, ValidationResult, Validator,
};
use super::validators::default_validators;

/// Automated testing framework
pub struct TestingFramework {
    /// Test configuration
    pub config: TestConfig,
    /// Test suite
    pub suite: TestSuite,
    /// Test results
    pub results: TestResults,
    /// Validators
    validators: Vec<Box<dyn Validator>>,
    /// Generators
    generators: Vec<Box<dyn TestGenerator>>,
}

impl TestingFramework {
    /// Create new testing framework
    pub fn new(config: TestConfig) -> Self {
        Self {
            config,
            suite: TestSuite {
                categories: Vec::new(),
                test_cases: Vec::new(),
                benchmarks: Vec::new(),
            },
            results: TestResults::default(),
            validators: default_validators(),
            generators: default_generators(),
        }
    }

    /// Add test category
    pub fn add_category(&mut self, category: TestCategory) {
        self.suite.categories.push(category);
    }

    /// Add custom generator
    pub fn add_generator(&mut self, generator: Box<dyn TestGenerator>) {
        self.generators.push(generator);
    }

    /// Add custom validator
    pub fn add_validator(&mut self, validator: Box<dyn Validator>) {
        self.validators.push(validator);
    }

    /// Generate test suite
    pub fn generate_suite(&mut self) -> Result<(), String> {
        let start_time = Instant::now();

        // Generate tests for each category
        for category in &self.suite.categories {
            for problem_type in &category.problem_types {
                for difficulty in &category.difficulties {
                    for size in &self.config.problem_sizes {
                        let config = GeneratorConfig {
                            problem_type: problem_type.clone(),
                            size: *size,
                            difficulty: difficulty.clone(),
                            seed: self.config.seed,
                            parameters: HashMap::new(),
                        };

                        // Find suitable generator
                        for generator in &self.generators {
                            if generator.supported_types().contains(problem_type) {
                                let test_cases = generator.generate(&config)?;
                                self.suite.test_cases.extend(test_cases);
                                break;
                            }
                        }
                    }
                }
            }
        }

        self.results.performance.runtime_stats.qubo_generation_time = start_time.elapsed();

        Ok(())
    }

    /// Run test suite
    pub fn run_suite<S: Sampler>(&mut self, sampler: &S) -> Result<(), String> {
        let total_start = Instant::now();

        let test_cases = self.suite.test_cases.clone();
        for test_case in &test_cases {
            let test_start = Instant::now();

            // Run test with timeout
            match self.run_single_test(test_case, sampler) {
                Ok(result) => {
                    self.results.test_results.push(result);
                    self.results.summary.passed += 1;
                }
                Err(e) => {
                    self.results.failures.push(TestFailure {
                        test_id: test_case.id.clone(),
                        failure_type: FailureType::SamplerError,
                        message: e,
                        stack_trace: None,
                        context: HashMap::new(),
                    });
                    self.results.summary.failed += 1;
                }
            }

            let test_time = test_start.elapsed();
            self.results
                .performance
                .runtime_stats
                .time_per_test
                .push((test_case.id.clone(), test_time));

            self.results.summary.total_tests += 1;
        }

        self.results.performance.runtime_stats.total_time = total_start.elapsed();
        self.calculate_summary();

        Ok(())
    }

    /// Run single test
    fn run_single_test<S: Sampler>(
        &mut self,
        test_case: &TestCase,
        sampler: &S,
    ) -> Result<TestResult, String> {
        let solve_start = Instant::now();

        // Run sampler
        let sample_result = sampler
            .run_qubo(
                &(test_case.qubo.clone(), test_case.var_map.clone()),
                self.config.samplers[0].num_samples,
            )
            .map_err(|e| format!("Sampler error: {e:?}"))?;

        let solve_time = solve_start.elapsed();

        // Get best solution
        let best_sample = sample_result
            .iter()
            .min_by(|a, b| {
                a.energy
                    .partial_cmp(&b.energy)
                    .unwrap_or(std::cmp::Ordering::Equal)
            })
            .ok_or("No samples returned")?;

        // Use the assignments directly (already decoded)
        let solution = best_sample.assignments.clone();

        // Validate
        let validation_start = Instant::now();
        let mut validation = ValidationResult {
            is_valid: true,
            checks: Vec::new(),
            warnings: Vec::new(),
        };

        for validator in &self.validators {
            let result = validator.validate(
                test_case,
                &TestResult {
                    test_id: test_case.id.clone(),
                    sampler: "test".to_string(),
                    solution: solution.clone(),
                    objective_value: best_sample.energy,
                    constraints_satisfied: true,
                    validation: validation.clone(),
                    runtime: solve_time,
                    metrics: HashMap::new(),
                },
            );

            validation.checks.extend(result.checks);
            validation.warnings.extend(result.warnings);
            validation.is_valid &= result.is_valid;
        }

        let validation_time = validation_start.elapsed();
        self.results.performance.runtime_stats.solving_time += solve_time;
        self.results.performance.runtime_stats.validation_time += validation_time;

        Ok(TestResult {
            test_id: test_case.id.clone(),
            sampler: self.config.samplers[0].name.clone(),
            solution,
            objective_value: best_sample.energy,
            constraints_satisfied: validation.is_valid,
            validation,
            runtime: solve_time + validation_time,
            metrics: HashMap::new(),
        })
    }

    /// Calculate summary statistics
    fn calculate_summary(&mut self) {
        if self.results.test_results.is_empty() {
            return;
        }

        // Success rate
        self.results.summary.success_rate =
            self.results.summary.passed as f64 / self.results.summary.total_tests as f64;

        // Average runtime
        let total_runtime: Duration = self.results.test_results.iter().map(|r| r.runtime).sum();
        self.results.summary.avg_runtime = total_runtime / self.results.test_results.len() as u32;

        // Quality metrics
        let qualities: Vec<f64> = self
            .results
            .test_results
            .iter()
            .map(|r| r.objective_value)
            .collect();

        self.results.summary.quality_metrics.avg_quality =
            qualities.iter().sum::<f64>() / qualities.len() as f64;

        self.results.summary.quality_metrics.best_quality = *qualities
            .iter()
            .min_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
            .unwrap_or(&0.0);

        self.results.summary.quality_metrics.worst_quality = *qualities
            .iter()
            .max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
            .unwrap_or(&0.0);

        // Standard deviation
        let mean = self.results.summary.quality_metrics.avg_quality;
        let variance =
            qualities.iter().map(|&x| (x - mean).powi(2)).sum::<f64>() / qualities.len() as f64;
        self.results.summary.quality_metrics.std_dev = variance.sqrt();

        // Constraint satisfaction rate
        let satisfied = self
            .results
            .test_results
            .iter()
            .filter(|r| r.constraints_satisfied)
            .count();
        self.results
            .summary
            .quality_metrics
            .constraint_satisfaction_rate =
            satisfied as f64 / self.results.test_results.len() as f64;
    }

    /// Generate report
    pub fn generate_report(&self) -> Result<String, String> {
        reports::generate_report(&self.config.output.format, &self.results, &self.suite)
    }

    /// Save report to file
    pub fn save_report(&self, filename: &str) -> Result<(), String> {
        let report = self.generate_report()?;
        reports::save_report(&report, filename)
    }

    /// Run regression tests against baseline
    pub fn run_regression_tests<S: Sampler>(
        &mut self,
        sampler: &S,
        baseline_file: &str,
    ) -> Result<RegressionReport, String> {
        // Load baseline results
        let baseline = self.load_baseline(baseline_file)?;

        // Run current tests
        self.run_suite(sampler)?;

        // Compare with baseline
        let mut regressions = Vec::new();
        let mut improvements = Vec::new();

        for current_result in &self.results.test_results {
            if let Some(baseline_result) = baseline
                .iter()
                .find(|b| b.test_id == current_result.test_id)
            {
                let quality_change = (current_result.objective_value
                    - baseline_result.objective_value)
                    / baseline_result.objective_value.abs();
                let runtime_change = (current_result.runtime.as_secs_f64()
                    - baseline_result.runtime.as_secs_f64())
                    / baseline_result.runtime.as_secs_f64();

                if quality_change > 0.05 || runtime_change > 0.2 {
                    regressions.push(RegressionIssue {
                        test_id: current_result.test_id.clone(),
                        metric: if quality_change > 0.05 {
                            "quality".to_string()
                        } else {
                            "runtime".to_string()
                        },
                        baseline_value: if quality_change > 0.05 {
                            baseline_result.objective_value
                        } else {
                            baseline_result.runtime.as_secs_f64()
                        },
                        current_value: if quality_change > 0.05 {
                            current_result.objective_value
                        } else {
                            current_result.runtime.as_secs_f64()
                        },
                        change_percent: if quality_change > 0.05 {
                            quality_change * 100.0
                        } else {
                            runtime_change * 100.0
                        },
                    });
                } else if quality_change < -0.05 || runtime_change < -0.2 {
                    improvements.push(RegressionIssue {
                        test_id: current_result.test_id.clone(),
                        metric: if quality_change < -0.05 {
                            "quality".to_string()
                        } else {
                            "runtime".to_string()
                        },
                        baseline_value: if quality_change < -0.05 {
                            baseline_result.objective_value
                        } else {
                            baseline_result.runtime.as_secs_f64()
                        },
                        current_value: if quality_change < -0.05 {
                            current_result.objective_value
                        } else {
                            current_result.runtime.as_secs_f64()
                        },
                        change_percent: if quality_change < -0.05 {
                            quality_change * 100.0
                        } else {
                            runtime_change * 100.0
                        },
                    });
                }
            }
        }

        Ok(RegressionReport {
            regressions,
            improvements,
            baseline_tests: baseline.len(),
            current_tests: self.results.test_results.len(),
        })
    }

    /// Load baseline results from file
    const fn load_baseline(&self, _filename: &str) -> Result<Vec<TestResult>, String> {
        // Simplified implementation - in practice would load from JSON/CSV
        Ok(Vec::new())
    }

    /// Run test suite in parallel
    pub fn run_suite_parallel<S: Sampler + Clone + Send + Sync + 'static>(
        &mut self,
        sampler: &S,
        num_threads: usize,
    ) -> Result<(), String> {
        let test_cases = Arc::new(self.suite.test_cases.clone());
        let results = Arc::new(Mutex::new(Vec::new()));
        let failures = Arc::new(Mutex::new(Vec::new()));

        let total_start = Instant::now();
        let chunk_size = test_cases.len().div_ceil(num_threads);

        let mut handles = Vec::new();

        for thread_id in 0..num_threads {
            let start_idx = thread_id * chunk_size;
            let end_idx = ((thread_id + 1) * chunk_size).min(test_cases.len());

            if start_idx >= test_cases.len() {
                break;
            }

            let test_cases_clone = Arc::clone(&test_cases);
            let results_clone = Arc::clone(&results);
            let failures_clone = Arc::clone(&failures);
            let sampler_clone = sampler.clone();

            let handle = thread::spawn(move || {
                for idx in start_idx..end_idx {
                    let test_case = &test_cases_clone[idx];

                    match Self::run_single_test_static(test_case, &sampler_clone) {
                        Ok(result) => {
                            if let Ok(mut guard) = results_clone.lock() {
                                guard.push(result);
                            }
                        }
                        Err(e) => {
                            if let Ok(mut guard) = failures_clone.lock() {
                                guard.push(TestFailure {
                                    test_id: test_case.id.clone(),
                                    failure_type: FailureType::SamplerError,
                                    message: e,
                                    stack_trace: None,
                                    context: HashMap::new(),
                                });
                            }
                        }
                    }
                }
            });

            handles.push(handle);
        }

        // Wait for all threads to complete
        for handle in handles {
            handle.join().map_err(|_| "Thread panic")?;
        }

        // Collect results
        self.results.test_results = results
            .lock()
            .map(|guard| guard.clone())
            .unwrap_or_default();
        self.results.failures = failures
            .lock()
            .map(|guard| guard.clone())
            .unwrap_or_default();

        self.results.performance.runtime_stats.total_time = total_start.elapsed();
        self.results.summary.passed = self.results.test_results.len();
        self.results.summary.failed = self.results.failures.len();
        self.results.summary.total_tests =
            self.results.summary.passed + self.results.summary.failed;

        self.calculate_summary();

        Ok(())
    }

    /// Static version of run_single_test for parallel execution
    fn run_single_test_static<S: Sampler>(
        test_case: &TestCase,
        sampler: &S,
    ) -> Result<TestResult, String> {
        let solve_start = Instant::now();

        // Run sampler
        let sample_result = sampler
            .run_qubo(&(test_case.qubo.clone(), test_case.var_map.clone()), 100)
            .map_err(|e| format!("Sampler error: {e:?}"))?;

        let solve_time = solve_start.elapsed();

        // Get best solution
        let best_sample = sample_result
            .iter()
            .min_by(|a, b| {
                a.energy
                    .partial_cmp(&b.energy)
                    .unwrap_or(std::cmp::Ordering::Equal)
            })
            .ok_or("No samples returned")?;

        let solution = best_sample.assignments.clone();

        Ok(TestResult {
            test_id: test_case.id.clone(),
            sampler: "parallel".to_string(),
            solution,
            objective_value: best_sample.energy,
            constraints_satisfied: true,
            validation: ValidationResult {
                is_valid: true,
                checks: Vec::new(),
                warnings: Vec::new(),
            },
            runtime: solve_time,
            metrics: HashMap::new(),
        })
    }

    /// Generate CI/CD report
    pub fn generate_ci_report(&self) -> Result<CIReport, String> {
        let passed_rate = if self.results.summary.total_tests > 0 {
            self.results.summary.passed as f64 / self.results.summary.total_tests as f64
        } else {
            0.0
        };

        let status = if passed_rate >= 0.95 {
            CIStatus::Pass
        } else if passed_rate >= 0.8 {
            CIStatus::Warning
        } else {
            CIStatus::Fail
        };

        Ok(CIReport {
            status,
            passed_rate,
            total_tests: self.results.summary.total_tests,
            failed_tests: self.results.summary.failed,
            critical_failures: self
                .results
                .failures
                .iter()
                .filter(|f| {
                    matches!(
                        f.failure_type,
                        FailureType::Timeout | FailureType::SamplerError
                    )
                })
                .count(),
            avg_runtime: self.results.summary.avg_runtime,
            quality_score: self.calculate_quality_score(),
        })
    }

    /// Calculate overall quality score
    fn calculate_quality_score(&self) -> f64 {
        if self.results.test_results.is_empty() {
            return 0.0;
        }

        let constraint_score = self
            .results
            .summary
            .quality_metrics
            .constraint_satisfaction_rate;
        let success_score = self.results.summary.success_rate;
        let quality_score = if self
            .results
            .summary
            .quality_metrics
            .best_quality
            .is_finite()
        {
            0.8 // Base score for having finite solutions
        } else {
            0.0
        };

        (constraint_score.mul_add(0.4, success_score * 0.4) + quality_score * 0.2) * 100.0
    }

    /// Add stress test cases
    pub fn add_stress_tests(&mut self) {
        let stress_categories = vec![
            TestCategory {
                name: "Large Scale Tests".to_string(),
                description: "Tests with large problem sizes".to_string(),
                problem_types: vec![ProblemType::MaxCut, ProblemType::TSP],
                difficulties: vec![Difficulty::Extreme],
                tags: vec!["stress".to_string(), "large".to_string()],
            },
            TestCategory {
                name: "Memory Stress Tests".to_string(),
                description: "Tests designed to stress memory usage".to_string(),
                problem_types: vec![ProblemType::Knapsack],
                difficulties: vec![Difficulty::VeryHard, Difficulty::Extreme],
                tags: vec!["stress".to_string(), "memory".to_string()],
            },
            TestCategory {
                name: "Runtime Stress Tests".to_string(),
                description: "Tests with challenging runtime requirements".to_string(),
                problem_types: vec![ProblemType::GraphColoring],
                difficulties: vec![Difficulty::Extreme],
                tags: vec!["stress".to_string(), "runtime".to_string()],
            },
        ];

        for category in stress_categories {
            self.suite.categories.push(category);
        }
    }

    /// Detect test environment
    pub fn detect_environment(&self) -> TestEnvironment {
        TestEnvironment {
            os: std::env::consts::OS.to_string(),
            cpu_model: "Unknown".to_string(), // Would need OS-specific detection
            memory_gb: 8.0,                   // Simplified - would need system detection
            gpu_info: None,
            rust_version: std::env::var("RUSTC_VERSION").unwrap_or_else(|_| "unknown".to_string()),
            compile_flags: vec!["--release".to_string()],
        }
    }

    /// Export test results for external analysis
    pub fn export_results(&self, format: &str) -> Result<String, String> {
        match format {
            "csv" => reports::export_csv(&self.results, &self.suite),
            "json" => reports::generate_json_report(&self.results),
            "xml" => reports::export_xml(&self.results),
            _ => Err(format!("Unsupported export format: {format}")),
        }
    }

    /// Add industry-specific test generators
    pub fn add_industry_generators(&mut self) {
        // Add finance test generator
        self.generators.push(Box::new(FinanceTestGenerator));

        // Add logistics test generator
        self.generators.push(Box::new(LogisticsTestGenerator));

        // Add manufacturing test generator
        self.generators.push(Box::new(ManufacturingTestGenerator));
    }

    /// Generate performance comparison report
    pub fn compare_samplers<S1: Sampler, S2: Sampler>(
        &mut self,
        sampler1: &S1,
        sampler2: &S2,
        sampler1_name: &str,
        sampler2_name: &str,
    ) -> Result<SamplerComparison, String> {
        // Run tests with first sampler
        self.run_suite(sampler1)?;
        let results1 = self.results.test_results.clone();

        // Clear results and run with second sampler
        self.results.test_results.clear();
        self.run_suite(sampler2)?;
        let results2 = self.results.test_results.clone();

        // Compare results
        let mut comparisons = Vec::new();

        for r1 in &results1 {
            if let Some(r2) = results2.iter().find(|r| r.test_id == r1.test_id) {
                let quality_diff = r2.objective_value - r1.objective_value;
                let runtime_ratio = r2.runtime.as_secs_f64() / r1.runtime.as_secs_f64();

                comparisons.push(TestComparison {
                    test_id: r1.test_id.clone(),
                    sampler1_quality: r1.objective_value,
                    sampler2_quality: r2.objective_value,
                    quality_improvement: -quality_diff, // Negative because lower is better
                    sampler1_runtime: r1.runtime,
                    sampler2_runtime: r2.runtime,
                    runtime_ratio,
                });
            }
        }

        let avg_quality_improvement = comparisons
            .iter()
            .map(|c| c.quality_improvement)
            .sum::<f64>()
            / comparisons.len() as f64;
        let avg_runtime_ratio =
            comparisons.iter().map(|c| c.runtime_ratio).sum::<f64>() / comparisons.len() as f64;

        Ok(SamplerComparison {
            sampler1_name: sampler1_name.to_string(),
            sampler2_name: sampler2_name.to_string(),
            test_comparisons: comparisons,
            avg_quality_improvement,
            avg_runtime_ratio,
            winner: if avg_quality_improvement > 0.0 {
                sampler2_name.to_string()
            } else {
                sampler1_name.to_string()
            },
        })
    }
}