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
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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
//! Machine learning and AutoML configuration for monitoring.

use serde::{Deserialize, Serialize};
use std::time::Duration;

/// AutoML configuration for monitoring optimization
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct AutoMLConfig {
    /// Enable AutoML
    pub enabled: bool,
    /// AutoML tasks
    pub tasks: Vec<AutoMLTask>,
    /// Model selection
    pub model_selection: ModelSelectionConfig,
    /// Hyperparameter optimization
    pub hyperparameter_optimization: HyperparameterOptimizationConfig,
    /// Model deployment
    pub deployment: ModelDeploymentConfig,
}

/// AutoML tasks
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum AutoMLTask {
    AnomalyDetection,
    ForecastingOptimization,
    AlertClassification,
    ResourcePrediction,
    CostOptimization,
    PerformanceTuning,
    Custom(String),
}

/// Model selection configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelSelectionConfig {
    /// Selection criteria
    pub criteria: Vec<SelectionCriterion>,
    /// Evaluation metrics
    pub metrics: Vec<EvaluationMetric>,
    /// Cross-validation
    pub cross_validation: AutoMLCrossValidationConfig,
    /// Model families to consider
    pub model_families: Vec<ModelFamily>,
}

/// Selection criteria
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum SelectionCriterion {
    Accuracy,
    Precision,
    Recall,
    F1Score,
    AUC,
    Speed,
    MemoryUsage,
    Interpretability,
    Custom(String),
}

/// Evaluation metrics
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum EvaluationMetric {
    Accuracy,
    Precision,
    Recall,
    F1Score,
    RocAuc,
    MAE,
    MSE,
    RMSE,
    Custom(String),
}

/// AutoML cross-validation configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AutoMLCrossValidationConfig {
    /// CV method
    pub method: CrossValidationMethod,
    /// Number of folds
    pub folds: u32,
    /// Stratified sampling
    pub stratified: bool,
    /// Random seed
    pub seed: Option<u64>,
}

/// Cross-validation methods
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum CrossValidationMethod {
    KFold,
    StratifiedKFold,
    TimeSeriesSplit,
    Custom(String),
}

/// Model families
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ModelFamily {
    LinearModels,
    TreeModels,
    EnsembleMethods,
    NeuralNetworks,
    SupportVectorMachines,
    NaiveBayes,
    KNearestNeighbors,
    Custom(String),
}

/// Hyperparameter optimization configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HyperparameterOptimizationConfig {
    /// Optimization method
    pub method: OptimizationMethod,
    /// Search space
    pub search_space: SearchSpaceConfig,
    /// Optimization budget
    pub budget: OptimizationBudget,
    /// Early stopping
    pub early_stopping: EarlyStoppingConfig,
}

/// Optimization methods
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum OptimizationMethod {
    RandomSearch,
    GridSearch,
    BayesianOptimization,
    GeneticAlgorithm,
    ParticleSwarmOptimization,
    Custom(String),
}

/// Search space configuration
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct SearchSpaceConfig {
    /// Parameter definitions
    pub parameters: Vec<ParameterDefinition>,
    /// Constraints
    pub constraints: Vec<ParameterConstraint>,
}

/// Parameter definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ParameterDefinition {
    /// Parameter name
    pub name: String,
    /// Parameter type
    pub param_type: ParameterType,
    /// Default value
    pub default: Option<String>,
    /// Description
    pub description: String,
}

/// Parameter types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ParameterType {
    Integer { min: i64, max: i64 },
    Float { min: f64, max: f64 },
    Categorical { values: Vec<String> },
    Boolean,
    Custom(String),
}

/// Parameter constraint
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ParameterConstraint {
    /// Constraint type
    pub constraint_type: ConstraintType,
    /// Parameters involved
    pub parameters: Vec<String>,
    /// Constraint expression
    pub expression: String,
}

/// Constraint types
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ConstraintType {
    LinearEquality,
    LinearInequality,
    NonLinear,
    Conditional,
    Custom(String),
}

/// Optimization budget
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OptimizationBudget {
    /// Maximum evaluations
    pub max_evaluations: u32,
    /// Maximum time
    pub max_time: Duration,
    /// Maximum parallel trials
    pub max_parallel: u32,
    /// Resource limits
    pub resources: ResourceLimits,
}

