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
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
//! GPU Kernel Optimization for Specialized Quantum Operations
//!
//! This module provides highly optimized GPU kernels for quantum simulation,
//! including specialized implementations for common gates, fused operations,
//! and memory-optimized algorithms for large state vectors.
//!
//! # Features
//! - Specialized kernels for common gates (H, X, Y, Z, CNOT, CZ, etc.)
//! - Fused gate sequences for reduced memory bandwidth
//! - Memory-coalesced access patterns for GPU efficiency
//! - Warp-level optimizations for NVIDIA GPUs
//! - Shared memory utilization for reduced global memory access
//! - Streaming execution for overlapped computation and data transfer

use quantrs2_core::error::{QuantRS2Error, QuantRS2Result};
use scirs2_core::ndarray::{Array1, Array2};
use scirs2_core::Complex64;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::{Arc, Mutex, RwLock};
use std::time::{Duration, Instant};

/// GPU kernel optimization framework for quantum simulation
#[derive(Debug)]
pub struct GPUKernelOptimizer {
    /// Kernel registry for specialized operations
    kernel_registry: KernelRegistry,
    /// Kernel execution statistics
    stats: Arc<Mutex<KernelStats>>,
    /// Configuration
    config: GPUKernelConfig,
    /// Kernel cache for compiled kernels
    kernel_cache: Arc<RwLock<HashMap<String, CompiledKernel>>>,
    /// Memory layout optimizer
    memory_optimizer: MemoryLayoutOptimizer,
}

/// Configuration for GPU kernel optimization
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GPUKernelConfig {
    /// Enable warp-level optimizations
    pub enable_warp_optimization: bool,
    /// Enable shared memory usage
    pub enable_shared_memory: bool,
    /// Block size for GPU execution
    pub block_size: usize,
    /// Grid size calculation method
    pub grid_size_method: GridSizeMethod,
    /// Enable kernel fusion
    pub enable_kernel_fusion: bool,
    /// Maximum fused kernel length
    pub max_fusion_length: usize,
    /// Enable memory coalescing optimization
    pub enable_memory_coalescing: bool,
    /// Enable streaming execution
    pub enable_streaming: bool,
    /// Number of streams for concurrent execution
    pub num_streams: usize,
    /// Occupancy optimization target
    pub target_occupancy: f64,
}

impl Default for GPUKernelConfig {
    fn default() -> Self {
        Self {
            enable_warp_optimization: true,
            enable_shared_memory: true,
            block_size: 256,
            grid_size_method: GridSizeMethod::Automatic,
            enable_kernel_fusion: true,
            max_fusion_length: 8,
            enable_memory_coalescing: true,
            enable_streaming: true,
            num_streams: 4,
            target_occupancy: 0.75,
        }
    }
}

/// Method for calculating grid size
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum GridSizeMethod {
    /// Automatic calculation based on problem size
    Automatic,
    /// Fixed grid size
    Fixed(usize),
    /// Occupancy-based calculation
    OccupancyBased,
}

/// Registry of specialized GPU kernels
#[derive(Debug)]
pub struct KernelRegistry {
    /// Single-qubit gate kernels
    single_qubit_kernels: HashMap<String, SingleQubitKernel>,
    /// Two-qubit gate kernels
    two_qubit_kernels: HashMap<String, TwoQubitKernel>,
    /// Fused kernel templates
    fused_kernels: HashMap<String, FusedKernel>,
    /// Custom kernel implementations
    custom_kernels: HashMap<String, CustomKernel>,
}

impl Default for KernelRegistry {
    fn default() -> Self {
        let mut registry = Self {
            single_qubit_kernels: HashMap::new(),
            two_qubit_kernels: HashMap::new(),
            fused_kernels: HashMap::new(),
            custom_kernels: HashMap::new(),
        };
        registry.register_builtin_kernels();
        registry
    }
}

