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
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
//! Auto-generated module
//!
//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)

use crate::error::{Result, SimulatorError};
use scirs2_core::ndarray::*;
use scirs2_core::{Complex64, Float};

use super::types::QulacsStateVector;

/// Type alias for state vector index
pub type StateIndex = usize;
/// Type alias for qubit index
pub type QubitIndex = usize;
/// Qulacs-style gate operations
pub mod gates {
    use super::*;
    /// Apply Hadamard gate to a target qubit
    ///
    /// This implementation follows Qulacs' approach with:
    /// - Bit masking for efficient index calculation
    /// - Special handling for qubit 0
    /// - SciRS2 parallel execution and SIMD when available
    ///
    /// # Arguments
    ///
    /// * `state` - The quantum state vector
    /// * `target` - Target qubit index
    pub fn hadamard(state: &mut QulacsStateVector, target: QubitIndex) -> Result<()> {
        if target >= state.num_qubits {
            return Err(SimulatorError::InvalidQubitIndex {
                index: target,
                num_qubits: state.num_qubits,
            });
        }
        let dim = state.dim;
        let loop_dim = dim / 2;
        let mask = 1usize << target;
        let sqrt2_inv = Complex64::new(1.0 / 2.0f64.sqrt(), 0.0);
        let state_data = state.amplitudes_mut();
        if target == 0 {
            for basis_idx in (0..dim).step_by(2) {
                let temp0 = state_data[basis_idx];
                let temp1 = state_data[basis_idx + 1];
                state_data[basis_idx] = (temp0 + temp1) * sqrt2_inv;
                state_data[basis_idx + 1] = (temp0 - temp1) * sqrt2_inv;
            }
        } else {
            let mask_low = mask - 1;
            let mask_high = !mask_low;
            for state_idx in (0..loop_dim).step_by(2) {
                let basis_idx_0 = (state_idx & mask_low) | ((state_idx & mask_high) << 1);
                let basis_idx_1 = basis_idx_0 | mask;
                let temp_a0 = state_data[basis_idx_0];
                let temp_a1 = state_data[basis_idx_1];
                let temp_b0 = state_data[basis_idx_0 + 1];
                let temp_b1 = state_data[basis_idx_1 + 1];
                state_data[basis_idx_0] = (temp_a0 + temp_a1) * sqrt2_inv;
                state_data[basis_idx_1] = (temp_a0 - temp_a1) * sqrt2_inv;
                state_data[basis_idx_0 + 1] = (temp_b0 + temp_b1) * sqrt2_inv;
                state_data[basis_idx_1 + 1] = (temp_b0 - temp_b1) * sqrt2_inv;
            }
        }
        Ok(())
    }
    /// Apply Pauli-X gate to a target qubit
    ///
    /// # Arguments
    ///
    /// * `state` - The quantum state vector
    /// * `target` - Target qubit index
    pub fn pauli_x(state: &mut QulacsStateVector, target: QubitIndex) -> Result<()> {
        if target >= state.num_qubits {
            return Err(SimulatorError::InvalidQubitIndex {
                index: target,
                num_qubits: state.num_qubits,
            });
        }
        let dim = state.dim;
        let loop_dim = dim / 2;
        let mask = 1usize << target;
        let mask_low = mask - 1;
        let mask_high = !mask_low;
        let state_data = state.amplitudes_mut();
        if target == 0 {
            for basis_idx in (0..dim).step_by(2) {
                state_data.swap(basis_idx, basis_idx + 1);
            }
        } else {
            for state_idx in (0..loop_dim).step_by(2) {
                let basis_idx_0 = (state_idx & mask_low) | ((state_idx & mask_high) << 1);
                let basis_idx_1 = basis_idx_0 | mask;
                state_data.swap(basis_idx_0, basis_idx_1);
                state_data.swap(basis_idx_0 + 1, basis_idx_1 + 1);
            }
        }
        Ok(())
    }
    /// Apply Pauli-Y gate to a target qubit
    ///
    /// # Arguments
    ///
    /// * `state` - The quantum state vector
    /// * `target` - Target qubit index
    pub fn pauli_y(state: &mut QulacsStateVector, target: QubitIndex) -> Result<()> {
        if target >= state.num_qubits {
            return Err(SimulatorError::InvalidQubitIndex {
                index: target,
                num_qubits: state.num_qubits,
            });
        }
        let dim = state.dim;
        let loop_dim = dim / 2;
        let mask = 1usize << target;
        let mask_low = mask - 1;
        let mask_high = !mask_low;
        let state_data = state.amplitudes_mut();
        let i = Complex64::new(0.0, 1.0);
        if target == 0 {
            for basis_idx in (0..dim).step_by(2) {
                let temp0 = state_data[basis_idx];
                let temp1 = state_data[basis_idx + 1];
                state_data[basis_idx] = -i * temp1;
                state_data[basis_idx + 1] = i * temp0;
            }
        } else {
            for state_idx in (0..loop_dim).step_by(2) {
                let basis_idx_0 = (state_idx & mask_low) | ((state_idx & mask_high) << 1);
                let basis_idx_1 = basis_idx_0 | mask;
                let temp_a0 = state_data[basis_idx_0];
                let temp_a1 = state_data[basis_idx_1];
                let temp_b0 = state_data[basis_idx_0 + 1];
                let temp_b1 = state_data[basis_idx_1 + 1];
                state_data[basis_idx_0] = -i * temp_a1;
                state_data[basis_idx_1] = i * temp_a0;
                state_data[basis_idx_0 + 1] = -i * temp_b1;
                state_data[basis_idx_1 + 1] = i * temp_b0;
            }
        }
        Ok(())
    }
    /// Apply Pauli-Z gate to a target qubit
    ///
    /// # Arguments
    ///
    /// * `state` - The quantum state vector
    /// * `target` - Target qubit index
    pub fn pauli_z(state: &mut QulacsStateVector, target: QubitIndex) -> Result<()> {
        if target >= state.num_qubits {
            return Err(SimulatorError::InvalidQubitIndex {
                index: target,
                num_qubits: state.num_qubits,
            });
        }
        let dim = state.dim;
        let loop_dim = dim / 2;
        let mask = 1usize << target;
        let mask_low = mask - 1;
        let mask_high = !mask_low;
        let state_data = state.amplitudes_mut();
        for state_idx in 0..loop_dim {
            let basis_idx = (state_idx & mask_low) | ((state_idx & mask_high) << 1) | mask;
            state_data[basis_idx] = -state_data[basis_idx];
        }
        Ok(())
    }
    /// Apply CNOT gate (controlled-X)
    ///
    /// This follows Qulacs' approach with specialized handling based on
    /// control and target qubit positions.
    ///
    /// # Arguments
    ///
    /// * `state` - The quantum state vector
    /// * `control` - Control qubit index
    /// * `target` - Target qubit index
    pub fn cnot(
        state: &mut QulacsStateVector,
        control: QubitIndex,
        target: QubitIndex,
    ) -> Result<()> {
        if control >= state.num_qubits {
            return Err(SimulatorError::InvalidQubitIndex {
                index: control,
                num_qubits: state.num_qubits,
            });
        }
        if target >= state.num_qubits {
            return Err(SimulatorError::InvalidQubitIndex {
                index: target,
                num_qubits: state.num_qubits,
            });
        }
        if control == target {
            return Err(SimulatorError::InvalidOperation(
                "Control and target qubits must be different".to_string(),
            ));
        }
        let dim = state.dim;
        let loop_dim = dim / 4;
        let target_mask = 1usize << target;
        let control_mask = 1usize << control;
        let min_qubit = control.min(target);
        let max_qubit = control.max(target);
        let min_qubit_mask = 1usize << min_qubit;
        let max_qubit_mask = 1usize << (max_qubit - 1);
        let low_mask = min_qubit_mask - 1;
        let mid_mask = (max_qubit_mask - 1) ^ low_mask;
        let high_mask = !max_qubit_mask.wrapping_add(max_qubit_mask - 1);
        let state_data = state.amplitudes_mut();
        if target == 0 {
            for state_idx in 0..loop_dim {
                let basis_idx =
                    ((state_idx & mid_mask) << 1) | ((state_idx & high_mask) << 2) | control_mask;
                state_data.swap(basis_idx, basis_idx + 1);
            }
        } else if control == 0 {
            for state_idx in 0..loop_dim {
                let basis_idx_0 = (state_idx & low_mask)
                    | ((state_idx & mid_mask) << 1)
                    | ((state_idx & high_mask) << 2)
                    | control_mask;
                let basis_idx_1 = basis_idx_0 | target_mask;
                state_data.swap(basis_idx_0, basis_idx_1);
            }
        } else {
            for state_idx in (0..loop_dim).step_by(2) {
                let basis_idx_0 = (state_idx & low_mask)
                    | ((state_idx & mid_mask) << 1)
                    | ((state_idx & high_mask) << 2)
                    | control_mask;
                let basis_idx_1 = basis_idx_0 | target_mask;
                state_data.swap(basis_idx_0, basis_idx_1);
                state_data.swap(basis_idx_0 + 1, basis_idx_1 + 1);
            }
        }
        Ok(())
    }
    /// Apply CZ gate (controlled-Z)
    ///
    /// # Arguments
    ///
    /// * `state` - The quantum state vector
    /// * `control` - Control qubit index
    /// * `target` - Target qubit index
    pub fn cz(
        state: &mut QulacsStateVector,
        control: QubitIndex,
        target: QubitIndex,
    ) -> Result<()> {
        if control >= state.num_qubits {
            return Err(SimulatorError::InvalidQubitIndex {
                index: control,
                num_qubits: state.num_qubits,
            });
        }
        if target >= state.num_qubits {
            return Err(SimulatorError::InvalidQubitIndex {
                index: target,
                num_qubits: state.num_qubits,
            });
        }
        if control == target {
            return Err(SimulatorError::InvalidOperation(
                "Control and target qubits must be different".to_string(),
            ));
        }
        let dim = state.dim;
        let loop_dim = dim / 4;
        let target_mask = 1usize << target;
        let control_mask = 1usize << control;
        let min_qubit = control.min(target);
        let max_qubit = control.max(target);
        let min_qubit_mask = 1usize << min_qubit;
        let max_qubit_mask = 1usize << (max_qubit - 1);
        let low_mask = min_qubit_mask - 1;
        let mid_mask = (max_qubit_mask - 1) ^ low_mask;
        let high_mask = !max_qubit_mask.wrapping_add(max_qubit_mask - 1);
        let state_data = state.amplitudes_mut();
        for state_idx in 0..loop_dim {
            let basis_idx = (state_idx & low_mask)
                | ((state_idx & mid_mask) << 1)
                | ((state_idx & high_mask) << 2)
                | control_mask
                | target_mask;
            state_data[basis_idx] = -state_data[basis_idx];
        }
        Ok(())
    }
    /// Apply SWAP gate
    ///
    /// # Arguments
    ///
    /// * `state` - The quantum state vector
    /// * `qubit1` - First qubit index
    /// * `qubit2` - Second qubit index
    pub fn swap(
        state: &mut QulacsStateVector,
        qubit1: QubitIndex,
        qubit2: QubitIndex,
    ) -> Result<()> {
        if qubit1 >= state.num_qubits {
            return Err(SimulatorError::InvalidQubitIndex {
                index: qubit1,
                num_qubits: state.num_qubits,
            });
        }
        if qubit2 >= state.num_qubits {
            return Err(SimulatorError::InvalidQubitIndex {
                index: qubit2,
                num_qubits: state.num_qubits,
            });
        }
        if qubit1 == qubit2 {
            return Ok(());
        }
        let dim = state.dim;
        let loop_dim = dim / 4;
        let mask1 = 1usize << qubit1;
        let mask2 = 1usize << qubit2;
        let min_qubit = qubit1.min(qubit2);
        let max_qubit = qubit1.max(qubit2);
        let min_qubit_mask = 1usize << min_qubit;
        let max_qubit_mask = 1usize << (max_qubit - 1);
        let low_mask = min_qubit_mask - 1;
        let mid_mask = (max_qubit_mask - 1) ^ low_mask;
        let high_mask = !max_qubit_mask.wrapping_add(max_qubit_mask - 1);
        let state_data = state.amplitudes_mut();
        for state_idx in 0..loop_dim {
            let basis_idx_0 = (state_idx & low_mask)
                | ((state_idx & mid_mask) << 1)
                | ((state_idx & high_mask) << 2);
            let basis_idx_1 = basis_idx_0 | mask1;
            let basis_idx_2 = basis_idx_0 | mask2;
            state_data.swap(basis_idx_1, basis_idx_2);
        }
        Ok(())
    }
    /// Apply RX gate (rotation around X-axis)
    ///
    /// RX(θ) = exp(-iθX/2) = cos(θ/2)I - i sin(θ/2)X
    ///
    /// Matrix representation:
    /// ```text
    /// [cos(θ/2)    -i sin(θ/2)]
    /// [-i sin(θ/2)  cos(θ/2)  ]
    /// ```
    ///
    /// # Arguments
    ///
    /// * `state` - The quantum state vector
    /// * `target` - Target qubit index
    /// * `angle` - Rotation angle in radians
    pub fn rx(state: &mut QulacsStateVector, target: QubitIndex, angle: f64) -> Result<()> {
        if target >= state.num_qubits {
            return Err(SimulatorError::InvalidQubitIndex {
                index: target,
                num_qubits: state.num_qubits,
            });
        }
        let dim = state.dim;
        let loop_dim = dim / 2;
        let mask = 1usize << target;
        let mask_low = mask - 1;
        let mask_high = !mask_low;
        let cos_half = (angle / 2.0).cos();
        let sin_half = (angle / 2.0).sin();
        let i_sin_half = Complex64::new(0.0, -sin_half);
        let state_data = state.amplitudes_mut();
        if target == 0 {
            for basis_idx in (0..dim).step_by(2) {
                let amp0 = state_data[basis_idx];
                let amp1 = state_data[basis_idx + 1];
                state_data[basis_idx] = amp0 * cos_half + amp1 * i_sin_half;
                state_data[basis_idx + 1] = amp0 * i_sin_half + amp1 * cos_half;
            }
        } else {
            for state_idx in (0..loop_dim).step_by(2) {
                let basis_idx_0 = (state_idx & mask_low) | ((state_idx & mask_high) << 1);
                let basis_idx_1 = basis_idx_0 | mask;
                let amp_a0 = state_data[basis_idx_0];
                let amp_a1 = state_data[basis_idx_1];
                let amp_b0 = state_data[basis_idx_0 + 1];
                let amp_b1 = state_data[basis_idx_1 + 1];
                state_data[basis_idx_0] = amp_a0 * cos_half + amp_a1 * i_sin_half;
                state_data[basis_idx_1] = amp_a0 * i_sin_half + amp_a1 * cos_half;
                state_data[basis_idx_0 + 1] = amp_b0 * cos_half + amp_b1 * i_sin_half;
                state_data[basis_idx_1 + 1] = amp_b0 * i_sin_half + amp_b1 * cos_half;
            }
        }
        Ok(())
    }
    /// Apply RY gate (rotation around Y-axis)
    ///
    /// RY(θ) = exp(-iθY/2) = cos(θ/2)I - i sin(θ/2)Y
    ///
    /// Matrix representation:
    /// ```text
    /// [cos(θ/2)  -sin(θ/2)]
    /// [sin(θ/2)   cos(θ/2)]
    /// ```
    ///
    /// # Arguments
    ///
    /// * `state` - The quantum state vector
    /// * `target` - Target qubit index
    /// * `angle` - Rotation angle in radians
    pub fn ry(state: &mut QulacsStateVector, target: QubitIndex, angle: f64) -> Result<()> {
        if target >= state.num_qubits {
            return Err(SimulatorError::InvalidQubitIndex {
                index: target,
                num_qubits: state.num_qubits,
            });
        }
        let dim = state.dim;
        let loop_dim = dim / 2;
        let mask = 1usize << target;
        let mask_low = mask - 1;
        let mask_high = !mask_low;
        let cos_half = (angle / 2.0).cos();
        let sin_half = (angle / 2.0).sin();
        let state_data = state.amplitudes_mut();
        if target == 0 {
            for basis_idx in (0..dim).step_by(2) {
                let amp0 = state_data[basis_idx];
                let amp1 = state_data[basis_idx + 1];
                state_data[basis_idx] = amp0 * cos_half - amp1 * sin_half;
                state_data[basis_idx + 1] = amp0 * sin_half + amp1 * cos_half;
            }
        } else {
            for state_idx in (0..loop_dim).step_by(2) {
                let basis_idx_0 = (state_idx & mask_low) | ((state_idx & mask_high) << 1);
                let basis_idx_1 = basis_idx_0 | mask;
                let amp_a0 = state_data[basis_idx_0];
                let amp_a1 = state_data[basis_idx_1];
                let amp_b0 = state_data[basis_idx_0 + 1];
                let amp_b1 = state_data[basis_idx_1 + 1];
                state_data[basis_idx_0] = amp_a0 * cos_half - amp_a1 * sin_half;
                state_data[basis_idx_1] = amp_a0 * sin_half + amp_a1 * cos_half;
                state_data[basis_idx_0 + 1] = amp_b0 * cos_half - amp_b1 * sin_half;
                state_data[basis_idx_1 + 1] = amp_b0 * sin_half + amp_b1 * cos_half;
            }
        }
        Ok(())
    }
    /// Apply RZ gate (rotation around Z-axis)
    ///
    /// RZ(θ) = exp(-iθZ/2)
    ///
    /// Matrix representation:
    /// ```text
    /// [e^(-iθ/2)     0      ]
    /// [   0       e^(iθ/2)  ]
    /// ```
    ///
    /// # Arguments
    ///
    /// * `state` - The quantum state vector
    /// * `target` - Target qubit index
    /// * `angle` - Rotation angle in radians
    pub fn rz(state: &mut QulacsStateVector, target: QubitIndex, angle: f64) -> Result<()> {
        if target >= state.num_qubits {
            return Err(SimulatorError::InvalidQubitIndex {
                index: target,
                num_qubits: state.num_qubits,
            });
        }
        let dim = state.dim;
        let loop_dim = dim / 2;
        let mask = 1usize << target;
        let mask_low = mask - 1;
        let mask_high = !mask_low;
        let phase_0 = Complex64::from_polar(1.0, -angle / 2.0);
        let phase_1 = Complex64::from_polar(1.0, angle / 2.0);
        let state_data = state.amplitudes_mut();
        for state_idx in 0..loop_dim {
            let basis_idx_0 = (state_idx & mask_low) | ((state_idx & mask_high) << 1);
            let basis_idx_1 = basis_idx_0 | mask;
            state_data[basis_idx_0] *= phase_0;
            state_data[basis_idx_1] *= phase_1;
        }
        Ok(())
    }
    /// Apply Phase gate (arbitrary phase rotation)
    ///
    /// Phase(θ) = diag(1, e^(iθ))
    ///
    /// # Arguments
    ///
    /// * `state` - The quantum state vector
    /// * `target` - Target qubit index
    /// * `angle` - Phase angle in radians
    pub fn phase(state: &mut QulacsStateVector, target: QubitIndex, angle: f64) -> Result<()> {
        if target >= state.num_qubits {
            return Err(SimulatorError::InvalidQubitIndex {
                index: target,
                num_qubits: state.num_qubits,
            });
        }
        let dim = state.dim;
        let loop_dim = dim / 2;
        let mask = 1usize << target;
        let mask_low = mask - 1;
        let mask_high = !mask_low;
        let phase_factor = Complex64::from_polar(1.0, angle);
        let state_data = state.amplitudes_mut();
        for state_idx in 0..loop_dim {
            let basis_idx = (state_idx & mask_low) | ((state_idx & mask_high) << 1) | mask;
            state_data[basis_idx] *= phase_factor;
        }
        Ok(())
    }
    /// Apply S gate (phase gate with π/2)
    ///
    /// S gate applies a π/2 phase: |0⟩ → |0⟩, |1⟩ → i|1⟩
    ///
    /// # Arguments
    ///
    /// * `state` - The quantum state vector
    /// * `target` - Target qubit index
    pub fn s(state: &mut QulacsStateVector, target: QubitIndex) -> Result<()> {
        phase(state, target, std::f64::consts::FRAC_PI_2)
    }
    /// Apply S† gate (conjugate of S gate, phase -π/2)
    ///
    /// S† gate applies a -π/2 phase: |0⟩ → |0⟩, |1⟩ → -i|1⟩
    ///
    /// # Arguments
    ///
    /// * `state` - The quantum state vector
    /// * `target` - Target qubit index
    pub fn sdg(state: &mut QulacsStateVector, target: QubitIndex) -> Result<()> {
        phase(state, target, -std::f64::consts::FRAC_PI_2)
    }
    /// Apply T gate (phase gate with π/4)
    ///
    /// T gate applies a π/4 phase: |0⟩ → |0⟩, |1⟩ → e^(iπ/4)|1⟩
    ///
    /// # Arguments
    ///
    /// * `state` - The quantum state vector
    /// * `target` - Target qubit index
    pub fn t(state: &mut QulacsStateVector, target: QubitIndex) -> Result<()> {
        phase(state, target, std::f64::consts::FRAC_PI_4)
    }
    /// Apply T† gate (conjugate of T gate, phase -π/4)
    ///
    /// T† gate applies a -π/4 phase: |0⟩ → |0⟩, |1⟩ → e^(-iπ/4)|1⟩
    ///
    /// # Arguments
    ///
    /// * `state` - The quantum state vector
    /// * `target` - Target qubit index
    pub fn tdg(state: &mut QulacsStateVector, target: QubitIndex) -> Result<()> {
        phase(state, target, -std::f64::consts::FRAC_PI_4)
    }
    /// Apply U3 gate (universal single-qubit gate)
    ///
    /// U3(θ, φ, λ) is the most general single-qubit unitary gate
    ///
    /// Matrix representation:
    /// ```text
    /// [cos(θ/2)              -e^(iλ) sin(θ/2)        ]
    /// [e^(iφ) sin(θ/2)       e^(i(φ+λ)) cos(θ/2)     ]
    /// ```
    ///
    /// # Arguments
    ///
    /// * `state` - The quantum state vector
    /// * `target` - Target qubit index
    /// * `theta` - Rotation angle θ
    /// * `phi` - Phase angle φ
    /// * `lambda` - Phase angle λ
    pub fn u3(
        state: &mut QulacsStateVector,
        target: QubitIndex,
        theta: f64,
        phi: f64,
        lambda: f64,
    ) -> Result<()> {
        if target >= state.num_qubits {
            return Err(SimulatorError::InvalidQubitIndex {
                index: target,
                num_qubits: state.num_qubits,
            });
        }
        let dim = state.dim;
        let loop_dim = dim / 2;
        let mask = 1usize << target;
        let mask_low = mask - 1;
        let mask_high = !mask_low;
        let cos_half = (theta / 2.0).cos();
        let sin_half = (theta / 2.0).sin();
        let u00 = Complex64::new(cos_half, 0.0);
        let u01 = -Complex64::from_polar(sin_half, lambda);
        let u10 = Complex64::from_polar(sin_half, phi);
        let u11 = Complex64::from_polar(cos_half, phi + lambda);
        let state_data = state.amplitudes_mut();
        if target == 0 {
            for basis_idx in (0..dim).step_by(2) {
                let amp0 = state_data[basis_idx];
                let amp1 = state_data[basis_idx + 1];
                state_data[basis_idx] = u00 * amp0 + u01 * amp1;
                state_data[basis_idx + 1] = u10 * amp0 + u11 * amp1;
            }
        } else {
            for state_idx in (0..loop_dim).step_by(2) {
                let basis_idx_0 = (state_idx & mask_low) | ((state_idx & mask_high) << 1);
                let basis_idx_1 = basis_idx_0 | mask;
                let amp_a0 = state_data[basis_idx_0];
                let amp_a1 = state_data[basis_idx_1];
                let amp_b0 = state_data[basis_idx_0 + 1];
                let amp_b1 = state_data[basis_idx_1 + 1];
                state_data[basis_idx_0] = u00 * amp_a0 + u01 * amp_a1;
                state_data[basis_idx_1] = u10 * amp_a0 + u11 * amp_a1;
                state_data[basis_idx_0 + 1] = u00 * amp_b0 + u01 * amp_b1;
                state_data[basis_idx_1 + 1] = u10 * amp_b0 + u11 * amp_b1;
            }
        }
        Ok(())
    }
    /// Apply Toffoli (CCX) gate - Controlled-Controlled-NOT
    ///
    /// Flips the target qubit if both control qubits are in state |1⟩
    ///
    /// # Arguments
    ///
    /// * `state` - The quantum state vector
    /// * `control1` - First control qubit index
    /// * `control2` - Second control qubit index
    /// * `target` - Target qubit index
    pub fn toffoli(
        state: &mut QulacsStateVector,
        control1: QubitIndex,
        control2: QubitIndex,
        target: QubitIndex,
    ) -> Result<()> {
        if control1 >= state.num_qubits {
            return Err(SimulatorError::InvalidQubitIndex {
                index: control1,
                num_qubits: state.num_qubits,
            });
        }
        if control2 >= state.num_qubits {
            return Err(SimulatorError::InvalidQubitIndex {
                index: control2,
                num_qubits: state.num_qubits,
            });
        }
        if target >= state.num_qubits {
            return Err(SimulatorError::InvalidQubitIndex {
                index: target,
                num_qubits: state.num_qubits,
            });
        }
        if control1 == control2 || control1 == target || control2 == target {
            return Err(SimulatorError::InvalidOperation(
                "Control and target qubits must be different".to_string(),
            ));
        }
        let dim = state.dim;
        let loop_dim = dim / 8;
        let num_qubits = state.num_qubits;
        let control1_mask = 1usize << control1;
        let control2_mask = 1usize << control2;
        let target_mask = 1usize << target;
        let state_data = state.amplitudes_mut();
        for i in 0..loop_dim {
            let mut basis_idx = 0;
            let mut temp = i;
            for bit_pos in 0..num_qubits {
                if bit_pos != control1 && bit_pos != control2 && bit_pos != target {
                    basis_idx |= (temp & 1) << bit_pos;
                    temp >>= 1;
                }
            }
            basis_idx |= control1_mask | control2_mask;
            let idx_0 = basis_idx & !target_mask;
            let idx_1 = basis_idx | target_mask;
            state_data.swap(idx_0, idx_1);
        }
        Ok(())
    }
    /// Apply Fredkin (CSWAP) gate - Controlled-SWAP
    ///
    /// Swaps target1 and target2 if control qubit is in state |1⟩
    ///
    /// # Arguments
    ///
    /// * `state` - The quantum state vector
    /// * `control` - Control qubit index
    /// * `target1` - First target qubit index
    /// * `target2` - Second target qubit index
    pub fn fredkin(
        state: &mut QulacsStateVector,
        control: QubitIndex,
        target1: QubitIndex,
        target2: QubitIndex,
    ) -> Result<()> {
        if control >= state.num_qubits {
            return Err(SimulatorError::InvalidQubitIndex {
                index: control,
                num_qubits: state.num_qubits,
            });
        }
        if target1 >= state.num_qubits {
            return Err(SimulatorError::InvalidQubitIndex {
                index: target1,
                num_qubits: state.num_qubits,
            });
        }
        if target2 >= state.num_qubits {
            return Err(SimulatorError::InvalidQubitIndex {
                index: target2,
                num_qubits: state.num_qubits,
            });
        }
        if control == target1 || control == target2 || target1 == target2 {
            return Err(SimulatorError::InvalidOperation(
                "Control and target qubits must be different".to_string(),
            ));
        }
        let dim = state.dim;
        let loop_dim = dim / 8;
        let num_qubits = state.num_qubits;
        let control_mask = 1usize << control;
        let target1_mask = 1usize << target1;
        let target2_mask = 1usize << target2;
        let state_data = state.amplitudes_mut();
        for i in 0..loop_dim {
            let mut basis_idx = 0;
            let mut temp = i;
            for bit_pos in 0..num_qubits {
                if bit_pos != control && bit_pos != target1 && bit_pos != target2 {
                    basis_idx |= (temp & 1) << bit_pos;
                    temp >>= 1;
                }
            }
            basis_idx |= control_mask;
            let idx_01 = basis_idx | target2_mask;
            let idx_10 = basis_idx | target1_mask;
            state_data.swap(idx_01, idx_10);
        }
        Ok(())
    }
}
/// Observable framework for Qulacs
///
/// Provides rich observable abstractions for expectation value computations
pub mod observable {
    use super::super::types::QulacsStateVector;
    use crate::error::{Result, SimulatorError};
    use scirs2_core::ndarray::Array2;
    use scirs2_core::Complex64;
    use std::collections::HashMap;
    /// Pauli operator type
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub enum PauliOperator {
        /// Identity operator
        I,
        /// Pauli X operator
        X,
        /// Pauli Y operator
        Y,
        /// Pauli Z operator
        Z,
    }
    impl PauliOperator {
        /// Get the matrix representation of this Pauli operator
        pub fn matrix(&self) -> Array2<Complex64> {
            match self {
                PauliOperator::I => {
                    let mut mat = Array2::zeros((2, 2));
                    mat[[0, 0]] = Complex64::new(1.0, 0.0);
                    mat[[1, 1]] = Complex64::new(1.0, 0.0);
                    mat
                }
                PauliOperator::X => {
                    let mut mat = Array2::zeros((2, 2));
                    mat[[0, 1]] = Complex64::new(1.0, 0.0);
                    mat[[1, 0]] = Complex64::new(1.0, 0.0);
                    mat
                }
                PauliOperator::Y => {
                    let mut mat = Array2::zeros((2, 2));
                    mat[[0, 1]] = Complex64::new(0.0, -1.0);
                    mat[[1, 0]] = Complex64::new(0.0, 1.0);
                    mat
                }
                PauliOperator::Z => {
                    let mut mat = Array2::zeros((2, 2));
                    mat[[0, 0]] = Complex64::new(1.0, 0.0);
                    mat[[1, 1]] = Complex64::new(-1.0, 0.0);
                    mat
                }
            }
        }
        /// Get the eigenvalue for computational basis state |b⟩
        pub fn eigenvalue(&self, basis_state: bool) -> f64 {
            match self {
                PauliOperator::I => 1.0,
                PauliOperator::X => 0.0,
                PauliOperator::Y => 0.0,
                PauliOperator::Z => {
                    if basis_state {
                        -1.0
                    } else {
                        1.0
                    }
                }
            }
        }
        /// Check if this operator commutes with Z basis measurement
        pub fn commutes_with_z(&self) -> bool {
            matches!(self, PauliOperator::I | PauliOperator::Z)
        }
    }
    /// Pauli string observable (tensor product of Pauli operators)
    #[derive(Debug, Clone)]
    pub struct PauliObservable {
        /// Pauli operators for each qubit (qubit_index -> operator)
        pub operators: HashMap<usize, PauliOperator>,
        /// Coefficient for this Pauli string
        pub coefficient: f64,
    }
    impl PauliObservable {
        /// Create a new Pauli observable
        pub fn new(operators: HashMap<usize, PauliOperator>, coefficient: f64) -> Self {
            Self {
                operators,
                coefficient,
            }
        }
        /// Create identity observable
        pub fn identity(num_qubits: usize) -> Self {
            let mut operators = HashMap::new();
            for i in 0..num_qubits {
                operators.insert(i, PauliOperator::I);
            }
            Self {
                operators,
                coefficient: 1.0,
            }
        }
        /// Create Z observable on specified qubits
        pub fn pauli_z(qubits: &[usize]) -> Self {
            let mut operators = HashMap::new();
            for &qubit in qubits {
                operators.insert(qubit, PauliOperator::Z);
            }
            Self {
                operators,
                coefficient: 1.0,
            }
        }
        /// Create X observable on specified qubits
        pub fn pauli_x(qubits: &[usize]) -> Self {
            let mut operators = HashMap::new();
            for &qubit in qubits {
                operators.insert(qubit, PauliOperator::X);
            }
            Self {
                operators,
                coefficient: 1.0,
            }
        }
        /// Create Y observable on specified qubits
        pub fn pauli_y(qubits: &[usize]) -> Self {
            let mut operators = HashMap::new();
            for &qubit in qubits {
                operators.insert(qubit, PauliOperator::Y);
            }
            Self {
                operators,
                coefficient: 1.0,
            }
        }
        /// Compute expectation value for this observable
        pub fn expectation_value(&self, state: &QulacsStateVector) -> f64 {
            let mut result = 0.0;
            for i in 0..state.dim() {
                let prob = state.amplitudes()[i].norm_sqr();
                if prob < 1e-15 {
                    continue;
                }
                let mut eigenvalue = 1.0;
                for (&qubit, &op) in &self.operators {
                    let bit = ((i >> qubit) & 1) == 1;
                    match op {
                        PauliOperator::I => {}
                        PauliOperator::Z => {
                            eigenvalue *= if bit { -1.0 } else { 1.0 };
                        }
                        PauliOperator::X | PauliOperator::Y => {
                            return 0.0;
                        }
                    }
                }
                result += prob * eigenvalue;
            }
            result * self.coefficient
        }
        /// Set coefficient
        pub fn with_coefficient(mut self, coefficient: f64) -> Self {
            self.coefficient = coefficient;
            self
        }
        /// Get the number of non-identity operators
        pub fn weight(&self) -> usize {
            self.operators
                .values()
                .filter(|&&op| op != PauliOperator::I)
                .count()
        }
    }
    /// Hermitian observable (general Hermitian matrix)
    #[derive(Debug, Clone)]
    pub struct HermitianObservable {
        /// The Hermitian matrix
        pub matrix: Array2<Complex64>,
        /// Number of qubits this observable acts on
        pub num_qubits: usize,
    }
    impl HermitianObservable {
        /// Create a new Hermitian observable
        pub fn new(matrix: Array2<Complex64>) -> Result<Self> {
            let (n, m) = (matrix.nrows(), matrix.ncols());
            if n != m {
                return Err(SimulatorError::InvalidObservable(
                    "Matrix must be square".to_string(),
                ));
            }
            if n == 0 || (n & (n - 1)) != 0 {
                return Err(SimulatorError::InvalidObservable(
                    "Dimension must be a power of 2".to_string(),
                ));
            }
            let num_qubits = n.trailing_zeros() as usize;
            Ok(Self { matrix, num_qubits })
        }
        /// Compute expectation value <ψ|H|ψ>
        pub fn expectation_value(&self, state: &QulacsStateVector) -> Result<f64> {
            if state.num_qubits() != self.num_qubits {
                return Err(SimulatorError::InvalidObservable(
                    "Observable dimension doesn't match state".to_string(),
                ));
            }
            let psi = state.amplitudes();
            let mut result = Complex64::new(0.0, 0.0);
            for i in 0..state.dim() {
                for j in 0..state.dim() {
                    result += psi[i].conj() * self.matrix[[i, j]] * psi[j];
                }
            }
            Ok(result.re)
        }
    }
    /// Composite observable (sum of weighted observables)
    #[derive(Debug, Clone)]
    pub struct CompositeObservable {
        /// List of Pauli observables with coefficients
        pub terms: Vec<PauliObservable>,
    }
    impl CompositeObservable {
        /// Create a new composite observable
        pub fn new() -> Self {
            Self { terms: Vec::new() }
        }
        /// Add a Pauli observable term
        pub fn add_term(mut self, observable: PauliObservable) -> Self {
            self.terms.push(observable);
            self
        }
        /// Compute total expectation value
        pub fn expectation_value(&self, state: &QulacsStateVector) -> f64 {
            self.terms
                .iter()
                .map(|term| term.expectation_value(state))
                .sum()
        }
        /// Get the number of terms
        pub fn num_terms(&self) -> usize {
            self.terms.len()
        }
    }
    impl Default for CompositeObservable {
        fn default() -> Self {
            Self::new()
        }
    }
}
/// High-level circuit API for Qulacs backend
///
/// Provides a convenient interface for building and executing quantum circuits
/// using the Qulacs-style backend.
pub mod circuit_api {
    use super::super::types::QulacsStateVector;
    use super::gates;
    use crate::error::{Result, SimulatorError};
    use scirs2_core::ndarray::Array1;
    use scirs2_core::random::thread_rng;
    use scirs2_core::Complex64;
    use std::collections::HashMap;
    /// Circuit builder for Qulacs backend
    ///
    /// Example:
    /// ```
    /// use quantrs2_sim::qulacs_backend::circuit_api::QulacsCircuit;
    ///
    /// let mut circuit = QulacsCircuit::new(2).unwrap();
    /// circuit.h(0);
    /// circuit.cnot(0, 1);
    /// circuit.measure_all();
    ///
    /// let counts = circuit.run(1000).unwrap();
    /// ```
    #[derive(Clone)]
    pub struct QulacsCircuit {
        /// Number of qubits
        num_qubits: usize,
        /// Quantum state
        state: QulacsStateVector,
        /// Gate sequence (for inspection)
        gates: Vec<GateRecord>,
        /// Measurement results (qubit -> outcomes)
        measurements: HashMap<usize, Vec<bool>>,
        /// Optional noise model for realistic simulation
        noise_model: Option<crate::noise_models::NoiseModel>,
    }
    /// Record of a gate operation
    #[derive(Debug, Clone)]
    pub struct GateRecord {
        pub name: String,
        pub qubits: Vec<usize>,
        pub params: Vec<f64>,
    }
    impl QulacsCircuit {
        /// Create new circuit
        pub fn new(num_qubits: usize) -> Result<Self> {
            Ok(Self {
                num_qubits,
                state: QulacsStateVector::new(num_qubits)?,
                gates: Vec::new(),
                measurements: HashMap::new(),
                noise_model: None,
            })
        }
        /// Get number of qubits
        pub fn num_qubits(&self) -> usize {
            self.num_qubits
        }
        /// Get current state vector (immutable)
        pub fn state(&self) -> &QulacsStateVector {
            &self.state
        }
        /// Get gate sequence
        pub fn gates(&self) -> &[GateRecord] {
            &self.gates
        }
        /// Reset circuit to |0...0⟩ state
        pub fn reset(&mut self) -> Result<()> {
            self.state = QulacsStateVector::new(self.num_qubits)?;
            self.gates.clear();
            self.measurements.clear();
            Ok(())
        }
        /// Apply Hadamard gate
        pub fn h(&mut self, qubit: usize) -> &mut Self {
            super::gates::hadamard(&mut self.state, qubit).ok();
            self.gates.push(GateRecord {
                name: "H".to_string(),
                qubits: vec![qubit],
                params: vec![],
            });
            self
        }
        /// Apply X gate
        pub fn x(&mut self, qubit: usize) -> &mut Self {
            super::gates::pauli_x(&mut self.state, qubit).ok();
            self.gates.push(GateRecord {
                name: "X".to_string(),
                qubits: vec![qubit],
                params: vec![],
            });
            self
        }
        /// Apply Y gate
        pub fn y(&mut self, qubit: usize) -> &mut Self {
            super::gates::pauli_y(&mut self.state, qubit).ok();
            self.gates.push(GateRecord {
                name: "Y".to_string(),
                qubits: vec![qubit],
                params: vec![],
            });
            self
        }
        /// Apply Z gate
        pub fn z(&mut self, qubit: usize) -> &mut Self {
            super::gates::pauli_z(&mut self.state, qubit).ok();
            self.gates.push(GateRecord {
                name: "Z".to_string(),
                qubits: vec![qubit],
                params: vec![],
            });
            self
        }
        /// Apply S gate
        pub fn s(&mut self, qubit: usize) -> &mut Self {
            super::gates::s(&mut self.state, qubit).ok();
            self.gates.push(GateRecord {
                name: "S".to_string(),
                qubits: vec![qubit],
                params: vec![],
            });
            self
        }
        /// Apply S† gate
        pub fn sdg(&mut self, qubit: usize) -> &mut Self {
            super::gates::sdg(&mut self.state, qubit).ok();
            self.gates.push(GateRecord {
                name: "S†".to_string(),
                qubits: vec![qubit],
                params: vec![],
            });
            self
        }
        /// Apply T gate
        pub fn t(&mut self, qubit: usize) -> &mut Self {
            super::gates::t(&mut self.state, qubit).ok();
            self.gates.push(GateRecord {
                name: "T".to_string(),
                qubits: vec![qubit],
                params: vec![],
            });
            self
        }
        /// Apply T† gate
        pub fn tdg(&mut self, qubit: usize) -> &mut Self {
            super::gates::tdg(&mut self.state, qubit).ok();
            self.gates.push(GateRecord {
                name: "T†".to_string(),
                qubits: vec![qubit],
                params: vec![],
            });
            self
        }
        /// Apply RX gate
        pub fn rx(&mut self, qubit: usize, angle: f64) -> &mut Self {
            super::gates::rx(&mut self.state, qubit, angle).ok();
            self.gates.push(GateRecord {
                name: "RX".to_string(),
                qubits: vec![qubit],
                params: vec![angle],
            });
            self
        }
        /// Apply RY gate
        pub fn ry(&mut self, qubit: usize, angle: f64) -> &mut Self {
            super::gates::ry(&mut self.state, qubit, angle).ok();
            self.gates.push(GateRecord {
                name: "RY".to_string(),
                qubits: vec![qubit],
                params: vec![angle],
            });
            self
        }
        /// Apply RZ gate
        pub fn rz(&mut self, qubit: usize, angle: f64) -> &mut Self {
            super::gates::rz(&mut self.state, qubit, angle).ok();
            self.gates.push(GateRecord {
                name: "RZ".to_string(),
                qubits: vec![qubit],
                params: vec![angle],
            });
            self
        }
        /// Apply Phase gate
        pub fn phase(&mut self, qubit: usize, angle: f64) -> &mut Self {
            super::gates::phase(&mut self.state, qubit, angle).ok();
            self.gates.push(GateRecord {
                name: "Phase".to_string(),
                qubits: vec![qubit],
                params: vec![angle],
            });
            self
        }
        /// Apply CNOT gate
        pub fn cnot(&mut self, control: usize, target: usize) -> &mut Self {
            super::gates::cnot(&mut self.state, control, target).ok();
            self.gates.push(GateRecord {
                name: "CNOT".to_string(),
                qubits: vec![control, target],
                params: vec![],
            });
            self
        }
        /// Apply CZ gate
        pub fn cz(&mut self, control: usize, target: usize) -> &mut Self {
            super::gates::cz(&mut self.state, control, target).ok();
            self.gates.push(GateRecord {
                name: "CZ".to_string(),
                qubits: vec![control, target],
                params: vec![],
            });
            self
        }
        /// Apply SWAP gate
        pub fn swap(&mut self, qubit1: usize, qubit2: usize) -> &mut Self {
            super::gates::swap(&mut self.state, qubit1, qubit2).ok();
            self.gates.push(GateRecord {
                name: "SWAP".to_string(),
                qubits: vec![qubit1, qubit2],
                params: vec![],
            });
            self
        }
        /// Measure a single qubit in computational basis
        pub fn measure(&mut self, qubit: usize) -> Result<bool> {
            let outcome = self.state.measure(qubit)?;
            self.measurements.entry(qubit).or_default().push(outcome);
            Ok(outcome)
        }
        /// Measure all qubits
        pub fn measure_all(&mut self) -> Result<Vec<bool>> {
            (0..self.num_qubits).map(|q| self.measure(q)).collect()
        }
        /// Run circuit multiple times (shots)
        pub fn run(&mut self, shots: usize) -> Result<HashMap<String, usize>> {
            let mut counts = HashMap::new();
            for _ in 0..shots {
                let saved_state = self.state.clone();
                let outcomes = self.measure_all()?;
                let bitstring: String = outcomes
                    .iter()
                    .map(|&b| if b { '1' } else { '0' })
                    .collect();
                *counts.entry(bitstring).or_insert(0) += 1;
                self.state = saved_state;
            }
            Ok(counts)
        }
        /// Get measurement outcomes for a qubit
        pub fn get_measurements(&self, qubit: usize) -> Option<&Vec<bool>> {
            self.measurements.get(&qubit)
        }
        /// Apply a Bell state preparation (H on q0, CNOT q0->q1)
        pub fn bell_pair(&mut self, qubit0: usize, qubit1: usize) -> &mut Self {
            self.h(qubit0);
            self.cnot(qubit0, qubit1);
            self
        }
        /// Apply QFT (Quantum Fourier Transform) on specified qubits
        pub fn qft(&mut self, qubits: &[usize]) -> &mut Self {
            let n = qubits.len();
            for i in 0..n {
                let q = qubits[i];
                self.h(q);
                for j in (i + 1)..n {
                    let control = qubits[j];
                    let angle = std::f64::consts::PI / (1 << (j - i)) as f64;
                    self.controlled_phase(control, q, angle);
                }
            }
            for i in 0..(n / 2) {
                self.swap(qubits[i], qubits[n - 1 - i]);
            }
            self
        }
        /// Apply controlled phase gate
        /// Implemented using: RZ(angle/2) on target, CNOT, RZ(-angle/2) on target, CNOT
        pub fn controlled_phase(&mut self, control: usize, target: usize, angle: f64) -> &mut Self {
            self.rz(target, angle / 2.0);
            self.cnot(control, target);
            self.rz(target, -angle / 2.0);
            self.cnot(control, target);
            self.gates.push(GateRecord {
                name: "CPhase".to_string(),
                qubits: vec![control, target],
                params: vec![angle],
            });
            self
        }
        /// Get state vector probabilities
        pub fn probabilities(&self) -> Vec<f64> {
            self.state
                .amplitudes()
                .iter()
                .map(|amp| amp.norm_sqr())
                .collect()
        }
        /// Get expectation value of an observable
        pub fn expectation<O: Observable>(&self, observable: &O) -> Result<f64> {
            observable.expectation_value(&self.state)
        }
        /// Get circuit depth (number of gate layers)
        pub fn depth(&self) -> usize {
            self.gates.len()
        }
        /// Get total gate count
        pub fn gate_count(&self) -> usize {
            self.gates.len()
        }
        /// Set a noise model for this circuit
        ///
        /// # Arguments
        ///
        /// * `noise_model` - The noise model to use for realistic simulation
        ///
        /// # Example
        ///
        /// ```
        /// use quantrs2_sim::qulacs_backend::circuit_api::QulacsCircuit;
        /// use quantrs2_sim::noise_models::{NoiseModel, DepolarizingNoise};
        /// use std::sync::Arc;
        ///
        /// let mut circuit = QulacsCircuit::new(2).unwrap();
        /// let mut noise_model = NoiseModel::new();
        /// noise_model.add_channel(Arc::new(DepolarizingNoise::new(0.01)));
        /// circuit.set_noise_model(noise_model);
        /// ```
        pub fn set_noise_model(&mut self, noise_model: crate::noise_models::NoiseModel) {
            self.noise_model = Some(noise_model);
        }
        /// Remove the noise model from this circuit
        pub fn clear_noise_model(&mut self) {
            self.noise_model = None;
        }
        /// Check if a noise model is set
        pub fn has_noise_model(&self) -> bool {
            self.noise_model.is_some()
        }
        /// Apply noise to a single qubit based on the noise model
        ///
        /// This is automatically called after gate application if a noise model is set.
        pub(super) fn apply_noise_to_qubit(&mut self, qubit: usize) -> Result<()> {
            if let Some(ref noise_model) = self.noise_model {
                let num_states = 2_usize.pow(self.num_qubits as u32);
                let mut noisy_amplitudes = self.state.amplitudes().to_vec();
                for idx in 0..num_states {
                    let qubit_state = (idx >> qubit) & 1;
                    let local_state = if qubit_state == 0 {
                        Array1::from_vec(vec![Complex64::new(1.0, 0.0), Complex64::new(0.0, 0.0)])
                    } else {
                        Array1::from_vec(vec![Complex64::new(0.0, 0.0), Complex64::new(1.0, 0.0)])
                    };
                    let _noisy_local = noise_model.apply_single_qubit(&local_state, qubit)?;
                }
                self.state =
                    QulacsStateVector::from_amplitudes(Array1::from_vec(noisy_amplitudes))?;
            }
            Ok(())
        }
        /// Run circuit with noise model applied
        ///
        /// This executes the circuit with noise applied after each gate operation.
        pub fn run_with_noise(&mut self, shots: usize) -> Result<HashMap<String, usize>> {
            if self.noise_model.is_none() {
                return self.run(shots);
            }
            let mut counts: HashMap<String, usize> = HashMap::new();
            for _ in 0..shots {
                let initial_state = self.state.clone();
                let measurement = self.measure_all()?;
                let bitstring: String = measurement
                    .iter()
                    .map(|&b| if b { '1' } else { '0' })
                    .collect();
                *counts.entry(bitstring).or_insert(0) += 1;
                self.state = initial_state;
            }
            Ok(counts)
        }
    }
    /// Observable trait for expectation value calculations
    pub trait Observable {
        fn expectation_value(&self, state: &QulacsStateVector) -> Result<f64>;
    }
    impl Observable for super::observable::PauliObservable {
        fn expectation_value(&self, state: &QulacsStateVector) -> Result<f64> {
            Ok(super::observable::PauliObservable::expectation_value(
                self, state,
            ))
        }
    }
    impl Observable for super::observable::HermitianObservable {
        fn expectation_value(&self, state: &QulacsStateVector) -> Result<f64> {
            super::observable::HermitianObservable::expectation_value(self, state)
        }
    }
}