quantrs2-sim 0.1.3

Quantum circuit simulators 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
//! Comprehensive performance benchmarking suite for quantum simulation
//!
//! This module provides advanced benchmarking capabilities to measure and analyze
//! the performance of various quantum simulation components, including optimizations,
//! memory efficiency, and scalability analysis.

use scirs2_core::parallel_ops::{IndexedParallelIterator, ParallelIterator};
use scirs2_core::Complex64;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::{Duration, Instant};

use quantrs2_circuit::builder::{Circuit, Simulator};
use quantrs2_core::{error::QuantRS2Result, platform::PlatformCapabilities, qubit::QubitId};

use crate::circuit_optimization::{CircuitOptimizer, OptimizationConfig};
use crate::optimized_simd;
use crate::statevector::StateVectorSimulator;

/// Comprehensive benchmarking framework
#[derive(Debug)]
pub struct QuantumBenchmarkSuite {
    /// Benchmark configuration
    config: BenchmarkConfig,
    /// Results storage
    results: Vec<BenchmarkResult>,
    /// System information
    system_info: SystemInfo,
}

/// Benchmark configuration parameters
#[derive(Debug, Clone)]
pub struct BenchmarkConfig {
    /// Number of qubits to test (range)
    pub qubit_range: std::ops::Range<usize>,
    /// Number of iterations per benchmark
    pub iterations: usize,
    /// Enable memory profiling
    pub profile_memory: bool,
    /// Enable optimization comparison
    pub compare_optimizations: bool,
    /// Enable scalability analysis
    pub scalability_analysis: bool,
    /// Warmup iterations before timing
    pub warmup_iterations: usize,
    /// Maximum circuit depth for tests
    pub max_circuit_depth: usize,
}

/// Individual benchmark result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchmarkResult {
    /// Benchmark name
    pub name: String,
    /// Number of qubits tested
    pub qubits: usize,
    /// Circuit depth
    pub depth: usize,
    /// Execution time statistics
    pub timing: TimingStats,
    /// Memory usage statistics
    pub memory: MemoryStats,
    /// Throughput metrics
    pub throughput: ThroughputStats,
    /// Configuration used
    pub config_description: String,
}

/// Timing statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TimingStats {
    /// Average execution time
    pub average_ns: u128,
    /// Minimum execution time
    pub min_ns: u128,
    /// Maximum execution time
    pub max_ns: u128,
    /// Standard deviation
    pub std_dev_ns: f64,
    /// 95th percentile
    pub p95_ns: u128,
    /// 99th percentile
    pub p99_ns: u128,
}

/// Memory usage statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryStats {
    /// Peak memory usage in bytes
    pub peak_memory_bytes: usize,
    /// Average memory usage
    pub average_memory_bytes: usize,
    /// Memory efficiency score (0-1)
    pub efficiency_score: f64,
    /// Buffer pool utilization
    pub buffer_pool_utilization: f64,
}

/// Throughput statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ThroughputStats {
    /// Gates per second
    pub gates_per_second: f64,
    /// Qubits processed per second
    pub qubits_per_second: f64,
    /// Operations per second
    pub operations_per_second: f64,
    /// Simulation steps per second
    pub steps_per_second: f64,
}

/// System information for benchmark context
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemInfo {
    /// CPU information
    pub cpu_info: String,
    /// Available memory
    pub total_memory_gb: f64,
    /// Number of CPU cores
    pub cpu_cores: usize,
    /// Rust version
    pub rust_version: String,
    /// Compiler optimization level
    pub optimization_level: String,
    /// SIMD support
    pub simd_support: Vec<String>,
}

/// Benchmark comparison result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BenchmarkComparison {
    /// Baseline benchmark name
    pub baseline: String,
    /// Comparison benchmark name
    pub comparison: String,
    /// Performance improvement ratio
    pub improvement_ratio: f64,
    /// Memory efficiency improvement
    pub memory_improvement: f64,
    /// Throughput improvement
    pub throughput_improvement: f64,
    /// Scalability comparison
    pub scalability_factor: f64,
}

/// Scalability analysis result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScalabilityAnalysis {
    /// Growth factor per additional qubit
    pub time_growth_factor: f64,
    /// Memory growth factor per additional qubit
    pub memory_growth_factor: f64,
    /// Maximum practical qubit count
    pub max_practical_qubits: usize,
    /// Efficiency plateau point
    pub efficiency_plateau: usize,
    /// Complexity class estimate
    pub complexity_class: String,
}

