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
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
//! Result types and data structures for mid-circuit measurements

use std::collections::HashMap;
use std::time::{Duration, SystemTime};

use quantrs2_core::qubit::QubitId;
use scirs2_core::ndarray::{Array1, Array2};
use serde::{Deserialize, Serialize};

/// Enhanced mid-circuit measurement execution result with SciRS2 analytics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MidCircuitExecutionResult {
    /// Final quantum measurement results
    pub final_measurements: HashMap<String, usize>,
    /// Classical register states
    pub classical_registers: HashMap<String, Vec<u8>>,
    /// Mid-circuit measurement history
    pub measurement_history: Vec<MeasurementEvent>,
    /// Execution statistics
    pub execution_stats: ExecutionStats,
    /// Performance metrics
    pub performance_metrics: PerformanceMetrics,
    /// Error analysis
    pub error_analysis: Option<ErrorAnalysis>,
    /// Advanced analytics results
    pub analytics_results: AdvancedAnalyticsResults,
    /// Prediction results
    pub prediction_results: Option<MeasurementPredictionResults>,
    /// Optimization recommendations
    pub optimization_recommendations: OptimizationRecommendations,
    /// Adaptive learning insights
    pub adaptive_insights: AdaptiveLearningInsights,
}

/// Individual measurement event during execution
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MeasurementEvent {
    /// Timestamp (microseconds from start)
    pub timestamp: f64,
    /// Measured qubit
    pub qubit: QubitId,
    /// Measurement result (0 or 1)
    pub result: u8,
    /// Classical bit/register where result was stored
    pub storage_location: StorageLocation,
    /// Measurement latency (microseconds)
    pub latency: f64,
    /// Confidence/fidelity of measurement
    pub confidence: f64,
}

/// Location where measurement result is stored
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum StorageLocation {
    /// Classical bit index
    ClassicalBit(usize),
    /// Classical register and bit index
    ClassicalRegister(String, usize),
    /// Temporary buffer
    Buffer(usize),
}

/// Execution statistics for mid-circuit measurement
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutionStats {
    /// Total execution time
    pub total_execution_time: Duration,
    /// Time spent on quantum operations
    pub quantum_time: Duration,
    /// Time spent on measurements
    pub measurement_time: Duration,
    /// Time spent on classical processing
    pub classical_time: Duration,
    /// Number of mid-circuit measurements
    pub num_measurements: usize,
    /// Number of conditional operations
    pub num_conditional_ops: usize,
    /// Average measurement latency
    pub avg_measurement_latency: f64,
    /// Maximum measurement latency
    pub max_measurement_latency: f64,
}

/// Performance metrics for mid-circuit measurements
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceMetrics {
    /// Measurement success rate
    pub measurement_success_rate: f64,
    /// Classical processing efficiency
    pub classical_efficiency: f64,
    /// Overall circuit fidelity
    pub circuit_fidelity: f64,
    /// Measurement error rate
    pub measurement_error_rate: f64,
    /// Timing overhead compared to no measurements
    pub timing_overhead: f64,
    /// Resource utilization
    pub resource_utilization: ResourceUtilization,
}

/// Resource utilization metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceUtilization {
    /// Quantum resource usage (0-1)
    pub quantum_utilization: f64,
    /// Classical resource usage (0-1)
    pub classical_utilization: f64,
    /// Memory usage for classical data
    pub memory_usage: usize,
    /// Communication overhead
    pub communication_overhead: f64,
}

/// Error analysis for mid-circuit measurements
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ErrorAnalysis {
    /// Measurement errors by qubit
    pub measurement_errors: HashMap<QubitId, MeasurementErrorStats>,
    /// Classical processing errors
    pub classical_errors: Vec<ClassicalError>,
    /// Timing violations
    pub timing_violations: Vec<TimingViolation>,
    /// Correlation analysis
    pub error_correlations: Array2<f64>,
}

/// Measurement error statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MeasurementErrorStats {
    /// Readout error rate
    pub readout_error_rate: f64,
    /// State preparation and measurement (SPAM) error
    pub spam_error: f64,
    /// Thermal relaxation during measurement
    pub thermal_relaxation: f64,
    /// Dephasing during measurement
    pub dephasing: f64,
}

/// Classical processing error
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClassicalError {
    /// Error type
    pub error_type: ClassicalErrorType,
    /// Timestamp when error occurred
    pub timestamp: f64,
    /// Error description
    pub description: String,
    /// Affected operations
    pub affected_operations: Vec<usize>,
}

/// Types of classical errors
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ClassicalErrorType {
    /// Timeout in classical condition evaluation
    Timeout,
    /// Invalid register access
    InvalidRegisterAccess,
    /// Condition evaluation error
    ConditionEvaluationError,
    /// Buffer overflow
    BufferOverflow,
    /// Communication error
    CommunicationError,
}

