quantrs2-core 0.1.3

Core types and traits for the QuantRS2 quantum computing framework
Documentation
//! Auto-generated module
//!
//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)

use crate::{
    error::{QuantRS2Error, QuantRS2Result},
    hardware_compilation::{HardwarePlatform, NativeGateType},
    qubit::QubitId,
};
use std::{
    collections::{HashMap, HashSet, VecDeque},
    fmt,
    sync::{Arc, RwLock},
    thread,
    time::{Duration, SystemTime},
};

use super::types::{
    Alert, AlertLevel, AlertStatus, AlertThresholds, Anomaly, Correlation, DifficultyLevel,
    ExpectedImprovement, ExportDestination, ExportFormat, ExportSettings, MetricMeasurement,
    MetricType, MetricValue, MonitoringConfig, MonitoringStatus, OptimizationRecommendation,
    PlatformMonitoringConfig, Prediction, RealtimeDataStore, RealtimeMonitor,
    RecommendationPriority, RecommendationType, SuperconductingCollector, SystemStatus,
    TrainingExample, TrendAnalysis, WidgetConfig, WidgetData,
};

/// Trait for platform-specific metric collection
pub trait MetricCollector: std::fmt::Debug + Send + Sync {
    /// Collect metrics from the platform
    fn collect_metrics(&self) -> QuantRS2Result<Vec<MetricMeasurement>>;
    /// Get supported metric types
    fn supported_metrics(&self) -> HashSet<MetricType>;
    /// Platform identifier
    fn platform(&self) -> HardwarePlatform;
    /// Initialize connection to hardware
    fn initialize(&mut self) -> QuantRS2Result<()>;
    /// Check connection status
    fn is_connected(&self) -> bool;
    /// Disconnect from hardware
    fn disconnect(&mut self) -> QuantRS2Result<()>;
}
/// Trend analysis trait
pub trait TrendAnalyzer: std::fmt::Debug + Send + Sync {
    /// Analyze trend in metric data
    fn analyze_trend(&self, data: &[MetricMeasurement]) -> QuantRS2Result<TrendAnalysis>;
    /// Get analyzer name
    fn name(&self) -> &str;
}
/// Anomaly detection trait
pub trait AnomalyDetector: std::fmt::Debug + Send + Sync {
    /// Detect anomalies in metric data
    fn detect_anomalies(&self, data: &[MetricMeasurement]) -> QuantRS2Result<Vec<Anomaly>>;
    /// Get detector name
    fn name(&self) -> &str;
    /// Get confidence threshold
    fn confidence_threshold(&self) -> f64;
}
/// Correlation analysis trait
pub trait CorrelationAnalyzer: std::fmt::Debug + Send + Sync {
    /// Analyze correlations between metrics
    fn analyze_correlations(
        &self,
        data: &HashMap<MetricType, Vec<MetricMeasurement>>,
    ) -> QuantRS2Result<Vec<Correlation>>;
    /// Get analyzer name
    fn name(&self) -> &str;
}
/// Predictive modeling trait
pub trait PredictiveModel: std::fmt::Debug + Send + Sync {
    /// Predict future values
    fn predict(
        &self,
        historical_data: &[MetricMeasurement],
        horizon: Duration,
    ) -> QuantRS2Result<Prediction>;
    /// Update model with new data
    fn update(&mut self, new_data: &[MetricMeasurement]) -> QuantRS2Result<()>;
    /// Get model name
    fn name(&self) -> &str;
    /// Get model accuracy
    fn accuracy(&self) -> f64;
}
/// Alert handler trait
pub trait AlertHandler: std::fmt::Debug + Send + Sync {
    /// Handle an alert
    fn handle_alert(&self, alert: &Alert) -> QuantRS2Result<()>;
    /// Get handler name
    fn name(&self) -> &str;
    /// Check if handler can handle this alert level
    fn can_handle(&self, level: AlertLevel) -> bool;
}
/// Optimization strategy trait
pub trait OptimizationStrategy: std::fmt::Debug + Send + Sync {
    /// Analyze performance data and generate recommendations
    fn analyze(&self, data: &RealtimeDataStore) -> QuantRS2Result<Vec<OptimizationRecommendation>>;
    /// Get strategy name
    fn name(&self) -> &str;
    /// Get strategy priority
    fn priority(&self) -> u32;
}
/// Machine learning model trait
pub trait MLModel: std::fmt::Debug + Send + Sync {
    /// Train model with historical data
    fn train(&mut self, training_data: &[TrainingExample]) -> QuantRS2Result<()>;
    /// Predict recommendations
    fn predict(
        &self,
        input_data: &[MetricMeasurement],
    ) -> QuantRS2Result<Vec<OptimizationRecommendation>>;
    /// Get model accuracy
    fn accuracy(&self) -> f64;
    /// Get model name
    fn name(&self) -> &str;
}
/// Dashboard widget trait
pub trait DashboardWidget: std::fmt::Debug + Send + Sync {
    /// Render widget with current data
    fn render(&self, data: &RealtimeDataStore) -> QuantRS2Result<WidgetData>;
    /// Get widget configuration
    fn get_config(&self) -> WidgetConfig;
    /// Update widget configuration
    fn update_config(&mut self, config: WidgetConfig) -> QuantRS2Result<()>;
}
/// Data exporter trait
pub trait DataExporter: std::fmt::Debug + Send + Sync {
    /// Export data in specific format
    fn export(
        &self,
        data: &[MetricMeasurement],
        destination: &ExportDestination,
    ) -> QuantRS2Result<()>;
    /// Get supported format
    fn format(&self) -> ExportFormat;
    /// Validate export configuration
    fn validate_config(&self, destination: &ExportDestination) -> QuantRS2Result<()>;
}
#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_realtime_monitor_creation() {
        let config = MonitoringConfig::default();
        let monitor = RealtimeMonitor::new(config);
        assert!(monitor.is_ok());
    }
    #[test]
    fn test_metric_measurement() {
        let measurement = MetricMeasurement {
            metric_type: MetricType::GateErrorRate,
            value: MetricValue::Float(0.001),
            timestamp: SystemTime::now(),
            qubit: Some(QubitId::new(0)),
            gate_type: Some(NativeGateType::CNOT),
            metadata: HashMap::new(),
            uncertainty: Some(0.0001),
        };
        assert_eq!(measurement.metric_type, MetricType::GateErrorRate);
        assert!(matches!(measurement.value, MetricValue::Float(0.001)));
    }
    #[test]
    fn test_superconducting_collector() {
        let config = PlatformMonitoringConfig {
            platform: HardwarePlatform::Superconducting,
            monitored_metrics: HashSet::new(),
            sampling_rates: HashMap::new(),
            custom_thresholds: HashMap::new(),
            connection_settings: HashMap::new(),
        };
        let mut collector = SuperconductingCollector::new(config);
        assert_eq!(collector.platform(), HardwarePlatform::Superconducting);
        assert!(!collector.is_connected());
        assert!(collector.initialize().is_ok());
        assert!(collector.is_connected());
        let metrics = collector.collect_metrics();
        assert!(metrics.is_ok());
        assert!(!metrics
            .expect("Metrics collection should succeed")
            .is_empty());
    }
    #[test]
    fn test_data_store() {
        let mut store = RealtimeDataStore::new(Duration::from_secs(3600));
        let measurement = MetricMeasurement {
            metric_type: MetricType::GateErrorRate,
            value: MetricValue::Float(0.001),
            timestamp: SystemTime::now(),
            qubit: None,
            gate_type: None,
            metadata: HashMap::new(),
            uncertainty: None,
        };
        store.add_measurement(measurement);
        assert!(store.time_series.contains_key(&MetricType::GateErrorRate));
        assert!(store
            .aggregated_stats
            .contains_key(&MetricType::GateErrorRate));
        let stats = store
            .aggregated_stats
            .get(&MetricType::GateErrorRate)
            .expect("GateErrorRate stats should exist after adding measurement");
        assert_eq!(stats.sample_count, 1);
        assert_eq!(stats.mean, 0.001);
    }
    #[test]
    fn test_alert_creation() {
        let alert = Alert {
            id: "test_alert".to_string(),
            level: AlertLevel::Warning,
            message: "Test alert message".to_string(),
            affected_metrics: vec![MetricType::GateErrorRate],
            timestamp: SystemTime::now(),
            source: "test".to_string(),
            suggested_actions: vec!["Check calibration".to_string()],
            status: AlertStatus::Active,
        };
        assert_eq!(alert.level, AlertLevel::Warning);
        assert_eq!(alert.status, AlertStatus::Active);
        assert!(alert.affected_metrics.contains(&MetricType::GateErrorRate));
    }
    #[test]
    fn test_monitoring_config() {
        let config = MonitoringConfig::default();
        assert_eq!(config.monitoring_interval, Duration::from_secs(1));
        assert_eq!(config.data_retention_period, Duration::from_secs(24 * 3600));
        assert!(!config.export_settings.enable_export);
    }
    #[test]
    fn test_metric_value_types() {
        let float_value = MetricValue::Float(1.23);
        let int_value = MetricValue::Integer(42);
        let bool_value = MetricValue::Boolean(true);
        let duration_value = MetricValue::Duration(Duration::from_millis(100));
        assert!(matches!(float_value, MetricValue::Float(1.23)));
        assert!(matches!(int_value, MetricValue::Integer(42)));
        assert!(matches!(bool_value, MetricValue::Boolean(true)));
        assert!(matches!(duration_value, MetricValue::Duration(_)));
    }
    #[test]
    fn test_alert_thresholds() {
        let thresholds = AlertThresholds::default();
        assert_eq!(thresholds.max_gate_error_rate, 0.01);
        assert_eq!(thresholds.max_readout_error_rate, 0.05);
        assert_eq!(thresholds.min_coherence_time, Duration::from_micros(50));
    }
    #[test]
    fn test_optimization_recommendation() {
        let recommendation = OptimizationRecommendation {
            id: "test_rec".to_string(),
            recommendation_type: RecommendationType::GateOptimization,
            description: "Optimize gate sequence".to_string(),
            affected_components: vec!["qubit_0".to_string()],
            expected_improvement: ExpectedImprovement {
                fidelity_improvement: Some(0.001),
                speed_improvement: Some(0.1),
                error_rate_reduction: Some(0.0005),
                resource_savings: None,
            },
            implementation_difficulty: DifficultyLevel::Medium,
            priority: RecommendationPriority::High,
            timestamp: SystemTime::now(),
        };
        assert_eq!(
            recommendation.recommendation_type,
            RecommendationType::GateOptimization
        );
        assert_eq!(
            recommendation.implementation_difficulty,
            DifficultyLevel::Medium
        );
        assert_eq!(recommendation.priority, RecommendationPriority::High);
    }
    #[test]
    fn test_export_settings() {
        let settings = ExportSettings {
            enable_export: true,
            export_formats: vec![ExportFormat::JSON, ExportFormat::CSV],
            export_destinations: vec![],
            export_frequency: Duration::from_secs(1800),
            compression_enabled: true,
        };
        assert!(settings.enable_export);
        assert_eq!(settings.export_formats.len(), 2);
        assert!(settings.compression_enabled);
    }
    #[test]
    fn test_monitoring_status() {
        let status = MonitoringStatus::new();
        assert_eq!(status.overall_status, SystemStatus::Offline);
        assert_eq!(status.active_collectors, 0);
        assert_eq!(status.total_data_points, 0);
    }
}