quantrs2-device 0.1.3

Quantum device connectors for the QuantRS2 framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
//! Anomaly detection components

use super::super::results::*;
use crate::DeviceResult;
use scirs2_core::ndarray::Array1;
use std::collections::HashMap;

/// Anomaly detector for measurement data
pub struct AnomalyDetector {
    threshold: f64,
    window_size: usize,
}

impl AnomalyDetector {
    /// Create new anomaly detector
    pub const fn new() -> Self {
        Self {
            threshold: 2.0, // Standard deviations
            window_size: 50,
        }
    }

    /// Create anomaly detector with custom parameters
    pub const fn with_parameters(threshold: f64, window_size: usize) -> Self {
        Self {
            threshold,
            window_size,
        }
    }

    /// Detect anomalies in measurement data
    pub fn detect(
        &self,
        latencies: &[f64],
        confidences: &[f64],
    ) -> DeviceResult<AnomalyDetectionResults> {
        if latencies.is_empty() || confidences.is_empty() {
            return Ok(AnomalyDetectionResults::default());
        }

        // Detect statistical anomalies
        let statistical_anomalies = self.detect_statistical_anomalies(latencies, confidences)?;

        // Detect temporal anomalies
        let temporal_anomalies = self.detect_temporal_anomalies(latencies)?;

        // Detect pattern anomalies
        let pattern_anomalies = self.detect_pattern_anomalies(latencies, confidences)?;

        // Calculate anomaly scores
        let anomaly_scores = self.calculate_anomaly_scores(latencies, confidences)?;

        // Generate anomaly summary
        let anomaly_summary = self.generate_anomaly_summary(
            &statistical_anomalies,
            &temporal_anomalies,
            &pattern_anomalies,
        )?;

        Ok(AnomalyDetectionResults {
            anomalies: vec![], // TODO: Convert different anomaly types to AnomalyEvent
            anomaly_scores,
            thresholds: HashMap::new(), // Placeholder - would need to be computed
            method_performance:
                crate::mid_circuit_measurements::results::AnomalyMethodPerformance {
                    precision: HashMap::new(),
                    recall: HashMap::new(),
                    f1_scores: HashMap::new(),
                    false_positive_rates: HashMap::new(),
                },
        })
    }

    /// Detect statistical anomalies using z-score method
    fn detect_statistical_anomalies(
        &self,
        latencies: &[f64],
        confidences: &[f64],
    ) -> DeviceResult<Vec<StatisticalAnomaly>> {
        let mut anomalies = Vec::new();

        // Detect anomalies in latencies
        let latency_anomalies = self.detect_zscore_anomalies(latencies, "latency")?;
        anomalies.extend(latency_anomalies);

        // Detect anomalies in confidences
        let confidence_anomalies = self.detect_zscore_anomalies(confidences, "confidence")?;
        anomalies.extend(confidence_anomalies);

        Ok(anomalies)
    }

    /// Detect z-score based anomalies
    fn detect_zscore_anomalies(
        &self,
        values: &[f64],
        metric_name: &str,
    ) -> DeviceResult<Vec<StatisticalAnomaly>> {
        if values.len() < 3 {
            return Ok(vec![]);
        }

        let mean = values.iter().sum::<f64>() / values.len() as f64;
        let variance =
            values.iter().map(|&x| (x - mean).powi(2)).sum::<f64>() / values.len() as f64;
        let std_dev = variance.sqrt();

        let mut anomalies = Vec::new();

        for (index, &value) in values.iter().enumerate() {
            let z_score = if std_dev > 1e-10 {
                (value - mean) / std_dev
            } else {
                0.0
            };

            if z_score.abs() > self.threshold {
                anomalies.push(StatisticalAnomaly {
                    index,
                    value,
                    z_score,
                    p_value: self.calculate_p_value(z_score),
                    metric_type: metric_name.to_string(),
                    anomaly_severity: self.classify_severity(z_score.abs()),
                });
            }
        }

        Ok(anomalies)
    }

    /// Detect temporal anomalies using moving window
    fn detect_temporal_anomalies(&self, values: &[f64]) -> DeviceResult<Vec<TemporalAnomaly>> {
        if values.len() < self.window_size * 2 {
            return Ok(vec![]);
        }

        let mut anomalies = Vec::new();

        for i in self.window_size..(values.len() - self.window_size) {
            let before_window = &values[(i - self.window_size)..i];
            let after_window = &values[i..(i + self.window_size)];

            let before_mean = before_window.iter().sum::<f64>() / before_window.len() as f64;
            let after_mean = after_window.iter().sum::<f64>() / after_window.len() as f64;

            let change_magnitude = (after_mean - before_mean).abs();
            let relative_change = if before_mean > 1e-10 {
                change_magnitude / before_mean
            } else {
                change_magnitude
            };

            // Detect significant changes
            if relative_change > 0.3 {
                // 30% change threshold
                anomalies.push(TemporalAnomaly {
                    start_index: i - self.window_size,
                    end_index: i + self.window_size,
                    change_point: i,
                    magnitude: change_magnitude,
                    direction: if after_mean > before_mean {
                        ChangeDirection::Increase
                    } else {
                        ChangeDirection::Decrease
                    },
                    confidence: self.calculate_change_confidence(relative_change),
                });
            }
        }

        Ok(anomalies)
    }