/// Timing constraint violation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TimingViolation {
    /// Operation that violated timing
    pub operation_index: usize,
    /// Expected timing (microseconds)
    pub expected_timing: f64,
    /// Actual timing (microseconds)
    pub actual_timing: f64,
    /// Violation severity (0-1)
    pub severity: f64,
}

/// Advanced analytics results for mid-circuit measurements
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct AdvancedAnalyticsResults {
    /// Statistical analysis results
    pub statistical_analysis: StatisticalAnalysisResults,
    /// Correlation analysis results
    pub correlation_analysis: CorrelationAnalysisResults,
    /// Time series analysis results
    pub time_series_analysis: Option<TimeSeriesAnalysisResults>,
    /// Anomaly detection results
    pub anomaly_detection: Option<AnomalyDetectionResults>,
    /// Distribution analysis results
    pub distribution_analysis: DistributionAnalysisResults,
    /// Causal inference results
    pub causal_analysis: Option<CausalAnalysisResults>,
}

/// Statistical analysis results
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct StatisticalAnalysisResults {
    /// Descriptive statistics
    pub descriptive_stats: DescriptiveStatistics,
    /// Hypothesis test results
    pub hypothesis_tests: HypothesisTestResults,
    /// Confidence intervals
    pub confidence_intervals: ConfidenceIntervals,
    /// Effect size measurements
    pub effect_sizes: EffectSizeAnalysis,
}

/// Descriptive statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DescriptiveStatistics {
    /// Mean measurement latency
    pub mean_latency: f64,
    /// Standard deviation of latency
    pub std_latency: f64,
    /// Median latency
    pub median_latency: f64,
    /// Percentiles (25th, 75th, 95th, 99th)
    pub latency_percentiles: Vec<f64>,
    /// Measurement success rate statistics
    pub success_rate_stats: MeasurementSuccessStats,
    /// Error rate distribution
    pub error_rate_distribution: ErrorRateDistribution,
}

/// Measurement success statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MeasurementSuccessStats {
    /// Overall success rate
    pub overall_success_rate: f64,
    /// Success rate by qubit
    pub per_qubit_success_rate: HashMap<QubitId, f64>,
    /// Success rate over time
    pub temporal_success_rate: Vec<(f64, f64)>, // (timestamp, success_rate)
    /// Success rate confidence interval
    pub success_rate_ci: (f64, f64),
}

/// Error rate distribution analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ErrorRateDistribution {
    /// Error rate histogram
    pub histogram: Vec<(f64, usize)>, // (error_rate, count)
    /// Best-fit distribution
    pub best_fit_distribution: String,
    /// Distribution parameters
    pub distribution_parameters: Vec<f64>,
    /// Goodness-of-fit statistic
    pub goodness_of_fit: f64,
}

/// Hypothesis test results
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct HypothesisTestResults {
    /// Tests for measurement independence
    pub independence_tests: HashMap<String, StatisticalTest>,
    /// Tests for stationarity
    pub stationarity_tests: HashMap<String, StatisticalTest>,
    /// Tests for normality
    pub normality_tests: HashMap<String, StatisticalTest>,
    /// Comparison tests between different conditions
    pub comparison_tests: HashMap<String, ComparisonTest>,
}

/// Individual statistical test result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StatisticalTest {
    /// Test statistic
    pub statistic: f64,
    /// P-value
    pub p_value: f64,
    /// Critical value
    pub critical_value: f64,
    /// Test conclusion
    pub is_significant: bool,
    /// Effect size
    pub effect_size: Option<f64>,
}

impl Default for StatisticalTest {
    fn default() -> Self {
        Self {
            statistic: 0.0,
            p_value: 0.1,
            critical_value: 1.96,
            is_significant: false,
            effect_size: None,
        }
    }
}

/// Comparison test result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComparisonTest {
    /// Test type
    pub test_type: String,
    /// Test statistic
    pub statistic: f64,
    /// P-value
    pub p_value: f64,
    /// Mean difference
    pub mean_difference: f64,
    /// Confidence interval for difference
    pub difference_ci: (f64, f64),
    /// Cohen's d effect size
    pub cohens_d: f64,
}

/// Confidence intervals
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConfidenceIntervals {
    /// Confidence level used
    pub confidence_level: f64,
    /// Confidence intervals for means
    pub mean_intervals: HashMap<String, (f64, f64)>,
    /// Bootstrap confidence intervals
    pub bootstrap_intervals: HashMap<String, (f64, f64)>,
    /// Prediction intervals
    pub prediction_intervals: HashMap<String, (f64, f64)>,
}