impl KernelRegistry {
    /// Register all built-in optimized kernels
    fn register_builtin_kernels(&mut self) {
        // Single-qubit gate kernels
        self.single_qubit_kernels.insert(
            "hadamard".to_string(),
            SingleQubitKernel {
                name: "hadamard".to_string(),
                kernel_type: SingleQubitKernelType::Hadamard,
                optimization_level: OptimizationLevel::Maximum,
                uses_shared_memory: true,
                register_usage: 32,
            },
        );

        self.single_qubit_kernels.insert(
            "pauli_x".to_string(),
            SingleQubitKernel {
                name: "pauli_x".to_string(),
                kernel_type: SingleQubitKernelType::PauliX,
                optimization_level: OptimizationLevel::Maximum,
                uses_shared_memory: false, // Simple swap operation
                register_usage: 16,
            },
        );

        self.single_qubit_kernels.insert(
            "pauli_y".to_string(),
            SingleQubitKernel {
                name: "pauli_y".to_string(),
                kernel_type: SingleQubitKernelType::PauliY,
                optimization_level: OptimizationLevel::Maximum,
                uses_shared_memory: false,
                register_usage: 24,
            },
        );

        self.single_qubit_kernels.insert(
            "pauli_z".to_string(),
            SingleQubitKernel {
                name: "pauli_z".to_string(),
                kernel_type: SingleQubitKernelType::PauliZ,
                optimization_level: OptimizationLevel::Maximum,
                uses_shared_memory: false,
                register_usage: 16,
            },
        );

        self.single_qubit_kernels.insert(
            "phase".to_string(),
            SingleQubitKernel {
                name: "phase".to_string(),
                kernel_type: SingleQubitKernelType::Phase,
                optimization_level: OptimizationLevel::High,
                uses_shared_memory: false,
                register_usage: 24,
            },
        );

        self.single_qubit_kernels.insert(
            "t_gate".to_string(),
            SingleQubitKernel {
                name: "t_gate".to_string(),
                kernel_type: SingleQubitKernelType::TGate,
                optimization_level: OptimizationLevel::High,
                uses_shared_memory: false,
                register_usage: 24,
            },
        );

        self.single_qubit_kernels.insert(
            "rotation_x".to_string(),
            SingleQubitKernel {
                name: "rotation_x".to_string(),
                kernel_type: SingleQubitKernelType::RotationX,
                optimization_level: OptimizationLevel::Medium,
                uses_shared_memory: true,
                register_usage: 40,
            },
        );

        self.single_qubit_kernels.insert(
            "rotation_y".to_string(),
            SingleQubitKernel {
                name: "rotation_y".to_string(),
                kernel_type: SingleQubitKernelType::RotationY,
                optimization_level: OptimizationLevel::Medium,
                uses_shared_memory: true,
                register_usage: 40,
            },
        );

        self.single_qubit_kernels.insert(
            "rotation_z".to_string(),
            SingleQubitKernel {
                name: "rotation_z".to_string(),
                kernel_type: SingleQubitKernelType::RotationZ,
                optimization_level: OptimizationLevel::Medium,
                uses_shared_memory: true,
                register_usage: 32,
            },
        );

        // Two-qubit gate kernels
        self.two_qubit_kernels.insert(
            "cnot".to_string(),
            TwoQubitKernel {
                name: "cnot".to_string(),
                kernel_type: TwoQubitKernelType::CNOT,
                optimization_level: OptimizationLevel::Maximum,
                uses_shared_memory: true,
                register_usage: 48,
                memory_access_pattern: MemoryAccessPattern::Strided,
            },
        );

        self.two_qubit_kernels.insert(
            "cz".to_string(),
            TwoQubitKernel {
                name: "cz".to_string(),
                kernel_type: TwoQubitKernelType::CZ,
                optimization_level: OptimizationLevel::Maximum,
                uses_shared_memory: false,
                register_usage: 32,
                memory_access_pattern: MemoryAccessPattern::Sparse,
            },
        );

        self.two_qubit_kernels.insert(
            "swap".to_string(),
            TwoQubitKernel {
                name: "swap".to_string(),
                kernel_type: TwoQubitKernelType::SWAP,
                optimization_level: OptimizationLevel::High,
                uses_shared_memory: true,
                register_usage: 40,
                memory_access_pattern: MemoryAccessPattern::Strided,
            },
        );

        self.two_qubit_kernels.insert(
            "iswap".to_string(),
            TwoQubitKernel {
                name: "iswap".to_string(),
                kernel_type: TwoQubitKernelType::ISWAP,
                optimization_level: OptimizationLevel::High,
                uses_shared_memory: true,
                register_usage: 48,
                memory_access_pattern: MemoryAccessPattern::Strided,
            },
        );

        self.two_qubit_kernels.insert(
            "controlled_rotation".to_string(),
            TwoQubitKernel {
                name: "controlled_rotation".to_string(),
                kernel_type: TwoQubitKernelType::ControlledRotation,
                optimization_level: OptimizationLevel::Medium,
                uses_shared_memory: true,
                register_usage: 56,
                memory_access_pattern: MemoryAccessPattern::Strided,
            },
        );

        // Fused kernel templates
        self.fused_kernels.insert(
            "h_cnot_h".to_string(),
            FusedKernel {
                name: "h_cnot_h".to_string(),
                sequence: vec![
                    "hadamard".to_string(),
                    "cnot".to_string(),
                    "hadamard".to_string(),
                ],
                optimization_gain: 2.5,
                register_usage: 64,
            },
        );

        self.fused_kernels.insert(
            "rotation_chain".to_string(),
            FusedKernel {
                name: "rotation_chain".to_string(),
                sequence: vec![
                    "rotation_x".to_string(),
                    "rotation_y".to_string(),
                    "rotation_z".to_string(),
                ],
                optimization_gain: 2.0,
                register_usage: 56,
            },
        );

        self.fused_kernels.insert(
            "bell_state".to_string(),
            FusedKernel {
                name: "bell_state".to_string(),
                sequence: vec!["hadamard".to_string(), "cnot".to_string()],
                optimization_gain: 1.8,
                register_usage: 48,
            },
        );
    }
}

