quantrs2-circuit 0.1.3

Quantum circuit representation and DSL 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
//! `SciRS2` statistical tools for circuit benchmarking
//!
//! This module leverages `SciRS2`'s advanced statistical analysis capabilities to provide
//! comprehensive benchmarking, performance analysis, and statistical insights for quantum circuits.

use crate::builder::Circuit;
use crate::noise_models::{NoiseAnalysisResult, NoiseModel};
use crate::simulator_interface::{ExecutionResult, SimulatorBackend};
use quantrs2_core::{
    error::{QuantRS2Error, QuantRS2Result},
    gate::GateOp,
};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::{Duration, Instant};

// Placeholder types representing SciRS2 statistical interface
// In the real implementation, these would be imported from SciRS2

/// Statistical distribution types supported by `SciRS2`
#[derive(Debug, Clone, PartialEq)]
pub enum Distribution {
    /// Normal distribution
    Normal { mean: f64, std_dev: f64 },
    /// Uniform distribution
    Uniform { min: f64, max: f64 },
    /// Exponential distribution
    Exponential { rate: f64 },
    /// Beta distribution
    Beta { alpha: f64, beta: f64 },
    /// Gamma distribution
    Gamma { shape: f64, scale: f64 },
    /// Poisson distribution
    Poisson { lambda: f64 },
    /// Chi-squared distribution
    ChiSquared { degrees_of_freedom: usize },
    /// Student's t-distribution
    StudentT { degrees_of_freedom: usize },
}

/// Statistical test types
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StatisticalTest {
    /// Kolmogorov-Smirnov test
    KolmogorovSmirnov,
    /// Anderson-Darling test
    AndersonDarling,
    /// Shapiro-Wilk test for normality
    ShapiroWilk,
    /// Mann-Whitney U test
    MannWhitney,
    /// Wilcoxon signed-rank test
    Wilcoxon,
    /// Chi-squared goodness of fit
    ChiSquaredGoodnessOfFit,
    /// ANOVA F-test
    ANOVA,
    /// Kruskal-Wallis test
    KruskalWallis,
}

/// Hypothesis test result
#[derive(Debug, Clone)]
pub struct HypothesisTestResult {
    /// Test statistic value
    pub test_statistic: f64,
    /// P-value
    pub p_value: f64,
    /// Critical value at significance level
    pub critical_value: f64,
    /// Whether null hypothesis is rejected
    pub reject_null: bool,
    /// Significance level used
    pub significance_level: f64,
    /// Effect size (if applicable)
    pub effect_size: Option<f64>,
    /// Confidence interval
    pub confidence_interval: Option<(f64, f64)>,
}

/// Descriptive statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DescriptiveStats {
    /// Sample size
    pub count: usize,
    /// Mean
    pub mean: f64,
    /// Standard deviation
    pub std_dev: f64,
    /// Variance
    pub variance: f64,
    /// Minimum value
    pub min: f64,
    /// Maximum value
    pub max: f64,
    /// Median (50th percentile)
    pub median: f64,
    /// First quartile (25th percentile)
    pub q1: f64,
    /// Third quartile (75th percentile)
    pub q3: f64,
    /// Interquartile range
    pub iqr: f64,
    /// Skewness
    pub skewness: f64,
    /// Kurtosis
    pub kurtosis: f64,
    /// Mode (most frequent value)
    pub mode: Option<f64>,
}

/// Benchmarking configuration
#[derive(Debug, Clone)]
pub struct BenchmarkConfig {
    /// Number of benchmark runs
    pub num_runs: usize,
    /// Warm-up runs to exclude from statistics
    pub warmup_runs: usize,
    /// Timeout per run
    pub timeout: Duration,
    /// Significance level for statistical tests
    pub significance_level: f64,
    /// Whether to collect detailed timing data
    pub collect_timing: bool,
    /// Whether to collect memory usage data
    pub collect_memory: bool,
    /// Whether to collect error statistics
    pub collect_errors: bool,
    /// Random seed for reproducible benchmarks
    pub seed: Option<u64>,
}

