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
//! Hardware benchmarking suite with SciRS2 analysis
//!
//! This module provides comprehensive benchmarking capabilities for quantum hardware,
//! using SciRS2's statistical and linear algebra capabilities for advanced analysis.

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

use scirs2_core::random::prelude::*;
use scirs2_core::random::thread_rng;
use serde::{Deserialize, Serialize};

use quantrs2_circuit::prelude::*;
use quantrs2_core::{
    error::{QuantRS2Error, QuantRS2Result},
    gate::GateOp,
    qubit::QubitId,
};

use scirs2_graph::{
    betweenness_centrality, closeness_centrality, clustering_coefficient, dijkstra_path,
    graph_density, spectral_radius, Graph,
};
use scirs2_linalg::{
    correlationmatrix, det, eigvals, inv, matrix_norm, svd, LinalgError, LinalgResult,
};
use scirs2_stats::{
    distributions, mean, median, pearsonr, spearmanr, std, ttest::Alternative, ttest_1samp, var,
    TTestResult,
};

use scirs2_core::ndarray::{s, Array1, Array2, ArrayView1, ArrayView2};

use crate::{
    backend_traits::{query_backend_capabilities, BackendCapabilities},
    calibration::{CalibrationManager, DeviceCalibration},
    noise_model::CalibrationNoiseModel,
    topology::HardwareTopology,
    CircuitResult, DeviceError, DeviceResult,
};

/// Comprehensive benchmarking suite configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchmarkConfig {
    /// Number of benchmark iterations
    pub iterations: usize,
    /// Statistical confidence level (0.95 = 95%)
    pub confidence_level: f64,
    /// Maximum runtime per benchmark (seconds)
    pub max_runtime: Duration,
    /// Circuit depths to benchmark
    pub circuit_depths: Vec<usize>,
    /// Qubit counts to benchmark
    pub qubit_counts: Vec<usize>,
    /// Gate types to benchmark
    pub gate_types: Vec<String>,
    /// Enable advanced statistical analysis
    pub enable_advanced_stats: bool,
    /// Enable graph-theoretic analysis
    pub enable_graph_analysis: bool,
    /// Enable noise correlation analysis
    pub enable_noise_analysis: bool,
}

impl Default for BenchmarkConfig {
    fn default() -> Self {
        Self {
            iterations: 100,
            confidence_level: 0.95,
            max_runtime: Duration::from_secs(300),
            circuit_depths: vec![5, 10, 20, 50, 100],
            qubit_counts: vec![2, 4, 8, 16],
            gate_types: vec!["H".to_string(), "CNOT".to_string(), "RZ".to_string()],
            enable_advanced_stats: true,
            enable_graph_analysis: true,
            enable_noise_analysis: true,
        }
    }
}

/// Comprehensive benchmark results with SciRS2 analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchmarkSuite {
    /// Device identifier
    pub device_id: String,
    /// Backend capabilities
    pub backend_capabilities: BackendCapabilities,
    /// Benchmark configuration used
    pub config: BenchmarkConfig,
    /// Individual benchmark results
    pub benchmark_results: Vec<BenchmarkResult>,
    /// Statistical analysis
    pub statistical_analysis: StatisticalAnalysis,
    /// Graph-theoretic analysis
    pub graph_analysis: Option<GraphAnalysis>,
    /// Noise correlation analysis
    pub noise_analysis: Option<NoiseAnalysis>,
    /// Performance metrics
    pub performance_metrics: PerformanceMetrics,
    /// Execution time
    pub execution_time: Duration,
}

/// Individual benchmark result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchmarkResult {
    /// Benchmark name
    pub name: String,
    /// Circuit used
    pub circuit_description: String,
    /// Number of qubits
    pub num_qubits: usize,
    /// Circuit depth
    pub circuit_depth: usize,
    /// Gate count
    pub gate_count: usize,
    /// Execution times (seconds)
    pub execution_times: Vec<f64>,
    /// Fidelity measurements
    pub fidelities: Vec<f64>,
    /// Error rates
    pub error_rates: Vec<f64>,
    /// Success probabilities
    pub success_probabilities: Vec<f64>,
    /// Queue times (seconds)
    pub queue_times: Vec<f64>,
}

/// Statistical analysis using SciRS2
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StatisticalAnalysis {
    /// Execution time statistics
    pub execution_time_stats: DescriptiveStats,
    /// Fidelity statistics
    pub fidelity_stats: DescriptiveStats,
    /// Error rate statistics
    pub error_rate_stats: DescriptiveStats,
    /// Correlation matrix between metrics
    pub correlationmatrix: Array2<f64>,
    /// Statistical tests results
    pub statistical_tests: HashMap<String, TestResult>,
    /// Distribution fitting results
    pub distribution_fits: HashMap<String, DistributionFit>,
}

/// Graph-theoretic analysis of connectivity and performance
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphAnalysis {
    /// Connectivity graph metrics
    pub connectivity_metrics: ConnectivityMetrics,
    /// Performance-topology correlations
    pub topology_correlations: HashMap<String, f64>,
    /// Critical path analysis
    pub critical_paths: Vec<CriticalPath>,
    /// Spectral properties
    pub spectral_properties: SpectralProperties,
}