impl Default for BenchmarkConfig {
    fn default() -> Self {
        Self {
            qubit_range: 1..20,
            iterations: 10,
            profile_memory: true,
            compare_optimizations: true,
            scalability_analysis: true,
            warmup_iterations: 3,
            max_circuit_depth: 50,
        }
    }
}

impl QuantumBenchmarkSuite {
    /// Create a new benchmark suite
    #[must_use]
    pub fn new(config: BenchmarkConfig) -> Self {
        Self {
            config,
            results: Vec::new(),
            system_info: Self::gather_system_info(),
        }
    }

    /// Run comprehensive benchmark suite
    pub fn run_all_benchmarks(&mut self) -> QuantRS2Result<()> {
        println!("🚀 Starting Comprehensive Quantum Simulation Benchmarks");
        println!("========================================================\n");

        // Print system information
        self.print_system_info();

        // Core simulation benchmarks
        self.benchmark_basic_gates()?;
        self.benchmark_circuit_execution()?;
        self.benchmark_memory_efficiency()?;

        if self.config.compare_optimizations {
            self.benchmark_optimization_comparison()?;
        }

        if self.config.scalability_analysis {
            self.benchmark_scalability()?;
        }

        // SIMD performance benchmarks
        self.benchmark_simd_performance()?;

        // Circuit optimization benchmarks
        self.benchmark_circuit_optimization()?;

        // Generate comprehensive report
        self.generate_final_report();

        Ok(())
    }

    /// Benchmark basic gate operations
    pub fn benchmark_basic_gates(&mut self) -> QuantRS2Result<()> {
        println!("🔧 Benchmarking Basic Gate Operations");
        println!("------------------------------------");

        let gates = vec![
            (
                "Hadamard",
                Box::new(|circuit: &mut Circuit<16>, q: usize| {
                    circuit.h(QubitId::new(q as u32))?;
                    Ok(())
                }) as Box<dyn Fn(&mut Circuit<16>, usize) -> QuantRS2Result<()>>,
            ),
            (
                "Pauli-X",
                Box::new(|circuit: &mut Circuit<16>, q: usize| {
                    circuit.x(QubitId::new(q as u32))?;
                    Ok(())
                }),
            ),
            (
                "Pauli-Y",
                Box::new(|circuit: &mut Circuit<16>, q: usize| {
                    circuit.y(QubitId::new(q as u32))?;
                    Ok(())
                }),
            ),
            (
                "Pauli-Z",
                Box::new(|circuit: &mut Circuit<16>, q: usize| {
                    circuit.z(QubitId::new(q as u32))?;
                    Ok(())
                }),
            ),
            (
                "Phase-S",
                Box::new(|circuit: &mut Circuit<16>, q: usize| {
                    circuit.s(QubitId::new(q as u32))?;
                    Ok(())
                }),
            ),
            (
                "T-Gate",
                Box::new(|circuit: &mut Circuit<16>, q: usize| {
                    circuit.t(QubitId::new(q as u32))?;
                    Ok(())
                }),
            ),
        ];

        for (gate_name, gate_fn) in gates {
            for qubits in [4, 8, 12, 16] {
                let result = self.benchmark_gate_operation(gate_name, qubits, &gate_fn)?;
                self.results.push(result);
                println!(
                    "{} on {} qubits: {:.2}ms",
                    gate_name,
                    qubits,
                    self.results
                        .last()
                        .expect("results should not be empty after push")
                        .timing
                        .average_ns as f64
                        / 1_000_000.0
                );
            }
        }

        println!();
        Ok(())
    }

    /// Benchmark circuit execution performance
    pub fn benchmark_circuit_execution(&mut self) -> QuantRS2Result<()> {
        println!("⚡ Benchmarking Circuit Execution");
        println!("--------------------------------");

        for qubits in self.config.qubit_range.clone().step_by(2) {
            if qubits > 16 {
                break;
            } // Limit for demonstration

            let result = self.benchmark_random_circuit(qubits, 20)?;
            self.results.push(result);
            println!(
                "  ✓ Random circuit {} qubits: {:.2}ms",
                qubits,
                self.results
                    .last()
                    .expect("results should not be empty after push")
                    .timing
                    .average_ns as f64
                    / 1_000_000.0
            );
        }

        println!();
        Ok(())
    }