impl Default for BenchmarkConfig {
    fn default() -> Self {
        Self {
            num_runs: 100,
            warmup_runs: 10,
            timeout: Duration::from_secs(60),
            significance_level: 0.05,
            collect_timing: true,
            collect_memory: false,
            collect_errors: true,
            seed: None,
        }
    }
}

/// Circuit benchmarking suite using `SciRS2` statistical tools
pub struct CircuitBenchmark {
    /// Benchmark configuration
    config: BenchmarkConfig,
    /// Collected benchmark data
    benchmark_data: Vec<BenchmarkRun>,
    /// Statistical analyzer
    stats_analyzer: StatisticalAnalyzer,
}

/// Single benchmark run data
#[derive(Debug, Clone)]
pub struct BenchmarkRun {
    /// Run identifier
    pub run_id: usize,
    /// Execution time
    pub execution_time: Duration,
    /// Memory usage in bytes
    pub memory_usage: Option<usize>,
    /// Success/failure status
    pub success: bool,
    /// Error message if failed
    pub error_message: Option<String>,
    /// Circuit metrics
    pub circuit_metrics: CircuitMetrics,
    /// Execution results
    pub execution_results: Option<ExecutionResult>,
    /// Noise analysis results
    pub noise_analysis: Option<NoiseAnalysisResult>,
    /// Custom metrics
    pub custom_metrics: HashMap<String, f64>,
}

/// Circuit performance metrics
#[derive(Debug, Clone)]
pub struct CircuitMetrics {
    /// Circuit depth
    pub depth: usize,
    /// Total gate count
    pub gate_count: usize,
    /// Gate count by type
    pub gate_counts: HashMap<String, usize>,
    /// Two-qubit gate count
    pub two_qubit_gates: usize,
    /// Circuit fidelity estimate
    pub fidelity: Option<f64>,
    /// Error rate estimate
    pub error_rate: Option<f64>,
}

/// Comprehensive benchmark report
#[derive(Debug, Clone)]
pub struct BenchmarkReport {
    /// Benchmark configuration used
    pub config: BenchmarkConfig,
    /// Total runs completed
    pub completed_runs: usize,
    /// Success rate
    pub success_rate: f64,
    /// Timing statistics
    pub timing_stats: DescriptiveStats,
    /// Memory statistics (if collected)
    pub memory_stats: Option<DescriptiveStats>,
    /// Performance regression analysis
    pub regression_analysis: Option<RegressionAnalysis>,
    /// Distribution fitting results
    pub distribution_fit: Option<DistributionFit>,
    /// Outlier analysis
    pub outlier_analysis: OutlierAnalysis,
    /// Performance comparison with baseline
    pub baseline_comparison: Option<BaselineComparison>,
    /// Statistical test results
    pub statistical_tests: Vec<HypothesisTestResult>,
    /// Performance insights and recommendations
    pub insights: Vec<PerformanceInsight>,
}

/// Regression analysis results
#[derive(Debug, Clone)]
pub struct RegressionAnalysis {
    /// Linear regression slope
    pub slope: f64,
    /// Y-intercept
    pub intercept: f64,
    /// R-squared correlation coefficient
    pub r_squared: f64,
    /// P-value for slope significance
    pub slope_p_value: f64,
    /// Whether there's a significant trend
    pub significant_trend: bool,
    /// Predicted performance degradation per run
    pub degradation_per_run: f64,
}

/// Distribution fitting analysis
#[derive(Debug, Clone)]
pub struct DistributionFit {
    /// Best fitting distribution
    pub best_distribution: Distribution,
    /// Goodness of fit score
    pub goodness_of_fit: f64,
    /// P-value for fit test
    pub fit_p_value: f64,
    /// Alternative distributions tested
    pub alternative_fits: Vec<(Distribution, f64)>,
}

/// Outlier detection and analysis
#[derive(Debug, Clone)]
pub struct OutlierAnalysis {
    /// Number of outliers detected
    pub num_outliers: usize,
    /// Outlier indices
    pub outlier_indices: Vec<usize>,
    /// Outlier detection method used
    pub detection_method: OutlierDetectionMethod,
    /// Outlier threshold used
    pub threshold: f64,
    /// Impact of outliers on statistics
    pub outlier_impact: OutlierImpact,
}