/// Noise correlation analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NoiseAnalysis {
    /// Cross-talk correlations
    pub crosstalk_correlations: Array2<f64>,
    /// Temporal noise correlations
    pub temporal_correlations: Array1<f64>,
    /// Spatial noise patterns
    pub spatial_patterns: HashMap<String, Array2<f64>>,
    /// Noise model validation
    pub model_validation: NoiseModelValidation,
}

/// Performance metrics summary
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceMetrics {
    /// Overall device score (0-100)
    pub overall_score: f64,
    /// Reliability score
    pub reliability_score: f64,
    /// Speed score
    pub speed_score: f64,
    /// Accuracy score
    pub accuracy_score: f64,
    /// Efficiency score
    pub efficiency_score: f64,
    /// Scalability metrics
    pub scalability_metrics: ScalabilityMetrics,
}

/// Descriptive statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DescriptiveStats {
    pub mean: f64,
    pub median: f64,
    pub std_dev: f64,
    pub variance: f64,
    pub min: f64,
    pub max: f64,
    pub q25: f64,
    pub q75: f64,
    pub confidence_interval: (f64, f64),
}

/// Statistical test result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TestResult {
    pub test_name: String,
    pub statistic: f64,
    pub p_value: f64,
    pub significant: bool,
    pub effect_size: Option<f64>,
}

/// Distribution fitting result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DistributionFit {
    pub distribution_name: String,
    pub parameters: Vec<f64>,
    pub goodness_of_fit: f64,
    pub p_value: f64,
}

/// Connectivity metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConnectivityMetrics {
    pub average_path_length: f64,
    pub clustering_coefficient: f64,
    pub graph_density: f64,
    pub diameter: usize,
    pub spectral_gap: f64,
    pub centrality_measures: HashMap<usize, CentralityMeasures>,
}

/// Centrality measures for qubits
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CentralityMeasures {
    pub betweenness: f64,
    pub closeness: f64,
    pub eigenvector: f64,
    pub degree: f64,
}

/// Critical path information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CriticalPath {
    pub path: Vec<usize>,
    pub length: usize,
    pub bottleneck_qubits: Vec<usize>,
    pub expected_performance_impact: f64,
}

/// Spectral properties of the connectivity graph
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SpectralProperties {
    pub eigenvalues: Array1<f64>,
    pub spectral_radius: f64,
    pub algebraic_connectivity: f64,
    pub spectral_gap: f64,
}

/// Noise model validation results
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NoiseModelValidation {
    pub model_accuracy: f64,
    pub prediction_errors: Array1<f64>,
    pub residual_analysis: ResidualAnalysis,
}

/// Residual analysis for noise model
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResidualAnalysis {
    pub normality_test: TestResult,
    pub autocorrelation: Array1<f64>,
    pub heteroscedasticity_test: TestResult,
}

/// Scalability metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScalabilityMetrics {
    pub depth_scaling_coefficient: f64,
    pub width_scaling_coefficient: f64,
    pub resource_efficiency: f64,
    pub parallelization_factor: f64,
}

/// Main benchmarking suite
pub struct HardwareBenchmarkSuite {
    calibration_manager: CalibrationManager,
    config: BenchmarkConfig,
}

impl HardwareBenchmarkSuite {
    /// Create a new benchmark suite
    pub const fn new(calibration_manager: CalibrationManager, config: BenchmarkConfig) -> Self {
        Self {
            calibration_manager,
            config,
        }
    }

    /// Run comprehensive benchmarking suite
    pub async fn run_benchmark_suite<E: DeviceExecutor>(
        &self,
        device_id: &str,
        executor: &E,
    ) -> DeviceResult<BenchmarkSuite> {
        let start_time = Instant::now();

        // Get device information
        let backend_capabilities = query_backend_capabilities(Self::get_backend_type(device_id)?);

        let calibration = self
            .calibration_manager
            .get_calibration(device_id)
            .ok_or_else(|| DeviceError::APIError("No calibration data".into()))?;

        // Run individual benchmarks
        let mut benchmark_results = Vec::new();

        // Basic gate benchmarks
        for gate_type in &self.config.gate_types {
            for &num_qubits in &self.config.qubit_counts {
                if num_qubits <= backend_capabilities.features.max_qubits {
                    let result = self
                        .benchmark_gate_type(gate_type, num_qubits, executor, calibration)
                        .await?;
                    benchmark_results.push(result);
                }
            }
        }

        // Circuit depth benchmarks
        for &depth in &self.config.circuit_depths {
            for &num_qubits in &self.config.qubit_counts {
                if num_qubits <= backend_capabilities.features.max_qubits {
                    let result = self
                        .benchmark_circuit_depth(depth, num_qubits, executor, calibration)
                        .await?;
                    benchmark_results.push(result);
                }
            }
        }

        // Randomized circuit benchmarks
        let random_results = self
            .benchmark_random_circuits(executor, calibration)
            .await?;
        benchmark_results.extend(random_results);

        // Perform statistical analysis
        let statistical_analysis = Self::perform_statistical_analysis(&benchmark_results)?;

        // Perform graph analysis if enabled
        let graph_analysis = if self.config.enable_graph_analysis {
            Some(Self::perform_graph_analysis(
                calibration,
                &benchmark_results,
            )?)
        } else {
            None
        };

        // Perform noise analysis if enabled
        let noise_analysis = if self.config.enable_noise_analysis {
            Some(Self::perform_noise_analysis(
                calibration,
                &benchmark_results,
            )?)
        } else {
            None
        };

        // Calculate performance metrics
        let performance_metrics = Self::calculate_performance_metrics(
            &benchmark_results,
            &statistical_analysis,
            graph_analysis.as_ref(),
        )?;

        let execution_time = start_time.elapsed();

        Ok(BenchmarkSuite {
            device_id: device_id.to_string(),
            backend_capabilities,
            config: self.config.clone(),
            benchmark_results,
            statistical_analysis,
            graph_analysis,
            noise_analysis,
            performance_metrics,
            execution_time,
        })
    }