/// Effect size analysis
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct EffectSizeAnalysis {
    /// Cohen's d for measurement differences
    pub cohens_d: HashMap<String, f64>,
    /// Correlation coefficients
    pub correlations: HashMap<String, f64>,
    /// R-squared values for relationships
    pub r_squared: HashMap<String, f64>,
    /// Practical significance indicators
    pub practical_significance: HashMap<String, bool>,
}

/// Correlation analysis results
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CorrelationAnalysisResults {
    /// Pearson correlation matrix
    pub pearson_correlations: Array2<f64>,
    /// Spearman correlation matrix
    pub spearman_correlations: Array2<f64>,
    /// Kendall's tau correlations
    pub kendall_correlations: Array2<f64>,
    /// Significant correlations
    pub significant_correlations: Vec<CorrelationPair>,
    /// Partial correlations
    pub partial_correlations: Array2<f64>,
    /// Correlation network analysis
    pub network_analysis: CorrelationNetworkAnalysis,
}

/// Correlation pair
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CorrelationPair {
    /// Variable 1
    pub variable1: String,
    /// Variable 2
    pub variable2: String,
    /// Correlation coefficient
    pub correlation: f64,
    /// P-value
    pub p_value: f64,
    /// Correlation type
    pub correlation_type: CorrelationType,
}

/// Types of correlation
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum CorrelationType {
    Pearson,
    Spearman,
    Kendall,
    Partial,
}

/// Correlation network analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CorrelationNetworkAnalysis {
    /// Graph adjacency matrix
    pub adjacency_matrix: Array2<f64>,
    /// Node centrality measures
    pub centrality_measures: NodeCentralityMeasures,
    /// Community detection results
    pub communities: Vec<Vec<usize>>,
    /// Network density
    pub network_density: f64,
    /// Clustering coefficient
    pub clustering_coefficient: f64,
}

/// Node centrality measures
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct NodeCentralityMeasures {
    /// Betweenness centrality
    pub betweenness: Vec<f64>,
    /// Closeness centrality
    pub closeness: Vec<f64>,
    /// Eigenvector centrality
    pub eigenvector: Vec<f64>,
    /// Degree centrality
    pub degree: Vec<f64>,
}

/// Time series analysis results
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TimeSeriesAnalysisResults {
    /// Trend analysis
    pub trend_analysis: TrendAnalysis,
    /// Seasonality analysis
    pub seasonality_analysis: Option<SeasonalityAnalysis>,
    /// Autocorrelation analysis
    pub autocorrelation: AutocorrelationAnalysis,
    /// Change point detection
    pub change_points: Vec<ChangePoint>,
    /// Stationarity test results
    pub stationarity: StationarityTestResults,
}

/// Trend analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrendAnalysis {
    /// Trend direction
    pub trend_direction: TrendDirection,
    /// Trend strength
    pub trend_strength: f64,
    /// Trend slope
    pub trend_slope: f64,
    /// Trend significance
    pub trend_significance: f64,
    /// Trend confidence interval
    pub trend_ci: (f64, f64),
}

/// Trend direction
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum TrendDirection {
    Increasing,
    Decreasing,
    Stable,
    Volatile,
    Cyclical,
}

/// Seasonality analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SeasonalityAnalysis {
    /// Detected seasonal periods
    pub periods: Vec<usize>,
    /// Seasonal strength
    pub seasonal_strength: f64,
    /// Seasonal components
    pub seasonal_components: Array1<f64>,
    /// Residual components
    pub residual_components: Array1<f64>,
}

/// Autocorrelation analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AutocorrelationAnalysis {
    /// Autocorrelation function
    pub acf: Array1<f64>,
    /// Partial autocorrelation function
    pub pacf: Array1<f64>,
    /// Significant lags
    pub significant_lags: Vec<usize>,
    /// Ljung-Box test statistic
    pub ljung_box_statistic: f64,
    /// Ljung-Box p-value
    pub ljung_box_p_value: f64,
}

/// Change point
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChangePoint {
    /// Change point index
    pub index: usize,
    /// Change point timestamp
    pub timestamp: f64,
    /// Change point confidence
    pub confidence: f64,
    /// Change magnitude
    pub magnitude: f64,
    /// Change type
    pub change_type: ChangePointType,
}

/// Change point types
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ChangePointType {
    MeanShift,
    VarianceChange,
    TrendChange,
    DistributionChange,
}

/// Stationarity test results
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StationarityTestResults {
    /// Augmented Dickey-Fuller test
    pub adf_test: StatisticalTest,
    /// KPSS test
    pub kpss_test: StatisticalTest,
    /// Phillips-Perron test
    pub pp_test: StatisticalTest,
    /// Overall stationarity conclusion
    pub is_stationary: bool,
}