/// Outlier detection methods
#[derive(Debug, Clone, PartialEq)]
pub enum OutlierDetectionMethod {
    /// Interquartile range method
    IQR { multiplier: f64 },
    /// Z-score method
    ZScore { threshold: f64 },
    /// Modified Z-score (median absolute deviation)
    ModifiedZScore { threshold: f64 },
    /// Isolation forest
    IsolationForest,
    /// Local outlier factor
    LocalOutlierFactor,
}

/// Impact of outliers on statistical measures
#[derive(Debug, Clone)]
pub struct OutlierImpact {
    /// Change in mean when outliers removed
    pub mean_change: f64,
    /// Change in standard deviation when outliers removed
    pub std_dev_change: f64,
    /// Change in median when outliers removed
    pub median_change: f64,
    /// Relative impact percentage
    pub relative_impact: f64,
}

/// Baseline performance comparison
#[derive(Debug, Clone)]
pub struct BaselineComparison {
    /// Baseline benchmark name
    pub baseline_name: String,
    /// Performance improvement/degradation factor
    pub performance_factor: f64,
    /// Statistical significance of difference
    pub significance: HypothesisTestResult,
    /// Confidence interval for difference
    pub difference_ci: (f64, f64),
    /// Effect size
    pub effect_size: f64,
    /// Practical significance assessment
    pub practical_significance: PracticalSignificance,
}

/// Practical significance assessment
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PracticalSignificance {
    /// Negligible difference
    Negligible,
    /// Small effect
    Small,
    /// Medium effect
    Medium,
    /// Large effect
    Large,
    /// Very large effect
    VeryLarge,
}

/// Performance insights and recommendations
#[derive(Debug, Clone)]
pub struct PerformanceInsight {
    /// Insight category
    pub category: InsightCategory,
    /// Insight message
    pub message: String,
    /// Confidence level (0.0 to 1.0)
    pub confidence: f64,
    /// Supporting evidence
    pub evidence: Vec<String>,
    /// Recommended actions
    pub recommendations: Vec<String>,
}

/// Performance insight categories
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum InsightCategory {
    /// Performance degradation detected
    PerformanceDegradation,
    /// Performance improvement detected
    PerformanceImprovement,
    /// High variability in results
    HighVariability,
    /// Outliers detected
    OutliersDetected,
    /// Memory usage concerns
    MemoryUsage,
    /// Error rate concerns
    ErrorRate,
    /// Circuit optimization opportunity
    OptimizationOpportunity,
}

impl CircuitBenchmark {
    /// Create a new circuit benchmark suite
    #[must_use]
    pub const fn new(config: BenchmarkConfig) -> Self {
        Self {
            config,
            benchmark_data: Vec::new(),
            stats_analyzer: StatisticalAnalyzer::new(),
        }
    }

    /// Run comprehensive benchmark suite
    pub fn run_benchmark<const N: usize>(
        &mut self,
        circuit: &Circuit<N>,
        simulator: &dyn SimulatorExecutor,
        noise_model: Option<&NoiseModel>,
    ) -> QuantRS2Result<BenchmarkReport> {
        self.benchmark_data.clear();

        let total_runs = self.config.num_runs + self.config.warmup_runs;

        for run_id in 0..total_runs {
            let is_warmup = run_id < self.config.warmup_runs;

            match self.run_single_benchmark(circuit, simulator, noise_model, run_id) {
                Ok(run_data) => {
                    if !is_warmup {
                        self.benchmark_data.push(run_data);
                    }
                }
                Err(e) => {
                    if !is_warmup {
                        // Record failed run
                        let failed_run = BenchmarkRun {
                            run_id,
                            execution_time: Duration::from_millis(0),
                            memory_usage: None,
                            success: false,
                            error_message: Some(e.to_string()),
                            circuit_metrics: self.calculate_circuit_metrics(circuit),
                            execution_results: None,
                            noise_analysis: None,
                            custom_metrics: HashMap::new(),
                        };
                        self.benchmark_data.push(failed_run);
                    }
                }
            }
        }

        self.generate_benchmark_report()
    }