    /// Benchmark specific gate type
    async fn benchmark_gate_type<E: DeviceExecutor>(
        &self,
        gate_type: &str,
        num_qubits: usize,
        executor: &E,
        calibration: &DeviceCalibration,
    ) -> DeviceResult<BenchmarkResult> {
        let mut execution_times = Vec::new();
        let mut fidelities = Vec::new();
        let mut error_rates = Vec::new();
        let mut success_probabilities = Vec::new();
        let mut queue_times = Vec::new();

        for _ in 0..self.config.iterations {
            let circuit: Circuit<8> = Self::create_gate_benchmark_circuit(gate_type, num_qubits)?;

            let queue_start = Instant::now();
            let exec_start = Instant::now();

            // Execute circuit
            let result = executor.execute_circuit(&circuit, 1000).await?;

            let exec_time = exec_start.elapsed().as_secs_f64();
            let queue_time = queue_start.elapsed().as_secs_f64();

            // Calculate metrics
            let fidelity = Self::calculate_fidelity(&result, &circuit, calibration)?;
            let error_rate = 1.0 - fidelity;
            let success_prob = Self::calculate_success_probability(&result)?;

            execution_times.push(exec_time);
            fidelities.push(fidelity);
            error_rates.push(error_rate);
            success_probabilities.push(success_prob);
            queue_times.push(queue_time);
        }

        Ok(BenchmarkResult {
            name: format!("{gate_type}_gate_benchmark"),
            circuit_description: format!("{gate_type} gate on {num_qubits} qubits"),
            num_qubits,
            circuit_depth: 1,
            gate_count: 1,
            execution_times,
            fidelities,
            error_rates,
            success_probabilities,
            queue_times,
        })
    }

    /// Benchmark circuit depth scaling
    async fn benchmark_circuit_depth<E: DeviceExecutor>(
        &self,
        depth: usize,
        num_qubits: usize,
        executor: &E,
        calibration: &DeviceCalibration,
    ) -> DeviceResult<BenchmarkResult> {
        let mut execution_times = Vec::new();
        let mut fidelities = Vec::new();
        let mut error_rates = Vec::new();
        let mut success_probabilities = Vec::new();
        let mut queue_times = Vec::new();

        for _ in 0..self.config.iterations {
            let circuit: Circuit<8> = Self::create_depth_benchmark_circuit(depth, num_qubits)?;

            let queue_start = Instant::now();
            let exec_start = Instant::now();

            let result = executor.execute_circuit(&circuit, 1000).await?;

            let exec_time = exec_start.elapsed().as_secs_f64();
            let queue_time = queue_start.elapsed().as_secs_f64();

            let fidelity = Self::calculate_fidelity(&result, &circuit, calibration)?;
            let error_rate = 1.0 - fidelity;
            let success_prob = Self::calculate_success_probability(&result)?;

            execution_times.push(exec_time);
            fidelities.push(fidelity);
            error_rates.push(error_rate);
            success_probabilities.push(success_prob);
            queue_times.push(queue_time);
        }

        Ok(BenchmarkResult {
            name: format!("depth_{depth}_benchmark"),
            circuit_description: format!("Depth {depth} circuit on {num_qubits} qubits"),
            num_qubits,
            circuit_depth: depth,
            gate_count: depth * num_qubits / 2, // Rough estimate
            execution_times,
            fidelities,
            error_rates,
            success_probabilities,
            queue_times,
        })
    }

    /// Benchmark random circuits
    async fn benchmark_random_circuits<E: DeviceExecutor>(
        &self,
        executor: &E,
        calibration: &DeviceCalibration,
    ) -> DeviceResult<Vec<BenchmarkResult>> {
        let mut results = Vec::new();

        // Random circuit volume benchmarks
        for &num_qubits in &self.config.qubit_counts {
            for &depth in &self.config.circuit_depths {
                if num_qubits <= 8 && depth <= 50 {
                    // Reasonable limits
                    let result = self
                        .benchmark_random_circuit_volume(num_qubits, depth, executor, calibration)
                        .await?;
                    results.push(result);
                }
            }
        }

        Ok(results)
    }

