quantrs2-anneal 0.1.3

Quantum annealing support 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
//! Cross-platform validation system

use super::{
    ApplicationError, ApplicationResult, Duration, ExpectedMetrics, HashMap, PlatformAvailability,
    PlatformConfig, PlatformType, ProblemSpecification, ProblemType, TestExecutionResult,
};

/// Cross-platform validation system
#[derive(Debug)]
pub struct CrossPlatformValidator {
    /// Supported platforms
    pub platforms: Vec<Platform>,
    /// Cross-platform test suites
    pub test_suites: HashMap<String, CrossPlatformTestSuite>,
    /// Compatibility matrix
    pub compatibility_matrix: CompatibilityMatrix,
    /// Platform-specific configurations
    pub platform_configs: HashMap<String, PlatformConfig>,
}

/// Platform specification
#[derive(Debug, Clone)]
pub struct Platform {
    /// Platform identifier
    pub id: String,
    /// Platform type
    pub platform_type: PlatformType,
    /// Availability status
    pub availability: PlatformAvailability,
    /// Capabilities
    pub capabilities: PlatformCapabilities,
    /// Performance characteristics
    pub performance: PlatformPerformance,
}

/// Platform capabilities
#[derive(Debug, Clone)]
pub struct PlatformCapabilities {
    /// Maximum problem size
    pub max_problem_size: usize,
    /// Supported problem types
    pub supported_types: Vec<ProblemType>,
    /// Native constraints support
    pub native_constraints: bool,
    /// Embedding required
    pub requires_embedding: bool,
}

/// Platform performance characteristics
#[derive(Debug, Clone)]
pub struct PlatformPerformance {
    /// Typical runtime range
    pub runtime_range: (Duration, Duration),
    /// Solution quality range
    pub quality_range: (f64, f64),
    /// Reliability score
    pub reliability: f64,
    /// Cost per problem
    pub cost_per_problem: Option<f64>,
}

/// Cross-platform test suite
#[derive(Debug)]
pub struct CrossPlatformTestSuite {
    /// Suite identifier
    pub id: String,
    /// Test cases in suite
    pub test_cases: Vec<CrossPlatformTestCase>,
    /// Comparison criteria
    pub comparison_criteria: Vec<ComparisonCriterion>,
    /// Expected differences
    pub expected_differences: HashMap<String, ExpectedDifference>,
}

/// Cross-platform test case
#[derive(Debug, Clone)]
pub struct CrossPlatformTestCase {
    /// Test case identifier
    pub id: String,
    /// Problem specification
    pub problem: ProblemSpecification,
    /// Platform-specific parameters
    pub platform_params: HashMap<String, PlatformSpecificParams>,
    /// Expected results per platform
    pub expected_results: HashMap<String, ExpectedMetrics>,
}

/// Platform-specific parameters
#[derive(Debug, Clone)]
pub struct PlatformSpecificParams {
    /// Annealing parameters
    pub annealing_params: HashMap<String, f64>,
    /// Solver settings
    pub solver_settings: HashMap<String, String>,
    /// Resource limits
    pub resource_limits: HashMap<String, f64>,
}

/// Comparison criterion for cross-platform validation
#[derive(Debug, Clone)]
pub struct ComparisonCriterion {
    /// Criterion identifier
    pub id: String,
    /// Metric to compare
    pub metric: String,
    /// Comparison type
    pub comparison_type: ComparisonType,
    /// Tolerance for differences
    pub tolerance: f64,
    /// Whether this is a critical criterion
    pub critical: bool,
}

/// Types of comparisons
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ComparisonType {
    /// Absolute difference
    AbsoluteDifference,
    /// Relative difference
    RelativeDifference,
    /// Statistical equivalence
    StatisticalEquivalence,
    /// Ranking comparison
    Ranking,
}

/// Expected difference between platforms
#[derive(Debug, Clone)]
pub struct ExpectedDifference {
    /// Platform pair
    pub platform_pair: (String, String),
    /// Expected difference range
    pub difference_range: (f64, f64),
    /// Reason for difference
    pub reason: String,
    /// Whether difference is acceptable
    pub acceptable: bool,
}

/// Compatibility matrix
#[derive(Debug)]
pub struct CompatibilityMatrix {
    /// Feature compatibility between platforms
    pub feature_compatibility: HashMap<String, HashMap<String, CompatibilityLevel>>,
    /// Performance compatibility
    pub performance_compatibility: HashMap<String, HashMap<String, f64>>,
    /// Known issues between platforms
    pub known_issues: HashMap<String, Vec<CompatibilityIssue>>,
}