    /// Run a single benchmark iteration
    fn run_single_benchmark<const N: usize>(
        &self,
        circuit: &Circuit<N>,
        simulator: &dyn SimulatorExecutor,
        noise_model: Option<&NoiseModel>,
        run_id: usize,
    ) -> QuantRS2Result<BenchmarkRun> {
        let start_time = Instant::now();
        let start_memory = if self.config.collect_memory {
            Some(self.get_memory_usage())
        } else {
            None
        };

        // Simulate circuit execution (placeholder)
        let execution_results = None; // Would call simulator.execute()

        let end_time = Instant::now();
        let end_memory = if self.config.collect_memory {
            Some(self.get_memory_usage())
        } else {
            None
        };

        let execution_time = end_time - start_time;
        let memory_usage = match (start_memory, end_memory) {
            (Some(start), Some(end)) => Some(end.saturating_sub(start)),
            _ => None,
        };

        // Analyze noise if model provided
        let noise_analysis = if let Some(noise) = noise_model {
            // Would perform noise analysis here
            None
        } else {
            None
        };

        Ok(BenchmarkRun {
            run_id,
            execution_time,
            memory_usage,
            success: true,
            error_message: None,
            circuit_metrics: self.calculate_circuit_metrics(circuit),
            execution_results,
            noise_analysis,
            custom_metrics: HashMap::new(),
        })
    }

    /// Calculate circuit metrics
    fn calculate_circuit_metrics<const N: usize>(&self, circuit: &Circuit<N>) -> CircuitMetrics {
        let gate_count = circuit.gates().len();
        let mut gate_counts = HashMap::new();
        let mut two_qubit_gates = 0;

        for gate in circuit.gates() {
            let gate_name = gate.name();
            *gate_counts.entry(gate_name.to_string()).or_insert(0) += 1;

            if gate.qubits().len() == 2 {
                two_qubit_gates += 1;
            }
        }

        CircuitMetrics {
            depth: gate_count, // Simplified depth calculation
            gate_count,
            gate_counts,
            two_qubit_gates,
            fidelity: None,
            error_rate: None,
        }
    }

    /// Get current memory usage (placeholder)
    const fn get_memory_usage(&self) -> usize {
        // In real implementation, this would use system APIs to get memory usage
        0
    }

    /// Generate comprehensive benchmark report
    fn generate_benchmark_report(&self) -> QuantRS2Result<BenchmarkReport> {
        let completed_runs = self.benchmark_data.len();
        let successful_runs: Vec<_> = self
            .benchmark_data
            .iter()
            .filter(|run| run.success)
            .collect();

        let success_rate = successful_runs.len() as f64 / completed_runs as f64;

        // Extract timing data
        let timing_data: Vec<f64> = successful_runs
            .iter()
            .map(|run| run.execution_time.as_secs_f64())
            .collect();

        let timing_stats = self
            .stats_analyzer
            .calculate_descriptive_stats(&timing_data)?;

        // Extract memory data if available
        let memory_stats = if self.config.collect_memory {
            let memory_data: Vec<f64> = successful_runs
                .iter()
                .filter_map(|run| run.memory_usage.map(|m| m as f64))
                .collect();

            if memory_data.is_empty() {
                None
            } else {
                Some(
                    self.stats_analyzer
                        .calculate_descriptive_stats(&memory_data)?,
                )
            }
        } else {
            None
        };

        // Perform regression analysis to detect performance trends
        let regression_analysis = self
            .stats_analyzer
            .perform_regression_analysis(&timing_data)?;

        // Fit distributions to timing data
        let distribution_fit = self.stats_analyzer.fit_distributions(&timing_data)?;

        // Detect outliers
        let outlier_analysis = self.stats_analyzer.detect_outliers(
            &timing_data,
            OutlierDetectionMethod::IQR { multiplier: 1.5 },
        )?;

        // Generate insights
        let insights = self.generate_performance_insights(
            &timing_stats,
            &regression_analysis,
            &outlier_analysis,
            success_rate,
        );

        Ok(BenchmarkReport {
            config: self.config.clone(),
            completed_runs,
            success_rate,
            timing_stats,
            memory_stats,
            regression_analysis: Some(regression_analysis),
            distribution_fit: Some(distribution_fit),
            outlier_analysis,
            baseline_comparison: None,
            statistical_tests: Vec::new(),
            insights,
        })
    }