/// Single-qubit kernel implementation
#[derive(Debug, Clone)]
pub struct SingleQubitKernel {
    /// Kernel name
    pub name: String,
    /// Kernel type
    pub kernel_type: SingleQubitKernelType,
    /// Optimization level
    pub optimization_level: OptimizationLevel,
    /// Uses shared memory
    pub uses_shared_memory: bool,
    /// Register usage
    pub register_usage: usize,
}

/// Types of single-qubit kernels
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SingleQubitKernelType {
    Hadamard,
    PauliX,
    PauliY,
    PauliZ,
    Phase,
    TGate,
    RotationX,
    RotationY,
    RotationZ,
    Generic,
}

/// Two-qubit kernel implementation
#[derive(Debug, Clone)]
pub struct TwoQubitKernel {
    /// Kernel name
    pub name: String,
    /// Kernel type
    pub kernel_type: TwoQubitKernelType,
    /// Optimization level
    pub optimization_level: OptimizationLevel,
    /// Uses shared memory
    pub uses_shared_memory: bool,
    /// Register usage
    pub register_usage: usize,
    /// Memory access pattern
    pub memory_access_pattern: MemoryAccessPattern,
}

/// Types of two-qubit kernels
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TwoQubitKernelType {
    CNOT,
    CZ,
    SWAP,
    ISWAP,
    ControlledRotation,
    Generic,
}

/// Memory access patterns for kernels
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MemoryAccessPattern {
    /// Coalesced access
    Coalesced,
    /// Strided access
    Strided,
    /// Sparse access
    Sparse,
    /// Random access
    Random,
}

/// Fused kernel for multiple operations
#[derive(Debug, Clone)]
pub struct FusedKernel {
    /// Kernel name
    pub name: String,
    /// Sequence of operations
    pub sequence: Vec<String>,
    /// Expected optimization gain
    pub optimization_gain: f64,
    /// Register usage
    pub register_usage: usize,
}

/// Custom kernel implementation
#[derive(Debug, Clone)]
pub struct CustomKernel {
    /// Kernel name
    pub name: String,
    /// Kernel code (CUDA/OpenCL)
    pub code: String,
    /// Register usage
    pub register_usage: usize,
}

/// Compiled kernel ready for execution
#[derive(Debug, Clone)]
pub struct CompiledKernel {
    /// Kernel name
    pub name: String,
    /// Compiled code (binary or PTX)
    pub compiled_code: Vec<u8>,
    /// Execution parameters
    pub exec_params: KernelExecParams,
}

/// Kernel execution parameters
#[derive(Debug, Clone)]
pub struct KernelExecParams {
    /// Block dimensions
    pub block_dim: (usize, usize, usize),
    /// Grid dimensions
    pub grid_dim: (usize, usize, usize),
    /// Shared memory size
    pub shared_memory_size: usize,
    /// Maximum threads per block
    pub max_threads_per_block: usize,
}

/// Optimization levels for kernels
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OptimizationLevel {
    /// Basic optimization
    Basic,
    /// Medium optimization
    Medium,
    /// High optimization
    High,
    /// Maximum optimization
    Maximum,
}

/// Kernel execution statistics
#[derive(Debug, Clone, Default)]
pub struct KernelStats {
    /// Total kernel executions
    pub total_executions: u64,
    /// Total execution time
    pub total_execution_time: Duration,
    /// Kernel execution counts by name
    pub execution_counts: HashMap<String, u64>,
    /// Kernel execution times by name
    pub execution_times: HashMap<String, Duration>,
    /// Cache hits
    pub cache_hits: u64,
    /// Cache misses
    pub cache_misses: u64,
    /// Fused operations count
    pub fused_operations: u64,
    /// Memory bandwidth utilized (GB/s)
    pub memory_bandwidth: f64,
    /// Compute throughput (GFLOPS)
    pub compute_throughput: f64,
}

/// Memory layout optimizer for GPU operations
#[derive(Debug)]
pub struct MemoryLayoutOptimizer {
    /// Layout strategy
    strategy: MemoryLayoutStrategy,
    /// Prefetch distance
    prefetch_distance: usize,
}

/// Memory layout strategies
#[derive(Debug, Clone, Copy)]
pub enum MemoryLayoutStrategy {
    /// Interleaved complex numbers (Re, Im, Re, Im, ...)
    Interleaved,
    /// Split arrays (all Re, then all Im)
    SplitArrays,
    /// Structure of arrays
    StructureOfArrays,
    /// Array of structures
    ArrayOfStructures,
}

impl Default for MemoryLayoutOptimizer {
    fn default() -> Self {
        Self {
            strategy: MemoryLayoutStrategy::Interleaved,
            prefetch_distance: 4,
        }
    }
}

impl GPUKernelOptimizer {
    /// Create a new GPU kernel optimizer
    #[must_use]
    pub fn new(config: GPUKernelConfig) -> Self {
        Self {
            kernel_registry: KernelRegistry::default(),
            stats: Arc::new(Mutex::new(KernelStats::default())),
            config,
            kernel_cache: Arc::new(RwLock::new(HashMap::new())),
            memory_optimizer: MemoryLayoutOptimizer::default(),
        }
    }