/// Anomaly detection results
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnomalyDetectionResults {
    /// Detected anomalies
    pub anomalies: Vec<AnomalyEvent>,
    /// Anomaly scores
    pub anomaly_scores: Array1<f64>,
    /// Detection thresholds
    pub thresholds: HashMap<String, f64>,
    /// Method performance
    pub method_performance: AnomalyMethodPerformance,
}

/// Anomaly event
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnomalyEvent {
    /// Event index
    pub index: usize,
    /// Event timestamp
    pub timestamp: f64,
    /// Anomaly score
    pub anomaly_score: f64,
    /// Anomaly type
    pub anomaly_type: AnomalyType,
    /// Affected measurements
    pub affected_measurements: Vec<usize>,
    /// Severity level
    pub severity: AnomalySeverity,
}

/// Anomaly types
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum AnomalyType {
    PointAnomaly,
    ContextualAnomaly,
    CollectiveAnomaly,
    TrendAnomaly,
    SeasonalAnomaly,
}

/// Anomaly severity levels
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum AnomalySeverity {
    Low,
    Medium,
    High,
    Critical,
}

/// Anomaly method performance
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnomalyMethodPerformance {
    /// Precision scores
    pub precision: HashMap<String, f64>,
    /// Recall scores
    pub recall: HashMap<String, f64>,
    /// F1 scores
    pub f1_scores: HashMap<String, f64>,
    /// False positive rates
    pub false_positive_rates: HashMap<String, f64>,
}

/// Distribution analysis results
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct DistributionAnalysisResults {
    /// Best-fit distributions
    pub best_fit_distributions: HashMap<String, DistributionFit>,
    /// Distribution comparison results
    pub distribution_comparisons: Vec<DistributionComparison>,
    /// Mixture model results
    pub mixture_models: Option<MixtureModelResults>,
    /// Normality assessment
    pub normality_assessment: NormalityAssessment,
}

/// Distribution fit result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DistributionFit {
    /// Distribution name
    pub distribution_name: String,
    /// Distribution parameters
    pub parameters: Vec<f64>,
    /// Log-likelihood
    pub log_likelihood: f64,
    /// AIC score
    pub aic: f64,
    /// BIC score
    pub bic: f64,
    /// Kolmogorov-Smirnov test statistic
    pub ks_statistic: f64,
    /// KS test p-value
    pub ks_p_value: f64,
}

/// Distribution comparison
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DistributionComparison {
    /// Distribution 1
    pub distribution1: String,
    /// Distribution 2
    pub distribution2: String,
    /// AIC difference
    pub aic_difference: f64,
    /// BIC difference
    pub bic_difference: f64,
    /// Likelihood ratio test
    pub likelihood_ratio_test: StatisticalTest,
    /// Better fitting distribution
    pub better_fit: String,
}

/// Mixture model results
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MixtureModelResults {
    /// Number of components
    pub n_components: usize,
    /// Component weights
    pub weights: Array1<f64>,
    /// Component parameters
    pub component_parameters: Vec<Vec<f64>>,
    /// Log-likelihood
    pub log_likelihood: f64,
    /// BIC score
    pub bic: f64,
    /// Component assignments
    pub assignments: Array1<usize>,
}

/// Normality assessment
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NormalityAssessment {
    /// Shapiro-Wilk test
    pub shapiro_wilk: StatisticalTest,
    /// Anderson-Darling test
    pub anderson_darling: StatisticalTest,
    /// Jarque-Bera test
    pub jarque_bera: StatisticalTest,
    /// Overall normality conclusion
    pub is_normal: bool,
    /// Normality confidence
    pub normality_confidence: f64,
}

/// Causal analysis results
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct CausalAnalysisResults {
    /// Causal graph
    pub causal_graph: CausalGraph,
    /// Causal effects
    pub causal_effects: Vec<CausalEffect>,
    /// Confounding analysis
    pub confounding_analysis: ConfoundingAnalysis,
    /// Causal strength measures
    pub causal_strength: HashMap<String, f64>,
}

/// Causal graph
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CausalGraph {
    /// Adjacency matrix
    pub adjacency_matrix: Array2<f64>,
    /// Node names
    pub node_names: Vec<String>,
    /// Edge weights
    pub edge_weights: HashMap<(usize, usize), f64>,
    /// Graph confidence
    pub graph_confidence: f64,
}

/// Causal effect
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CausalEffect {
    /// Cause variable
    pub cause: String,
    /// Effect variable
    pub effect: String,
    /// Effect size
    pub effect_size: f64,
    /// Confidence interval
    pub confidence_interval: (f64, f64),
    /// P-value
    pub p_value: f64,
    /// Causal mechanism
    pub mechanism: CausalMechanism,
}

/// Causal mechanisms
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum CausalMechanism {
    Direct,
    Indirect,
    Mediated,
    Confounded,
    Spurious,
}