/// Resource limits
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceLimits {
    /// CPU cores
    pub cpu_cores: Option<u32>,
    /// Memory (GB)
    pub memory_gb: Option<u32>,
    /// GPU count
    pub gpu_count: Option<u32>,
}

/// Early stopping configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EarlyStoppingConfig {
    /// Enable early stopping
    pub enabled: bool,
    /// Patience (evaluations without improvement)
    pub patience: u32,
    /// Minimum improvement threshold
    pub min_improvement: f64,
    /// Evaluation frequency
    pub evaluation_frequency: u32,
}

/// Model deployment configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelDeploymentConfig {
    /// Deployment strategy
    pub strategy: DeploymentStrategy,
    /// Model versioning
    pub versioning: VersioningConfig,
    /// Model monitoring
    pub monitoring: ModelMonitoringConfig,
    /// Rollback configuration
    pub rollback: RollbackConfig,
}

/// Deployment strategies
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum DeploymentStrategy {
    BlueGreen,
    Canary,
    RollingUpdate,
    Immediate,
    Custom(String),
}

/// Versioning configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VersioningConfig {
    /// Version scheme
    pub scheme: VersionScheme,
    /// Model registry
    pub registry: ModelRegistryConfig,
    /// Artifact storage
    pub artifacts: ArtifactStorageConfig,
}

/// Version schemes
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum VersionScheme {
    Semantic,
    Sequential,
    Timestamp,
    Custom(String),
}

/// Model registry configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelRegistryConfig {
    /// Registry type
    pub registry_type: RegistryType,
    /// Connection settings
    pub connection: RegistryConnection,
    /// Metadata tracking
    pub metadata: MetadataConfig,
}

/// Registry types
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum RegistryType {
    MLflow,
    ModelDB,
    KubeflowPipelines,
    Custom(String),
}

/// Registry connection
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RegistryConnection {
    /// Connection URL
    pub url: String,
    /// Authentication
    pub auth: AuthConfig,
    /// Connection timeout
    pub timeout: Duration,
}

/// Authentication configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthConfig {
    /// Auth method
    pub method: AuthMethod,
    /// Credentials
    pub credentials: std::collections::HashMap<String, String>,
}

/// Authentication methods
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum AuthMethod {
    Token,
    BasicAuth,
    OAuth2,
    Certificate,
    Custom(String),
}

/// Metadata configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetadataConfig {
    /// Track hyperparameters
    pub track_hyperparameters: bool,
    /// Track metrics
    pub track_metrics: bool,
    /// Track artifacts
    pub track_artifacts: bool,
    /// Custom metadata
    pub custom_fields: Vec<MetadataField>,
}

/// Metadata field
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetadataField {
    /// Field name
    pub name: String,
    /// Field type
    pub field_type: MetadataFieldType,
    /// Required field
    pub required: bool,
}

/// Metadata field types
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum MetadataFieldType {
    String,
    Number,
    Boolean,
    Date,
    JSON,
    Custom(String),
}

/// Artifact storage configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ArtifactStorageConfig {
    /// Storage backend
    pub backend: StorageBackend,
    /// Storage path
    pub path: String,
    /// Compression settings
    pub compression: CompressionConfig,
}

/// Storage backends
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum StorageBackend {
    Local,
    S3,
    GCS,
    Azure,
    HDFS,
    Custom(String),
}

/// Compression configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompressionConfig {
    /// Compression enabled
    pub enabled: bool,
    /// Compression algorithm
    pub algorithm: CompressionAlgorithm,
    /// Compression level
    pub level: u8,
}

/// Compression algorithms
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum CompressionAlgorithm {
    Gzip,
    Lz4,
    Zstd,
    Bzip2,
    Custom(String),
}

/// Model monitoring configuration
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ModelMonitoringConfig {
    /// Performance monitoring
    pub performance: PerformanceMonitoringConfig,
    /// Data drift detection
    pub data_drift: DataDriftConfig,
    /// Model drift detection
    pub model_drift: ModelDriftConfig,
    /// Alert configuration
    pub alerts: ModelAlertConfig,
}