    /// Generate performance insights based on statistical analysis
    fn generate_performance_insights(
        &self,
        timing_stats: &DescriptiveStats,
        regression: &RegressionAnalysis,
        outliers: &OutlierAnalysis,
        success_rate: f64,
    ) -> Vec<PerformanceInsight> {
        let mut insights = Vec::new();

        // Check for performance degradation
        if regression.significant_trend && regression.slope > 0.0 {
            insights.push(PerformanceInsight {
                category: InsightCategory::PerformanceDegradation,
                message: format!(
                    "Significant performance degradation detected: {:.4} seconds per run increase",
                    regression.degradation_per_run
                ),
                confidence: 1.0 - regression.slope_p_value,
                evidence: vec![
                    format!("Linear trend slope: {:.6}", regression.slope),
                    format!("R-squared: {:.4}", regression.r_squared),
                    format!("P-value: {:.4}", regression.slope_p_value),
                ],
                recommendations: vec![
                    "Investigate potential memory leaks".to_string(),
                    "Check for resource contention".to_string(),
                    "Profile execution to identify bottlenecks".to_string(),
                ],
            });
        }

        // Check for high variability
        let coefficient_of_variation = timing_stats.std_dev / timing_stats.mean;
        if coefficient_of_variation > 0.2 {
            insights.push(PerformanceInsight {
                category: InsightCategory::HighVariability,
                message: format!(
                    "High performance variability detected: CV = {:.2}%",
                    coefficient_of_variation * 100.0
                ),
                confidence: 0.8,
                evidence: vec![
                    format!("Standard deviation: {:.4} seconds", timing_stats.std_dev),
                    format!("Mean: {:.4} seconds", timing_stats.mean),
                    format!(
                        "Coefficient of variation: {:.2}%",
                        coefficient_of_variation * 100.0
                    ),
                ],
                recommendations: vec![
                    "Increase warm-up runs to stabilize performance".to_string(),
                    "Check for system load variations".to_string(),
                    "Consider running benchmarks in isolated environment".to_string(),
                ],
            });
        }

        // Check for outliers
        if outliers.num_outliers > 0 {
            let outlier_percentage =
                outliers.num_outliers as f64 / timing_stats.count as f64 * 100.0;
            insights.push(PerformanceInsight {
                category: InsightCategory::OutliersDetected,
                message: format!(
                    "Performance outliers detected: {} outliers ({:.1}% of runs)",
                    outliers.num_outliers, outlier_percentage
                ),
                confidence: 0.9,
                evidence: vec![
                    format!("Number of outliers: {}", outliers.num_outliers),
                    format!("Outlier percentage: {:.1}%", outlier_percentage),
                    format!("Detection method: {:?}", outliers.detection_method),
                ],
                recommendations: vec![
                    "Investigate causes of outlier runs".to_string(),
                    "Consider removing outliers from performance metrics".to_string(),
                    "Check for system interruptions during benchmarking".to_string(),
                ],
            });
        }

        // Check success rate
        if success_rate < 0.95 {
            insights.push(PerformanceInsight {
                category: InsightCategory::ErrorRate,
                message: format!("Low success rate detected: {:.1}%", success_rate * 100.0),
                confidence: 1.0,
                evidence: vec![
                    format!("Success rate: {:.1}%", success_rate * 100.0),
                    format!(
                        "Failed runs: {}",
                        timing_stats.count - (timing_stats.count as f64 * success_rate) as usize
                    ),
                ],
                recommendations: vec![
                    "Investigate failure causes".to_string(),
                    "Check circuit validity and simulator compatibility".to_string(),
                    "Increase timeout limits if timeouts are occurring".to_string(),
                ],
            });
        }

        insights
    }