    /// Detect pattern anomalies
    fn detect_pattern_anomalies(
        &self,
        latencies: &[f64],
        confidences: &[f64],
    ) -> DeviceResult<Vec<PatternAnomaly>> {
        let mut anomalies = Vec::new();

        // Detect correlation anomalies
        if latencies.len() == confidences.len() && latencies.len() > 10 {
            let correlation = self.calculate_correlation(latencies, confidences);

            // Expected negative correlation (higher latency -> lower confidence)
            if correlation > -0.1 {
                // Unexpectedly positive or weak correlation
                anomalies.push(PatternAnomaly {
                    pattern_type: PatternType::CorrelationAnomaly,
                    description: format!(
                        "Unexpected correlation between latency and confidence: {correlation:.3}"
                    ),
                    severity: if correlation > 0.3 {
                        AnomalySeverity::High
                    } else {
                        AnomalySeverity::Medium
                    },
                    affected_indices: (0..latencies.len()).collect(),
                    confidence: 0.8,
                });
            }
        }

        // Detect sequence anomalies
        let sequence_anomalies = self.detect_sequence_anomalies(latencies)?;
        anomalies.extend(sequence_anomalies);

        Ok(anomalies)
    }

    /// Detect sequence anomalies (repeated patterns, outlier sequences)
    fn detect_sequence_anomalies(&self, values: &[f64]) -> DeviceResult<Vec<PatternAnomaly>> {
        let mut anomalies = Vec::new();

        if values.len() < 5 {
            return Ok(anomalies);
        }

        // Detect constant sequences (potential stuck measurements)
        let mut constant_start = 0;
        let mut constant_length = 1;

        for i in 1..values.len() {
            if (values[i] - values[i - 1]).abs() < 1e-6 {
                constant_length += 1;
            } else {
                if constant_length >= 5 {
                    // 5+ identical measurements
                    anomalies.push(PatternAnomaly {
                        pattern_type: PatternType::ConstantSequence,
                        description: format!(
                            "Constant sequence of {} identical values: {:.3}",
                            constant_length, values[constant_start]
                        ),
                        severity: AnomalySeverity::Medium,
                        affected_indices: (constant_start..constant_start + constant_length)
                            .collect(),
                        confidence: 0.9,
                    });
                }
                constant_start = i;
                constant_length = 1;
            }
        }

        // Check final sequence
        if constant_length >= 5 {
            anomalies.push(PatternAnomaly {
                pattern_type: PatternType::ConstantSequence,
                description: format!(
                    "Constant sequence of {} identical values: {:.3}",
                    constant_length, values[constant_start]
                ),
                severity: AnomalySeverity::Medium,
                affected_indices: (constant_start..constant_start + constant_length).collect(),
                confidence: 0.9,
            });
        }

        Ok(anomalies)
    }

    /// Calculate anomaly scores for all measurements
    fn calculate_anomaly_scores(
        &self,
        latencies: &[f64],
        confidences: &[f64],
    ) -> DeviceResult<Array1<f64>> {
        let n = latencies.len().min(confidences.len());
        let mut scores = Array1::zeros(n);

        if n == 0 {
            return Ok(scores);
        }

        // Calculate z-scores for latencies
        let latency_mean = latencies.iter().sum::<f64>() / latencies.len() as f64;
        let latency_std = {
            let variance = latencies
                .iter()
                .map(|&x| (x - latency_mean).powi(2))
                .sum::<f64>()
                / latencies.len() as f64;
            variance.sqrt()
        };

        // Calculate z-scores for confidences
        let confidence_mean = confidences.iter().sum::<f64>() / confidences.len() as f64;
        let confidence_std = {
            let variance = confidences
                .iter()
                .map(|&x| (x - confidence_mean).powi(2))
                .sum::<f64>()
                / confidences.len() as f64;
            variance.sqrt()
        };

        for i in 0..n {
            let latency_zscore = if latency_std > 1e-10 {
                (latencies[i] - latency_mean) / latency_std
            } else {
                0.0
            };

            let confidence_zscore = if confidence_std > 1e-10 {
                (confidences[i] - confidence_mean) / confidence_std
            } else {
                0.0
            };

            // Combine scores (higher is more anomalous)
            scores[i] = f64::midpoint(latency_zscore.abs(), confidence_zscore.abs());
        }

        Ok(scores)
    }