/// Compatibility levels
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CompatibilityLevel {
    /// Fully compatible
    Full,
    /// Partially compatible
    Partial,
    /// Incompatible
    Incompatible,
    /// Unknown compatibility
    Unknown,
}

/// Compatibility issue
#[derive(Debug, Clone)]
pub struct CompatibilityIssue {
    /// Issue identifier
    pub id: String,
    /// Issue description
    pub description: String,
    /// Severity level
    pub severity: IssueSeverity,
    /// Workaround available
    pub workaround: Option<String>,
    /// Affected features
    pub affected_features: Vec<String>,
}

/// Issue severity levels
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum IssueSeverity {
    /// Critical issue
    Critical,
    /// Major issue
    Major,
    /// Minor issue
    Minor,
    /// Cosmetic issue
    Cosmetic,
}

impl CrossPlatformValidator {
    #[must_use]
    pub fn new() -> Self {
        Self {
            platforms: Self::create_default_platforms(),
            test_suites: HashMap::new(),
            compatibility_matrix: CompatibilityMatrix {
                feature_compatibility: HashMap::new(),
                performance_compatibility: HashMap::new(),
                known_issues: HashMap::new(),
            },
            platform_configs: HashMap::new(),
        }
    }

    /// Create default platform configurations
    fn create_default_platforms() -> Vec<Platform> {
        vec![
            Platform {
                id: "classical_simulator".to_string(),
                platform_type: PlatformType::Classical,
                availability: PlatformAvailability::Available,
                capabilities: PlatformCapabilities {
                    max_problem_size: 10_000,
                    supported_types: vec![
                        ProblemType::RandomIsing,
                        ProblemType::MaxCut,
                        ProblemType::VertexCover,
                        ProblemType::TSP,
                        ProblemType::Portfolio,
                    ],
                    native_constraints: true,
                    requires_embedding: false,
                },
                performance: PlatformPerformance {
                    runtime_range: (Duration::from_millis(1), Duration::from_secs(3600)),
                    quality_range: (0.8, 1.0),
                    reliability: 0.99,
                    cost_per_problem: Some(0.0),
                },
            },
            Platform {
                id: "dwave_simulator".to_string(),
                platform_type: PlatformType::DWave,
                availability: PlatformAvailability::RequiresAuth,
                capabilities: PlatformCapabilities {
                    max_problem_size: 5000,
                    supported_types: vec![ProblemType::RandomIsing, ProblemType::MaxCut],
                    native_constraints: false,
                    requires_embedding: true,
                },
                performance: PlatformPerformance {
                    runtime_range: (Duration::from_millis(20), Duration::from_secs(20)),
                    quality_range: (0.7, 0.95),
                    reliability: 0.95,
                    cost_per_problem: Some(0.00_037),
                },
            },
            Platform {
                id: "aws_braket".to_string(),
                platform_type: PlatformType::AWSBraket,
                availability: PlatformAvailability::RequiresAuth,
                capabilities: PlatformCapabilities {
                    max_problem_size: 2000,
                    supported_types: vec![ProblemType::RandomIsing, ProblemType::MaxCut],
                    native_constraints: false,
                    requires_embedding: true,
                },
                performance: PlatformPerformance {
                    runtime_range: (Duration::from_secs(1), Duration::from_secs(300)),
                    quality_range: (0.6, 0.9),
                    reliability: 0.92,
                    cost_per_problem: Some(0.001),
                },
            },
        ]
    }

    /// Add platform
    pub fn add_platform(&mut self, platform: Platform) {
        self.platforms.push(platform);
    }

    /// Get platform by ID
    #[must_use]
    pub fn get_platform(&self, platform_id: &str) -> Option<&Platform> {
        self.platforms.iter().find(|p| p.id == platform_id)
    }

    /// Add test suite
    pub fn add_test_suite(&mut self, suite: CrossPlatformTestSuite) {
        self.test_suites.insert(suite.id.clone(), suite);
    }