    /// Compare with baseline benchmark
    pub fn compare_with_baseline(
        &self,
        baseline: &BenchmarkReport,
    ) -> QuantRS2Result<BaselineComparison> {
        if self.benchmark_data.is_empty() {
            return Err(QuantRS2Error::InvalidInput(
                "No benchmark data available for comparison".to_string(),
            ));
        }

        let current_timing: Vec<f64> = self
            .benchmark_data
            .iter()
            .filter(|run| run.success)
            .map(|run| run.execution_time.as_secs_f64())
            .collect();

        let baseline_mean = baseline.timing_stats.mean;
        let current_mean = self
            .stats_analyzer
            .calculate_descriptive_stats(&current_timing)?
            .mean;

        let performance_factor = current_mean / baseline_mean;

        // Perform statistical test for significance
        let significance = self.stats_analyzer.mann_whitney_test(
            &current_timing,
            &[baseline_mean], // Simplified - would use actual baseline data
            self.config.significance_level,
        )?;

        // Calculate effect size (Cohen's d)
        let effect_size = (current_mean - baseline_mean) / baseline.timing_stats.std_dev;

        // Assess practical significance
        let practical_significance = match effect_size.abs() {
            x if x < 0.2 => PracticalSignificance::Negligible,
            x if x < 0.5 => PracticalSignificance::Small,
            x if x < 0.8 => PracticalSignificance::Medium,
            x if x < 1.2 => PracticalSignificance::Large,
            _ => PracticalSignificance::VeryLarge,
        };

        Ok(BaselineComparison {
            baseline_name: "baseline".to_string(),
            performance_factor,
            significance,
            difference_ci: (0.0, 0.0), // Would calculate proper CI
            effect_size,
            practical_significance,
        })
    }
}

/// Statistical analyzer using `SciRS2` capabilities
pub struct StatisticalAnalyzer;

impl StatisticalAnalyzer {
    /// Create a new statistical analyzer
    #[must_use]
    pub const fn new() -> Self {
        Self
    }

    /// Calculate descriptive statistics
    pub fn calculate_descriptive_stats(&self, data: &[f64]) -> QuantRS2Result<DescriptiveStats> {
        if data.is_empty() {
            return Err(QuantRS2Error::InvalidInput("Empty data".to_string()));
        }

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

        let count = data.len();
        let mean = data.iter().sum::<f64>() / count as f64;
        let variance = data.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / count as f64;
        let std_dev = variance.sqrt();
        let min = sorted_data[0];
        let max = sorted_data[count - 1];

        let median = if count % 2 == 0 {
            f64::midpoint(sorted_data[count / 2 - 1], sorted_data[count / 2])
        } else {
            sorted_data[count / 2]
        };

        let q1 = self.percentile(&sorted_data, 0.25);
        let q3 = self.percentile(&sorted_data, 0.75);
        let iqr = q3 - q1;

        // Calculate skewness and kurtosis
        let skewness = self.calculate_skewness(data, mean, std_dev);
        let kurtosis = self.calculate_kurtosis(data, mean, std_dev);

        Ok(DescriptiveStats {
            count,
            mean,
            std_dev,
            variance,
            min,
            max,
            median,
            q1,
            q3,
            iqr,
            skewness,
            kurtosis,
            mode: None, // Would implement mode calculation
        })
    }

    /// Calculate percentile
    fn percentile(&self, sorted_data: &[f64], p: f64) -> f64 {
        let index = (p * (sorted_data.len() - 1) as f64).round() as usize;
        sorted_data[index.min(sorted_data.len() - 1)]
    }

    /// Calculate skewness
    fn calculate_skewness(&self, data: &[f64], mean: f64, std_dev: f64) -> f64 {
        let n = data.len() as f64;
        let skew_sum = data
            .iter()
            .map(|x| ((x - mean) / std_dev).powi(3))
            .sum::<f64>();
        skew_sum / n
    }

    /// Calculate kurtosis
    fn calculate_kurtosis(&self, data: &[f64], mean: f64, std_dev: f64) -> f64 {
        let n = data.len() as f64;
        let kurt_sum = data
            .iter()
            .map(|x| ((x - mean) / std_dev).powi(4))
            .sum::<f64>();
        kurt_sum / n - 3.0 // Excess kurtosis
    }