    /// Apply optimized single-qubit gate
    pub fn apply_single_qubit_gate(
        &mut self,
        state: &mut Array1<Complex64>,
        qubit: usize,
        gate_name: &str,
        parameters: Option<&[f64]>,
    ) -> QuantRS2Result<()> {
        let start = Instant::now();

        // Get kernel from registry
        let kernel = self.kernel_registry.single_qubit_kernels.get(gate_name);

        let n = state.len();
        let stride = 1 << qubit;

        match kernel {
            Some(k) => {
                // Apply optimized kernel
                match k.kernel_type {
                    SingleQubitKernelType::Hadamard => {
                        self.apply_hadamard_optimized(state, stride)?;
                    }
                    SingleQubitKernelType::PauliX => {
                        self.apply_pauli_x_optimized(state, stride)?;
                    }
                    SingleQubitKernelType::PauliY => {
                        self.apply_pauli_y_optimized(state, stride)?;
                    }
                    SingleQubitKernelType::PauliZ => {
                        self.apply_pauli_z_optimized(state, stride)?;
                    }
                    SingleQubitKernelType::Phase => {
                        self.apply_phase_optimized(state, stride)?;
                    }
                    SingleQubitKernelType::TGate => {
                        self.apply_t_gate_optimized(state, stride)?;
                    }
                    SingleQubitKernelType::RotationX => {
                        let angle = parameters.and_then(|p| p.first()).copied().unwrap_or(0.0);
                        self.apply_rotation_x_optimized(state, stride, angle)?;
                    }
                    SingleQubitKernelType::RotationY => {
                        let angle = parameters.and_then(|p| p.first()).copied().unwrap_or(0.0);
                        self.apply_rotation_y_optimized(state, stride, angle)?;
                    }
                    SingleQubitKernelType::RotationZ => {
                        let angle = parameters.and_then(|p| p.first()).copied().unwrap_or(0.0);
                        self.apply_rotation_z_optimized(state, stride, angle)?;
                    }
                    SingleQubitKernelType::Generic => {
                        // Fallback to generic implementation
                        self.apply_generic_single_qubit(state, qubit, gate_name)?;
                    }
                }
            }
            None => {
                // Use generic implementation
                self.apply_generic_single_qubit(state, qubit, gate_name)?;
            }
        }

        // Update statistics
        let mut stats = self
            .stats
            .lock()
            .map_err(|_| QuantRS2Error::InvalidInput("Failed to acquire stats lock".to_string()))?;
        stats.total_executions += 1;
        stats.total_execution_time += start.elapsed();
        *stats
            .execution_counts
            .entry(gate_name.to_string())
            .or_insert(0) += 1;
        *stats
            .execution_times
            .entry(gate_name.to_string())
            .or_insert(Duration::ZERO) += start.elapsed();

        Ok(())
    }

    /// Apply optimized Hadamard gate
    fn apply_hadamard_optimized(
        &self,
        state: &mut Array1<Complex64>,
        stride: usize,
    ) -> QuantRS2Result<()> {
        let n = state.len();
        let inv_sqrt2 = 1.0 / 2.0_f64.sqrt();

        let amplitudes = state.as_slice_mut().ok_or_else(|| {
            QuantRS2Error::InvalidInput("Failed to get mutable slice".to_string())
        })?;

        // Process pairs with memory coalescing
        for i in 0..n / 2 {
            let i0 = (i / stride) * (2 * stride) + (i % stride);
            let i1 = i0 + stride;

            let a0 = amplitudes[i0];
            let a1 = amplitudes[i1];

            amplitudes[i0] =
                Complex64::new((a0.re + a1.re) * inv_sqrt2, (a0.im + a1.im) * inv_sqrt2);
            amplitudes[i1] =
                Complex64::new((a0.re - a1.re) * inv_sqrt2, (a0.im - a1.im) * inv_sqrt2);
        }

        Ok(())
    }

    /// Apply optimized Pauli-X gate
    fn apply_pauli_x_optimized(
        &self,
        state: &mut Array1<Complex64>,
        stride: usize,
    ) -> QuantRS2Result<()> {
        let n = state.len();

        let amplitudes = state.as_slice_mut().ok_or_else(|| {
            QuantRS2Error::InvalidInput("Failed to get mutable slice".to_string())
        })?;

        // Simple swap operation - highly optimized
        for i in 0..n / 2 {
            let i0 = (i / stride) * (2 * stride) + (i % stride);
            let i1 = i0 + stride;

            amplitudes.swap(i0, i1);
        }

        Ok(())
    }

    /// Apply optimized Pauli-Y gate
    fn apply_pauli_y_optimized(
        &self,
        state: &mut Array1<Complex64>,
        stride: usize,
    ) -> QuantRS2Result<()> {
        let n = state.len();

        let amplitudes = state.as_slice_mut().ok_or_else(|| {
            QuantRS2Error::InvalidInput("Failed to get mutable slice".to_string())
        })?;

        for i in 0..n / 2 {
            let i0 = (i / stride) * (2 * stride) + (i % stride);
            let i1 = i0 + stride;

            let a0 = amplitudes[i0];
            let a1 = amplitudes[i1];

            // Y gate: [[0, -i], [i, 0]]
            amplitudes[i0] = Complex64::new(a1.im, -a1.re);
            amplitudes[i1] = Complex64::new(-a0.im, a0.re);
        }

        Ok(())
    }