    /// Benchmark random circuit volume
    async fn benchmark_random_circuit_volume<E: DeviceExecutor>(
        &self,
        num_qubits: usize,
        depth: usize,
        executor: &E,
        calibration: &DeviceCalibration,
    ) -> DeviceResult<BenchmarkResult> {
        let mut execution_times = Vec::new();
        let mut fidelities = Vec::new();
        let mut error_rates = Vec::new();
        let mut success_probabilities = Vec::new();
        let mut queue_times = Vec::new();

        for _ in 0..self.config.iterations.min(20) {
            // Limit for random circuits
            let circuit = Self::create_random_circuit(num_qubits, depth)?;

            let queue_start = Instant::now();
            let exec_start = Instant::now();

            let result = executor.execute_circuit(&circuit, 1000).await?;

            let exec_time = exec_start.elapsed().as_secs_f64();
            let queue_time = queue_start.elapsed().as_secs_f64();

            let fidelity = Self::calculate_fidelity(&result, &circuit, calibration)?;
            let error_rate = 1.0 - fidelity;
            let success_prob = Self::calculate_success_probability(&result)?;

            execution_times.push(exec_time);
            fidelities.push(fidelity);
            error_rates.push(error_rate);
            success_probabilities.push(success_prob);
            queue_times.push(queue_time);
        }

        Ok(BenchmarkResult {
            name: format!("random_circuit_{num_qubits}q_{depth}d"),
            circuit_description: format!("Random {num_qubits} qubit, depth {depth} circuit"),
            num_qubits,
            circuit_depth: depth,
            gate_count: depth * num_qubits,
            execution_times,
            fidelities,
            error_rates,
            success_probabilities,
            queue_times,
        })
    }

    /// Perform comprehensive statistical analysis using SciRS2
    fn perform_statistical_analysis(
        results: &[BenchmarkResult],
    ) -> DeviceResult<StatisticalAnalysis> {
        // Collect all execution times and fidelities
        let all_exec_times: Vec<f64> = results
            .iter()
            .flat_map(|r| r.execution_times.iter())
            .copied()
            .collect();

        let all_fidelities: Vec<f64> = results
            .iter()
            .flat_map(|r| r.fidelities.iter())
            .copied()
            .collect();

        let all_error_rates: Vec<f64> = results
            .iter()
            .flat_map(|r| r.error_rates.iter())
            .copied()
            .collect();

        // Convert to ndarray for SciRS2
        let exec_times_array = Array1::from_vec(all_exec_times);
        let fidelities_array = Array1::from_vec(all_fidelities);
        let error_rates_array = Array1::from_vec(all_error_rates);

        // Compute descriptive statistics
        let execution_time_stats = Self::compute_descriptive_stats(&exec_times_array)?;
        let fidelity_stats = Self::compute_descriptive_stats(&fidelities_array)?;
        let error_rate_stats = Self::compute_descriptive_stats(&error_rates_array)?;

        // Compute correlation matrix
        let exec_slice = exec_times_array
            .as_slice()
            .ok_or_else(|| DeviceError::APIError("Failed to get exec_times slice".to_string()))?;
        let fid_slice = fidelities_array
            .as_slice()
            .ok_or_else(|| DeviceError::APIError("Failed to get fidelities slice".to_string()))?;
        let err_slice = error_rates_array
            .as_slice()
            .ok_or_else(|| DeviceError::APIError("Failed to get error_rates slice".to_string()))?;

        let data_matrix = Array2::from_shape_vec(
            (exec_times_array.len(), 3),
            [exec_slice, fid_slice, err_slice].concat(),
        )
        .map_err(|e| DeviceError::APIError(format!("Array creation error: {e}")))?;

        let correlationmatrix = correlationmatrix(&data_matrix.view(), None)
            .map_err(|e| DeviceError::APIError(format!("Correlation computation error: {e:?}")))?;

        // Statistical tests
        let mut statistical_tests = HashMap::new();

        // Test if execution times follow normal distribution
        if exec_times_array.len() >= 8 {
            // One-sample t-test against expected baseline
            let baseline_time = 1.0; // 1 second baseline
            let t_test = ttest_1samp(
                &exec_times_array.view(),
                baseline_time,
                Alternative::TwoSided,
                "propagate",
            )
            .map_err(|e| DeviceError::APIError(format!("T-test error: {e:?}")))?;

            statistical_tests.insert(
                "execution_time_t_test".to_string(),
                TestResult {
                    test_name: "One-sample t-test (execution time)".to_string(),
                    statistic: t_test.statistic,
                    p_value: t_test.pvalue,
                    significant: t_test.pvalue < 0.05,
                    effect_size: Some(
                        (execution_time_stats.mean - baseline_time) / execution_time_stats.std_dev,
                    ),
                },
            );
        }

        // Distribution fitting
        let mut distribution_fits = HashMap::new();

        // Fit normal distribution to execution times
        if let Ok(normal_dist) =
            distributions::norm(execution_time_stats.mean, execution_time_stats.std_dev)
        {
            // Calculate goodness of fit (simplified)
            let goodness_of_fit = Self::calculate_goodness_of_fit(&exec_times_array, &normal_dist)?;

            distribution_fits.insert(
                "execution_time_normal".to_string(),
                DistributionFit {
                    distribution_name: "Normal".to_string(),
                    parameters: vec![execution_time_stats.mean, execution_time_stats.std_dev],
                    goodness_of_fit,
                    p_value: 0.5, // Placeholder
                },
            );
        }

        Ok(StatisticalAnalysis {
            execution_time_stats,
            fidelity_stats,
            error_rate_stats,
            correlationmatrix,
            statistical_tests,
            distribution_fits,
        })
    }