/// Confounding analysis
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ConfoundingAnalysis {
    /// Detected confounders
    pub confounders: Vec<String>,
    /// Confounder strength
    pub confounder_strength: HashMap<String, f64>,
    /// Backdoor criteria satisfaction
    pub backdoor_satisfied: bool,
    /// Frontdoor criteria satisfaction
    pub frontdoor_satisfied: bool,
}

/// Measurement prediction results
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MeasurementPredictionResults {
    /// Predicted measurement outcomes
    pub predictions: Array1<f64>,
    /// Prediction confidence intervals
    pub confidence_intervals: Array2<f64>,
    /// Prediction timestamps
    pub timestamps: Vec<f64>,
    /// Model performance metrics
    pub model_performance: PredictionModelPerformance,
    /// Uncertainty quantification
    pub uncertainty: PredictionUncertainty,
}

/// Prediction model performance
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PredictionModelPerformance {
    /// Mean absolute error
    pub mae: f64,
    /// Mean squared error
    pub mse: f64,
    /// Root mean squared error
    pub rmse: f64,
    /// Mean absolute percentage error
    pub mape: f64,
    /// R-squared score
    pub r2_score: f64,
    /// Prediction accuracy
    pub accuracy: f64,
}

/// Prediction uncertainty
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PredictionUncertainty {
    /// Aleatoric uncertainty
    pub aleatoric_uncertainty: Array1<f64>,
    /// Epistemic uncertainty
    pub epistemic_uncertainty: Array1<f64>,
    /// Total uncertainty
    pub total_uncertainty: Array1<f64>,
    /// Uncertainty bounds
    pub uncertainty_bounds: Array2<f64>,
}

/// Optimization recommendations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OptimizationRecommendations {
    /// Scheduling optimizations
    pub scheduling_optimizations: Vec<SchedulingOptimization>,
    /// Protocol optimizations
    pub protocol_optimizations: Vec<ProtocolOptimization>,
    /// Resource optimizations
    pub resource_optimizations: Vec<ResourceOptimization>,
    /// Performance improvements
    pub performance_improvements: Vec<PerformanceImprovement>,
}

/// Scheduling optimization recommendation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SchedulingOptimization {
    /// Optimization type
    pub optimization_type: SchedulingOptimizationType,
    /// Expected improvement
    pub expected_improvement: f64,
    /// Implementation difficulty
    pub difficulty: OptimizationDifficulty,
    /// Recommendation description
    pub description: String,
    /// Implementation steps
    pub implementation_steps: Vec<String>,
}

/// Scheduling optimization types
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum SchedulingOptimizationType {
    MeasurementBatching,
    TemporalReordering,
    ParallelExecution,
    ConditionalOptimization,
    LatencyReduction,
}

/// Protocol optimization recommendation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProtocolOptimization {
    /// Protocol type
    pub protocol_type: String,
    /// Optimization description
    pub description: String,
    /// Expected benefit
    pub expected_benefit: f64,
    /// Risk assessment
    pub risk_level: RiskLevel,
    /// Validation requirements
    pub validation_requirements: Vec<String>,
}

/// Resource optimization recommendation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceOptimization {
    /// Resource type
    pub resource_type: ResourceType,
    /// Current utilization
    pub current_utilization: f64,
    /// Optimal utilization
    pub optimal_utilization: f64,
    /// Optimization strategy
    pub strategy: String,
    /// Expected savings
    pub expected_savings: f64,
}

/// Resource types
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ResourceType {
    QuantumProcessor,
    ClassicalProcessor,
    Memory,
    NetworkBandwidth,
    StorageCapacity,
}

/// Performance improvement recommendation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceImprovement {
    /// Improvement area
    pub area: PerformanceArea,
    /// Current performance
    pub current_performance: f64,
    /// Target performance
    pub target_performance: f64,
    /// Improvement strategy
    pub strategy: String,
    /// Implementation priority
    pub priority: Priority,
}

/// Performance areas
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum PerformanceArea {
    MeasurementLatency,
    MeasurementAccuracy,
    ThroughputRate,
    ErrorRate,
    ResourceEfficiency,
}

/// Priority levels
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum Priority {
    Low,
    Medium,
    High,
    Critical,
}

/// Risk levels
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum RiskLevel {
    Low,
    Medium,
    High,
    VeryHigh,
}

/// Optimization difficulty levels
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum OptimizationDifficulty {
    Easy,
    Moderate,
    Difficult,
    VeryDifficult,
}

/// Adaptive learning insights
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AdaptiveLearningInsights {
    /// Learning progress metrics
    pub learning_progress: LearningProgress,
    /// Model adaptation history
    pub adaptation_history: Vec<AdaptationEvent>,
    /// Performance trends
    pub performance_trends: PerformanceTrends,
    /// Concept drift detection
    pub drift_detection: DriftDetectionResults,
    /// Knowledge transfer insights
    pub transfer_learning: TransferLearningInsights,
}