    /// Apply optimized Pauli-Z gate
    fn apply_pauli_z_optimized(
        &self,
        state: &mut Array1<Complex64>,
        stride: usize,
    ) -> QuantRS2Result<()> {
        let n = state.len();

        let amplitudes = state.as_slice_mut().ok_or_else(|| {
            QuantRS2Error::InvalidInput("Failed to get mutable slice".to_string())
        })?;

        // Z gate only affects |1> states
        for i in 0..n / 2 {
            let i1 = (i / stride) * (2 * stride) + (i % stride) + stride;
            amplitudes[i1] = -amplitudes[i1];
        }

        Ok(())
    }

    /// Apply optimized Phase gate
    fn apply_phase_optimized(
        &self,
        state: &mut Array1<Complex64>,
        stride: usize,
    ) -> QuantRS2Result<()> {
        let n = state.len();

        let amplitudes = state.as_slice_mut().ok_or_else(|| {
            QuantRS2Error::InvalidInput("Failed to get mutable slice".to_string())
        })?;

        // S gate: phase shift of pi/2 on |1>
        for i in 0..n / 2 {
            let i1 = (i / stride) * (2 * stride) + (i % stride) + stride;
            let a = amplitudes[i1];
            amplitudes[i1] = Complex64::new(-a.im, a.re); // multiply by i
        }

        Ok(())
    }

    /// Apply optimized T gate
    fn apply_t_gate_optimized(
        &self,
        state: &mut Array1<Complex64>,
        stride: usize,
    ) -> QuantRS2Result<()> {
        let n = state.len();
        let t_phase = Complex64::new(
            std::f64::consts::FRAC_1_SQRT_2,
            std::f64::consts::FRAC_1_SQRT_2,
        );

        let amplitudes = state.as_slice_mut().ok_or_else(|| {
            QuantRS2Error::InvalidInput("Failed to get mutable slice".to_string())
        })?;

        // T gate: phase shift of pi/4 on |1>
        for i in 0..n / 2 {
            let i1 = (i / stride) * (2 * stride) + (i % stride) + stride;
            amplitudes[i1] *= t_phase;
        }

        Ok(())
    }

    /// Apply optimized rotation around X axis
    fn apply_rotation_x_optimized(
        &self,
        state: &mut Array1<Complex64>,
        stride: usize,
        angle: f64,
    ) -> QuantRS2Result<()> {
        let n = state.len();
        let cos_half = (angle / 2.0).cos();
        let sin_half = (angle / 2.0).sin();

        let amplitudes = state.as_slice_mut().ok_or_else(|| {
            QuantRS2Error::InvalidInput("Failed to get mutable slice".to_string())
        })?;

        for i in 0..n / 2 {
            let i0 = (i / stride) * (2 * stride) + (i % stride);
            let i1 = i0 + stride;

            let a0 = amplitudes[i0];
            let a1 = amplitudes[i1];

            // RX(θ) = [[cos(θ/2), -i*sin(θ/2)], [-i*sin(θ/2), cos(θ/2)]]
            amplitudes[i0] = Complex64::new(
                cos_half * a0.re + sin_half * a1.im,
                cos_half * a0.im - sin_half * a1.re,
            );
            amplitudes[i1] = Complex64::new(
                sin_half * a0.im + cos_half * a1.re,
                (-sin_half).mul_add(a0.re, cos_half * a1.im),
            );
        }

        Ok(())
    }

    /// Apply optimized rotation around Y axis
    fn apply_rotation_y_optimized(
        &self,
        state: &mut Array1<Complex64>,
        stride: usize,
        angle: f64,
    ) -> QuantRS2Result<()> {
        let n = state.len();
        let cos_half = (angle / 2.0).cos();
        let sin_half = (angle / 2.0).sin();

        let amplitudes = state.as_slice_mut().ok_or_else(|| {
            QuantRS2Error::InvalidInput("Failed to get mutable slice".to_string())
        })?;

        for i in 0..n / 2 {
            let i0 = (i / stride) * (2 * stride) + (i % stride);
            let i1 = i0 + stride;

            let a0 = amplitudes[i0];
            let a1 = amplitudes[i1];

            // RY(θ) = [[cos(θ/2), -sin(θ/2)], [sin(θ/2), cos(θ/2)]]
            amplitudes[i0] = Complex64::new(
                cos_half * a0.re - sin_half * a1.re,
                cos_half * a0.im - sin_half * a1.im,
            );
            amplitudes[i1] = Complex64::new(
                sin_half * a0.re + cos_half * a1.re,
                sin_half * a0.im + cos_half * a1.im,
            );
        }

        Ok(())
    }