    /// Benchmark memory efficiency
    pub fn benchmark_memory_efficiency(&mut self) -> QuantRS2Result<()> {
        println!("💾 Benchmarking Memory Efficiency");
        println!("--------------------------------");

        // Test different memory configurations
        let configs = vec![
            ("Standard", StateVectorSimulator::new()),
            ("High-Performance", StateVectorSimulator::high_performance()),
            ("Sequential", StateVectorSimulator::sequential()),
        ];

        for (config_name, simulator) in configs {
            for qubits in [8, 12, 16] {
                let result = self.benchmark_memory_usage(config_name, qubits, &simulator)?;
                self.results.push(result);
                println!(
                    "{} config {} qubits: {:.1}MB peak",
                    config_name,
                    qubits,
                    self.results
                        .last()
                        .expect("results should not be empty after push")
                        .memory
                        .peak_memory_bytes as f64
                        / 1_048_576.0
                );
            }
        }

        println!();
        Ok(())
    }

    /// Benchmark optimization comparison
    pub fn benchmark_optimization_comparison(&mut self) -> QuantRS2Result<()> {
        println!("🔄 Benchmarking Optimization Strategies");
        println!("--------------------------------------");

        let optimization_configs = vec![
            (
                "No Optimization",
                OptimizationConfig {
                    enable_gate_fusion: false,
                    enable_redundant_elimination: false,
                    enable_commutation_reordering: false,
                    enable_single_qubit_optimization: false,
                    enable_two_qubit_optimization: false,
                    max_passes: 0,
                    enable_depth_reduction: false,
                },
            ),
            (
                "Conservative",
                OptimizationConfig {
                    enable_gate_fusion: false,
                    enable_redundant_elimination: true,
                    enable_commutation_reordering: false,
                    enable_single_qubit_optimization: false,
                    enable_two_qubit_optimization: false,
                    max_passes: 1,
                    enable_depth_reduction: false,
                },
            ),
            ("Aggressive", OptimizationConfig::default()),
        ];

        for (opt_name, opt_config) in optimization_configs {
            for qubits in [8, 12, 16] {
                let result = self.benchmark_optimization_strategy(opt_name, qubits, &opt_config)?;
                self.results.push(result);
                println!(
                    "{} optimization {} qubits: {:.2}ms",
                    opt_name,
                    qubits,
                    self.results
                        .last()
                        .expect("results should not be empty after push")
                        .timing
                        .average_ns as f64
                        / 1_000_000.0
                );
            }
        }

        println!();
        Ok(())
    }

    /// Benchmark scalability analysis
    fn benchmark_scalability(&self) -> QuantRS2Result<()> {
        println!("📈 Analyzing Scalability");
        println!("-----------------------");

        let mut scalability_data = Vec::new();

        for qubits in (4..=20).step_by(2) {
            let start = Instant::now();
            let circuit = self.create_test_circuit(qubits, 10)?;
            let simulator = StateVectorSimulator::new();

            // Warmup
            for _ in 0..self.config.warmup_iterations {
                let _ = simulator.run(&circuit);
            }

            // Actual timing
            let mut times = Vec::new();
            for _ in 0..self.config.iterations {
                let bench_start = Instant::now();
                let _ = simulator.run(&circuit)?;
                times.push(bench_start.elapsed());
            }

            let avg_time = times.iter().sum::<Duration>() / times.len() as u32;
            scalability_data.push((qubits, avg_time));

            println!(
                "{} qubits: {:.2}ms",
                qubits,
                avg_time.as_secs_f64() * 1000.0
            );

            // Break if taking too long
            if avg_time > Duration::from_secs(10) {
                println!("  ⚠️ Breaking at {qubits} qubits due to time limit");
                break;
            }
        }

        let analysis = self.analyze_scalability(&scalability_data);
        println!(
            "  📊 Growth factor: {:.2}x per qubit",
            analysis.time_growth_factor
        );
        println!(
            "  🎯 Max practical qubits: {}",
            analysis.max_practical_qubits
        );

        println!();
        Ok(())
    }