/// Learning progress metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LearningProgress {
    /// Training iterations completed
    pub iterations_completed: usize,
    /// Current learning rate
    pub current_learning_rate: f64,
    /// Training loss history
    pub loss_history: Array1<f64>,
    /// Validation accuracy history
    pub accuracy_history: Array1<f64>,
    /// Convergence status
    pub convergence_status: ConvergenceStatus,
}

/// Convergence status
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ConvergenceStatus {
    NotStarted,
    InProgress,
    Converged,
    Diverged,
    Plateaued,
    Stuck,
    Improving,
    Diverging,
    Oscillating,
}

/// Adaptation event
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AdaptationEvent {
    /// Event timestamp
    pub timestamp: SystemTime,
    /// Adaptation type
    pub adaptation_type: AdaptationType,
    /// Trigger condition
    pub trigger: String,
    /// Performance before adaptation
    pub performance_before: f64,
    /// Performance after adaptation
    pub performance_after: f64,
    /// Performance snapshot at time of adaptation
    pub performance_snapshot: PerformanceMetrics,
    /// Magnitude of adaptation
    pub adaptation_magnitude: f64,
    /// Success indicator
    pub success_indicator: f64,
    /// Adaptation success
    pub success: bool,
}

/// Adaptation types
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum AdaptationType {
    ParameterTuning,
    ArchitectureChange,
    FeatureSelection,
    HyperparameterOptimization,
    ModelRetrained,
    ThresholdAdjustment,
    PerformanceOptimization,
}

/// Performance trends
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceTrends {
    /// Short-term trend
    pub short_term_trend: TrendDirection,
    /// Long-term trend
    pub long_term_trend: TrendDirection,
    /// Trend strength
    pub trend_strength: f64,
    /// Seasonal patterns
    pub seasonal_patterns: Option<SeasonalityAnalysis>,
    /// Performance volatility
    pub volatility: f64,
}

/// Drift detection results
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DriftDetectionResults {
    /// Drift detected
    pub drift_detected: bool,
    /// Drift type
    pub drift_type: Option<DriftType>,
    /// Drift magnitude
    pub drift_magnitude: f64,
    /// Detection confidence
    pub detection_confidence: f64,
    /// Recommended actions
    pub recommended_actions: Vec<String>,
}

/// Drift types
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum DriftType {
    Gradual,
    Sudden,
    Incremental,
    Recurring,
    Virtual,
    PerformanceDegradation,
    QualityDrift,
    ConceptDrift,
}

/// Transfer learning insights
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransferLearningInsights {
    /// Knowledge transfer effectiveness
    pub transfer_effectiveness: f64,
    /// Source domain similarity
    pub domain_similarity: f64,
    /// Feature transferability
    pub feature_transferability: Array1<f64>,
    /// Adaptation requirements
    pub adaptation_requirements: Vec<String>,
    /// Transfer learning recommendations
    pub recommendations: Vec<String>,
}

/// Device capabilities for mid-circuit measurements
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MidCircuitCapabilities {
    /// Maximum number of mid-circuit measurements
    pub max_measurements: Option<usize>,
    /// Supported measurement types
    pub supported_measurement_types: Vec<MeasurementType>,
    /// Classical register capacity
    pub classical_register_capacity: usize,
    /// Maximum classical processing time
    pub max_classical_processing_time: f64,
    /// Real-time feedback support
    pub realtime_feedback: bool,
    /// Parallel measurement support
    pub parallel_measurements: bool,
    /// Native measurement protocols
    pub native_protocols: Vec<String>,
    /// Timing constraints
    pub timing_constraints: TimingConstraints,
}

/// Supported measurement types
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum MeasurementType {
    /// Standard Z-basis measurement
    ZBasis,
    /// X-basis measurement
    XBasis,
    /// Y-basis measurement
    YBasis,
    /// Custom Pauli measurement
    Pauli(String),
    /// Joint measurement of multiple qubits
    Joint,
    /// Non-destructive measurement
    NonDestructive,
}

/// Timing constraints for mid-circuit measurements
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TimingConstraints {
    /// Minimum time between measurements (nanoseconds)
    pub min_measurement_spacing: f64,
    /// Maximum measurement duration (nanoseconds)
    pub max_measurement_duration: f64,
    /// Classical processing deadline (nanoseconds)
    pub classical_deadline: f64,
    /// Coherence time limits
    pub coherence_limits: HashMap<QubitId, f64>,
}

// Additional types for analytics modules

/// Statistical anomaly information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StatisticalAnomaly {
    /// Index in the data series
    pub index: usize,
    /// Anomalous value
    pub value: f64,
    /// Z-score of the anomaly
    pub z_score: f64,
    /// Statistical p-value
    pub p_value: f64,
    /// Metric type
    pub metric_type: String,
    /// Severity of the anomaly
    pub anomaly_severity: AnomalySeverity,
}