    /// Perform graph-theoretic analysis
    fn perform_graph_analysis(
        calibration: &DeviceCalibration,
        results: &[BenchmarkResult],
    ) -> DeviceResult<GraphAnalysis> {
        // Build connectivity graph from topology
        let mut graph = Graph::new();
        let mut node_map = HashMap::new();

        // Add nodes for each qubit
        for i in 0..calibration.topology.num_qubits {
            let node = graph.add_node(i);
            node_map.insert(i, node);
        }

        // Add edges based on coupling map
        for &(q1, q2) in &calibration.topology.coupling_map {
            let q1_idx = q1.0 as usize;
            let q2_idx = q2.0 as usize;
            if let (Some(&n1), Some(&n2)) = (node_map.get(&q1_idx), node_map.get(&q2_idx)) {
                let _ = graph.add_edge(n1.index(), n2.index(), 1.0);
            }
        }

        // Calculate graph metrics
        let graph_density_val = graph_density(&graph)
            .map_err(|e| DeviceError::APIError(format!("Graph density error: {e:?}")))?;

        let clustering_coeff = clustering_coefficient(&graph)
            .map_err(|e| DeviceError::APIError(format!("Clustering coefficient error: {e:?}")))?;

        // Calculate centrality measures for each qubit
        let betweenness_values = betweenness_centrality(&graph, false);

        let closeness_values = closeness_centrality(&graph, true);

        let mut centrality_measures = HashMap::new();
        for i in 0..calibration.topology.num_qubits {
            if let Some(&node) = node_map.get(&i) {
                let node_idx = node.index();
                centrality_measures.insert(
                    i,
                    CentralityMeasures {
                        betweenness: betweenness_values.get(&node_idx).copied().unwrap_or(0.0),
                        closeness: closeness_values.get(&node_idx).copied().unwrap_or(0.0),
                        eigenvector: 0.0, // Placeholder
                        degree: graph
                            .neighbors(&node_idx)
                            .map(|neighbors| neighbors.len() as f64)
                            .unwrap_or(0.0),
                    },
                );
            }
        }

        // Calculate spectral properties
        let spectral_properties = Self::calculate_spectral_properties(calibration)?;

        // Find critical paths
        let compatible_node_map: HashMap<usize, usize> =
            node_map.iter().map(|(&k, &v)| (k, v.index())).collect();
        let critical_paths = Self::find_critical_paths(&graph, &compatible_node_map, results)?;

        // Calculate topology-performance correlations
        let mut topology_correlations = HashMap::new();

        // Correlate connectivity with performance
        let connectivity_scores: Vec<f64> = centrality_measures
            .values()
            .map(|c| c.betweenness + c.closeness)
            .collect();

        let avg_fidelities: Vec<f64> = results
            .iter()
            .take(connectivity_scores.len())
            .map(|r| r.fidelities.iter().sum::<f64>() / r.fidelities.len() as f64)
            .collect();

        if connectivity_scores.len() == avg_fidelities.len() && connectivity_scores.len() >= 3 {
            let conn_array = Array1::from_vec(connectivity_scores);
            let fid_array = Array1::from_vec(avg_fidelities);

            if let Ok((correlation, _)) =
                pearsonr(&conn_array.view(), &fid_array.view(), "two-sided")
            {
                topology_correlations.insert("connectivity_fidelity".to_string(), correlation);
            }
        }

        let connectivity_metrics = ConnectivityMetrics {
            average_path_length: 0.0, // Placeholder - would calculate actual shortest paths
            clustering_coefficient: clustering_coeff.values().sum::<f64>()
                / clustering_coeff.len() as f64,
            graph_density: graph_density_val,
            diameter: 0, // Placeholder
            spectral_gap: spectral_properties.spectral_gap,
            centrality_measures,
        };

        Ok(GraphAnalysis {
            connectivity_metrics,
            topology_correlations,
            critical_paths,
            spectral_properties,
        })
    }

    /// Perform noise correlation analysis
    fn perform_noise_analysis(
        calibration: &DeviceCalibration,
        results: &[BenchmarkResult],
    ) -> DeviceResult<NoiseAnalysis> {
        let num_qubits = calibration.topology.num_qubits;

        // Build crosstalk correlation matrix
        let crosstalk_correlations = Array2::from_shape_fn((num_qubits, num_qubits), |(i, j)| {
            if i == j {
                1.0
            } else {
                // Use crosstalk matrix data if available
                calibration
                    .crosstalk_matrix
                    .matrix
                    .get(i)
                    .and_then(|row| row.get(j))
                    .copied()
                    .unwrap_or(0.0)
            }
        });

        // Temporal correlations (placeholder)
        let temporal_correlations = Array1::zeros(results.len());

        // Spatial noise patterns
        let mut spatial_patterns = HashMap::new();

        // Error rate spatial pattern
        let error_pattern = Array2::from_shape_fn((num_qubits, num_qubits), |(i, j)| {
            // Simplified spatial error pattern based on distance
            let distance = ((i as f64) - (j as f64)).abs();
            (-distance * 0.1).exp() * 0.01 // Exponential decay with distance
        });
        spatial_patterns.insert("error_rates".to_string(), error_pattern);

        // Noise model validation
        let noise_model = CalibrationNoiseModel::from_calibration(calibration);
        let model_validation = Self::validate_noise_model(&noise_model, results)?;

        Ok(NoiseAnalysis {
            crosstalk_correlations,
            temporal_correlations,
            spatial_patterns,
            model_validation,
        })
    }

