quantrs2-ml 0.1.3

Quantum Machine Learning module for QuantRS2
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
#![allow(
    unused_must_use,
    clippy::pedantic,
    clippy::unnecessary_wraps,
    clippy::field_reassign_with_default,
    clippy::match_same_arms
)]
//! Unit tests for quantum anomaly detection module

use quantrs2_ml::anomaly_detection::*;
use quantrs2_ml::error::Result;
use scirs2_core::ndarray::{Array1, Array2};
use scirs2_core::random::prelude::*;

/// Create default anomaly detection configuration
fn create_default_anomaly_config() -> QuantumAnomalyConfig {
    QuantumAnomalyConfig::default()
}

/// Create comprehensive anomaly detection configuration
fn create_comprehensive_anomaly_config(domain: &str) -> Result<QuantumAnomalyConfig> {
    let mut config = QuantumAnomalyConfig::default();
    config.primary_method = match domain {
        "network_security" => AnomalyDetectionMethod::QuantumIsolationForest {
            n_estimators: 100,
            max_samples: 256,
            max_depth: None,
            quantum_splitting: true,
        },
        "financial_fraud" => AnomalyDetectionMethod::QuantumOneClassSVM {
            kernel_type: QuantumKernelType::RBF,
            nu: 0.05,
            gamma: 0.1,
        },
        "iot_monitoring" => AnomalyDetectionMethod::QuantumAutoencoder {
            encoder_layers: vec![16, 8],
            latent_dim: 4,
            decoder_layers: vec![8, 16],
            reconstruction_threshold: 0.1,
        },
        _ => AnomalyDetectionMethod::QuantumIsolationForest {
            n_estimators: 100,
            max_samples: 256,
            max_depth: None,
            quantum_splitting: true,
        },
    };
    Ok(config)
}

/// Test basic detector creation
#[test]
fn test_detector_creation() {
    let config = create_default_anomaly_config();
    let detector = QuantumAnomalyDetector::new(config);
    assert!(detector.is_ok());
}

/// Test different method configurations
#[test]
fn test_method_configurations() {
    // Test Isolation Forest
    let mut config = create_default_anomaly_config();
    config.primary_method = AnomalyDetectionMethod::QuantumIsolationForest {
        n_estimators: 50,
        max_samples: 100,
        max_depth: Some(8),
        quantum_splitting: true,
    };
    assert!(QuantumAnomalyDetector::new(config).is_ok());

    // Test Autoencoder
    let mut config = create_default_anomaly_config();
    config.primary_method = AnomalyDetectionMethod::QuantumAutoencoder {
        encoder_layers: vec![4, 2],
        latent_dim: 1,
        decoder_layers: vec![2, 4],
        reconstruction_threshold: 0.5,
    };
    assert!(QuantumAnomalyDetector::new(config).is_ok());

    // Test One-Class SVM
    let mut config = create_default_anomaly_config();
    config.primary_method = AnomalyDetectionMethod::QuantumOneClassSVM {
        kernel_type: QuantumKernelType::RBF,
        nu: 0.1,
        gamma: 0.1,
    };
    assert!(QuantumAnomalyDetector::new(config).is_ok());

    // Test LOF
    let mut config = create_default_anomaly_config();
    config.primary_method = AnomalyDetectionMethod::QuantumLOF {
        n_neighbors: 10,
        contamination: 0.1,
        quantum_distance: true,
    };
    assert!(QuantumAnomalyDetector::new(config).is_ok());
}

/// Test comprehensive configurations
#[test]
fn test_comprehensive_configs() -> Result<()> {
    let network_config = create_comprehensive_anomaly_config("network_security")?;
    assert!(QuantumAnomalyDetector::new(network_config).is_ok());

    let fraud_config = create_comprehensive_anomaly_config("financial_fraud")?;
    assert!(QuantumAnomalyDetector::new(fraud_config).is_ok());

    let iot_config = create_comprehensive_anomaly_config("iot_monitoring")?;
    assert!(QuantumAnomalyDetector::new(iot_config).is_ok());

    let default_config = create_comprehensive_anomaly_config("unknown")?;
    assert!(QuantumAnomalyDetector::new(default_config).is_ok());

    Ok(())
}

/// Create synthetic data for testing
fn create_synthetic_data(n_samples: usize, n_features: usize, noise_level: f64) -> Array2<f64> {
    let mut data = Array2::zeros((n_samples, n_features));
    let mut rng = thread_rng();

    for i in 0..n_samples {
        for j in 0..n_features {
            data[[i, j]] = rng.random::<f64>() * noise_level;
        }
    }

    data
}