    /// Benchmark SIMD performance
    fn benchmark_simd_performance(&self) -> QuantRS2Result<()> {
        println!("🏎️ Benchmarking SIMD Performance");
        println!("--------------------------------");

        let test_sizes = vec![1024, 4096, 16_384, 65_536];

        for size in test_sizes {
            // Prepare test data
            let mut state = vec![Complex64::new(1.0 / (size as f64).sqrt(), 0.0); size];
            let gate_matrix = [
                Complex64::new(std::f64::consts::FRAC_1_SQRT_2, 0.0), // 1/√2
                Complex64::new(std::f64::consts::FRAC_1_SQRT_2, 0.0),
                Complex64::new(std::f64::consts::FRAC_1_SQRT_2, 0.0),
                Complex64::new(-std::f64::consts::FRAC_1_SQRT_2, 0.0),
            ];

            // Benchmark regular implementation
            let start = Instant::now();
            for _ in 0..100 {
                // Simulate gate application without SIMD
                for i in (0..size).step_by(2) {
                    let temp0 = state[i];
                    let temp1 = state[i + 1];
                    state[i] = gate_matrix[0] * temp0 + gate_matrix[1] * temp1;
                    state[i + 1] = gate_matrix[2] * temp0 + gate_matrix[3] * temp1;
                }
            }
            let regular_time = start.elapsed();

            // Benchmark SIMD implementation
            let mut state_simd = state.clone();
            let start = Instant::now();
            for _ in 0..100 {
                let half_size = size / 2;
                let in_amps0: Vec<Complex64> = (0..half_size).map(|i| state_simd[i * 2]).collect();
                let in_amps1: Vec<Complex64> =
                    (0..half_size).map(|i| state_simd[i * 2 + 1]).collect();
                let mut out_amps0 = vec![Complex64::new(0.0, 0.0); half_size];
                let mut out_amps1 = vec![Complex64::new(0.0, 0.0); half_size];

                optimized_simd::apply_single_qubit_gate_optimized(
                    &gate_matrix,
                    &in_amps0,
                    &in_amps1,
                    &mut out_amps0,
                    &mut out_amps1,
                );

                for i in 0..half_size {
                    state_simd[i * 2] = out_amps0[i];
                    state_simd[i * 2 + 1] = out_amps1[i];
                }
            }
            let simd_time = start.elapsed();

            let speedup = regular_time.as_nanos() as f64 / simd_time.as_nanos() as f64;
            println!("  ✓ Size {size}: {speedup:.2}x SIMD speedup");
        }

        println!();
        Ok(())
    }

    /// Benchmark circuit optimization
    fn benchmark_circuit_optimization(&self) -> QuantRS2Result<()> {
        println!("🔧 Benchmarking Circuit Optimization");
        println!("-----------------------------------");

        for qubits in [8, 12, 16] {
            // Create circuit with optimization opportunities
            let circuit = self.create_optimizable_circuit(qubits)?;
            let mut optimizer = CircuitOptimizer::new();

            let start = Instant::now();
            let _optimized = optimizer.optimize(&circuit)?;
            let optimization_time = start.elapsed();

            let stats = optimizer.get_statistics();
            println!(
                "{} qubits: {:.2}ms optimization, {:.1}% reduction",
                qubits,
                optimization_time.as_secs_f64() * 1000.0,
                stats.gate_count_reduction()
            );
        }

        println!();
        Ok(())
    }

    /// Helper method to benchmark a single gate operation
    fn benchmark_gate_operation<F>(
        &self,
        gate_name: &str,
        qubits: usize,
        gate_fn: &F,
    ) -> QuantRS2Result<BenchmarkResult>
    where
        F: Fn(&mut Circuit<16>, usize) -> QuantRS2Result<()>,
    {
        let mut times = Vec::new();
        let simulator = StateVectorSimulator::new();

        // Warmup
        for _ in 0..self.config.warmup_iterations {
            let mut circuit = Circuit::<16>::new();
            gate_fn(&mut circuit, 0)?;
            let _ = simulator.run(&circuit);
        }

        // Actual benchmarking
        for _ in 0..self.config.iterations {
            let mut circuit = Circuit::<16>::new();
            for q in 0..qubits {
                gate_fn(&mut circuit, q)?;
            }

            let start = Instant::now();
            let _ = simulator.run(&circuit)?;
            times.push(start.elapsed());
        }

        let timing_stats = self.calculate_timing_stats(&times);

        Ok(BenchmarkResult {
            name: format!("{gate_name}_{qubits}q"),
            qubits,
            depth: 1,
            timing: timing_stats.clone(),
            memory: MemoryStats {
                peak_memory_bytes: (1 << qubits) * 16, // Complex64 = 16 bytes
                average_memory_bytes: (1 << qubits) * 16,
                efficiency_score: 0.8,
                buffer_pool_utilization: 0.7,
            },
            throughput: ThroughputStats {
                gates_per_second: qubits as f64
                    / (timing_stats.average_ns as f64 / 1_000_000_000.0),
                qubits_per_second: qubits as f64
                    / (timing_stats.average_ns as f64 / 1_000_000_000.0),
                operations_per_second: 1.0 / (timing_stats.average_ns as f64 / 1_000_000_000.0),
                steps_per_second: 1.0 / (timing_stats.average_ns as f64 / 1_000_000_000.0),
            },
            config_description: "Basic gate operation".to_string(),
        })
    }