    /// Calculate performance metrics
    fn calculate_performance_metrics(
        results: &[BenchmarkResult],
        stats: &StatisticalAnalysis,
        graph_analysis: Option<&GraphAnalysis>,
    ) -> DeviceResult<PerformanceMetrics> {
        // Calculate component scores (0-100)
        let reliability_score = (stats.fidelity_stats.mean * 100.0).min(100.0);
        let speed_score = (1.0 / stats.execution_time_stats.mean * 10.0).min(100.0);
        let accuracy_score = ((1.0 - stats.error_rate_stats.mean) * 100.0).min(100.0);

        // Efficiency based on correlation between resources and performance
        let efficiency_score = graph_analysis.map_or(50.0, |graph| {
            // Use connectivity metrics to evaluate efficiency
            (graph.connectivity_metrics.clustering_coefficient * 100.0).min(100.0)
        });

        // Scalability metrics
        let scalability_metrics = Self::calculate_scalability_metrics(results)?;

        // Overall score (weighted average)
        let overall_score = (reliability_score * 0.3
            + speed_score * 0.2
            + accuracy_score * 0.3
            + efficiency_score * 0.2)
            .min(100.0);

        Ok(PerformanceMetrics {
            overall_score,
            reliability_score,
            speed_score,
            accuracy_score,
            efficiency_score,
            scalability_metrics,
        })
    }

    // Helper methods

    fn get_backend_type(device_id: &str) -> DeviceResult<crate::translation::HardwareBackend> {
        // Infer backend type from device ID
        if device_id.contains("ibm") {
            Ok(crate::translation::HardwareBackend::IBMQuantum)
        } else if device_id.contains("ionq") {
            Ok(crate::translation::HardwareBackend::IonQ)
        } else {
            Ok(crate::translation::HardwareBackend::IBMQuantum) // Default
        }
    }

    fn create_gate_benchmark_circuit(
        gate_type: &str,
        num_qubits: usize,
    ) -> DeviceResult<Circuit<8>> {
        let mut circuit = Circuit::<8>::new();

        match gate_type {
            "H" => {
                let _ = circuit.h(QubitId(0));
            }
            "CNOT" => {
                let _ = circuit.cnot(QubitId(0), QubitId(1));
            }
            "RZ" => {
                let _ = circuit.rz(QubitId(0), std::f64::consts::PI / 4.0);
            }
            _ => {
                return Err(DeviceError::UnsupportedOperation(format!(
                    "Gate type: {gate_type}"
                )))
            }
        }

        Ok(circuit)
    }

    fn create_depth_benchmark_circuit(depth: usize, num_qubits: usize) -> DeviceResult<Circuit<8>> {
        let mut circuit = Circuit::<8>::new();

        for layer in 0..depth {
            for qubit in 0..num_qubits {
                if layer % 2 == 0 {
                    let _ = circuit.h(QubitId(qubit as u32));
                } else if qubit + 1 < num_qubits {
                    let _ = circuit.cnot(QubitId(qubit as u32), QubitId((qubit + 1) as u32));
                }
            }
        }

        Ok(circuit)
    }

    fn create_random_circuit<const N: usize>(
        num_qubits: usize,
        depth: usize,
    ) -> DeviceResult<Circuit<N>> {
        use scirs2_core::random::prelude::*;
        let mut rng = thread_rng();
        let mut circuit = Circuit::<N>::new();

        let gates = ["H", "X", "Y", "Z", "CNOT", "RZ"];

        for _ in 0..depth {
            let gate_idx = rng.random_range(0..gates.len());
            let gate = gates[gate_idx];
            let qubit = rng.random_range(0..num_qubits) as u32;

            match gate {
                "H" => {
                    let _ = circuit.h(QubitId(qubit));
                }
                "X" => {
                    let _ = circuit.x(QubitId(qubit));
                }
                "Y" => {
                    let _ = circuit.y(QubitId(qubit));
                }
                "Z" => {
                    let _ = circuit.z(QubitId(qubit));
                }
                "CNOT" => {
                    let target = rng.random_range(0..num_qubits) as u32;
                    if target != qubit {
                        let _ = circuit.cnot(QubitId(qubit), QubitId(target));
                    }
                }
                "RZ" => {
                    let angle = rng.random_range(-std::f64::consts::PI..std::f64::consts::PI);
                    let _ = circuit.rz(QubitId(qubit), angle);
                }
                _ => {}
            }
        }

        Ok(circuit)
    }

    fn calculate_fidelity(
        result: &CircuitResult,
        circuit: &Circuit<8>,
        calibration: &DeviceCalibration,
    ) -> DeviceResult<f64> {
        // Simplified fidelity calculation based on expected vs actual outcomes
        // In practice, this would use state tomography or process tomography

        // For now, estimate based on gate error rates
        let mut total_fidelity = 1.0;

        for gate in circuit.gates() {
            let qubits = gate.qubits();

            if qubits.len() == 1 {
                if let Some(gate_cal) = calibration.single_qubit_gates.get(gate.name()) {
                    if let Some(qubit_data) = gate_cal.qubit_data.get(&qubits[0]) {
                        total_fidelity *= qubit_data.fidelity;
                    }
                }
            } else if qubits.len() == 2 {
                let qubit_pair = (qubits[0], qubits[1]);
                if let Some(gate_cal) = calibration.two_qubit_gates.get(&qubit_pair) {
                    total_fidelity *= gate_cal.fidelity;
                }
            }
        }

        Ok(total_fidelity)
    }