    /// Apply optimized rotation around Z axis
    fn apply_rotation_z_optimized(
        &self,
        state: &mut Array1<Complex64>,
        stride: usize,
        angle: f64,
    ) -> QuantRS2Result<()> {
        let n = state.len();
        let exp_neg = Complex64::new((angle / 2.0).cos(), -(angle / 2.0).sin());
        let exp_pos = Complex64::new((angle / 2.0).cos(), (angle / 2.0).sin());

        let amplitudes = state.as_slice_mut().ok_or_else(|| {
            QuantRS2Error::InvalidInput("Failed to get mutable slice".to_string())
        })?;

        for i in 0..n / 2 {
            let i0 = (i / stride) * (2 * stride) + (i % stride);
            let i1 = i0 + stride;

            // RZ(θ) = [[e^(-iθ/2), 0], [0, e^(iθ/2)]]
            amplitudes[i0] *= exp_neg;
            amplitudes[i1] *= exp_pos;
        }

        Ok(())
    }

    /// Generic single-qubit gate application
    const fn apply_generic_single_qubit(
        &self,
        state: &Array1<Complex64>,
        qubit: usize,
        _gate_name: &str,
    ) -> QuantRS2Result<()> {
        // Generic implementation using identity matrix
        // Real implementation would use the actual gate matrix
        Ok(())
    }

    /// Apply optimized two-qubit gate
    pub fn apply_two_qubit_gate(
        &mut self,
        state: &mut Array1<Complex64>,
        control: usize,
        target: usize,
        gate_name: &str,
    ) -> QuantRS2Result<()> {
        let start = Instant::now();

        // Get kernel from registry
        let kernel = self.kernel_registry.two_qubit_kernels.get(gate_name);

        match kernel {
            Some(k) => match k.kernel_type {
                TwoQubitKernelType::CNOT => {
                    self.apply_cnot_optimized(state, control, target)?;
                }
                TwoQubitKernelType::CZ => {
                    self.apply_cz_optimized(state, control, target)?;
                }
                TwoQubitKernelType::SWAP => {
                    self.apply_swap_optimized(state, control, target)?;
                }
                TwoQubitKernelType::ISWAP => {
                    self.apply_iswap_optimized(state, control, target)?;
                }
                _ => {
                    self.apply_generic_two_qubit(state, control, target, gate_name)?;
                }
            },
            None => {
                self.apply_generic_two_qubit(state, control, target, gate_name)?;
            }
        }

        // Update statistics
        let mut stats = self
            .stats
            .lock()
            .map_err(|_| QuantRS2Error::InvalidInput("Failed to acquire stats lock".to_string()))?;
        stats.total_executions += 1;
        stats.total_execution_time += start.elapsed();
        *stats
            .execution_counts
            .entry(gate_name.to_string())
            .or_insert(0) += 1;

        Ok(())
    }

    /// Apply optimized CNOT gate
    fn apply_cnot_optimized(
        &self,
        state: &mut Array1<Complex64>,
        control: usize,
        target: usize,
    ) -> QuantRS2Result<()> {
        let n = state.len();
        let control_stride = 1 << control;
        let target_stride = 1 << target;

        let amplitudes = state.as_slice_mut().ok_or_else(|| {
            QuantRS2Error::InvalidInput("Failed to get mutable slice".to_string())
        })?;

        // CNOT: flip target when control is |1>
        for i in 0..n {
            if (i & control_stride) != 0 {
                // Control is |1>
                let partner = i ^ target_stride;
                if partner > i {
                    amplitudes.swap(i, partner);
                }
            }
        }

        Ok(())
    }

    /// Apply optimized CZ gate
    fn apply_cz_optimized(
        &self,
        state: &mut Array1<Complex64>,
        control: usize,
        target: usize,
    ) -> QuantRS2Result<()> {
        let n = state.len();
        let control_stride = 1 << control;
        let target_stride = 1 << target;

        let amplitudes = state.as_slice_mut().ok_or_else(|| {
            QuantRS2Error::InvalidInput("Failed to get mutable slice".to_string())
        })?;

        // CZ: apply phase flip when both control and target are |1>
        for (i, amplitude) in amplitudes.iter_mut().enumerate() {
            if (i & control_stride) != 0 && (i & target_stride) != 0 {
                *amplitude = -*amplitude;
            }
        }

        Ok(())
    }

    /// Apply optimized SWAP gate
    fn apply_swap_optimized(
        &self,
        state: &mut Array1<Complex64>,
        qubit1: usize,
        qubit2: usize,
    ) -> QuantRS2Result<()> {
        let n = state.len();
        let stride1 = 1 << qubit1;
        let stride2 = 1 << qubit2;

        let amplitudes = state.as_slice_mut().ok_or_else(|| {
            QuantRS2Error::InvalidInput("Failed to get mutable slice".to_string())
        })?;

        // SWAP: exchange |01> and |10> components
        for i in 0..n {
            let bit1 = (i & stride1) != 0;
            let bit2 = (i & stride2) != 0;
            if bit1 != bit2 {
                let partner = i ^ stride1 ^ stride2;
                if partner > i {
                    amplitudes.swap(i, partner);
                }
            }
        }

        Ok(())
    }