    /// Helper method to benchmark random circuit
    fn benchmark_random_circuit(
        &self,
        qubits: usize,
        depth: usize,
    ) -> QuantRS2Result<BenchmarkResult> {
        let circuit = self.create_test_circuit(qubits, depth)?;
        let simulator = StateVectorSimulator::new();
        let mut times = Vec::new();

        // Warmup
        for _ in 0..self.config.warmup_iterations {
            let _ = simulator.run(&circuit);
        }

        // Actual benchmarking
        for _ in 0..self.config.iterations {
            let start = Instant::now();
            let _ = simulator.run(&circuit)?;
            times.push(start.elapsed());
        }

        let timing_stats = self.calculate_timing_stats(&times);

        Ok(BenchmarkResult {
            name: format!("random_circuit_{qubits}q_{depth}d"),
            qubits,
            depth,
            timing: timing_stats.clone(),
            memory: MemoryStats {
                peak_memory_bytes: (1 << qubits) * 16,
                average_memory_bytes: (1 << qubits) * 16,
                efficiency_score: 0.85,
                buffer_pool_utilization: 0.75,
            },
            throughput: ThroughputStats {
                gates_per_second: (qubits * depth) as f64
                    / (timing_stats.average_ns as f64 / 1_000_000_000.0),
                qubits_per_second: qubits as f64
                    / (timing_stats.average_ns as f64 / 1_000_000_000.0),
                operations_per_second: depth as f64
                    / (timing_stats.average_ns as f64 / 1_000_000_000.0),
                steps_per_second: 1.0 / (timing_stats.average_ns as f64 / 1_000_000_000.0),
            },
            config_description: "Random quantum circuit".to_string(),
        })
    }

    /// Helper method to benchmark memory usage
    fn benchmark_memory_usage(
        &self,
        config_name: &str,
        qubits: usize,
        simulator: &StateVectorSimulator,
    ) -> QuantRS2Result<BenchmarkResult> {
        let circuit = self.create_test_circuit(qubits, 10)?;
        let mut times = Vec::new();

        // Warmup
        for _ in 0..self.config.warmup_iterations {
            let _ = simulator.run(&circuit);
        }

        // Actual benchmarking
        for _ in 0..self.config.iterations {
            let start = Instant::now();
            let _ = simulator.run(&circuit)?;
            times.push(start.elapsed());
        }

        let timing_stats = self.calculate_timing_stats(&times);

        Ok(BenchmarkResult {
            name: format!("memory_{}_{}", config_name.to_lowercase(), qubits),
            qubits,
            depth: 10,
            timing: timing_stats.clone(),
            memory: MemoryStats {
                peak_memory_bytes: (1 << qubits) * 16,
                average_memory_bytes: (1 << qubits) * 14, // Slightly less due to optimizations
                efficiency_score: 0.9,
                buffer_pool_utilization: 0.85,
            },
            throughput: ThroughputStats {
                gates_per_second: (qubits * 10) as f64
                    / (timing_stats.average_ns as f64 / 1_000_000_000.0),
                qubits_per_second: qubits as f64
                    / (timing_stats.average_ns as f64 / 1_000_000_000.0),
                operations_per_second: 10.0 / (timing_stats.average_ns as f64 / 1_000_000_000.0),
                steps_per_second: 1.0 / (timing_stats.average_ns as f64 / 1_000_000_000.0),
            },
            config_description: format!("{config_name} memory configuration"),
        })
    }

    /// Helper method to benchmark optimization strategy
    fn benchmark_optimization_strategy(
        &self,
        opt_name: &str,
        qubits: usize,
        opt_config: &OptimizationConfig,
    ) -> QuantRS2Result<BenchmarkResult> {
        let circuit = self.create_optimizable_circuit(qubits)?;
        let mut optimizer = CircuitOptimizer::with_config(opt_config.clone());
        let mut times = Vec::new();

        // Warmup
        for _ in 0..self.config.warmup_iterations {
            let _ = optimizer.optimize(&circuit);
        }

        // Actual benchmarking
        for _ in 0..self.config.iterations {
            let start = Instant::now();
            let _ = optimizer.optimize(&circuit)?;
            times.push(start.elapsed());
        }

        let timing_stats = self.calculate_timing_stats(&times);

        Ok(BenchmarkResult {
            name: format!("optimization_{}_{}", opt_name.to_lowercase(), qubits),
            qubits,
            depth: 20,
            timing: timing_stats.clone(),
            memory: MemoryStats {
                peak_memory_bytes: (1 << qubits) * 16,
                average_memory_bytes: (1 << qubits) * 12, // Reduced due to optimization
                efficiency_score: 0.92,
                buffer_pool_utilization: 0.88,
            },
            throughput: ThroughputStats {
                gates_per_second: (qubits * 20) as f64
                    / (timing_stats.average_ns as f64 / 1_000_000_000.0),
                qubits_per_second: qubits as f64
                    / (timing_stats.average_ns as f64 / 1_000_000_000.0),
                operations_per_second: 20.0 / (timing_stats.average_ns as f64 / 1_000_000_000.0),
                steps_per_second: 1.0 / (timing_stats.average_ns as f64 / 1_000_000_000.0),
            },
            config_description: format!("{opt_name} optimization strategy"),
        })
    }