    /// Generate comprehensive anomaly summary
    fn generate_anomaly_summary(
        &self,
        statistical_anomalies: &[StatisticalAnomaly],
        temporal_anomalies: &[TemporalAnomaly],
        pattern_anomalies: &[PatternAnomaly],
    ) -> DeviceResult<AnomalySummary> {
        let total_anomalies =
            statistical_anomalies.len() + temporal_anomalies.len() + pattern_anomalies.len();

        // Count by severity
        let high_severity = statistical_anomalies
            .iter()
            .filter(|a| matches!(a.anomaly_severity, AnomalySeverity::High))
            .count()
            + pattern_anomalies
                .iter()
                .filter(|a| matches!(a.severity, AnomalySeverity::High))
                .count();

        let medium_severity = statistical_anomalies
            .iter()
            .filter(|a| matches!(a.anomaly_severity, AnomalySeverity::Medium))
            .count()
            + pattern_anomalies
                .iter()
                .filter(|a| matches!(a.severity, AnomalySeverity::Medium))
                .count();

        let low_severity = total_anomalies - high_severity - medium_severity;

        // Calculate overall anomaly rate
        let anomaly_rate = if total_anomalies > 0 {
            total_anomalies as f64 / 100.0 // Assuming base of 100 measurements
        } else {
            0.0
        };

        // Determine dominant anomaly types
        let mut anomaly_types = Vec::new();
        if !statistical_anomalies.is_empty() {
            anomaly_types.push("Statistical".to_string());
        }
        if !temporal_anomalies.is_empty() {
            anomaly_types.push("Temporal".to_string());
        }
        if !pattern_anomalies.is_empty() {
            anomaly_types.push("Pattern".to_string());
        }

        // Generate recommendations
        let mut recommendations = Vec::new();
        if high_severity > 0 {
            recommendations.push("Investigate high-severity anomalies immediately".to_string());
        }
        if temporal_anomalies.len() > 3 {
            recommendations.push("Review measurement timing and calibration".to_string());
        }
        if pattern_anomalies
            .iter()
            .any(|a| matches!(a.pattern_type, PatternType::ConstantSequence))
        {
            recommendations.push("Check for stuck measurement equipment".to_string());
        }

        Ok(AnomalySummary {
            total_anomalies,
            anomaly_rate,
            severity_distribution: vec![
                ("High".to_string(), high_severity),
                ("Medium".to_string(), medium_severity),
                ("Low".to_string(), low_severity),
            ],
            anomaly_types,
            recommendations,
        })
    }

    /// Calculate p-value for z-score (simplified)
    fn calculate_p_value(&self, z_score: f64) -> f64 {
        // Simplified p-value calculation
        let abs_z = z_score.abs();
        if abs_z > 3.0 {
            0.001
        } else if abs_z > 2.5 {
            0.01
        } else if abs_z > 2.0 {
            0.05
        } else {
            0.1
        }
    }

    /// Classify anomaly severity based on z-score
    fn classify_severity(&self, abs_z_score: f64) -> AnomalySeverity {
        if abs_z_score > 3.0 {
            AnomalySeverity::High
        } else if abs_z_score > 2.5 {
            AnomalySeverity::Medium
        } else {
            AnomalySeverity::Low
        }
    }

    /// Calculate change confidence
    fn calculate_change_confidence(&self, relative_change: f64) -> f64 {
        // Higher relative changes have higher confidence
        (relative_change * 2.0).min(1.0)
    }

    /// Calculate Pearson correlation
    fn calculate_correlation(&self, x: &[f64], y: &[f64]) -> f64 {
        if x.len() != y.len() || x.len() < 2 {
            return 0.0;
        }

        let n = x.len() as f64;
        let mean_x = x.iter().sum::<f64>() / n;
        let mean_y = y.iter().sum::<f64>() / n;

        let numerator: f64 = x
            .iter()
            .zip(y.iter())
            .map(|(&xi, &yi)| (xi - mean_x) * (yi - mean_y))
            .sum();

        let sum_sq_x: f64 = x.iter().map(|&xi| (xi - mean_x).powi(2)).sum();
        let sum_sq_y: f64 = y.iter().map(|&yi| (yi - mean_y).powi(2)).sum();

        let denominator = (sum_sq_x * sum_sq_y).sqrt();

        if denominator > 1e-10 {
            numerator / denominator
        } else {
            0.0
        }
    }
}

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

impl Default for AnomalyDetectionResults {
    fn default() -> Self {
        Self {
            anomalies: vec![],
            anomaly_scores: Array1::zeros(0),
            thresholds: HashMap::new(),
            method_performance:
                crate::mid_circuit_measurements::results::AnomalyMethodPerformance {
                    precision: HashMap::new(),
                    recall: HashMap::new(),
                    f1_scores: HashMap::new(),
                    false_positive_rates: HashMap::new(),
                },
        }
    }
}

impl Default for AnomalySummary {
    fn default() -> Self {
        Self {
            total_anomalies: 0,
            anomaly_rate: 0.0,
            severity_distribution: vec![],
            anomaly_types: vec![],
            recommendations: vec![],
        }
    }
}