quantrs2-core 0.1.3

Core types and traits for the QuantRS2 quantum computing 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
//! Noise-adaptive error correction threshold estimation
//!
//! This module provides dynamic adjustment of error correction thresholds based on
//! observed noise characteristics and environmental conditions for optimal performance.

use super::pauli::{Pauli, PauliString};
use std::collections::{HashMap, VecDeque};
use std::time::{Duration, Instant};

/// Adaptive threshold estimator for error correction
pub struct AdaptiveThresholdEstimator {
    /// Historical error pattern data
    error_history: VecDeque<ErrorObservation>,
    /// Current noise model parameters
    noise_model: NoiseModel,
    /// Threshold estimation algorithm
    estimation_algorithm: ThresholdEstimationAlgorithm,
    /// Performance metrics
    performance_tracker: PerformanceTracker,
    /// Configuration parameters
    config: AdaptiveConfig,
}

/// Observation of an error and correction attempt
#[derive(Debug, Clone)]
pub struct ErrorObservation {
    /// Syndrome measured
    pub syndrome: Vec<bool>,
    /// Correction applied
    pub correction: PauliString,
    /// Whether correction was successful
    pub success: bool,
    /// Measured error rate at this time
    pub observed_error_rate: f64,
    /// Timestamp of observation
    pub timestamp: Instant,
    /// Environmental conditions
    pub environment: EnvironmentalConditions,
}

/// Environmental conditions affecting error rates
#[derive(Debug, Clone)]
pub struct EnvironmentalConditions {
    /// Temperature in Kelvin
    pub temperature: f64,
    /// Magnetic field strength in Tesla
    pub magnetic_field: f64,
    /// Vibration level (arbitrary units)
    pub vibration_level: f64,
    /// Electromagnetic interference level
    pub emi_level: f64,
    /// Device uptime in seconds
    pub uptime: f64,
}

/// Noise model for quantum errors
#[derive(Debug, Clone)]
pub struct NoiseModel {
    /// Single-qubit error rates by qubit and error type
    pub single_qubit_rates: HashMap<(usize, Pauli), f64>,
    /// Two-qubit correlated error rates
    pub correlated_rates: HashMap<(usize, usize), f64>,
    /// Temporal correlation in errors
    pub temporal_correlation: f64,
    /// Environmental sensitivity coefficients
    pub environment_sensitivity: EnvironmentSensitivity,
    /// Model confidence (0.0 to 1.0)
    pub confidence: f64,
}

/// Sensitivity to environmental factors
#[derive(Debug, Clone)]
pub struct EnvironmentSensitivity {
    /// Temperature coefficient (per Kelvin)
    pub temperature_coeff: f64,
    /// Magnetic field coefficient (per Tesla)
    pub magnetic_field_coeff: f64,
    /// Vibration coefficient
    pub vibration_coeff: f64,
    /// EMI coefficient
    pub emi_coeff: f64,
    /// Drift coefficient (per second)
    pub drift_coeff: f64,
}

/// Algorithm for threshold estimation
#[derive(Debug, Clone)]
pub enum ThresholdEstimationAlgorithm {
    /// Bayesian inference with prior knowledge
    Bayesian {
        prior_strength: f64,
        update_rate: f64,
    },
    /// Machine learning based prediction
    MachineLearning {
        model_type: MLModelType,
        training_window: usize,
    },
    /// Kalman filter for dynamic estimation
    KalmanFilter {
        process_noise: f64,
        measurement_noise: f64,
    },
    /// Exponential moving average
    ExponentialAverage { alpha: f64 },
}

/// Machine learning model types
#[derive(Debug, Clone)]
pub enum MLModelType {
    LinearRegression,
    RandomForest,
    NeuralNetwork { hidden_layers: Vec<usize> },
    SupportVectorMachine,
}