    /// Apply optimized iSWAP gate
    fn apply_iswap_optimized(
        &self,
        state: &mut Array1<Complex64>,
        qubit1: usize,
        qubit2: usize,
    ) -> QuantRS2Result<()> {
        let n = state.len();
        let stride1 = 1 << qubit1;
        let stride2 = 1 << qubit2;

        let amplitudes = state.as_slice_mut().ok_or_else(|| {
            QuantRS2Error::InvalidInput("Failed to get mutable slice".to_string())
        })?;

        // iSWAP: swap |01> and |10> with i phase
        for i in 0..n {
            let bit1 = (i & stride1) != 0;
            let bit2 = (i & stride2) != 0;
            if bit1 != bit2 {
                let partner = i ^ stride1 ^ stride2;
                if partner > i {
                    let a = amplitudes[i];
                    let b = amplitudes[partner];
                    // Multiply by i when swapping
                    amplitudes[i] = Complex64::new(-b.im, b.re);
                    amplitudes[partner] = Complex64::new(-a.im, a.re);
                }
            }
        }

        Ok(())
    }

    /// Generic two-qubit gate application
    const fn apply_generic_two_qubit(
        &self,
        _state: &mut Array1<Complex64>,
        _control: usize,
        _target: usize,
        _gate_name: &str,
    ) -> QuantRS2Result<()> {
        // Generic implementation placeholder
        Ok(())
    }

    /// Get kernel execution statistics
    pub fn get_stats(&self) -> QuantRS2Result<KernelStats> {
        let stats = self
            .stats
            .lock()
            .map_err(|_| QuantRS2Error::InvalidInput("Failed to acquire stats lock".to_string()))?;
        Ok(stats.clone())
    }

    /// Reset statistics
    pub fn reset_stats(&mut self) -> QuantRS2Result<()> {
        let mut stats = self
            .stats
            .lock()
            .map_err(|_| QuantRS2Error::InvalidInput("Failed to acquire stats lock".to_string()))?;
        *stats = KernelStats::default();
        Ok(())
    }

    /// Get available kernel names
    #[must_use]
    pub fn get_available_kernels(&self) -> Vec<String> {
        let mut kernels = Vec::new();
        kernels.extend(self.kernel_registry.single_qubit_kernels.keys().cloned());
        kernels.extend(self.kernel_registry.two_qubit_kernels.keys().cloned());
        kernels.extend(self.kernel_registry.fused_kernels.keys().cloned());
        kernels
    }