    /// Perform linear regression analysis
    pub fn perform_regression_analysis(&self, data: &[f64]) -> QuantRS2Result<RegressionAnalysis> {
        if data.len() < 3 {
            return Err(QuantRS2Error::InvalidInput(
                "Insufficient data for regression".to_string(),
            ));
        }

        let n = data.len() as f64;
        let x_values: Vec<f64> = (0..data.len()).map(|i| i as f64).collect();

        let x_mean = x_values.iter().sum::<f64>() / n;
        let y_mean = data.iter().sum::<f64>() / n;

        let numerator: f64 = x_values
            .iter()
            .zip(data.iter())
            .map(|(x, y)| (x - x_mean) * (y - y_mean))
            .sum();

        let denominator: f64 = x_values.iter().map(|x| (x - x_mean).powi(2)).sum();

        let slope = numerator / denominator;
        let intercept = slope.mul_add(-x_mean, y_mean);

        // Calculate R-squared
        let ss_tot: f64 = data.iter().map(|y| (y - y_mean).powi(2)).sum();
        let ss_res: f64 = x_values
            .iter()
            .zip(data.iter())
            .map(|(x, y)| {
                let predicted = slope * x + intercept;
                (y - predicted).powi(2)
            })
            .sum();

        let r_squared = 1.0 - (ss_res / ss_tot);

        // Calculate p-value for slope (simplified)
        let slope_p_value = if slope.abs() > 0.001 { 0.05 } else { 0.5 };
        let significant_trend = slope_p_value < 0.05;

        Ok(RegressionAnalysis {
            slope,
            intercept,
            r_squared,
            slope_p_value,
            significant_trend,
            degradation_per_run: slope,
        })
    }

    /// Fit probability distributions to data
    pub fn fit_distributions(&self, data: &[f64]) -> QuantRS2Result<DistributionFit> {
        let stats = self.calculate_descriptive_stats(data)?;

        // Fit normal distribution
        let normal_dist = Distribution::Normal {
            mean: stats.mean,
            std_dev: stats.std_dev,
        };

        // Simple goodness of fit (would use proper statistical tests in SciRS2)
        let goodness_of_fit = 0.8; // Placeholder
        let fit_p_value = 0.3; // Placeholder

        Ok(DistributionFit {
            best_distribution: normal_dist,
            goodness_of_fit,
            fit_p_value,
            alternative_fits: Vec::new(),
        })
    }

    /// Detect outliers using specified method
    pub fn detect_outliers(
        &self,
        data: &[f64],
        method: OutlierDetectionMethod,
    ) -> QuantRS2Result<OutlierAnalysis> {
        let outlier_indices = match method {
            OutlierDetectionMethod::IQR { multiplier } => {
                self.detect_outliers_iqr(data, multiplier)?
            }
            OutlierDetectionMethod::ZScore { threshold } => {
                self.detect_outliers_zscore(data, threshold)?
            }
            _ => Vec::new(), // Other methods would be implemented
        };

        let num_outliers = outlier_indices.len();

        // Calculate outlier impact
        let outlier_impact = if num_outliers > 0 {
            self.calculate_outlier_impact(data, &outlier_indices)?
        } else {
            OutlierImpact {
                mean_change: 0.0,
                std_dev_change: 0.0,
                median_change: 0.0,
                relative_impact: 0.0,
            }
        };

        Ok(OutlierAnalysis {
            num_outliers,
            outlier_indices,
            detection_method: method,
            threshold: 1.5, // Would depend on method
            outlier_impact,
        })
    }

    /// Detect outliers using IQR method
    fn detect_outliers_iqr(&self, data: &[f64], multiplier: f64) -> QuantRS2Result<Vec<usize>> {
        let stats = self.calculate_descriptive_stats(data)?;
        let lower_bound = multiplier.mul_add(-stats.iqr, stats.q1);
        let upper_bound = multiplier.mul_add(stats.iqr, stats.q3);

        Ok(data
            .iter()
            .enumerate()
            .filter_map(|(i, &value)| {
                if value < lower_bound || value > upper_bound {
                    Some(i)
                } else {
                    None
                }
            })
            .collect())
    }

    /// Detect outliers using Z-score method
    fn detect_outliers_zscore(&self, data: &[f64], threshold: f64) -> QuantRS2Result<Vec<usize>> {
        let stats = self.calculate_descriptive_stats(data)?;

        Ok(data
            .iter()
            .enumerate()
            .filter_map(|(i, &value)| {
                let z_score = (value - stats.mean) / stats.std_dev;
                if z_score.abs() > threshold {
                    Some(i)
                } else {
                    None
                }
            })
            .collect())
    }

