quantrs2-anneal 0.2.1

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
//! Execution result types.
//!
//! This module contains types for representing execution results,
//! predictions, and metadata.

use std::collections::HashMap;
use std::time::{Duration, Instant};

use super::compilation::CompilationResult;
use super::config::{OptimizationLevel, ResourceAllocationStrategy, SchedulingPriority};
use super::platform::QuantumPlatform;

/// Universal execution result
#[derive(Debug, Clone)]
pub struct UniversalExecutionResult {
    /// Problem identifier
    pub problem_id: String,
    /// Selected optimal platform
    pub optimal_platform: QuantumPlatform,
    /// Compilation results for all platforms
    pub compilation_results: HashMap<QuantumPlatform, CompilationResult>,
    /// Performance predictions
    pub performance_predictions: HashMap<QuantumPlatform, PlatformPerformancePrediction>,
    /// Execution result
    pub execution_result: PlatformExecutionResult,
    /// Total execution time
    pub total_time: Duration,
    /// Execution metadata
    pub metadata: UniversalExecutionMetadata,
}

/// Platform performance prediction
#[derive(Debug, Clone)]
pub struct PlatformPerformancePrediction {
    /// Target platform
    pub platform: QuantumPlatform,
    /// Predicted performance
    pub predicted_performance: PredictedPerformance,
    /// Confidence in prediction
    pub confidence_score: f64,
    /// Prediction metadata
    pub prediction_metadata: PredictionMetadata,
}

/// Predicted performance
#[derive(Debug, Clone)]
pub struct PredictedPerformance {
    /// Execution time
    pub execution_time: Duration,
    /// Solution quality
    pub solution_quality: f64,
    /// Success probability
    pub success_probability: f64,
    /// Cost
    pub cost: f64,
    /// Reliability score
    pub reliability_score: f64,
}

/// Prediction metadata
#[derive(Debug, Clone)]
pub struct PredictionMetadata {
    /// Model version
    pub model_version: String,
    /// Prediction timestamp
    pub prediction_timestamp: Instant,
    /// Features used
    pub features_used: Vec<String>,
    /// Model accuracy
    pub model_accuracy: f64,
}

/// Optimal platform selection
#[derive(Debug, Clone)]
pub struct OptimalPlatformSelection {
    /// Selected platform
    pub platform: QuantumPlatform,
    /// Selection score
    pub selection_score: f64,
    /// Selection rationale
    pub selection_rationale: String,
    /// Alternative platforms
    pub alternatives: Vec<QuantumPlatform>,
    /// Selection metadata
    pub selection_metadata: SelectionMetadata,
}

/// Selection metadata
#[derive(Debug, Clone)]
pub struct SelectionMetadata {
    /// Selection timestamp
    pub selection_timestamp: Instant,
    /// Strategy used
    pub strategy_used: ResourceAllocationStrategy,
    /// Confidence
    pub confidence: f64,
}

/// Execution plan
#[derive(Debug, Clone)]
pub struct ExecutionPlan {
    /// Target platform
    pub platform: QuantumPlatform,
    /// Scheduled start time
    pub scheduled_start_time: Instant,
    /// Estimated duration
    pub estimated_duration: Duration,
    /// Resource allocation
    pub resource_allocation: PlatformResourceAllocation,
    /// Execution parameters
    pub execution_parameters: ExecutionParameters,
}

/// Platform resource allocation
#[derive(Debug, Clone)]
pub struct PlatformResourceAllocation {
    /// Allocated qubits
    pub qubits: Vec<usize>,
    /// Execution priority
    pub execution_priority: SchedulingPriority,
    /// Resource reservation
    pub resource_reservation: ResourceReservationInfo,
}

/// Resource reservation information
#[derive(Debug, Clone)]
pub struct ResourceReservationInfo {
    /// Reservation identifier
    pub reservation_id: String,
    /// Reserved until
    pub reserved_until: Instant,
}

/// Execution parameters
#[derive(Debug, Clone)]
pub struct ExecutionParameters {
    /// Number of shots
    pub shots: usize,
    /// Optimization level
    pub optimization_level: OptimizationLevel,
    /// Error mitigation enabled
    pub error_mitigation: bool,
}