/// Performance tracking for adaptive threshold
#[derive(Debug, Clone)]
pub struct PerformanceTracker {
    /// Number of successful corrections
    pub successful_corrections: u64,
    /// Number of failed corrections
    pub failed_corrections: u64,
    /// Number of false positives (unnecessary corrections)
    pub false_positives: u64,
    /// Number of false negatives (missed errors)
    pub false_negatives: u64,
    /// Average correction latency
    pub average_latency: Duration,
    /// Current threshold accuracy
    pub threshold_accuracy: f64,
}

/// Configuration for adaptive threshold estimation
#[derive(Debug, Clone)]
pub struct AdaptiveConfig {
    /// Maximum history size
    pub max_history_size: usize,
    /// Minimum observations before adaptation
    pub min_observations: usize,
    /// Update frequency
    pub update_frequency: Duration,
    /// Confidence threshold for model updates
    pub confidence_threshold: f64,
    /// Environmental monitoring enabled
    pub environmental_monitoring: bool,
    /// Real-time adaptation enabled
    pub real_time_adaptation: bool,
}

/// Threshold recommendation result
#[derive(Debug, Clone)]
pub struct ThresholdRecommendation {
    /// Recommended threshold value
    pub threshold: f64,
    /// Confidence in recommendation (0.0 to 1.0)
    pub confidence: f64,
    /// Predicted error rate
    pub predicted_error_rate: f64,
    /// Quality of recommendation (0.0 to 1.0)
    pub recommendation_quality: f64,
    /// Environmental impact assessment
    pub environmental_impact: f64,
}

impl Default for AdaptiveConfig {
    fn default() -> Self {
        Self {
            max_history_size: 10000,
            min_observations: 100,
            update_frequency: Duration::from_secs(30),
            confidence_threshold: 0.8,
            environmental_monitoring: true,
            real_time_adaptation: true,
        }
    }
}

impl Default for EnvironmentalConditions {
    fn default() -> Self {
        Self {
            temperature: 300.0, // Room temperature in Kelvin
            magnetic_field: 0.0,
            vibration_level: 0.0,
            emi_level: 0.0,
            uptime: 0.0,
        }
    }
}

impl Default for EnvironmentSensitivity {
    fn default() -> Self {
        Self {
            temperature_coeff: 1e-5,
            magnetic_field_coeff: 1e-3,
            vibration_coeff: 1e-4,
            emi_coeff: 1e-4,
            drift_coeff: 1e-7,
        }
    }
}

impl Default for NoiseModel {
    fn default() -> Self {
        Self {
            single_qubit_rates: HashMap::new(),
            correlated_rates: HashMap::new(),
            temporal_correlation: 0.1,
            environment_sensitivity: EnvironmentSensitivity::default(),
            confidence: 0.5,
        }
    }
}

impl PerformanceTracker {
    pub const fn new() -> Self {
        Self {
            successful_corrections: 0,
            failed_corrections: 0,
            false_positives: 0,
            false_negatives: 0,
            average_latency: Duration::from_nanos(0),
            threshold_accuracy: 0.0,
        }
    }

    pub fn precision(&self) -> f64 {
        let total_positive = self.successful_corrections + self.false_positives;
        if total_positive == 0 {
            1.0
        } else {
            self.successful_corrections as f64 / total_positive as f64
        }
    }

    pub fn recall(&self) -> f64 {
        let total_actual_positive = self.successful_corrections + self.false_negatives;
        if total_actual_positive == 0 {
            1.0
        } else {
            self.successful_corrections as f64 / total_actual_positive as f64
        }
    }

    pub fn f1_score(&self) -> f64 {
        let p = self.precision();
        let r = self.recall();
        if p + r == 0.0 {
            0.0
        } else {
            2.0 * p * r / (p + r)
        }
    }
}

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

impl AdaptiveThresholdEstimator {
    /// Create a new adaptive threshold estimator
    pub fn new(
        initial_noise_model: NoiseModel,
        algorithm: ThresholdEstimationAlgorithm,
        config: AdaptiveConfig,
    ) -> Self {
        Self {
            error_history: VecDeque::with_capacity(config.max_history_size),
            noise_model: initial_noise_model,
            estimation_algorithm: algorithm,
            performance_tracker: PerformanceTracker::new(),
            config,
        }
    }