    fn calculate_success_probability(result: &CircuitResult) -> DeviceResult<f64> {
        // Calculate success probability based on measurement outcomes
        let total_shots = result.shots as f64;
        let successful_outcomes = result
            .counts
            .values()
            .map(|&count| count as f64)
            .sum::<f64>();

        Ok(successful_outcomes / total_shots)
    }

    fn compute_descriptive_stats(data: &Array1<f64>) -> DeviceResult<DescriptiveStats> {
        let mean_val = mean(&data.view())
            .map_err(|e| DeviceError::APIError(format!("Mean calculation error: {e:?}")))?;

        let median_val = median(&data.view())
            .map_err(|e| DeviceError::APIError(format!("Median calculation error: {e:?}")))?;

        let std_val = std(&data.view(), 1, None)
            .map_err(|e| DeviceError::APIError(format!("Std calculation error: {e:?}")))?;

        let var_val = var(&data.view(), 1, None)
            .map_err(|e| DeviceError::APIError(format!("Variance calculation error: {e:?}")))?;

        let min_val = data.iter().fold(f64::INFINITY, |a, &b| a.min(b));
        let max_val = data.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b));

        // Calculate quartiles (simplified)
        let mut sorted_data = data.to_vec();
        sorted_data.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

        let n = sorted_data.len();
        let q25 = sorted_data[n / 4];
        let q75 = sorted_data[3 * n / 4];

        // Confidence interval (95%)
        let t_critical = 1.96; // Approximate for large samples
        let sem = std_val / (n as f64).sqrt();
        let margin = t_critical * sem;
        let confidence_interval = (mean_val - margin, mean_val + margin);

        Ok(DescriptiveStats {
            mean: mean_val,
            median: median_val,
            std_dev: std_val,
            variance: var_val,
            min: min_val,
            max: max_val,
            q25,
            q75,
            confidence_interval,
        })
    }

    fn calculate_goodness_of_fit(
        data: &Array1<f64>,
        distribution: &dyn scirs2_stats::traits::Distribution<f64>,
    ) -> DeviceResult<f64> {
        // Simplified goodness of fit using Kolmogorov-Smirnov test
        // In practice, would use proper KS test from SciRS2

        let mut sorted_data = data.to_vec();
        sorted_data.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

        let n = sorted_data.len() as f64;
        let mut max_diff: f64 = 0.0;

        for (i, &value) in sorted_data.iter().enumerate() {
            let empirical_cdf = (i + 1) as f64 / n;
            let theoretical_cdf = 0.5; // Fallback for missing CDF method
            let diff = (empirical_cdf - theoretical_cdf).abs();
            max_diff = max_diff.max(diff);
        }

        Ok(1.0 - max_diff) // Convert to goodness (higher is better)
    }

    fn calculate_spectral_properties(
        calibration: &DeviceCalibration,
    ) -> DeviceResult<SpectralProperties> {
        let n = calibration.topology.num_qubits;

        // Build adjacency matrix
        let mut adj_matrix = Array2::zeros((n, n));
        for &(q1, q2) in &calibration.topology.coupling_map {
            adj_matrix[[q1.0 as usize, q2.0 as usize]] = 1.0;
            adj_matrix[[q2.0 as usize, q1.0 as usize]] = 1.0;
        }

        // Calculate eigenvalues
        let eigenvalues = match eigvals(&adj_matrix.view(), None) {
            Ok(vals) => vals.mapv(|c| c.re), // Take real parts
            Err(_) => Array1::zeros(n),      // Fallback
        };

        let spectral_radius = eigenvalues
            .iter()
            .fold(0.0_f64, |max, &val: &f64| max.max(val.abs()));

        // Sort eigenvalues
        let mut sorted_eigenvals = eigenvalues.to_vec();
        sorted_eigenvals.sort_by(|a, b| b.partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal));

        let algebraic_connectivity = if sorted_eigenvals.len() > 1 {
            sorted_eigenvals[sorted_eigenvals.len() - 2]
        } else {
            0.0
        };

        let spectral_gap = if sorted_eigenvals.len() > 1 {
            sorted_eigenvals[0] - sorted_eigenvals[1]
        } else {
            0.0
        };

        Ok(SpectralProperties {
            eigenvalues: Array1::from_vec(sorted_eigenvals),
            spectral_radius,
            algebraic_connectivity,
            spectral_gap,
        })
    }

    fn find_critical_paths(
        graph: &Graph<usize, f64>,
        node_map: &HashMap<usize, usize>,
        results: &[BenchmarkResult],
    ) -> DeviceResult<Vec<CriticalPath>> {
        let mut critical_paths = Vec::new();

        // Find paths between high-error qubits
        // This is a simplified version - would use more sophisticated analysis

        for i in 0..3 {
            // Find up to 3 critical paths
            let path = vec![i, i + 1, i + 2]; // Simplified linear path

            critical_paths.push(CriticalPath {
                path: path.clone(),
                length: path.len(),
                bottleneck_qubits: vec![i + 1], // Middle qubit as bottleneck
                expected_performance_impact: 0.1 * (i + 1) as f64,
            });
        }

        Ok(critical_paths)
    }

    fn validate_noise_model(
        noise_model: &CalibrationNoiseModel,
        results: &[BenchmarkResult],
    ) -> DeviceResult<NoiseModelValidation> {
        // Compare predicted vs actual error rates
        let predicted_errors: Vec<f64> = results
            .iter()
            .map(|r| {
                // Simplified prediction based on gate count
                let base_error = 0.001;
                base_error * r.gate_count as f64
            })
            .collect();

        let actual_errors: Vec<f64> = results
            .iter()
            .map(|r| r.error_rates.iter().sum::<f64>() / r.error_rates.len() as f64)
            .collect();

        let prediction_errors = Array1::from_vec(
            predicted_errors
                .iter()
                .zip(actual_errors.iter())
                .map(|(pred, actual)| (pred - actual).abs())
                .collect(),
        );

        let model_accuracy = 1.0 - (prediction_errors.mean().unwrap_or(0.5));

        // Residual analysis (simplified)
        let residuals = Array1::from_vec(
            predicted_errors
                .iter()
                .zip(actual_errors.iter())
                .map(|(pred, actual)| pred - actual)
                .collect(),
        );

        let normality_test = TestResult {
            test_name: "Residual normality test".to_string(),
            statistic: 0.0,
            p_value: 0.5,
            significant: false,
            effect_size: None,
        };

        let residual_analysis = ResidualAnalysis {
            normality_test,
            autocorrelation: Array1::zeros(5), // Placeholder
            heteroscedasticity_test: TestResult {
                test_name: "Heteroscedasticity test".to_string(),
                statistic: 0.0,
                p_value: 0.5,
                significant: false,
                effect_size: None,
            },
        };

        Ok(NoiseModelValidation {
            model_accuracy,
            prediction_errors,
            residual_analysis,
        })
    }

    fn calculate_scalability_metrics(
        results: &[BenchmarkResult],
    ) -> DeviceResult<ScalabilityMetrics> {
        // Analyze how performance scales with circuit size

        // Group results by depth and width
        let mut depth_times = HashMap::new();
        let mut width_times = HashMap::new();

        for result in results {
            depth_times
                .entry(result.circuit_depth)
                .or_insert_with(Vec::new)
                .extend(&result.execution_times);

            width_times
                .entry(result.num_qubits)
                .or_insert_with(Vec::new)
                .extend(&result.execution_times);
        }

        // Calculate scaling coefficients (simplified linear regression)
        let depth_scaling_coefficient = Self::calculate_scaling_coefficient(&depth_times)?;
        let width_scaling_coefficient = Self::calculate_scaling_coefficient(&width_times)?;

        let resource_efficiency =
            1.0 / (depth_scaling_coefficient + width_scaling_coefficient).max(0.1);
        let parallelization_factor = 0.8; // Placeholder - would measure actual parallelization

        Ok(ScalabilityMetrics {
            depth_scaling_coefficient,
            width_scaling_coefficient,
            resource_efficiency,
            parallelization_factor,
        })
    }

    fn calculate_scaling_coefficient(data: &HashMap<usize, Vec<f64>>) -> DeviceResult<f64> {
        if data.len() < 2 {
            return Ok(1.0); // Default linear scaling
        }

        // Simple linear regression: time = a * size + b
        // Return coefficient 'a'

        let mut x_values = Vec::new();
        let mut y_values = Vec::new();

        for (&size, times) in data {
            let avg_time = times.iter().sum::<f64>() / times.len() as f64;
            x_values.push(size as f64);
            y_values.push(avg_time);
        }

        // Calculate slope (scaling coefficient)
        let n = x_values.len() as f64;
        let sum_x: f64 = x_values.iter().sum();
        let sum_y: f64 = y_values.iter().sum();
        let sum_xy: f64 = x_values
            .iter()
            .zip(y_values.iter())
            .map(|(x, y)| x * y)
            .sum();
        let sum_x_sq: f64 = x_values.iter().map(|x| x * x).sum();

        let slope = n.mul_add(sum_xy, -(sum_x * sum_y)) / n.mul_add(sum_x_sq, -(sum_x * sum_x));

        Ok(slope.max(0.1)) // Ensure positive scaling
    }
}