/// Temporal anomaly information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TemporalAnomaly {
    /// Start index of anomalous period
    pub start_index: usize,
    /// End index of anomalous period
    pub end_index: usize,
    /// Change point index
    pub change_point: usize,
    /// Magnitude of change
    pub magnitude: f64,
    /// Direction of change
    pub direction: ChangeDirection,
    /// Confidence in anomaly detection
    pub confidence: f64,
}

/// Direction of change in temporal anomaly
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ChangeDirection {
    Increase,
    Decrease,
    Oscillation,
}

/// Pattern anomaly information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PatternAnomaly {
    /// Type of pattern anomaly
    pub pattern_type: PatternType,
    /// Description of the anomaly
    pub description: String,
    /// Severity level
    pub severity: AnomalySeverity,
    /// Indices affected by the anomaly
    pub affected_indices: Vec<usize>,
    /// Confidence in detection
    pub confidence: f64,
}

/// Types of pattern anomalies
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum PatternType {
    CorrelationAnomaly,
    ConstantSequence,
    PeriodicityBreak,
    TrendReversal,
    VariabilityAnomaly,
}

/// Summary of all anomaly detection results
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnomalySummary {
    /// Total number of anomalies detected
    pub total_anomalies: usize,
    /// Overall anomaly rate
    pub anomaly_rate: f64,
    /// Distribution by severity
    pub severity_distribution: Vec<(String, usize)>,
    /// Types of anomalies found
    pub anomaly_types: Vec<String>,
    /// Recommendations for addressing anomalies
    pub recommendations: Vec<String>,
}

/// Causal relationship information (only missing fields if any)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CausalRelationship {
    /// Cause variable
    pub cause: String,
    /// Effect variable
    pub effect: String,
    /// Strength of causal relationship
    pub causal_strength: f64,
    /// Direction of causality
    pub causal_direction: CausalDirection,
    /// P-value of causal test
    pub p_value: f64,
    /// Confidence interval for effect
    pub confidence_interval: (f64, f64),
    /// Causal mechanism
    pub mechanism: CausalMechanism,
}

/// Direction of causal relationship
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum CausalDirection {
    Forward,
    Backward,
    Bidirectional,
    None,
}

/// Edge type in causal graph
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum EdgeType {
    Directed,
    Undirected,
    Bidirected,
}

/// Intervention analysis results
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InterventionAnalysis {
    /// Type of intervention
    pub intervention_type: String,
    /// Target variable
    pub target_variable: String,
    /// Magnitude of intervention
    pub intervention_magnitude: f64,
    /// Predicted effects
    pub predicted_effects: Vec<PredictedEffect>,
    /// Cost of intervention
    pub intervention_cost: f64,
    /// Benefit-cost ratio
    pub benefit_ratio: f64,
}

/// Predicted effect of intervention
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PredictedEffect {
    /// Variable affected
    pub variable: String,
    /// Effect size
    pub effect_size: f64,
    /// Confidence interval
    pub confidence_interval: (f64, f64),
    /// P-value
    pub p_value: f64,
}

/// Confounding assessment
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConfoundingAssessment {
    /// Identified confounders
    pub confounders: Vec<ConfoundingVariable>,
    /// Overall confounding risk
    pub overall_confounding_risk: String,
    /// Recommendations
    pub recommendations: Vec<String>,
}

/// Confounding variable information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConfoundingVariable {
    /// Variable name
    pub variable: String,
    /// Confounding strength
    pub confounding_strength: f64,
    /// Adjustment method
    pub adjustment_method: String,
    /// P-value
    pub p_value: f64,
}

/// Prediction model information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PredictionModel {
    /// Model type
    pub model_type: String,
    /// Model coefficients
    pub coefficients: Vec<f64>,
    /// Feature names
    pub feature_names: Vec<String>,
    /// Training accuracy
    pub training_accuracy: f64,
    /// Validation accuracy
    pub validation_accuracy: f64,
    /// Last training time
    pub last_trained: SystemTime,
    /// Hyperparameters
    pub hyperparameters: Vec<(String, f64)>,
}

impl Default for PredictionModel {
    fn default() -> Self {
        Self {
            model_type: "linear".to_string(),
            coefficients: vec![],
            feature_names: vec![],
            training_accuracy: 0.0,
            validation_accuracy: 0.0,
            last_trained: SystemTime::now(),
            hyperparameters: vec![],
        }
    }
}

/// ML features for optimization
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct MLFeatures {
    /// Statistical features
    pub statistical_features: StatisticalFeatures,
    /// Temporal features
    pub temporal_features: TemporalFeatures,
    /// Pattern features
    pub pattern_features: PatternFeatures,
    /// Feature importance
    pub feature_importance: Vec<FeatureImportance>,
}