/// Platform execution result
#[derive(Debug, Clone)]
pub struct PlatformExecutionResult {
    /// Platform used
    pub platform: QuantumPlatform,
    /// Execution identifier
    pub execution_id: String,
    /// Solution found
    pub solution: Vec<i32>,
    /// Objective value
    pub objective_value: f64,
    /// Execution time
    pub execution_time: Duration,
    /// Success indicator
    pub success: bool,
    /// Quality metrics
    pub quality_metrics: ExecutionQualityMetrics,
    /// Resource usage
    pub resource_usage: ExecutionResourceUsage,
    /// Execution metadata
    pub metadata: ExecutionMetadata,
}

/// Execution quality metrics
#[derive(Debug, Clone)]
pub struct ExecutionQualityMetrics {
    /// Solution quality
    pub solution_quality: f64,
    /// Fidelity
    pub fidelity: f64,
    /// Success probability
    pub success_probability: f64,
}

/// Execution resource usage
#[derive(Debug, Clone)]
pub struct ExecutionResourceUsage {
    /// Qubits used
    pub qubits_used: usize,
    /// Shots executed
    pub shots_executed: usize,
    /// Classical compute time
    pub classical_compute_time: Duration,
    /// Cost incurred
    pub cost_incurred: f64,
}

/// Execution metadata
#[derive(Debug, Clone)]
pub struct ExecutionMetadata {
    /// Execution timestamp
    pub execution_timestamp: Instant,
    /// Platform version
    pub platform_version: String,
    /// Execution environment
    pub execution_environment: String,
}

/// Universal execution metadata
#[derive(Debug, Clone)]
pub struct UniversalExecutionMetadata {
    /// Compiler version
    pub compiler_version: String,
    /// Platforms considered
    pub platforms_considered: usize,
    /// Optimization level used
    pub optimization_level: OptimizationLevel,
    /// Cost savings achieved
    pub cost_savings: f64,
    /// Performance improvement
    pub performance_improvement: f64,
}

/// Performance predictor backed by a real, growing history of observed
/// [`PlatformExecutionResult`]s per platform.
///
/// Rather than emitting fixed confidence/accuracy constants regardless of
/// input, [`Self::predict`]/[`Self::model_accuracy`]/[`Self::confidence_score`]
/// derive their outputs from whatever execution history has actually been
/// recorded via [`Self::record_result`] for that platform. With no history
/// for a platform, prediction honestly returns `None` rather than a
/// fabricated guess.
///
/// Note: this crate does not yet wire `record_result`/`predict` into
/// [`super::compiler::UniversalAnnealingCompiler`]'s `predict_performance` /
/// `update_performance_models`, which still construct
/// [`PlatformPerformancePrediction`] with fixed constants; see the crate's
/// TODOs for that remaining integration.
#[derive(Debug, Default)]
pub struct PerformancePredictor {
    /// Observed execution results, keyed by platform.
    history: HashMap<QuantumPlatform, Vec<PlatformExecutionResult>>,
}

impl PerformancePredictor {
    /// Create a new performance predictor with empty history.
    #[must_use]
    pub fn new() -> Self {
        Self {
            history: HashMap::new(),
        }
    }

    /// Record a real execution outcome, growing this platform's history.
    pub fn record_result(&mut self, result: &PlatformExecutionResult) {
        self.history
            .entry(result.platform.clone())
            .or_default()
            .push(result.clone());
    }

    /// Number of recorded results for `platform`.
    #[must_use]
    pub fn sample_count(&self, platform: &QuantumPlatform) -> usize {
        self.history.get(platform).map_or(0, Vec::len)
    }

    /// Predict performance for `platform` from its real recorded history.
    /// Returns `None` if nothing has been recorded for this platform yet.
    #[must_use]
    pub fn predict(&self, platform: &QuantumPlatform) -> Option<PredictedPerformance> {
        let results = self.history.get(platform)?;
        if results.is_empty() {
            return None;
        }
        let n = results.len() as f64;

        let mean_time_secs = results
            .iter()
            .map(|r| r.execution_time.as_secs_f64())
            .sum::<f64>()
            / n;
        let mean_quality = results
            .iter()
            .map(|r| r.quality_metrics.solution_quality)
            .sum::<f64>()
            / n;
        let mean_success_probability = results
            .iter()
            .map(|r| r.quality_metrics.success_probability)
            .sum::<f64>()
            / n;
        let mean_cost = results
            .iter()
            .map(|r| r.resource_usage.cost_incurred)
            .sum::<f64>()
            / n;
        let success_rate = results.iter().filter(|r| r.success).count() as f64 / n;

        Some(PredictedPerformance {
            execution_time: Duration::from_secs_f64(mean_time_secs.max(0.0)),
            solution_quality: mean_quality,
            success_probability: mean_success_probability,
            cost: mean_cost,
            reliability_score: success_rate,
        })
    }