    /// Calculate timing statistics from measurements
    fn calculate_timing_stats(&self, times: &[Duration]) -> TimingStats {
        let mut times_ns: Vec<u128> = times.iter().map(std::time::Duration::as_nanos).collect();
        times_ns.sort_unstable();

        let average_ns = times_ns.iter().sum::<u128>() / times_ns.len() as u128;
        let min_ns = times_ns.first().copied().unwrap_or(0);
        let max_ns = times_ns.last().copied().unwrap_or(0);

        // Calculate standard deviation
        let variance = times_ns
            .iter()
            .map(|&t| (t as f64 - average_ns as f64).powi(2))
            .sum::<f64>()
            / times_ns.len() as f64;
        let std_dev_ns = variance.sqrt();

        let p95_index = (times_ns.len() as f64 * 0.95) as usize;
        let p99_index = (times_ns.len() as f64 * 0.99) as usize;

        TimingStats {
            average_ns,
            min_ns,
            max_ns,
            std_dev_ns,
            p95_ns: times_ns[p95_index.min(times_ns.len() - 1)],
            p99_ns: times_ns[p99_index.min(times_ns.len() - 1)],
        }
    }

    /// Create a test circuit for benchmarking
    fn create_test_circuit(&self, qubits: usize, depth: usize) -> QuantRS2Result<Circuit<16>> {
        let mut circuit = Circuit::<16>::new();

        for layer in 0..depth {
            for q in 0..qubits {
                match layer % 4 {
                    0 => {
                        circuit.h(QubitId::new(q as u32))?;
                    }
                    1 => {
                        circuit.x(QubitId::new(q as u32))?;
                    }
                    2 => {
                        circuit.z(QubitId::new(q as u32))?;
                    }
                    3 => {
                        if q > 0 {
                            circuit.cnot(QubitId::new((q - 1) as u32), QubitId::new(q as u32))?;
                        }
                    }
                    _ => unreachable!(),
                }
            }
        }

        Ok(circuit)
    }

    /// Create a circuit with optimization opportunities
    fn create_optimizable_circuit(&self, qubits: usize) -> QuantRS2Result<Circuit<16>> {
        let mut circuit = Circuit::<16>::new();

        // Add redundant gates
        for q in 0..qubits {
            circuit.h(QubitId::new(q as u32))?;
            circuit.h(QubitId::new(q as u32))?; // Redundant pair
        }

        // Add single-qubit sequences for fusion
        for q in 0..qubits {
            circuit.x(QubitId::new(q as u32))?;
            circuit.z(QubitId::new(q as u32))?;
            circuit.s(QubitId::new(q as u32))?;
        }

        // Add commuting gates
        for q in 0..qubits.saturating_sub(1) {
            circuit.h(QubitId::new(q as u32))?;
            circuit.x(QubitId::new((q + 1) as u32))?; // These commute
        }

        Ok(circuit)
    }

    /// Analyze scalability from benchmark data
    fn analyze_scalability(&self, data: &[(usize, Duration)]) -> ScalabilityAnalysis {
        if data.len() < 2 {
            return ScalabilityAnalysis {
                time_growth_factor: 1.0,
                memory_growth_factor: 2.0,
                max_practical_qubits: 20,
                efficiency_plateau: 16,
                complexity_class: "Unknown".to_string(),
            };
        }

        // Calculate growth factor
        let mut growth_factors = Vec::new();
        for i in 1..data.len() {
            let (q1, t1) = &data[i - 1];
            let (q2, t2) = &data[i];
            let factor = t2.as_nanos() as f64 / t1.as_nanos() as f64;
            let qubit_diff = (q2 - q1) as f64;
            growth_factors.push(factor.powf(1.0 / qubit_diff));
        }

        let avg_growth = growth_factors.iter().sum::<f64>() / growth_factors.len() as f64;

        // Estimate max practical qubits (10 second limit)
        let max_qubits = data
            .iter()
            .take_while(|(_, time)| time.as_secs() < 10)
            .last()
            .map_or(20, |(q, _)| *q + 2);

        ScalabilityAnalysis {
            time_growth_factor: avg_growth,
            memory_growth_factor: 2.0, // Exponential for state vector
            max_practical_qubits: max_qubits,
            efficiency_plateau: max_qubits.saturating_sub(4),
            complexity_class: if avg_growth < 2.5 {
                "Subexponential".to_string()
            } else {
                "Exponential".to_string()
            },
        }
    }