/// Test training and detection pipeline
#[test]
fn test_training_detection_pipeline() -> Result<()> {
    let config = create_default_anomaly_config();
    let mut detector = QuantumAnomalyDetector::new(config)?;

    // Create training data
    let training_data = create_synthetic_data(100, 4, 1.0);

    // Train detector
    detector.fit(&training_data)?;

    // Verify training stats
    assert!(detector.get_training_stats().is_some());
    let stats = detector.get_training_stats().unwrap();
    assert_eq!(stats.n_training_samples, 100);
    assert!(stats.training_time > 0.0);

    // Create test data
    let test_data = create_synthetic_data(20, 4, 1.0);

    // Detect anomalies
    let result = detector.detect(&test_data)?;

    // Verify results
    assert_eq!(result.anomaly_scores.len(), 20);
    assert_eq!(result.anomaly_labels.len(), 20);
    assert_eq!(result.confidence_scores.len(), 20);
    assert_eq!(result.feature_importance.nrows(), 20);
    assert_eq!(result.feature_importance.ncols(), 4);

    // Verify metrics are reasonable
    assert!(result.metrics.auc_roc >= 0.0 && result.metrics.auc_roc <= 1.0);
    assert!(result.metrics.precision >= 0.0 && result.metrics.precision <= 1.0);
    assert!(result.metrics.recall >= 0.0 && result.metrics.recall <= 1.0);
    assert!(result.metrics.f1_score >= 0.0 && result.metrics.f1_score <= 1.0);

    // Verify quantum metrics
    assert!(result.metrics.quantum_metrics.quantum_advantage >= 1.0);
    assert!(result.metrics.quantum_metrics.entanglement_utilization >= 0.0);
    assert!(result.metrics.quantum_metrics.entanglement_utilization <= 1.0);

    // Verify processing stats
    assert!(result.processing_stats.total_time > 0.0);
    assert!(result.processing_stats.quantum_executions > 0);
    assert!(result.processing_stats.memory_usage > 0.0);

    Ok(())
}

/// Test streaming detection
#[test]
fn test_streaming_detection() -> Result<()> {
    let mut config = create_default_anomaly_config();
    config.realtime_config = Some(RealtimeConfig {
        buffer_size: 50,
        update_frequency: 10,
        drift_detection: true,
        online_learning: true,
        max_latency_ms: 100,
    });

    let mut detector = QuantumAnomalyDetector::new(config)?;

    // Train detector
    let training_data = create_synthetic_data(100, 4, 1.0);
    detector.fit(&training_data)?;

    // Test streaming samples
    for i in 0..10 {
        let sample = Array1::from_vec(vec![0.1 * f64::from(i); 4]);
        let sample_2d = sample.insert_axis(scirs2_core::ndarray::Axis(0));
        let result = detector.detect(&sample_2d)?;
        let score = result.anomaly_scores[0]; // Extract first score from batch
        assert!(score >= 0.0);
    }

    Ok(())
}

/// Test data preprocessor
#[test]
fn test_data_preprocessor() -> Result<()> {
    let config = PreprocessingConfig {
        normalization: NormalizationType::ZScore,
        dimensionality_reduction: Some(DimensionalityReduction::PCA),
        feature_selection: Some(FeatureSelection::Variance),
        noise_filtering: Some(NoiseFiltering::GaussianFilter),
        missing_value_strategy: MissingValueStrategy::Mean,
    };

    let mut preprocessor = DataPreprocessor::new(config);

    // Create test data
    let data = create_synthetic_data(50, 6, 2.0);

    // Fit and transform
    let processed = preprocessor.fit_transform(&data)?;

    // Verify output dimensions (may be reduced due to feature selection)
    assert!(processed.nrows() == data.nrows());
    assert!(processed.ncols() <= data.ncols());

    // Test transform on new data
    let new_data = create_synthetic_data(10, 6, 2.0);
    let new_processed = preprocessor.transform(&new_data)?;
    assert_eq!(new_processed.ncols(), processed.ncols());

    Ok(())
}

/// Test isolation forest specifically
#[test]
fn test_isolation_forest() -> Result<()> {
    let config = create_default_anomaly_config();
    let mut forest = QuantumIsolationForest::new(config)?;

    // Create test data
    let data = create_synthetic_data(100, 4, 1.0);

    // Train forest
    forest.fit(&data)?;

    // Test detection
    let test_data = create_synthetic_data(20, 4, 1.0);
    let result = forest.detect(&test_data)?;

    // Verify forest-specific results
    assert!(result.method_results.contains_key("isolation_forest"));
    if let Some(MethodSpecificResult::IsolationForest {
        path_lengths,
        tree_depths,
    }) = result.method_results.get("isolation_forest")
    {
        assert_eq!(path_lengths.len(), 20);
        assert_eq!(tree_depths.len(), 20);
    }

    Ok(())
}