    /// Real model accuracy for `platform`: `1 - coefficient_of_variation` of
    /// the recorded solution-quality samples, clamped to `[0, 1]`. A
    /// platform whose real outcomes are consistent scores near 1; one whose
    /// outcomes vary wildly scores low. Returns `0.0` (honestly "no
    /// evidence yet") when fewer than two samples have been recorded.
    #[must_use]
    pub fn model_accuracy(&self, platform: &QuantumPlatform) -> f64 {
        let Some(results) = self.history.get(platform) else {
            return 0.0;
        };
        if results.len() < 2 {
            return 0.0;
        }
        let n = results.len() as f64;
        let mean = results
            .iter()
            .map(|r| r.quality_metrics.solution_quality)
            .sum::<f64>()
            / n;
        if mean.abs() < 1e-12 {
            return 0.0;
        }
        let variance = results
            .iter()
            .map(|r| (r.quality_metrics.solution_quality - mean).powi(2))
            .sum::<f64>()
            / n;
        let coefficient_of_variation = variance.sqrt() / mean.abs();
        (1.0 - coefficient_of_variation).clamp(0.0, 1.0)
    }

    /// Real confidence score for `platform`: grows monotonically with the
    /// amount of real recorded evidence (`n / (n + 5)`), rather than a fixed
    /// constant regardless of how much (or how little) history exists.
    #[must_use]
    pub fn confidence_score(&self, platform: &QuantumPlatform) -> f64 {
        let n = self.sample_count(platform) as f64;
        n / (n + 5.0)
    }
}

/// Cost optimizer backed by a real, growing history of observed platform
/// costs, rather than emitting fabricated recommendations.
#[derive(Debug, Default)]
pub struct CostOptimizer {
    /// Observed incurred costs, keyed by platform.
    cost_history: HashMap<QuantumPlatform, Vec<f64>>,
}

impl CostOptimizer {
    /// Create a new cost optimizer with empty history.
    #[must_use]
    pub fn new() -> Self {
        Self {
            cost_history: HashMap::new(),
        }
    }

    /// Record a real observed cost for `platform`.
    pub fn record_cost(&mut self, platform: QuantumPlatform, cost: f64) {
        self.cost_history.entry(platform).or_default().push(cost);
    }

    /// Mean of the real recorded costs for `platform`, or `None` if nothing
    /// has been recorded yet.
    #[must_use]
    pub fn estimate_cost(&self, platform: &QuantumPlatform) -> Option<f64> {
        let costs = self.cost_history.get(platform)?;
        if costs.is_empty() {
            return None;
        }
        Some(costs.iter().sum::<f64>() / costs.len() as f64)
    }