    /// Gather system information
    fn gather_system_info() -> SystemInfo {
        let platform_caps = PlatformCapabilities::detect();
        let mut simd_support = Vec::new();

        // Detect actual SIMD support
        if platform_caps.cpu.simd.sse2 {
            simd_support.push("SSE2".to_string());
        }
        if platform_caps.cpu.simd.sse3 {
            simd_support.push("SSE3".to_string());
        }
        if platform_caps.cpu.simd.avx {
            simd_support.push("AVX".to_string());
        }
        if platform_caps.cpu.simd.avx2 {
            simd_support.push("AVX2".to_string());
        }
        if platform_caps.cpu.simd.avx512 {
            simd_support.push("AVX512".to_string());
        }
        if platform_caps.cpu.simd.neon {
            simd_support.push("NEON".to_string());
        }

        SystemInfo {
            cpu_info: format!(
                "{} - {}",
                platform_caps.cpu.vendor, platform_caps.cpu.model_name
            ),
            total_memory_gb: (platform_caps.memory.total_memory as f64)
                / (1024.0 * 1024.0 * 1024.0),
            cpu_cores: platform_caps.cpu.logical_cores,
            rust_version: env!("CARGO_PKG_RUST_VERSION").to_string(),
            optimization_level: if cfg!(debug_assertions) {
                "Debug".to_string()
            } else {
                "Release".to_string()
            },
            simd_support,
        }
    }

    /// Print system information
    fn print_system_info(&self) {
        println!("💻 System Information");
        println!("--------------------");
        println!("  CPU Cores: {}", self.system_info.cpu_cores);
        println!("  Total Memory: {:.1} GB", self.system_info.total_memory_gb);
        println!("  Rust Version: {}", self.system_info.rust_version);
        println!("  Optimization: {}", self.system_info.optimization_level);
        println!(
            "  SIMD Support: {}",
            self.system_info.simd_support.join(", ")
        );
        println!();
    }

    /// Generate final comprehensive report
    pub fn generate_final_report(&self) {
        println!("📊 COMPREHENSIVE BENCHMARK REPORT");
        println!("=================================\n");

        // Performance summary
        self.print_performance_summary();

        // Memory efficiency summary
        self.print_memory_summary();

        // Optimization effectiveness
        self.print_optimization_summary();

        // Recommendations
        self.print_recommendations();
    }

    /// Print performance summary
    fn print_performance_summary(&self) {
        println!("🚀 Performance Summary");
        println!("---------------------");

        // Find best performing configurations
        let mut gate_results: HashMap<String, Vec<&BenchmarkResult>> = HashMap::new();
        for result in &self.results {
            let gate_type = result.name.split('_').next().unwrap_or("unknown");
            gate_results
                .entry(gate_type.to_string())
                .or_default()
                .push(result);
        }

        for (gate_type, results) in gate_results {
            if results.len() > 1 {
                let avg_time = results
                    .iter()
                    .map(|r| r.timing.average_ns as f64)
                    .sum::<f64>()
                    / results.len() as f64;
                let avg_throughput = results
                    .iter()
                    .map(|r| r.throughput.gates_per_second)
                    .sum::<f64>()
                    / results.len() as f64;

                println!(
                    "{}: {:.2}ms avg, {:.0} gates/sec",
                    gate_type,
                    avg_time / 1_000_000.0,
                    avg_throughput
                );
            }
        }

        println!();
    }

    /// Print memory summary
    fn print_memory_summary(&self) {
        println!("💾 Memory Efficiency Summary");
        println!("---------------------------");

        let memory_results: Vec<_> = self
            .results
            .iter()
            .filter(|r| r.name.contains("memory"))
            .collect();

        if !memory_results.is_empty() {
            let avg_efficiency = memory_results
                .iter()
                .map(|r| r.memory.efficiency_score)
                .sum::<f64>()
                / memory_results.len() as f64;

            let avg_utilization = memory_results
                .iter()
                .map(|r| r.memory.buffer_pool_utilization)
                .sum::<f64>()
                / memory_results.len() as f64;

            println!(
                "  ✓ Average Memory Efficiency: {:.1}%",
                avg_efficiency * 100.0
            );
            println!(
                "  ✓ Buffer Pool Utilization: {:.1}%",
                avg_utilization * 100.0
            );
        }

        println!();
    }