/// Trait for devices that can execute circuits (for benchmarking)
#[async_trait::async_trait]
pub trait DeviceExecutor {
    async fn execute_circuit<const N: usize>(
        &self,
        circuit: &Circuit<N>,
        shots: usize,
    ) -> DeviceResult<CircuitResult>;
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::calibration::create_ideal_calibration;

    #[test]
    fn test_benchmark_config_default() {
        let config = BenchmarkConfig::default();
        assert_eq!(config.iterations, 100);
        assert!(config.enable_advanced_stats);
    }

    #[test]
    fn test_descriptive_stats_calculation() {
        let data = Array1::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0]);
        let suite =
            HardwareBenchmarkSuite::new(CalibrationManager::new(), BenchmarkConfig::default());

        let stats = HardwareBenchmarkSuite::compute_descriptive_stats(&data)
            .expect("Descriptive stats computation should succeed");
        assert!((stats.mean - 3.0).abs() < 1e-10);
        assert!((stats.median - 3.0).abs() < 1e-10);
    }

    #[test]
    fn test_spectral_properties() {
        let calibration = create_ideal_calibration("test".to_string(), 4);
        let suite =
            HardwareBenchmarkSuite::new(CalibrationManager::new(), BenchmarkConfig::default());

        let spectral_props = HardwareBenchmarkSuite::calculate_spectral_properties(&calibration)
            .expect("Spectral properties calculation should succeed");
        assert!(spectral_props.eigenvalues.len() == 4);
        assert!(spectral_props.spectral_radius >= 0.0);
    }
}