    /// Recommend the platform among `candidates` with the lowest real mean
    /// recorded cost. Candidates with no recorded history are skipped
    /// (rather than fabricating a cost for them); returns `None` if none of
    /// the candidates have any recorded history.
    #[must_use]
    pub fn recommend_cheapest<'a>(
        &self,
        candidates: &'a [QuantumPlatform],
    ) -> Option<&'a QuantumPlatform> {
        candidates
            .iter()
            .filter_map(|platform| self.estimate_cost(platform).map(|cost| (platform, cost)))
            .min_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
            .map(|(platform, _)| platform)
    }
}

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

    fn make_result(
        platform: QuantumPlatform,
        quality: f64,
        cost: f64,
        success: bool,
    ) -> PlatformExecutionResult {
        PlatformExecutionResult {
            platform,
            execution_id: "test".to_string(),
            solution: vec![1, 0, 1],
            objective_value: -1.0,
            execution_time: Duration::from_millis(100),
            success,
            quality_metrics: ExecutionQualityMetrics {
                solution_quality: quality,
                fidelity: 0.95,
                success_probability: if success { 0.9 } else { 0.1 },
            },
            resource_usage: ExecutionResourceUsage {
                qubits_used: 4,
                shots_executed: 100,
                classical_compute_time: Duration::from_millis(10),
                cost_incurred: cost,
            },
            metadata: ExecutionMetadata {
                execution_timestamp: Instant::now(),
                platform_version: "1.0".to_string(),
                execution_environment: "test".to_string(),
            },
        }
    }

    #[test]
    fn performance_predictor_has_no_prediction_without_real_history() {
        let predictor = PerformancePredictor::new();
        assert!(predictor.predict(&QuantumPlatform::DWave).is_none());
        assert_eq!(predictor.model_accuracy(&QuantumPlatform::DWave), 0.0);
        assert_eq!(predictor.confidence_score(&QuantumPlatform::DWave), 0.0);
    }

    #[test]
    fn performance_predictor_derives_real_predictions_from_recorded_history() {
        let mut predictor = PerformancePredictor::new();
        predictor.record_result(&make_result(QuantumPlatform::DWave, 0.8, 1.0, true));
        predictor.record_result(&make_result(QuantumPlatform::DWave, 0.9, 2.0, true));
        predictor.record_result(&make_result(QuantumPlatform::DWave, 0.7, 3.0, false));

        let prediction = predictor
            .predict(&QuantumPlatform::DWave)
            .expect("prediction should exist once history has been recorded");

        assert!((prediction.solution_quality - 0.8).abs() < 1e-9);
        assert!((prediction.cost - 2.0).abs() < 1e-9);
        // 2 of 3 recorded runs succeeded -> real reliability, not a fixed 0.9.
        assert!((prediction.reliability_score - (2.0 / 3.0)).abs() < 1e-9);

        // Confidence must grow with recorded evidence rather than stay fixed.
        let confidence_after_3 = predictor.confidence_score(&QuantumPlatform::DWave);
        predictor.record_result(&make_result(QuantumPlatform::DWave, 0.85, 1.5, true));
        let confidence_after_4 = predictor.confidence_score(&QuantumPlatform::DWave);
        assert!(confidence_after_4 > confidence_after_3);
    }

    #[test]
    fn performance_predictor_accuracy_reflects_real_outcome_consistency() {
        let mut consistent = PerformancePredictor::new();
        consistent.record_result(&make_result(QuantumPlatform::IBM, 0.9, 1.0, true));
        consistent.record_result(&make_result(QuantumPlatform::IBM, 0.91, 1.0, true));
        consistent.record_result(&make_result(QuantumPlatform::IBM, 0.89, 1.0, true));

        let mut erratic = PerformancePredictor::new();
        erratic.record_result(&make_result(QuantumPlatform::IBM, 0.1, 1.0, true));
        erratic.record_result(&make_result(QuantumPlatform::IBM, 0.9, 1.0, true));
        erratic.record_result(&make_result(QuantumPlatform::IBM, 0.2, 1.0, false));

        let consistent_accuracy = consistent.model_accuracy(&QuantumPlatform::IBM);
        let erratic_accuracy = erratic.model_accuracy(&QuantumPlatform::IBM);

        assert!(
            consistent_accuracy > erratic_accuracy,
            "a platform with consistent real outcomes must score higher accuracy than an erratic one \
             (consistent={consistent_accuracy}, erratic={erratic_accuracy})"
        );
    }

    #[test]
    fn cost_optimizer_recommends_the_real_cheapest_platform() {
        let mut optimizer = CostOptimizer::new();
        optimizer.record_cost(QuantumPlatform::DWave, 5.0);
        optimizer.record_cost(QuantumPlatform::DWave, 7.0);
        optimizer.record_cost(QuantumPlatform::IBM, 1.0);
        optimizer.record_cost(QuantumPlatform::IBM, 2.0);

        assert!((optimizer.estimate_cost(&QuantumPlatform::DWave).unwrap() - 6.0).abs() < 1e-9);
        assert!((optimizer.estimate_cost(&QuantumPlatform::IBM).unwrap() - 1.5).abs() < 1e-9);

        let candidates = vec![QuantumPlatform::DWave, QuantumPlatform::IBM];
        let cheapest = optimizer
            .recommend_cheapest(&candidates)
            .expect("a cheapest platform should be found");
        assert_eq!(*cheapest, QuantumPlatform::IBM);
    }

    #[test]
    fn cost_optimizer_skips_platforms_with_no_recorded_history() {
        let mut optimizer = CostOptimizer::new();
        optimizer.record_cost(QuantumPlatform::IBM, 3.0);

        assert!(optimizer.estimate_cost(&QuantumPlatform::DWave).is_none());

        let candidates = vec![QuantumPlatform::DWave, QuantumPlatform::IBM];
        let cheapest = optimizer
            .recommend_cheapest(&candidates)
            .expect("should still find the one platform with real history");
        assert_eq!(*cheapest, QuantumPlatform::IBM);
    }
}