quantrs2_device/adaptive_compilation/
monitoring.rs

1//! Performance Monitoring and Anomaly Detection Configuration
2
3use std::time::Duration;
4
5/// Performance monitoring configuration
6#[derive(Debug, Clone)]
7pub struct PerformanceMonitoringConfig {
8    /// Enable comprehensive monitoring
9    pub enable_monitoring: bool,
10    /// Monitoring metrics
11    pub metrics: Vec<PerformanceMetric>,
12    /// Monitoring interval
13    pub monitoring_interval: Duration,
14    /// Historical data retention
15    pub history_retention: Duration,
16    /// Anomaly detection settings
17    pub anomaly_detection: AnomalyDetectionConfig,
18    /// Performance prediction settings
19    pub performance_prediction: PerformancePredictionConfig,
20}
21
22/// Performance metrics to monitor
23#[derive(Debug, Clone, PartialEq)]
24pub enum PerformanceMetric {
25    ExecutionTime,
26    Fidelity,
27    GateErrorRate,
28    DecoherenceRate,
29    ThroughputRate,
30    ResourceUtilization,
31    EnergyConsumption,
32    SuccessRate,
33    LatencyVariance,
34    CustomMetric(String),
35}
36
37/// Anomaly detection configuration
38#[derive(Debug, Clone)]
39pub struct AnomalyDetectionConfig {
40    /// Enable anomaly detection
41    pub enable_detection: bool,
42    /// Detection algorithms
43    pub detection_algorithms: Vec<AnomalyDetectionAlgorithm>,
44    /// Detection sensitivity
45    pub sensitivity: f64,
46    /// Historical baseline window
47    pub baseline_window: Duration,
48    /// Anomaly response configuration
49    pub response_config: AnomalyResponseConfig,
50}
51
52/// Anomaly detection algorithms
53#[derive(Debug, Clone, PartialEq)]
54pub enum AnomalyDetectionAlgorithm {
55    StatisticalOutlier,
56    MachineLearningBased,
57    ThresholdBased,
58    ChangePointDetection,
59    ClusteringBased,
60    EnsembleBased,
61}
62
63/// Configuration for anomaly response
64#[derive(Debug, Clone)]
65pub struct AnomalyResponseConfig {
66    /// Response strategies
67    pub response_strategies: Vec<AnomalyResponse>,
68    /// Response delay
69    pub response_delay: Duration,
70    /// Escalation thresholds
71    pub escalation_thresholds: EscalationThresholds,
72    /// Notification settings
73    pub notification_settings: NotificationSettings,
74}
75
76/// Responses to detected anomalies
77#[derive(Debug, Clone, PartialEq)]
78pub enum AnomalyResponse {
79    LogOnly,
80    RecalibrateDevice,
81    SwitchOptimizationStrategy,
82    IncreaseErrorMitigation,
83    NotifyOperator,
84    HaltExecution,
85    AutomaticRecovery,
86}
87
88/// Escalation thresholds for anomaly responses
89#[derive(Debug, Clone)]
90pub struct EscalationThresholds {
91    /// Warning threshold
92    pub warning_threshold: f64,
93    /// Critical threshold
94    pub critical_threshold: f64,
95    /// Emergency threshold
96    pub emergency_threshold: f64,
97    /// Escalation timeouts
98    pub escalation_timeouts: EscalationTimeouts,
99}
100
101/// Timeouts for different escalation levels
102#[derive(Debug, Clone)]
103pub struct EscalationTimeouts {
104    /// Warning level timeout
105    pub warning_timeout: Duration,
106    /// Critical level timeout
107    pub critical_timeout: Duration,
108    /// Emergency level timeout
109    pub emergency_timeout: Duration,
110}
111
112/// Notification settings for monitoring
113#[derive(Debug, Clone)]
114pub struct NotificationSettings {
115    /// Enable notifications
116    pub enable_notifications: bool,
117    /// Notification channels
118    pub notification_channels: Vec<NotificationChannel>,
119    /// Notification frequency limits
120    pub frequency_limits: NotificationFrequencyLimits,
121    /// Notification content configuration
122    pub content_config: NotificationContentConfig,
123}
124
125/// Notification delivery channels
126#[derive(Debug, Clone, PartialEq)]
127pub enum NotificationChannel {
128    Email,
129    SMS,
130    Dashboard,
131    API,
132    Webhook,
133    Log,
134}
135
136/// Frequency limits for notifications
137#[derive(Debug, Clone)]
138pub struct NotificationFrequencyLimits {
139    /// Maximum notifications per hour
140    pub max_per_hour: usize,
141    /// Minimum interval between notifications
142    pub min_interval: Duration,
143    /// Burst allowance
144    pub burst_allowance: usize,
145}
146
147/// Notification content configuration
148#[derive(Debug, Clone)]
149pub struct NotificationContentConfig {
150    /// Include performance data
151    pub include_performance_data: bool,
152    /// Include suggested actions
153    pub include_suggested_actions: bool,
154    /// Include historical context
155    pub include_historical_context: bool,
156    /// Content verbosity level
157    pub verbosity_level: VerbosityLevel,
158}
159
160/// Verbosity levels for notifications
161#[derive(Debug, Clone, PartialEq)]
162pub enum VerbosityLevel {
163    Minimal,
164    Standard,
165    Detailed,
166    Comprehensive,
167}
168
169/// Performance prediction configuration
170#[derive(Debug, Clone)]
171pub struct PerformancePredictionConfig {
172    /// Enable performance prediction
173    pub enable_prediction: bool,
174    /// Prediction models
175    pub prediction_models: Vec<PredictionModelType>,
176    /// Prediction horizon
177    pub prediction_horizon: Duration,
178    /// Model update frequency
179    pub model_update_frequency: Duration,
180    /// Prediction accuracy requirements
181    pub accuracy_requirements: AccuracyRequirements,
182}
183
184/// Types of prediction models
185#[derive(Debug, Clone, PartialEq)]
186pub enum PredictionModelType {
187    LinearRegression,
188    ARIMA,
189    LSTM,
190    GaussianProcess,
191    RandomForest,
192    EnsembleModel,
193}
194
195/// Accuracy requirements for predictions
196#[derive(Debug, Clone)]
197pub struct AccuracyRequirements {
198    /// Minimum accuracy threshold
199    pub min_accuracy: f64,
200    /// Confidence interval requirement
201    pub confidence_interval: f64,
202    /// Maximum prediction error
203    pub max_prediction_error: f64,
204    /// Model validation requirements
205    pub validation_requirements: ValidationRequirements,
206}
207
208/// Model validation requirements
209#[derive(Debug, Clone)]
210pub struct ValidationRequirements {
211    /// Validation method
212    pub validation_method: ValidationMethod,
213    /// Validation frequency
214    pub validation_frequency: Duration,
215    /// Cross-validation folds
216    pub cross_validation_folds: usize,
217    /// Test data percentage
218    pub test_data_percentage: f64,
219}
220
221/// Model validation methods
222#[derive(Debug, Clone, PartialEq)]
223pub enum ValidationMethod {
224    CrossValidation,
225    TimeSeriesSplit,
226    WalkForward,
227    Bootstrap,
228    HoldOut,
229}
230
231impl Default for PerformanceMonitoringConfig {
232    fn default() -> Self {
233        Self {
234            enable_monitoring: true,
235            metrics: vec![
236                PerformanceMetric::ExecutionTime,
237                PerformanceMetric::Fidelity,
238                PerformanceMetric::GateErrorRate,
239                PerformanceMetric::ThroughputRate,
240            ],
241            monitoring_interval: Duration::from_secs(10),
242            history_retention: Duration::from_secs(86400 * 7), // 7 days
243            anomaly_detection: AnomalyDetectionConfig::default(),
244            performance_prediction: PerformancePredictionConfig::default(),
245        }
246    }
247}
248
249impl Default for AnomalyDetectionConfig {
250    fn default() -> Self {
251        Self {
252            enable_detection: true,
253            detection_algorithms: vec![
254                AnomalyDetectionAlgorithm::StatisticalOutlier,
255                AnomalyDetectionAlgorithm::ThresholdBased,
256            ],
257            sensitivity: 0.95,
258            baseline_window: Duration::from_secs(3600), // 1 hour
259            response_config: AnomalyResponseConfig::default(),
260        }
261    }
262}
263
264impl Default for AnomalyResponseConfig {
265    fn default() -> Self {
266        Self {
267            response_strategies: vec![AnomalyResponse::LogOnly, AnomalyResponse::NotifyOperator],
268            response_delay: Duration::from_secs(5),
269            escalation_thresholds: EscalationThresholds::default(),
270            notification_settings: NotificationSettings::default(),
271        }
272    }
273}
274
275impl Default for EscalationThresholds {
276    fn default() -> Self {
277        Self {
278            warning_threshold: 0.1,
279            critical_threshold: 0.05,
280            emergency_threshold: 0.01,
281            escalation_timeouts: EscalationTimeouts::default(),
282        }
283    }
284}
285
286impl Default for EscalationTimeouts {
287    fn default() -> Self {
288        Self {
289            warning_timeout: Duration::from_secs(300),  // 5 minutes
290            critical_timeout: Duration::from_secs(60),  // 1 minute
291            emergency_timeout: Duration::from_secs(10), // 10 seconds
292        }
293    }
294}
295
296impl Default for NotificationSettings {
297    fn default() -> Self {
298        Self {
299            enable_notifications: true,
300            notification_channels: vec![NotificationChannel::Dashboard, NotificationChannel::Log],
301            frequency_limits: NotificationFrequencyLimits::default(),
302            content_config: NotificationContentConfig::default(),
303        }
304    }
305}
306
307impl Default for NotificationFrequencyLimits {
308    fn default() -> Self {
309        Self {
310            max_per_hour: 10,
311            min_interval: Duration::from_secs(60),
312            burst_allowance: 3,
313        }
314    }
315}
316
317impl Default for NotificationContentConfig {
318    fn default() -> Self {
319        Self {
320            include_performance_data: true,
321            include_suggested_actions: true,
322            include_historical_context: false,
323            verbosity_level: VerbosityLevel::Standard,
324        }
325    }
326}
327
328impl Default for PerformancePredictionConfig {
329    fn default() -> Self {
330        Self {
331            enable_prediction: true,
332            prediction_models: vec![
333                PredictionModelType::LinearRegression,
334                PredictionModelType::ARIMA,
335            ],
336            prediction_horizon: Duration::from_secs(3600), // 1 hour
337            model_update_frequency: Duration::from_secs(1800), // 30 minutes
338            accuracy_requirements: AccuracyRequirements::default(),
339        }
340    }
341}
342
343impl Default for AccuracyRequirements {
344    fn default() -> Self {
345        Self {
346            min_accuracy: 0.8,
347            confidence_interval: 0.95,
348            max_prediction_error: 0.1,
349            validation_requirements: ValidationRequirements::default(),
350        }
351    }
352}
353
354impl Default for ValidationRequirements {
355    fn default() -> Self {
356        Self {
357            validation_method: ValidationMethod::CrossValidation,
358            validation_frequency: Duration::from_secs(3600 * 6), // 6 hours
359            cross_validation_folds: 5,
360            test_data_percentage: 0.2,
361        }
362    }
363}