    /// Calculate impact of outliers on statistics
    fn calculate_outlier_impact(
        &self,
        data: &[f64],
        outlier_indices: &[usize],
    ) -> QuantRS2Result<OutlierImpact> {
        let original_stats = self.calculate_descriptive_stats(data)?;

        // Create data without outliers
        let filtered_data: Vec<f64> = data
            .iter()
            .enumerate()
            .filter_map(|(i, &value)| {
                if outlier_indices.contains(&i) {
                    None
                } else {
                    Some(value)
                }
            })
            .collect();

        let filtered_stats = self.calculate_descriptive_stats(&filtered_data)?;

        let mean_change = (original_stats.mean - filtered_stats.mean).abs();
        let std_dev_change = (original_stats.std_dev - filtered_stats.std_dev).abs();
        let median_change = (original_stats.median - filtered_stats.median).abs();
        let relative_impact = mean_change / original_stats.mean * 100.0;

        Ok(OutlierImpact {
            mean_change,
            std_dev_change,
            median_change,
            relative_impact,
        })
    }

    /// Perform Mann-Whitney U test
    pub fn mann_whitney_test(
        &self,
        sample1: &[f64],
        sample2: &[f64],
        significance_level: f64,
    ) -> QuantRS2Result<HypothesisTestResult> {
        // Simplified implementation - would use SciRS2's statistical functions
        let test_statistic = 0.0; // Placeholder
        let p_value = 0.1; // Placeholder
        let critical_value = 1.96; // Placeholder
        let reject_null = p_value < significance_level;

        Ok(HypothesisTestResult {
            test_statistic,
            p_value,
            critical_value,
            reject_null,
            significance_level,
            effect_size: None,
            confidence_interval: None,
        })
    }
}

/// Trait for simulator execution (placeholder)
pub trait SimulatorExecutor {
    fn execute(&self, circuit: &dyn std::any::Any) -> QuantRS2Result<ExecutionResult>;
}

impl Default for StatisticalAnalyzer {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_descriptive_stats() {
        let analyzer = StatisticalAnalyzer::new();
        let data = vec![1.0, 2.0, 3.0, 4.0, 5.0];

        let stats = analyzer
            .calculate_descriptive_stats(&data)
            .expect("calculate_descriptive_stats should succeed");
        assert_eq!(stats.mean, 3.0);
        assert_eq!(stats.median, 3.0);
        assert_eq!(stats.min, 1.0);
        assert_eq!(stats.max, 5.0);
    }

    #[test]
    fn test_outlier_detection_iqr() {
        let analyzer = StatisticalAnalyzer::new();
        let data = vec![1.0, 2.0, 3.0, 4.0, 5.0, 100.0]; // 100.0 is an outlier

        let outliers = analyzer
            .detect_outliers_iqr(&data, 1.5)
            .expect("outlier detection should succeed");
        assert_eq!(outliers.len(), 1);
        assert_eq!(outliers[0], 5); // Index of 100.0
    }

    #[test]
    fn test_regression_analysis() {
        let analyzer = StatisticalAnalyzer::new();
        let data = vec![1.0, 2.0, 3.0, 4.0, 5.0]; // Perfect linear trend

        let regression = analyzer
            .perform_regression_analysis(&data)
            .expect("perform_regression_analysis should succeed");
        assert!((regression.slope - 1.0).abs() < 1e-10);
        assert!((regression.r_squared - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_benchmark_config() {
        let config = BenchmarkConfig::default();
        assert_eq!(config.num_runs, 100);
        assert_eq!(config.warmup_runs, 10);
        assert_eq!(config.significance_level, 0.05);
    }

    #[test]
    fn test_distribution_creation() {
        let normal = Distribution::Normal {
            mean: 0.0,
            std_dev: 1.0,
        };
        match normal {
            Distribution::Normal { mean, std_dev } => {
                assert_eq!(mean, 0.0);
                assert_eq!(std_dev, 1.0);
            }
            _ => panic!("Wrong distribution type"),
        }
    }
}