/// Performance monitoring configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceMonitoringConfig {
    /// Metrics to track
    pub metrics: Vec<PerformanceMetric>,
    /// Monitoring frequency
    pub frequency: Duration,
    /// Performance thresholds
    pub thresholds: PerformanceThresholds,
}

/// Performance metrics
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum PerformanceMetric {
    Accuracy,
    Precision,
    Recall,
    F1Score,
    Latency,
    Throughput,
    MemoryUsage,
    CPUUsage,
    Custom(String),
}

/// Performance thresholds
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceThresholds {
    /// Accuracy threshold
    pub accuracy: Option<f64>,
    /// Latency threshold (ms)
    pub latency_ms: Option<f64>,
    /// Throughput threshold (requests/sec)
    pub throughput: Option<f64>,
    /// Memory threshold (MB)
    pub memory_mb: Option<f64>,
}

/// Data drift configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DataDriftConfig {
    /// Enable drift detection
    pub enabled: bool,
    /// Detection methods
    pub methods: Vec<DriftDetectionMethod>,
    /// Detection frequency
    pub frequency: Duration,
    /// Reference window
    pub reference_window: Duration,
}

/// Drift detection methods
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum DriftDetectionMethod {
    KolmogorovSmirnov,
    ChiSquare,
    PopulationStabilityIndex,
    JensenShannonDivergence,
    Custom(String),
}

/// Model drift configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelDriftConfig {
    /// Enable model drift detection
    pub enabled: bool,
    /// Drift threshold
    pub threshold: f64,
    /// Detection window
    pub window: Duration,
    /// Retraining trigger
    pub retrain_trigger: RetrainTrigger,
}

/// Retrain triggers
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum RetrainTrigger {
    PerformanceDegradation,
    DataDrift,
    TimeBased,
    Manual,
    Custom(String),
}

/// Model alert configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelAlertConfig {
    /// Alert types
    pub types: Vec<ModelAlertType>,
    /// Notification channels
    pub channels: Vec<String>,
    /// Alert thresholds
    pub thresholds: AlertThresholds,
}

/// Model alert types
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ModelAlertType {
    PerformanceDegradation,
    DataDrift,
    ModelDrift,
    PredictionBias,
    ServiceUnavailable,
    Custom(String),
}

/// Alert thresholds
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AlertThresholds {
    /// Performance degradation threshold
    pub performance_degradation: f64,
    /// Data drift threshold
    pub data_drift: f64,
    /// Model drift threshold
    pub model_drift: f64,
    /// Bias threshold
    pub bias: f64,
}

/// Rollback configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RollbackConfig {
    /// Enable automatic rollback
    pub auto_rollback: bool,
    /// Rollback triggers
    pub triggers: Vec<RollbackTrigger>,
    /// Rollback strategy
    pub strategy: RollbackStrategy,
    /// Rollback timeout
    pub timeout: Duration,
}

/// Rollback triggers
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum RollbackTrigger {
    PerformanceDegradation,
    ErrorRateIncrease,
    LatencyIncrease,
    Manual,
    Custom(String),
}

/// Rollback strategies
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum RollbackStrategy {
    PreviousVersion,
    StableVersion,
    SpecificVersion,
    Custom(String),
}

impl Default for ModelSelectionConfig {
    fn default() -> Self {
        Self {
            criteria: vec![SelectionCriterion::Accuracy],
            metrics: vec![EvaluationMetric::Accuracy],
            cross_validation: AutoMLCrossValidationConfig::default(),
            model_families: vec![ModelFamily::LinearModels, ModelFamily::TreeModels],
        }
    }
}

impl Default for AutoMLCrossValidationConfig {
    fn default() -> Self {
        Self {
            method: CrossValidationMethod::KFold,
            folds: 5,
            stratified: true,
            seed: Some(42),
        }
    }
}

impl Default for HyperparameterOptimizationConfig {
    fn default() -> Self {
        Self {
            method: OptimizationMethod::BayesianOptimization,
            search_space: SearchSpaceConfig::default(),
            budget: OptimizationBudget::default(),
            early_stopping: EarlyStoppingConfig::default(),
        }
    }
}