    /// Add a new error observation
    pub fn add_observation(&mut self, observation: ErrorObservation) {
        // Add to history
        if self.error_history.len() >= self.config.max_history_size {
            self.error_history.pop_front();
        }
        self.error_history.push_back(observation.clone());

        // Update performance tracking
        self.update_performance_tracking(&observation);

        // Update noise model if real-time adaptation is enabled
        if self.config.real_time_adaptation
            && self.error_history.len() >= self.config.min_observations
        {
            self.update_noise_model();
        }
    }

    /// Estimate current error correction threshold
    pub fn estimate_threshold(
        &self,
        syndrome: &[bool],
        environment: &EnvironmentalConditions,
    ) -> f64 {
        match &self.estimation_algorithm {
            ThresholdEstimationAlgorithm::Bayesian {
                prior_strength,
                update_rate,
            } => self.bayesian_threshold_estimation(
                syndrome,
                environment,
                *prior_strength,
                *update_rate,
            ),
            ThresholdEstimationAlgorithm::MachineLearning {
                model_type,
                training_window,
            } => self.ml_threshold_estimation(syndrome, environment, model_type, *training_window),
            ThresholdEstimationAlgorithm::KalmanFilter {
                process_noise,
                measurement_noise,
            } => self.kalman_threshold_estimation(
                syndrome,
                environment,
                *process_noise,
                *measurement_noise,
            ),
            ThresholdEstimationAlgorithm::ExponentialAverage { alpha } => {
                self.exponential_average_threshold(syndrome, environment, *alpha)
            }
        }
    }

    /// Get current threshold recommendation
    pub fn get_threshold_recommendation(&self, syndrome: &[bool]) -> ThresholdRecommendation {
        let current_env = EnvironmentalConditions::default(); // Would get from sensors
        let threshold = self.estimate_threshold(syndrome, &current_env);
        let confidence = self.noise_model.confidence;
        let predicted_rate = self.predict_error_rate(&current_env, Duration::from_secs(60));

        ThresholdRecommendation {
            threshold,
            confidence,
            predicted_error_rate: predicted_rate,
            recommendation_quality: self.assess_recommendation_quality(),
            environmental_impact: self.assess_environmental_impact(&current_env),
        }
    }

    /// Predict future error rate based on current conditions
    pub fn predict_error_rate(
        &self,
        environment: &EnvironmentalConditions,
        horizon: Duration,
    ) -> f64 {
        let base_rate = self.calculate_base_error_rate();
        let environmental_factor = self.calculate_environmental_factor(environment);
        let temporal_factor = self.calculate_temporal_factor(horizon);

        base_rate * environmental_factor * temporal_factor
    }

    /// Bayesian threshold estimation
    fn bayesian_threshold_estimation(
        &self,
        syndrome: &[bool],
        environment: &EnvironmentalConditions,
        prior_strength: f64,
        update_rate: f64,
    ) -> f64 {
        let syndrome_weight = syndrome.iter().filter(|&&x| x).count() as f64;
        let base_threshold = self.calculate_base_threshold(syndrome_weight);

        // Update based on historical observations
        let historical_adjustment = self.calculate_historical_adjustment(update_rate);

        // Environmental adjustment
        let env_adjustment = self.calculate_environmental_adjustment(environment);

        // Bayesian update
        let prior = base_threshold;
        let likelihood_weight = 1.0 / (1.0 + prior_strength);

        prior.mul_add(
            1.0 - likelihood_weight,
            (base_threshold + historical_adjustment + env_adjustment) * likelihood_weight,
        )
    }

    /// Machine learning based threshold estimation
    fn ml_threshold_estimation(
        &self,
        syndrome: &[bool],
        environment: &EnvironmentalConditions,
        model_type: &MLModelType,
        training_window: usize,
    ) -> f64 {
        // Extract features
        let features = self.extract_features(syndrome, environment);

        // Get recent training data
        let training_data = self.get_recent_observations(training_window);

        match model_type {
            MLModelType::LinearRegression => {
                self.linear_regression_predict(&features, &training_data)
            }
            _ => {
                // Simplified implementation for other ML models
                self.linear_regression_predict(&features, &training_data)
            }
        }
    }

