quantrs2_circuit/profiler/
metrics.rs

1//! Performance metrics collection types and utilities
2//!
3//! This module provides types and functionality for collecting and aggregating
4//! performance metrics during quantum circuit profiling.
5
6use serde::{Deserialize, Serialize};
7use std::collections::{HashMap, VecDeque};
8use std::time::{Duration, SystemTime};
9
10pub struct MetricsCollector {
11    /// Collected performance metrics
12    pub metrics: VecDeque<PerformanceMetric>,
13    /// Metric aggregation rules
14    pub aggregation_rules: HashMap<String, AggregationRule>,
15    /// Real-time metric streams
16    pub metric_streams: HashMap<String, MetricStream>,
17    /// Collection statistics
18    pub collection_stats: CollectionStatistics,
19}
20
21/// Individual performance metric
22#[derive(Debug, Clone, Serialize, Deserialize)]
23pub struct PerformanceMetric {
24    /// Metric name
25    pub name: String,
26    /// Metric value
27    pub value: f64,
28    /// Measurement timestamp
29    pub timestamp: SystemTime,
30    /// Metric category
31    pub category: MetricCategory,
32    /// Additional metadata
33    pub metadata: HashMap<String, String>,
34    /// Confidence score
35    pub confidence: f64,
36    /// Statistical significance
37    pub significance: Option<f64>,
38}
39
40/// Categories of performance metrics
41#[derive(Debug, Clone, Serialize, Deserialize)]
42pub enum MetricCategory {
43    /// Execution timing metrics
44    Timing,
45    /// Memory usage metrics
46    Memory,
47    /// Resource utilization metrics
48    Resource,
49    /// Gate operation metrics
50    Gate,
51    /// Circuit complexity metrics
52    Complexity,
53    /// Error rate metrics
54    Error,
55    /// Throughput metrics
56    Throughput,
57    /// Latency metrics
58    Latency,
59    /// Custom metric category
60    Custom { name: String },
61}
62
63/// Metric aggregation rules
64#[derive(Debug, Clone, Serialize, Deserialize)]
65pub struct AggregationRule {
66    /// Aggregation function type
67    pub function: AggregationFunction,
68    /// Time window for aggregation
69    pub window: Duration,
70    /// Minimum samples required
71    pub min_samples: usize,
72    /// Statistical confidence level
73    pub confidence_level: f64,
74}
75
76/// Aggregation function types
77#[derive(Debug, Clone, Serialize, Deserialize)]
78pub enum AggregationFunction {
79    /// Mean value
80    Mean,
81    /// Median value
82    Median,
83    /// Maximum value
84    Maximum,
85    /// Minimum value
86    Minimum,
87    /// Standard deviation
88    StandardDeviation,
89    /// Percentile value
90    Percentile { percentile: f64 },
91    /// Moving average
92    MovingAverage { window_size: usize },
93    /// Exponential moving average
94    ExponentialMovingAverage { alpha: f64 },
95}
96
97/// Real-time metric stream
98#[derive(Debug, Clone)]
99pub struct MetricStream {
100    /// Stream name
101    pub name: String,
102    /// Current value
103    pub current_value: f64,
104    /// Value history
105    pub history: VecDeque<f64>,
106    /// Stream statistics
107    pub statistics: StreamStatistics,
108    /// Anomaly detection threshold
109    pub anomaly_threshold: f64,
110}
111
112/// Stream statistics
113#[derive(Debug, Clone)]
114pub struct StreamStatistics {
115    /// Sample count
116    pub sample_count: usize,
117    /// Mean value
118    pub mean: f64,
119    /// Standard deviation
120    pub std_dev: f64,
121    /// Minimum value
122    pub min: f64,
123    /// Maximum value
124    pub max: f64,
125    /// Trend direction
126    pub trend: TrendDirection,
127}
128
129/// Trend direction analysis
130#[derive(Debug, Clone, Serialize, Deserialize)]
131pub enum TrendDirection {
132    /// Increasing trend
133    Increasing,
134    /// Decreasing trend
135    Decreasing,
136    /// Stable trend
137    Stable,
138    /// Oscillating trend
139    Oscillating,
140    /// Unknown trend
141    Unknown,
142}
143
144/// Collection statistics
145#[derive(Debug, Clone)]
146pub struct CollectionStatistics {
147    /// Total metrics collected
148    pub total_metrics: usize,
149    /// Collection duration
150    pub collection_duration: Duration,
151    /// Average collection rate
152    pub average_rate: f64,
153    /// Collection errors
154    pub collection_errors: usize,
155    /// Memory usage for collection
156    pub memory_usage: usize,
157}