/// Test autoencoder specifically
#[test]
fn test_autoencoder() -> Result<()> {
    let mut config = create_default_anomaly_config();
    config.primary_method = AnomalyDetectionMethod::QuantumAutoencoder {
        encoder_layers: vec![4, 2],
        latent_dim: 1,
        decoder_layers: vec![2, 4],
        reconstruction_threshold: 0.5,
    };

    let mut autoencoder = QuantumAutoencoder::new(config)?;

    // Create test data
    let data = create_synthetic_data(50, 4, 1.0);

    // Train autoencoder
    autoencoder.fit(&data)?;

    // Test detection
    let test_data = create_synthetic_data(10, 4, 1.0);
    let result = autoencoder.detect(&test_data)?;

    // Verify autoencoder-specific results
    assert!(result.method_results.contains_key("autoencoder"));
    if let Some(MethodSpecificResult::Autoencoder {
        reconstruction_errors,
        latent_representations,
    }) = result.method_results.get("autoencoder")
    {
        assert_eq!(reconstruction_errors.len(), 10);
        assert_eq!(latent_representations.nrows(), 10);
        assert_eq!(latent_representations.ncols(), 1); // latent_dim
    }

    Ok(())
}

/// Test LOF specifically
#[test]
fn test_lof() -> Result<()> {
    let mut config = create_default_anomaly_config();
    config.primary_method = AnomalyDetectionMethod::QuantumLOF {
        n_neighbors: 5,
        contamination: 0.1,
        quantum_distance: true,
    };

    let mut lof = QuantumLOF::new(config)?;

    // Create test data
    let data = create_synthetic_data(50, 4, 1.0);

    // Train LOF
    lof.fit(&data)?;

    // Test detection
    let result = lof.detect(&data)?; // Use same data for simplicity

    // Verify LOF-specific results
    assert!(result.method_results.contains_key("lof"));

    Ok(())
}

/// Test invalid configurations
#[test]
fn test_invalid_configurations() {
    // Test zero qubits
    let mut config = create_default_anomaly_config();
    config.num_qubits = 0;
    assert!(QuantumAnomalyDetector::new(config).is_err());

    // Test invalid contamination
    let mut config = create_default_anomaly_config();
    config.contamination = 1.5; // > 1.0
    assert!(QuantumAnomalyDetector::new(config).is_err());

    let mut config = create_default_anomaly_config();
    config.contamination = -0.1; // < 0.0
    assert!(QuantumAnomalyDetector::new(config).is_err());
}

/// Test performance monitor
#[test]
fn test_performance_monitor() {
    let mut monitor = PerformanceMonitor::new();

    // Record some metrics
    monitor.record_latency(0.1);
    monitor.record_latency(0.2);
    monitor.record_latency(0.15);

    monitor.record_memory_usage(100.0);
    monitor.record_memory_usage(150.0);
    monitor.record_memory_usage(120.0);

    // Test calculations
    assert!((monitor.get_average_latency() - 0.15).abs() < 1e-10);
    assert_eq!(monitor.get_peak_memory_usage(), 150.0);
}

/// Test ensemble methods
#[test]
fn test_ensemble_methods() -> Result<()> {
    let mut config = create_default_anomaly_config();
    config.ensemble_methods = vec![
        AnomalyDetectionMethod::QuantumLOF {
            n_neighbors: 10,
            contamination: 0.1,
            quantum_distance: true,
        },
        AnomalyDetectionMethod::QuantumAutoencoder {
            encoder_layers: vec![4, 2],
            latent_dim: 1,
            decoder_layers: vec![2, 4],
            reconstruction_threshold: 0.5,
        },
    ];

    let mut detector = QuantumAnomalyDetector::new(config)?;

    // Create test data
    let training_data = create_synthetic_data(100, 4, 1.0);
    let test_data = create_synthetic_data(20, 4, 1.0);

    // Train and detect
    detector.fit(&training_data)?;
    let result = detector.detect(&test_data)?;

    // Should have results from multiple methods
    assert!(result.method_results.len() > 1);

    Ok(())
}

/// Test update functionality
#[test]
fn test_online_learning() -> Result<()> {
    let config = create_default_anomaly_config();
    let mut detector = QuantumAnomalyDetector::new(config)?;

    // Initial training
    let training_data = create_synthetic_data(100, 4, 1.0);
    detector.fit(&training_data)?;

    // Update with new data
    let new_data = create_synthetic_data(20, 4, 1.0);
    let labels = Some(Array1::zeros(20)); // All normal
    detector.update(&new_data, labels.as_ref())?;

    Ok(())
}