    /// Kalman filter based threshold estimation
    fn kalman_threshold_estimation(
        &self,
        syndrome: &[bool],
        _environment: &EnvironmentalConditions,
        process_noise: f64,
        measurement_noise: f64,
    ) -> f64 {
        let syndrome_weight = syndrome.iter().filter(|&&x| x).count() as f64;
        let base_threshold = self.calculate_base_threshold(syndrome_weight);

        // Simplified Kalman filter implementation
        let prediction_error = self.calculate_prediction_error();
        let kalman_gain = process_noise / (process_noise + measurement_noise);

        kalman_gain.mul_add(prediction_error, base_threshold)
    }

    /// Exponential moving average threshold estimation
    fn exponential_average_threshold(
        &self,
        syndrome: &[bool],
        _environment: &EnvironmentalConditions,
        alpha: f64,
    ) -> f64 {
        let syndrome_weight = syndrome.iter().filter(|&&x| x).count() as f64;
        let current_threshold = self.calculate_base_threshold(syndrome_weight);

        if let Some(_last_obs) = self.error_history.back() {
            let last_threshold = syndrome_weight; // Simplified
            alpha.mul_add(current_threshold, (1.0 - alpha) * last_threshold)
        } else {
            current_threshold
        }
    }

    // Helper methods
    fn calculate_base_error_rate(&self) -> f64 {
        if self.error_history.is_empty() {
            return 0.001; // Default 0.1% error rate
        }

        let recent_errors: Vec<_> = self.error_history.iter().rev().take(100).collect();

        let total_errors = recent_errors.len() as f64;
        let failed_corrections = recent_errors.iter().filter(|obs| !obs.success).count() as f64;

        failed_corrections / total_errors
    }

    fn calculate_environmental_factor(&self, environment: &EnvironmentalConditions) -> f64 {
        let sensitivity = &self.noise_model.environment_sensitivity;

        sensitivity.drift_coeff.mul_add(
            environment.uptime,
            sensitivity.emi_coeff.mul_add(
                environment.emi_level,
                sensitivity.vibration_coeff.mul_add(
                    environment.vibration_level,
                    sensitivity.magnetic_field_coeff.mul_add(
                        environment.magnetic_field,
                        sensitivity
                            .temperature_coeff
                            .mul_add(environment.temperature - 300.0, 1.0),
                    ),
                ),
            ),
        )
    }

    fn calculate_temporal_factor(&self, horizon: Duration) -> f64 {
        let temporal_corr = self.noise_model.temporal_correlation;
        let time_factor = horizon.as_secs_f64() / 3600.0; // Hours

        temporal_corr.mul_add(time_factor, 1.0)
    }

    fn calculate_base_threshold(&self, syndrome_weight: f64) -> f64 {
        // Simple heuristic: higher syndrome weight suggests higher error probability
        (syndrome_weight + 1.0) / 10.0
    }

    fn calculate_historical_adjustment(&self, update_rate: f64) -> f64 {
        if self.error_history.is_empty() {
            return 0.0;
        }

        let recent_success_rate = self.calculate_recent_success_rate();
        update_rate * (0.5 - recent_success_rate) // Adjust towards 50% success rate
    }

    fn calculate_environmental_adjustment(&self, environment: &EnvironmentalConditions) -> f64 {
        let env_factor = self.calculate_environmental_factor(environment);
        (env_factor - 1.0) * 0.1 // Scale environmental impact
    }

    fn calculate_recent_success_rate(&self) -> f64 {
        let recent_window = 50.min(self.error_history.len());
        if recent_window == 0 {
            return 0.5;
        }

        let recent_successes = self
            .error_history
            .iter()
            .rev()
            .take(recent_window)
            .filter(|obs| obs.success)
            .count();

        recent_successes as f64 / recent_window as f64
    }