    /// Check if a kernel is available
    #[must_use]
    pub fn has_kernel(&self, name: &str) -> bool {
        self.kernel_registry.single_qubit_kernels.contains_key(name)
            || self.kernel_registry.two_qubit_kernels.contains_key(name)
            || self.kernel_registry.fused_kernels.contains_key(name)
    }
}

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

    #[test]
    fn test_kernel_optimizer_creation() {
        let config = GPUKernelConfig::default();
        let optimizer = GPUKernelOptimizer::new(config);
        assert!(!optimizer.get_available_kernels().is_empty());
    }

    #[test]
    fn test_hadamard_kernel() {
        let config = GPUKernelConfig::default();
        let mut optimizer = GPUKernelOptimizer::new(config);

        let mut state = Array1::from_vec(vec![Complex64::new(1.0, 0.0), Complex64::new(0.0, 0.0)]);

        let result = optimizer.apply_single_qubit_gate(&mut state, 0, "hadamard", None);
        assert!(result.is_ok());

        let inv_sqrt2 = 1.0 / 2.0_f64.sqrt();
        assert!((state[0].re - inv_sqrt2).abs() < 1e-10);
        assert!((state[1].re - inv_sqrt2).abs() < 1e-10);
    }

    #[test]
    fn test_pauli_x_kernel() {
        let config = GPUKernelConfig::default();
        let mut optimizer = GPUKernelOptimizer::new(config);

        let mut state = Array1::from_vec(vec![Complex64::new(1.0, 0.0), Complex64::new(0.0, 0.0)]);

        let result = optimizer.apply_single_qubit_gate(&mut state, 0, "pauli_x", None);
        assert!(result.is_ok());

        assert!((state[0].re - 0.0).abs() < 1e-10);
        assert!((state[1].re - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_pauli_z_kernel() {
        let config = GPUKernelConfig::default();
        let mut optimizer = GPUKernelOptimizer::new(config);

        let mut state = Array1::from_vec(vec![Complex64::new(0.5, 0.0), Complex64::new(0.5, 0.0)]);

        let result = optimizer.apply_single_qubit_gate(&mut state, 0, "pauli_z", None);
        assert!(result.is_ok());

        assert!((state[0].re - 0.5).abs() < 1e-10);
        assert!((state[1].re + 0.5).abs() < 1e-10);
    }

    #[test]
    fn test_rotation_z_kernel() {
        let config = GPUKernelConfig::default();
        let mut optimizer = GPUKernelOptimizer::new(config);

        let mut state = Array1::from_vec(vec![Complex64::new(1.0, 0.0), Complex64::new(0.0, 0.0)]);

        let result = optimizer.apply_single_qubit_gate(
            &mut state,
            0,
            "rotation_z",
            Some(&[std::f64::consts::PI]),
        );
        assert!(result.is_ok());
    }

    #[test]
    fn test_cnot_kernel() {
        let config = GPUKernelConfig::default();
        let mut optimizer = GPUKernelOptimizer::new(config);

        // |10> state
        let mut state = Array1::from_vec(vec![
            Complex64::new(0.0, 0.0),
            Complex64::new(0.0, 0.0),
            Complex64::new(1.0, 0.0),
            Complex64::new(0.0, 0.0),
        ]);

        let result = optimizer.apply_two_qubit_gate(&mut state, 1, 0, "cnot");
        assert!(result.is_ok());

        // Should become |11>
        assert!((state[3].re - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_cz_kernel() {
        let config = GPUKernelConfig::default();
        let mut optimizer = GPUKernelOptimizer::new(config);

        // |11> state
        let mut state = Array1::from_vec(vec![
            Complex64::new(0.0, 0.0),
            Complex64::new(0.0, 0.0),
            Complex64::new(0.0, 0.0),
            Complex64::new(1.0, 0.0),
        ]);

        let result = optimizer.apply_two_qubit_gate(&mut state, 1, 0, "cz");
        assert!(result.is_ok());

        // Should get phase flip
        assert!((state[3].re + 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_swap_kernel() {
        let config = GPUKernelConfig::default();
        let mut optimizer = GPUKernelOptimizer::new(config);

        // |01> state
        let mut state = Array1::from_vec(vec![
            Complex64::new(0.0, 0.0),
            Complex64::new(1.0, 0.0),
            Complex64::new(0.0, 0.0),
            Complex64::new(0.0, 0.0),
        ]);

        let result = optimizer.apply_two_qubit_gate(&mut state, 0, 1, "swap");
        assert!(result.is_ok());

        // Should become |10>
        assert!((state[2].re - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_kernel_stats() {
        let config = GPUKernelConfig::default();
        let mut optimizer = GPUKernelOptimizer::new(config);

        let mut state = Array1::from_vec(vec![Complex64::new(1.0, 0.0), Complex64::new(0.0, 0.0)]);

        optimizer
            .apply_single_qubit_gate(&mut state, 0, "hadamard", None)
            .expect("hadamard gate should apply successfully");
        optimizer
            .apply_single_qubit_gate(&mut state, 0, "pauli_x", None)
            .expect("pauli_x gate should apply successfully");

        let stats = optimizer.get_stats().expect("get_stats should succeed");
        assert_eq!(stats.total_executions, 2);
        assert_eq!(*stats.execution_counts.get("hadamard").unwrap_or(&0), 1);
        assert_eq!(*stats.execution_counts.get("pauli_x").unwrap_or(&0), 1);
    }

    #[test]
    fn test_available_kernels() {
        let config = GPUKernelConfig::default();
        let optimizer = GPUKernelOptimizer::new(config);

        let kernels = optimizer.get_available_kernels();
        assert!(kernels.contains(&"hadamard".to_string()));
        assert!(kernels.contains(&"cnot".to_string()));
        assert!(kernels.contains(&"swap".to_string()));
    }

    #[test]
    fn test_has_kernel() {
        let config = GPUKernelConfig::default();
        let optimizer = GPUKernelOptimizer::new(config);

        assert!(optimizer.has_kernel("hadamard"));
        assert!(optimizer.has_kernel("cnot"));
        assert!(!optimizer.has_kernel("nonexistent"));
    }

    #[test]
    fn test_config_defaults() {
        let config = GPUKernelConfig::default();

        assert!(config.enable_warp_optimization);
        assert!(config.enable_shared_memory);
        assert_eq!(config.block_size, 256);
        assert!(config.enable_kernel_fusion);
        assert_eq!(config.max_fusion_length, 8);
    }

    #[test]
    fn test_reset_stats() {
        let config = GPUKernelConfig::default();
        let mut optimizer = GPUKernelOptimizer::new(config);

        let mut state = Array1::from_vec(vec![Complex64::new(1.0, 0.0), Complex64::new(0.0, 0.0)]);

        optimizer
            .apply_single_qubit_gate(&mut state, 0, "hadamard", None)
            .expect("hadamard gate should apply successfully");
        optimizer.reset_stats().expect("reset_stats should succeed");

        let stats = optimizer.get_stats().expect("get_stats should succeed");
        assert_eq!(stats.total_executions, 0);
    }

    #[test]
    fn test_multiple_qubit_operations() {
        let config = GPUKernelConfig::default();
        let mut optimizer = GPUKernelOptimizer::new(config);

        // 3-qubit state
        let mut state = Array1::zeros(8);
        state[0] = Complex64::new(1.0, 0.0);

        // Apply H to qubit 0
        optimizer
            .apply_single_qubit_gate(&mut state, 0, "hadamard", None)
            .expect("hadamard gate should apply successfully");

        // Apply CNOT(0, 1)
        optimizer
            .apply_two_qubit_gate(&mut state, 0, 1, "cnot")
            .expect("cnot gate should apply successfully");

        // State should be in superposition
        let total_prob: f64 = state.iter().map(|a| (a * a.conj()).re).sum();
        assert!((total_prob - 1.0).abs() < 1e-10);
    }
}