    /// Run cross-platform validation
    pub fn run_validation(
        &self,
        suite_id: &str,
    ) -> ApplicationResult<CrossPlatformValidationResult> {
        let suite = self.test_suites.get(suite_id).ok_or_else(|| {
            ApplicationError::ConfigurationError(format!("Test suite not found: {suite_id}"))
        })?;

        let mut platform_results = HashMap::new();
        let mut comparison_results = Vec::new();

        // Run tests on each available platform
        for platform in &self.platforms {
            if platform.availability == PlatformAvailability::Available {
                let results = self.run_suite_on_platform(suite, platform)?;
                platform_results.insert(platform.id.clone(), results);
            }
        }

        // Compare results across platforms
        for criterion in &suite.comparison_criteria {
            let comparison = self.compare_platforms(&platform_results, criterion)?;
            comparison_results.push(comparison);
        }

        // Calculate overall compatibility score
        let compatibility_score = self.calculate_compatibility_score(&comparison_results);

        Ok(CrossPlatformValidationResult {
            suite_id: suite_id.to_string(),
            platform_results,
            comparison_results,
            compatibility_score,
            validation_time: Duration::from_secs(60), // Simplified
        })
    }

    /// Run test suite on specific platform
    fn run_suite_on_platform(
        &self,
        suite: &CrossPlatformTestSuite,
        platform: &Platform,
    ) -> ApplicationResult<PlatformTestResults> {
        let mut test_results = HashMap::new();

        for test_case in &suite.test_cases {
            // Check if platform supports this test case
            if !self.is_test_supported(test_case, platform) {
                continue;
            }

            let result = self.run_test_case_on_platform(test_case, platform)?;
            test_results.insert(test_case.id.clone(), result);
        }

        Ok(PlatformTestResults {
            platform_id: platform.id.clone(),
            test_results,
            platform_info: platform.clone(),
            execution_time: Duration::from_secs(30), // Simplified
        })
    }

    /// Check if test is supported on platform
    fn is_test_supported(&self, test_case: &CrossPlatformTestCase, platform: &Platform) -> bool {
        platform
            .capabilities
            .supported_types
            .contains(&test_case.problem.problem_type)
    }

    /// Run individual test case on platform
    fn run_test_case_on_platform(
        &self,
        test_case: &CrossPlatformTestCase,
        platform: &Platform,
    ) -> ApplicationResult<TestExecutionResult> {
        // Simplified implementation - would interface with actual platform
        let base_quality = match platform.platform_type {
            PlatformType::Classical => 0.95,
            PlatformType::DWave => 0.85,
            PlatformType::AWSBraket => 0.80,
            PlatformType::FujitsuDA => 0.88,
            PlatformType::Custom(_) => 0.75,
        };

        let problem_size = usize::midpoint(
            test_case.problem.size_range.0,
            test_case.problem.size_range.1,
        );
        let size_factor = (problem_size as f64 / 1000.0).min(1.0);
        let quality = base_quality * (1.0 - size_factor * 0.2);

        Ok(TestExecutionResult {
            solution_quality: quality,
            execution_time: Duration::from_millis((problem_size as u64).min(5000)),
            final_energy: -quality * problem_size as f64,
            best_solution: vec![1; problem_size],
            convergence_achieved: true,
            memory_used: problem_size * 8,
        })
    }

    /// Compare results across platforms
    fn compare_platforms(
        &self,
        platform_results: &HashMap<String, PlatformTestResults>,
        criterion: &ComparisonCriterion,
    ) -> ApplicationResult<ComparisonResult> {
        let mut metric_values = HashMap::new();

        // Extract metric values from each platform
        for (platform_id, results) in platform_results {
            let values: Vec<f64> = results
                .test_results
                .values()
                .map(|result| self.extract_metric_value(result, &criterion.metric))
                .collect();

            if !values.is_empty() {
                let mean_value = values.iter().sum::<f64>() / values.len() as f64;
                metric_values.insert(platform_id.clone(), mean_value);
            }
        }

        // Calculate differences between platforms
        let mut differences = HashMap::new();
        let platforms: Vec<_> = metric_values.keys().collect();

        for i in 0..platforms.len() {
            for j in (i + 1)..platforms.len() {
                let platform1 = platforms[i];
                let platform2 = platforms[j];
                let value1 = metric_values[platform1];
                let value2 = metric_values[platform2];

                let difference = match criterion.comparison_type {
                    ComparisonType::AbsoluteDifference => (value1 - value2).abs(),
                    ComparisonType::RelativeDifference => {
                        ((value1 - value2) / value1.max(value2)).abs()
                    }
                    _ => (value1 - value2).abs(), // Simplified
                };

                let pair_key = format!("{platform1}_{platform2}");
                differences.insert(pair_key, difference);
            }
        }

        // Check if differences are within tolerance
        let max_difference = differences
            .values()
            .fold(0.0f64, |max, &diff| max.max(diff));
        let within_tolerance = max_difference <= criterion.tolerance;

        Ok(ComparisonResult {
            criterion_id: criterion.id.clone(),
            metric: criterion.metric.clone(),
            platform_values: metric_values,
            differences,
            max_difference,
            within_tolerance,
            critical: criterion.critical,
        })
    }