impl Default for OptimizationBudget {
    fn default() -> Self {
        Self {
            max_evaluations: 100,
            max_time: Duration::from_secs(3600), // 1 hour
            max_parallel: 4,
            resources: ResourceLimits::default(),
        }
    }
}

impl Default for ResourceLimits {
    fn default() -> Self {
        Self {
            cpu_cores: Some(4),
            memory_gb: Some(8),
            gpu_count: None,
        }
    }
}

impl Default for EarlyStoppingConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            patience: 10,
            min_improvement: 0.001,
            evaluation_frequency: 5,
        }
    }
}

impl Default for ModelDeploymentConfig {
    fn default() -> Self {
        Self {
            strategy: DeploymentStrategy::RollingUpdate,
            versioning: VersioningConfig::default(),
            monitoring: ModelMonitoringConfig::default(),
            rollback: RollbackConfig::default(),
        }
    }
}

impl Default for VersioningConfig {
    fn default() -> Self {
        Self {
            scheme: VersionScheme::Semantic,
            registry: ModelRegistryConfig::default(),
            artifacts: ArtifactStorageConfig::default(),
        }
    }
}

impl Default for ModelRegistryConfig {
    fn default() -> Self {
        Self {
            registry_type: RegistryType::MLflow,
            connection: RegistryConnection::default(),
            metadata: MetadataConfig::default(),
        }
    }
}

impl Default for RegistryConnection {
    fn default() -> Self {
        Self {
            url: "http://localhost:5000".to_string(),
            auth: AuthConfig::default(),
            timeout: Duration::from_secs(30),
        }
    }
}

impl Default for AuthConfig {
    fn default() -> Self {
        Self {
            method: AuthMethod::Token,
            credentials: std::collections::HashMap::new(),
        }
    }
}

impl Default for MetadataConfig {
    fn default() -> Self {
        Self {
            track_hyperparameters: true,
            track_metrics: true,
            track_artifacts: true,
            custom_fields: vec![],
        }
    }
}

impl Default for ArtifactStorageConfig {
    fn default() -> Self {
        Self {
            backend: StorageBackend::Local,
            path: "./models".to_string(),
            compression: CompressionConfig::default(),
        }
    }
}

impl Default for CompressionConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            algorithm: CompressionAlgorithm::Gzip,
            level: 6,
        }
    }
}

impl Default for PerformanceMonitoringConfig {
    fn default() -> Self {
        Self {
            metrics: vec![PerformanceMetric::Accuracy, PerformanceMetric::Latency],
            frequency: Duration::from_secs(300), // 5 minutes
            thresholds: PerformanceThresholds::default(),
        }
    }
}

impl Default for PerformanceThresholds {
    fn default() -> Self {
        Self {
            accuracy: Some(0.85),
            latency_ms: Some(1000.0),
            throughput: Some(100.0),
            memory_mb: Some(1024.0),
        }
    }
}

impl Default for DataDriftConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            methods: vec![DriftDetectionMethod::KolmogorovSmirnov],
            frequency: Duration::from_secs(3600), // hourly
            reference_window: Duration::from_secs(86400 * 7), // 1 week
        }
    }
}

impl Default for ModelDriftConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            threshold: 0.1,
            window: Duration::from_secs(86400), // daily
            retrain_trigger: RetrainTrigger::PerformanceDegradation,
        }
    }
}

impl Default for ModelAlertConfig {
    fn default() -> Self {
        Self {
            types: vec![ModelAlertType::PerformanceDegradation],
            channels: vec!["email".to_string()],
            thresholds: AlertThresholds::default(),
        }
    }
}

impl Default for AlertThresholds {
    fn default() -> Self {
        Self {
            performance_degradation: 0.05,
            data_drift: 0.1,
            model_drift: 0.1,
            bias: 0.05,
        }
    }
}

impl Default for RollbackConfig {
    fn default() -> Self {
        Self {
            auto_rollback: false,
            triggers: vec![RollbackTrigger::PerformanceDegradation],
            strategy: RollbackStrategy::PreviousVersion,
            timeout: Duration::from_secs(300), // 5 minutes
        }
    }
}