    fn calculate_prediction_error(&self) -> f64 {
        // Simplified prediction error calculation
        let target_success_rate = 0.95;
        let actual_success_rate = self.calculate_recent_success_rate();
        target_success_rate - actual_success_rate
    }

    fn extract_features(
        &self,
        syndrome: &[bool],
        environment: &EnvironmentalConditions,
    ) -> Vec<f64> {
        let mut features = vec![
            syndrome.iter().filter(|&&x| x).count() as f64,
            environment.temperature,
            environment.magnetic_field,
            environment.vibration_level,
            environment.emi_level,
            environment.uptime,
        ];

        // Add syndrome pattern features
        for &bit in syndrome {
            features.push(if bit { 1.0 } else { 0.0 });
        }

        features
    }

    fn get_recent_observations(&self, window: usize) -> Vec<ErrorObservation> {
        self.error_history
            .iter()
            .rev()
            .take(window)
            .cloned()
            .collect()
    }

    fn linear_regression_predict(
        &self,
        _features: &[f64],
        training_data: &[ErrorObservation],
    ) -> f64 {
        // Simplified linear regression
        if training_data.is_empty() {
            return 0.5;
        }

        let avg_syndrome_weight: f64 = training_data
            .iter()
            .map(|obs| obs.syndrome.iter().filter(|&&x| x).count() as f64)
            .sum::<f64>()
            / training_data.len() as f64;

        (avg_syndrome_weight + 1.0) / 10.0
    }

    fn update_performance_tracking(&mut self, observation: &ErrorObservation) {
        if observation.success {
            self.performance_tracker.successful_corrections += 1;
        } else {
            self.performance_tracker.failed_corrections += 1;
        }

        // Update accuracy
        let total = self.performance_tracker.successful_corrections
            + self.performance_tracker.failed_corrections;
        if total > 0 {
            self.performance_tracker.threshold_accuracy =
                self.performance_tracker.successful_corrections as f64 / total as f64;
        }
    }

    fn update_noise_model(&mut self) {
        let recent_window = self.config.min_observations.min(self.error_history.len());
        let recent_observations: Vec<ErrorObservation> = self
            .error_history
            .iter()
            .rev()
            .take(recent_window)
            .cloned()
            .collect();

        // Update single-qubit error rates
        self.update_single_qubit_rates(&recent_observations);

        // Update model confidence
        self.update_model_confidence(&recent_observations);
    }

    fn update_single_qubit_rates(&mut self, observations: &[ErrorObservation]) {
        // Update single-qubit error rates based on observations
        for obs in observations {
            for (i, pauli) in obs.correction.paulis.iter().enumerate() {
                if *pauli != Pauli::I {
                    let key = (i, *pauli);
                    let current_rate = self
                        .noise_model
                        .single_qubit_rates
                        .get(&key)
                        .copied()
                        .unwrap_or(0.001);
                    let new_rate = if obs.success {
                        current_rate * 0.99
                    } else {
                        current_rate * 1.01
                    };
                    self.noise_model.single_qubit_rates.insert(key, new_rate);
                }
            }
        }
    }

    fn update_model_confidence(&mut self, observations: &[ErrorObservation]) {
        if observations.is_empty() {
            return;
        }

        let success_rate = observations.iter().filter(|obs| obs.success).count() as f64
            / observations.len() as f64;

        // Higher success rate increases confidence, but not linearly
        let stability = (success_rate - 0.5).abs().mul_add(-2.0, 1.0);
        self.noise_model.confidence = self.noise_model.confidence.mul_add(0.95, stability * 0.05);
    }

    fn assess_recommendation_quality(&self) -> f64 {
        // Quality based on model confidence and recent performance
        let confidence_component = self.noise_model.confidence;
        let performance_component = self.performance_tracker.threshold_accuracy;
        let history_component =
            (self.error_history.len() as f64 / self.config.max_history_size as f64).min(1.0);

        (confidence_component + performance_component + history_component) / 3.0
    }

    fn assess_environmental_impact(&self, environment: &EnvironmentalConditions) -> f64 {
        let env_factor = self.calculate_environmental_factor(environment);
        (env_factor - 1.0).abs()
    }
}