    /// Extract metric value from test result
    fn extract_metric_value(&self, result: &TestExecutionResult, metric: &str) -> f64 {
        match metric {
            "solution_quality" => result.solution_quality,
            "execution_time" => result.execution_time.as_secs_f64(),
            "final_energy" => result.final_energy,
            "memory_used" => result.memory_used as f64,
            _ => 0.0,
        }
    }

    /// Calculate overall compatibility score
    fn calculate_compatibility_score(&self, comparison_results: &[ComparisonResult]) -> f64 {
        if comparison_results.is_empty() {
            return 1.0;
        }

        let total_weight: f64 = comparison_results
            .iter()
            .map(|r| if r.critical { 2.0 } else { 1.0 })
            .sum();

        let weighted_score: f64 = comparison_results
            .iter()
            .map(|r| {
                let score = if r.within_tolerance { 1.0 } else { 0.0 };
                let weight = if r.critical { 2.0 } else { 1.0 };
                score * weight
            })
            .sum();

        weighted_score / total_weight
    }

    /// Get compatibility information between platforms
    #[must_use]
    pub fn get_compatibility(&self, platform1: &str, platform2: &str) -> CompatibilityInfo {
        let feature_compat = self
            .compatibility_matrix
            .feature_compatibility
            .get(platform1)
            .and_then(|map| map.get(platform2))
            .unwrap_or(&CompatibilityLevel::Unknown);

        let performance_compat = self
            .compatibility_matrix
            .performance_compatibility
            .get(platform1)
            .and_then(|map| map.get(platform2))
            .unwrap_or(&0.5);

        let default_issues = Vec::new();
        let issues = self
            .compatibility_matrix
            .known_issues
            .get(&format!("{platform1}_{platform2}"))
            .unwrap_or(&default_issues);

        CompatibilityInfo {
            platform_pair: (platform1.to_string(), platform2.to_string()),
            feature_compatibility: feature_compat.clone(),
            performance_compatibility: *performance_compat,
            known_issues: issues.clone(),
        }
    }
}

/// Result from cross-platform validation
#[derive(Debug)]
pub struct CrossPlatformValidationResult {
    /// Test suite identifier
    pub suite_id: String,
    /// Results from each platform
    pub platform_results: HashMap<String, PlatformTestResults>,
    /// Comparison results
    pub comparison_results: Vec<ComparisonResult>,
    /// Overall compatibility score
    pub compatibility_score: f64,
    /// Validation execution time
    pub validation_time: Duration,
}

/// Test results from a specific platform
#[derive(Debug)]
pub struct PlatformTestResults {
    /// Platform identifier
    pub platform_id: String,
    /// Individual test results
    pub test_results: HashMap<String, TestExecutionResult>,
    /// Platform information
    pub platform_info: Platform,
    /// Total execution time
    pub execution_time: Duration,
}

/// Result from platform comparison
#[derive(Debug)]
pub struct ComparisonResult {
    /// Comparison criterion identifier
    pub criterion_id: String,
    /// Metric being compared
    pub metric: String,
    /// Values from each platform
    pub platform_values: HashMap<String, f64>,
    /// Differences between platforms
    pub differences: HashMap<String, f64>,
    /// Maximum difference observed
    pub max_difference: f64,
    /// Whether differences are within tolerance
    pub within_tolerance: bool,
    /// Whether this is a critical criterion
    pub critical: bool,
}

/// Compatibility information between platforms
#[derive(Debug, Clone)]
pub struct CompatibilityInfo {
    /// Platform pair
    pub platform_pair: (String, String),
    /// Feature compatibility level
    pub feature_compatibility: CompatibilityLevel,
    /// Performance compatibility score
    pub performance_compatibility: f64,
    /// Known compatibility issues
    pub known_issues: Vec<CompatibilityIssue>,
}