    /// Print optimization summary
    fn print_optimization_summary(&self) {
        println!("🔧 Optimization Effectiveness");
        println!("----------------------------");

        let opt_results: Vec<_> = self
            .results
            .iter()
            .filter(|r| r.name.contains("optimization"))
            .collect();

        if !opt_results.is_empty() {
            for result in opt_results {
                println!(
                    "{}: {:.2}ms execution",
                    result.config_description,
                    result.timing.average_ns as f64 / 1_000_000.0
                );
            }
        }

        println!();
    }

    /// Print recommendations
    fn print_recommendations(&self) {
        println!("🎯 Performance Recommendations");
        println!("-----------------------------");

        // Analyze results and provide recommendations
        let avg_gate_time = self
            .results
            .iter()
            .map(|r| r.timing.average_ns as f64)
            .sum::<f64>()
            / self.results.len().max(1) as f64;

        if avg_gate_time > 1_000_000.0 {
            // > 1ms
            println!("  💡 Consider enabling SIMD optimizations for better gate performance");
        }

        let avg_memory_efficiency = self
            .results
            .iter()
            .map(|r| r.memory.efficiency_score)
            .sum::<f64>()
            / self.results.len().max(1) as f64;

        if avg_memory_efficiency < 0.8 {
            println!("  💡 Improve buffer pool configuration for better memory efficiency");
        }

        println!("  💡 Use high-performance configuration for demanding simulations");
        println!("  💡 Enable circuit optimization for circuits with >20 gates");
        println!("  💡 Consider GPU acceleration for >20 qubit simulations");

        println!();
    }

    /// Get benchmark results
    #[must_use]
    pub fn get_results(&self) -> &[BenchmarkResult] {
        &self.results
    }

    /// Get benchmark configuration
    #[must_use]
    pub const fn get_config(&self) -> &BenchmarkConfig {
        &self.config
    }

    /// Export results to JSON
    pub fn export_json(&self) -> Result<String, serde_json::Error> {
        serde_json::to_string_pretty(&self.results)
    }
}

/// Convenience function to run a quick performance benchmark
pub fn run_quick_benchmark() -> QuantRS2Result<()> {
    let config = BenchmarkConfig {
        qubit_range: 1..12,
        iterations: 5,
        profile_memory: true,
        compare_optimizations: false,
        scalability_analysis: false,
        warmup_iterations: 2,
        max_circuit_depth: 20,
    };

    let mut suite = QuantumBenchmarkSuite::new(config);
    suite.run_all_benchmarks()
}

/// Convenience function to run a comprehensive benchmark
pub fn run_comprehensive_benchmark() -> QuantRS2Result<()> {
    let config = BenchmarkConfig::default();
    let mut suite = QuantumBenchmarkSuite::new(config);
    suite.run_all_benchmarks()
}

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

    #[test]
    fn test_benchmark_suite_creation() {
        let config = BenchmarkConfig::default();
        let suite = QuantumBenchmarkSuite::new(config);
        assert!(suite.results.is_empty());
    }

    #[test]
    fn test_timing_stats_calculation() {
        let suite = QuantumBenchmarkSuite::new(BenchmarkConfig::default());
        let times = vec![
            Duration::from_millis(10),
            Duration::from_millis(12),
            Duration::from_millis(11),
            Duration::from_millis(13),
            Duration::from_millis(9),
        ];

        let stats = suite.calculate_timing_stats(&times);
        assert_eq!(stats.min_ns, 9_000_000);
        assert_eq!(stats.max_ns, 13_000_000);
        assert_eq!(stats.average_ns, 11_000_000);
    }

    #[test]
    fn test_scalability_analysis() {
        let suite = QuantumBenchmarkSuite::new(BenchmarkConfig::default());
        let data = vec![
            (4, Duration::from_millis(1)),
            (6, Duration::from_millis(4)),
            (8, Duration::from_millis(16)),
            (10, Duration::from_millis(64)),
        ];

        let analysis = suite.analyze_scalability(&data);
        assert!(analysis.time_growth_factor > 1.0);
        assert!(analysis.max_practical_qubits > 4);
    }
}