/// Statistical features
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StatisticalFeatures {
    /// Mean latency
    pub mean_latency: f64,
    /// Standard deviation of latency
    pub std_latency: f64,
    /// Mean confidence
    pub mean_confidence: f64,
    /// Standard deviation of confidence
    pub std_confidence: f64,
    /// Skewness of latency
    pub skewness_latency: f64,
    /// Kurtosis of latency
    pub kurtosis_latency: f64,
}

impl Default for StatisticalFeatures {
    fn default() -> Self {
        Self {
            mean_latency: 0.0,
            std_latency: 0.0,
            mean_confidence: 0.0,
            std_confidence: 0.0,
            skewness_latency: 0.0,
            kurtosis_latency: 0.0,
        }
    }
}

/// Temporal features
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TemporalFeatures {
    /// Measurement rate
    pub measurement_rate: f64,
    /// Temporal autocorrelation
    pub temporal_autocorrelation: f64,
    /// Trend slope
    pub trend_slope: f64,
    /// Periodicity strength
    pub periodicity_strength: f64,
}

impl Default for TemporalFeatures {
    fn default() -> Self {
        Self {
            measurement_rate: 0.0,
            temporal_autocorrelation: 0.0,
            trend_slope: 0.0,
            periodicity_strength: 0.0,
        }
    }
}

/// Pattern features
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PatternFeatures {
    /// Correlation between latency and confidence
    pub latency_confidence_correlation: f64,
    /// Measurement consistency
    pub measurement_consistency: f64,
    /// Outlier ratio
    pub outlier_ratio: f64,
    /// Pattern complexity
    pub pattern_complexity: f64,
}

impl Default for PatternFeatures {
    fn default() -> Self {
        Self {
            latency_confidence_correlation: 0.0,
            measurement_consistency: 0.0,
            outlier_ratio: 0.0,
            pattern_complexity: 0.0,
        }
    }
}

/// Feature importance
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FeatureImportance {
    /// Feature name
    pub feature_name: String,
    /// Importance score
    pub importance: f64,
}

/// Training epoch information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrainingEpoch {
    /// Epoch number
    pub epoch_number: usize,
    /// Features used
    pub features: MLFeatures,
    /// Target metrics
    pub target_metrics: PerformanceMetrics,
    /// Training loss
    pub training_loss: f64,
    /// Validation loss
    pub validation_loss: f64,
    /// Learning rate
    pub learning_rate: f64,
}

/// Model performance metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelPerformance {
    /// Training accuracy
    pub training_accuracy: f64,
    /// Validation accuracy
    pub validation_accuracy: f64,
    /// Cross-validation score
    pub cross_validation_score: f64,
    /// Overfitting score
    pub overfitting_score: f64,
}

/// Optimization model information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OptimizationModel {
    /// Model type
    pub model_type: String,
    /// Model parameters
    pub parameters: Vec<f64>,
    /// Training features
    pub training_features: MLFeatures,
    /// Model performance
    pub model_performance: ModelPerformance,
    /// Last update time
    pub last_updated: SystemTime,
}

/// Performance improvements prediction
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceImprovements {
    /// Latency reduction
    pub latency_reduction: f64,
    /// Confidence increase
    pub confidence_increase: f64,
    /// Throughput increase
    pub throughput_increase: f64,
    /// Error rate reduction
    pub error_rate_reduction: f64,
    /// Overall score improvement
    pub overall_score_improvement: f64,
}

impl Default for PerformanceImprovements {
    fn default() -> Self {
        Self {
            latency_reduction: 0.0,
            confidence_increase: 0.0,
            throughput_increase: 0.0,
            error_rate_reduction: 0.0,
            overall_score_improvement: 0.0,
        }
    }
}

/// Optimization recommendation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OptimizationRecommendation {
    /// Parameter to optimize
    pub parameter: String,
    /// Current value
    pub current_value: f64,
    /// Recommended value
    pub recommended_value: f64,
    /// Expected improvement
    pub expected_improvement: f64,
    /// Confidence in recommendation
    pub confidence: f64,
    /// Rationale for recommendation
    pub rationale: String,
}

/// Optimization result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OptimizationResult {
    /// Optimization recommendations
    pub recommendations: Vec<OptimizationRecommendation>,
    /// Predicted improvements
    pub predicted_improvements: PerformanceImprovements,
    /// Confidence in optimization
    pub confidence: f64,
    /// Model version
    pub model_version: String,
}

impl Default for OptimizationResult {
    fn default() -> Self {
        Self {
            recommendations: vec![],
            predicted_improvements: PerformanceImprovements::default(),
            confidence: 0.5,
            model_version: "1.0".to_string(),
        }
    }
}