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
//! Auto-generated module
//!
//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)

use scirs2_core::ndarray::*;
use scirs2_core::{Complex64, Float};

use super::types::QulacsStateVector;

#[cfg(test)]
mod tests {
    use super::*;
    use crate::qulacs_backend::circuit_api::{self, QulacsCircuit};
    use crate::qulacs_backend::gates;
    use crate::qulacs_backend::observable;
    use crate::qulacs_backend::observable::PauliObservable;
    use scirs2_core::Float;
    use std::f64::consts::PI;
    use std::sync::Arc;
    #[test]
    fn test_state_creation() {
        let state = QulacsStateVector::new(2).unwrap();
        assert_eq!(state.num_qubits(), 2);
        assert_eq!(state.dim(), 4);
        assert!((state.norm_squared() - 1.0).abs() < 1e-10);
    }
    #[test]
    fn test_hadamard_gate() {
        let mut state = QulacsStateVector::new(1).unwrap();
        gates::hadamard(&mut state, 0).unwrap();
        let expected_amp = 1.0 / 2.0f64.sqrt();
        assert!((state.amplitudes()[0].re - expected_amp).abs() < 1e-10);
        assert!((state.amplitudes()[1].re - expected_amp).abs() < 1e-10);
    }
    #[test]
    fn test_pauli_x_gate() {
        let mut state = QulacsStateVector::new(1).unwrap();
        gates::pauli_x(&mut state, 0).unwrap();
        assert!((state.amplitudes()[0].norm() - 0.0).abs() < 1e-10);
        assert!((state.amplitudes()[1].norm() - 1.0).abs() < 1e-10);
    }
    #[test]
    fn test_cnot_gate() {
        let mut state = QulacsStateVector::new(2).unwrap();
        gates::pauli_x(&mut state, 0).unwrap();
        gates::pauli_x(&mut state, 1).unwrap();
        assert!((state.amplitudes()[3].norm() - 1.0).abs() < 1e-10);
        gates::cnot(&mut state, 0, 1).unwrap();
        assert!((state.amplitudes()[0].norm() - 0.0).abs() < 1e-10);
        assert!((state.amplitudes()[1].norm() - 1.0).abs() < 1e-10);
        assert!((state.amplitudes()[2].norm() - 0.0).abs() < 1e-10);
        assert!((state.amplitudes()[3].norm() - 0.0).abs() < 1e-10);
    }
    #[test]
    fn test_bell_state() {
        let mut state = QulacsStateVector::new(2).unwrap();
        gates::hadamard(&mut state, 0).unwrap();
        gates::cnot(&mut state, 0, 1).unwrap();
        let expected_amp = 1.0 / 2.0f64.sqrt();
        assert!((state.amplitudes()[0].re - expected_amp).abs() < 1e-10);
        assert!((state.amplitudes()[1].norm() - 0.0).abs() < 1e-10);
        assert!((state.amplitudes()[2].norm() - 0.0).abs() < 1e-10);
        assert!((state.amplitudes()[3].re - expected_amp).abs() < 1e-10);
    }
    #[test]
    fn test_norm_squared() {
        let state = QulacsStateVector::new(3).unwrap();
        assert!((state.norm_squared() - 1.0).abs() < 1e-10);
    }
    #[test]
    fn test_inner_product() {
        let state1 = QulacsStateVector::new(2).unwrap();
        let state2 = QulacsStateVector::new(2).unwrap();
        let inner = state1.inner_product(&state2).unwrap();
        assert!((inner.re - 1.0).abs() < 1e-10);
        assert!(inner.im.abs() < 1e-10);
    }
    #[test]
    fn test_rx_gate() {
        use std::f64::consts::PI;
        let mut state1 = QulacsStateVector::new(1).unwrap();
        gates::rx(&mut state1, 0, PI).unwrap();
        let mut state2 = QulacsStateVector::new(1).unwrap();
        gates::pauli_x(&mut state2, 0).unwrap();
        assert!(state1.amplitudes()[0].norm() < 1e-10);
        assert!((state1.amplitudes()[1].norm() - 1.0).abs() < 1e-10);
        let mut state3 = QulacsStateVector::new(1).unwrap();
        gates::rx(&mut state3, 0, PI / 2.0).unwrap();
        let expected = 1.0 / 2.0f64.sqrt();
        assert!((state3.amplitudes()[0].re - expected).abs() < 1e-10);
        assert!(state3.amplitudes()[0].im.abs() < 1e-10);
        assert!(state3.amplitudes()[1].re.abs() < 1e-10);
        assert!((state3.amplitudes()[1].im + expected).abs() < 1e-10);
    }
    #[test]
    fn test_ry_gate() {
        let mut state = QulacsStateVector::new(1).unwrap();
        gates::ry(&mut state, 0, PI / 2.0).unwrap();
        let expected = 1.0 / 2.0f64.sqrt();
        assert!((state.amplitudes()[0].re - expected).abs() < 1e-10);
        assert!((state.amplitudes()[1].re - expected).abs() < 1e-10);
        assert!(state.amplitudes()[0].im.abs() < 1e-10);
        assert!(state.amplitudes()[1].im.abs() < 1e-10);
        let mut bell_state = QulacsStateVector::new(2).unwrap();
        gates::ry(&mut bell_state, 0, PI / 2.0).unwrap();
        gates::cnot(&mut bell_state, 0, 1).unwrap();
        assert!((bell_state.amplitudes()[0].norm() - expected).abs() < 1e-10);
        assert!((bell_state.amplitudes()[3].norm() - expected).abs() < 1e-10);
    }
    #[test]
    fn test_rz_gate() {
        let mut state1 = QulacsStateVector::new(1).unwrap();
        gates::pauli_x(&mut state1, 0).unwrap();
        gates::rz(&mut state1, 0, PI).unwrap();
        assert!(state1.amplitudes()[0].norm() < 1e-10);
        assert!((state1.amplitudes()[1].norm() - 1.0).abs() < 1e-10);
        let mut state2 = QulacsStateVector::new(1).unwrap();
        gates::hadamard(&mut state2, 0).unwrap();
        gates::rz(&mut state2, 0, PI / 4.0).unwrap();
        assert!((state2.amplitudes()[0].norm() - 1.0 / 2.0f64.sqrt()).abs() < 1e-10);
        assert!((state2.amplitudes()[1].norm() - 1.0 / 2.0f64.sqrt()).abs() < 1e-10);
    }
    #[test]
    fn test_phase_gate() {
        let mut state = QulacsStateVector::new(1).unwrap();
        gates::pauli_x(&mut state, 0).unwrap();
        gates::phase(&mut state, 0, PI / 2.0).unwrap();
        assert!(state.amplitudes()[0].norm() < 1e-10);
        assert!((state.amplitudes()[1].re).abs() < 1e-10);
        assert!((state.amplitudes()[1].im - 1.0).abs() < 1e-10);
    }
    #[test]
    fn test_u3_gate() {
        let mut state1 = QulacsStateVector::new(1).unwrap();
        gates::u3(&mut state1, 0, PI / 2.0, 0.0, PI).unwrap();
        let mut state2 = QulacsStateVector::new(1).unwrap();
        gates::hadamard(&mut state2, 0).unwrap();
        let ratio = state1.amplitudes()[0] / state2.amplitudes()[0];
        assert!((state1.amplitudes()[0] / ratio - state2.amplitudes()[0]).norm() < 1e-10);
        assert!((state1.amplitudes()[1] / ratio - state2.amplitudes()[1]).norm() < 1e-10);
        let mut state3 = QulacsStateVector::new(1).unwrap();
        gates::u3(&mut state3, 0, PI, 0.0, PI).unwrap();
        let mut state4 = QulacsStateVector::new(1).unwrap();
        gates::pauli_x(&mut state4, 0).unwrap();
        let ratio2 = state3.amplitudes()[1] / state4.amplitudes()[1];
        assert!((state3.amplitudes()[0] / ratio2 - state4.amplitudes()[0]).norm() < 1e-10);
        assert!((state3.amplitudes()[1] / ratio2 - state4.amplitudes()[1]).norm() < 1e-10);
    }
    #[test]
    fn test_rotation_gates_on_multi_qubit_state() {
        let mut state = QulacsStateVector::new(3).unwrap();
        gates::ry(&mut state, 0, PI / 2.0).unwrap();
        gates::rx(&mut state, 1, PI / 4.0).unwrap();
        gates::rz(&mut state, 2, PI / 3.0).unwrap();
        assert!((state.norm_squared() - 1.0).abs() < 1e-10);
    }
    #[test]
    fn test_rotation_composition() {
        let mut state1 = QulacsStateVector::new(1).unwrap();
        gates::rx(&mut state1, 0, PI / 4.0).unwrap();
        gates::ry(&mut state1, 0, PI / 4.0).unwrap();
        gates::rz(&mut state1, 0, PI / 4.0).unwrap();
        assert!((state1.norm_squared() - 1.0).abs() < 1e-10);
        let mut state2 = QulacsStateVector::new(1).unwrap();
        gates::u3(&mut state2, 0, PI / 4.0, PI / 4.0, PI / 4.0).unwrap();
        assert!((state2.norm_squared() - 1.0).abs() < 1e-10);
    }
    #[test]
    fn test_probability_calculation() {
        let state0 = QulacsStateVector::new(1).unwrap();
        assert!((state0.probability_zero(0).unwrap() - 1.0).abs() < 1e-10);
        assert!(state0.probability_one(0).unwrap().abs() < 1e-10);
        let mut state1 = QulacsStateVector::new(1).unwrap();
        gates::pauli_x(&mut state1, 0).unwrap();
        assert!(state1.probability_zero(0).unwrap().abs() < 1e-10);
        assert!((state1.probability_one(0).unwrap() - 1.0).abs() < 1e-10);
        let mut state_plus = QulacsStateVector::new(1).unwrap();
        gates::hadamard(&mut state_plus, 0).unwrap();
        assert!((state_plus.probability_zero(0).unwrap() - 0.5).abs() < 1e-10);
        assert!((state_plus.probability_one(0).unwrap() - 0.5).abs() < 1e-10);
    }
    #[test]
    fn test_measurement_collapse() {
        let mut outcomes_0 = 0;
        let mut outcomes_1 = 0;
        let num_trials = 1000;
        for _ in 0..num_trials {
            let mut state = QulacsStateVector::new(1).unwrap();
            gates::hadamard(&mut state, 0).unwrap();
            let outcome = state.measure(0).unwrap();
            if outcome {
                outcomes_1 += 1;
            } else {
                outcomes_0 += 1;
            }
            assert!((state.norm_squared() - 1.0).abs() < 1e-10);
            if outcome {
                assert!((state.probability_one(0).unwrap() - 1.0).abs() < 1e-10);
            } else {
                assert!((state.probability_zero(0).unwrap() - 1.0).abs() < 1e-10);
            }
        }
        let ratio = outcomes_1 as f64 / num_trials as f64;
        assert!(ratio > 0.4 && ratio < 0.6, "Ratio: {}", ratio);
    }
    #[test]
    fn test_sampling() {
        let mut bell_state = QulacsStateVector::new(2).unwrap();
        gates::hadamard(&mut bell_state, 0).unwrap();
        gates::cnot(&mut bell_state, 0, 1).unwrap();
        let samples = bell_state.sample(1000).unwrap();
        assert_eq!(samples.len(), 1000);
        let mut count_00 = 0;
        let mut count_11 = 0;
        for bitstring in &samples {
            assert_eq!(bitstring.len(), 2);
            if !bitstring[0] && !bitstring[1] {
                count_00 += 1;
            } else if bitstring[0] && bitstring[1] {
                count_11 += 1;
            } else {
                panic!("Unexpected outcome: {:?}", bitstring);
            }
        }
        let ratio = count_00 as f64 / 1000.0;
        assert!(ratio > 0.4 && ratio < 0.6, "Ratio |00⟩: {}", ratio);
        assert!((bell_state.norm_squared() - 1.0).abs() < 1e-10);
    }
    #[test]
    fn test_get_counts() {
        let mut state = QulacsStateVector::new(1).unwrap();
        gates::hadamard(&mut state, 0).unwrap();
        let counts = state.get_counts(1000).unwrap();
        assert!(counts.len() <= 2);
        let count_0 = *counts.get(&vec![false]).unwrap_or(&0);
        let count_1 = *counts.get(&vec![true]).unwrap_or(&0);
        assert_eq!(count_0 + count_1, 1000);
        let ratio = count_1 as f64 / 1000.0;
        assert!(ratio > 0.4 && ratio < 0.6, "Ratio |1⟩: {}", ratio);
    }
    #[test]
    fn test_sample_qubits() {
        let mut bell = QulacsStateVector::new(2).unwrap();
        gates::hadamard(&mut bell, 0).unwrap();
        gates::cnot(&mut bell, 0, 1).unwrap();
        let samples = bell.sample_qubits(&[0], 1000).unwrap();
        let mut count_0 = 0;
        let mut count_1 = 0;
        for bitstring in &samples {
            assert_eq!(bitstring.len(), 1);
            if bitstring[0] {
                count_1 += 1;
            } else {
                count_0 += 1;
            }
        }
        let ratio = count_1 as f64 / 1000.0;
        assert!(ratio > 0.4 && ratio < 0.6, "Ratio: {}", ratio);
    }
    #[test]
    fn test_measurement_multi_qubit() {
        let mut bell_state = QulacsStateVector::new(2).unwrap();
        gates::hadamard(&mut bell_state, 0).unwrap();
        gates::cnot(&mut bell_state, 0, 1).unwrap();
        let outcome0 = bell_state.measure(0).unwrap();
        let outcome1 = bell_state.measure(1).unwrap();
        assert_eq!(outcome0, outcome1);
    }
    #[test]
    fn test_toffoli_gate() {
        let mut state = QulacsStateVector::new(3).unwrap();
        gates::toffoli(&mut state, 0, 1, 2).unwrap();
        assert!((state.amplitudes()[0].norm() - 1.0).abs() < 1e-10);
        assert!(state.amplitudes()[7].norm() < 1e-10);
        let mut state2 = QulacsStateVector::new(3).unwrap();
        gates::pauli_x(&mut state2, 0).unwrap();
        gates::pauli_x(&mut state2, 1).unwrap();
        gates::toffoli(&mut state2, 0, 1, 2).unwrap();
        assert!((state2.amplitudes()[7].norm() - 1.0).abs() < 1e-10);
        assert!(state2.amplitudes()[3].norm() < 1e-10);
        let mut state3 = QulacsStateVector::new(3).unwrap();
        gates::hadamard(&mut state3, 0).unwrap();
        gates::pauli_x(&mut state3, 0).unwrap();
        gates::pauli_x(&mut state3, 1).unwrap();
        gates::pauli_x(&mut state3, 2).unwrap();
        gates::toffoli(&mut state3, 0, 1, 2).unwrap();
        assert!((state3.norm_squared() - 1.0).abs() < 1e-10);
    }
    #[test]
    fn test_toffoli_reversibility() {
        let mut state1 = QulacsStateVector::new(3).unwrap();
        gates::hadamard(&mut state1, 0).unwrap();
        gates::hadamard(&mut state1, 1).unwrap();
        gates::hadamard(&mut state1, 2).unwrap();
        let original_state = state1.clone();
        gates::toffoli(&mut state1, 0, 1, 2).unwrap();
        gates::toffoli(&mut state1, 0, 1, 2).unwrap();
        for i in 0..8 {
            let diff = (state1.amplitudes()[i] - original_state.amplitudes()[i]).norm();
            assert!(diff < 1e-10, "Difference at index {}: {}", i, diff);
        }
    }
    #[test]
    fn test_fredkin_gate() {
        let mut state = QulacsStateVector::new(3).unwrap();
        gates::fredkin(&mut state, 0, 1, 2).unwrap();
        assert!((state.amplitudes()[0].norm() - 1.0).abs() < 1e-10);
        let mut state2 = QulacsStateVector::new(3).unwrap();
        gates::pauli_x(&mut state2, 0).unwrap();
        gates::pauli_x(&mut state2, 2).unwrap();
        assert!((state2.amplitudes()[0b101].norm() - 1.0).abs() < 1e-10);
        gates::fredkin(&mut state2, 0, 1, 2).unwrap();
        assert!((state2.amplitudes()[0b011].norm() - 1.0).abs() < 1e-10);
        assert!(state2.amplitudes()[0b101].norm() < 1e-10);
        let mut state3 = QulacsStateVector::new(3).unwrap();
        gates::pauli_x(&mut state3, 1).unwrap();
        gates::pauli_x(&mut state3, 2).unwrap();
        let before = state3.clone();
        gates::fredkin(&mut state3, 0, 1, 2).unwrap();
        for i in 0..8 {
            let diff = (state3.amplitudes()[i] - before.amplitudes()[i]).norm();
            assert!(diff < 1e-10);
        }
    }
    #[test]
    fn test_fredkin_reversibility() {
        let mut state1 = QulacsStateVector::new(3).unwrap();
        gates::hadamard(&mut state1, 0).unwrap();
        gates::hadamard(&mut state1, 1).unwrap();
        gates::hadamard(&mut state1, 2).unwrap();
        let original_state = state1.clone();
        gates::fredkin(&mut state1, 0, 1, 2).unwrap();
        gates::fredkin(&mut state1, 0, 1, 2).unwrap();
        for i in 0..8 {
            let diff = (state1.amplitudes()[i] - original_state.amplitudes()[i]).norm();
            assert!(diff < 1e-10, "Difference at index {}: {}", i, diff);
        }
    }
    #[test]
    fn test_toffoli_error_cases() {
        let mut state = QulacsStateVector::new(3).unwrap();
        assert!(gates::toffoli(&mut state, 0, 1, 5).is_err());
        assert!(gates::toffoli(&mut state, 5, 1, 2).is_err());
        assert!(gates::toffoli(&mut state, 0, 0, 2).is_err());
        assert!(gates::toffoli(&mut state, 0, 1, 1).is_err());
        assert!(gates::toffoli(&mut state, 0, 1, 0).is_err());
    }
    #[test]
    fn test_fredkin_error_cases() {
        let mut state = QulacsStateVector::new(3).unwrap();
        assert!(gates::fredkin(&mut state, 5, 1, 2).is_err());
        assert!(gates::fredkin(&mut state, 0, 5, 2).is_err());
        assert!(gates::fredkin(&mut state, 0, 1, 5).is_err());
        assert!(gates::fredkin(&mut state, 0, 0, 2).is_err());
        assert!(gates::fredkin(&mut state, 0, 1, 1).is_err());
        assert!(gates::fredkin(&mut state, 0, 1, 0).is_err());
    }
    #[test]
    fn test_s_gate() {
        let mut state = QulacsStateVector::new(1).unwrap();
        gates::pauli_x(&mut state, 0).unwrap();
        gates::s(&mut state, 0).unwrap();
        assert!((state.amplitudes()[0].norm() - 0.0).abs() < 1e-10);
        assert!((state.amplitudes()[1].re - 0.0).abs() < 1e-10);
        assert!((state.amplitudes()[1].im - 1.0).abs() < 1e-10);
    }
    #[test]
    fn test_s_dag_gate() {
        let mut state = QulacsStateVector::new(1).unwrap();
        gates::pauli_x(&mut state, 0).unwrap();
        gates::sdg(&mut state, 0).unwrap();
        assert!((state.amplitudes()[0].norm() - 0.0).abs() < 1e-10);
        assert!((state.amplitudes()[1].re - 0.0).abs() < 1e-10);
        assert!((state.amplitudes()[1].im + 1.0).abs() < 1e-10);
    }
    #[test]
    fn test_s_s_dag_identity() {
        let mut state = QulacsStateVector::new(1).unwrap();
        gates::hadamard(&mut state, 0).unwrap();
        let original = state.clone();
        gates::s(&mut state, 0).unwrap();
        gates::sdg(&mut state, 0).unwrap();
        for i in 0..2 {
            let diff = (state.amplitudes()[i] - original.amplitudes()[i]).norm();
            assert!(diff < 1e-10);
        }
    }
    #[test]
    fn test_t_gate() {
        let mut state = QulacsStateVector::new(1).unwrap();
        gates::pauli_x(&mut state, 0).unwrap();
        gates::t(&mut state, 0).unwrap();
        let expected = Complex64::from_polar(1.0, std::f64::consts::FRAC_PI_4);
        assert!((state.amplitudes()[0].norm() - 0.0).abs() < 1e-10);
        assert!((state.amplitudes()[1].re - expected.re).abs() < 1e-10);
        assert!((state.amplitudes()[1].im - expected.im).abs() < 1e-10);
    }
    #[test]
    fn test_t_dag_gate() {
        let mut state = QulacsStateVector::new(1).unwrap();
        gates::pauli_x(&mut state, 0).unwrap();
        gates::tdg(&mut state, 0).unwrap();
        let expected = Complex64::from_polar(1.0, -std::f64::consts::FRAC_PI_4);
        assert!((state.amplitudes()[0].norm() - 0.0).abs() < 1e-10);
        assert!((state.amplitudes()[1].re - expected.re).abs() < 1e-10);
        assert!((state.amplitudes()[1].im - expected.im).abs() < 1e-10);
    }
    #[test]
    fn test_t_t_dag_identity() {
        let mut state = QulacsStateVector::new(1).unwrap();
        gates::hadamard(&mut state, 0).unwrap();
        let original = state.clone();
        gates::t(&mut state, 0).unwrap();
        gates::tdg(&mut state, 0).unwrap();
        for i in 0..2 {
            let diff = (state.amplitudes()[i] - original.amplitudes()[i]).norm();
            assert!(diff < 1e-10);
        }
    }
    #[test]
    fn test_s_equals_two_t() {
        let mut state1 = QulacsStateVector::new(1).unwrap();
        let mut state2 = QulacsStateVector::new(1).unwrap();
        gates::hadamard(&mut state1, 0).unwrap();
        gates::hadamard(&mut state2, 0).unwrap();
        gates::s(&mut state1, 0).unwrap();
        gates::t(&mut state2, 0).unwrap();
        gates::t(&mut state2, 0).unwrap();
        for i in 0..2 {
            let diff = (state1.amplitudes()[i] - state2.amplitudes()[i]).norm();
            assert!(diff < 1e-10);
        }
    }
    #[test]
    fn test_pauli_operator_matrices() {
        use observable::PauliOperator;
        let i_mat = PauliOperator::I.matrix();
        assert_eq!(i_mat[[0, 0]], Complex64::new(1.0, 0.0));
        assert_eq!(i_mat[[1, 1]], Complex64::new(1.0, 0.0));
        let x_mat = PauliOperator::X.matrix();
        assert_eq!(x_mat[[0, 1]], Complex64::new(1.0, 0.0));
        assert_eq!(x_mat[[1, 0]], Complex64::new(1.0, 0.0));
        let z_mat = PauliOperator::Z.matrix();
        assert_eq!(z_mat[[0, 0]], Complex64::new(1.0, 0.0));
        assert_eq!(z_mat[[1, 1]], Complex64::new(-1.0, 0.0));
    }
    #[test]
    fn test_pauli_observable_creation() {
        use observable::PauliObservable;
        let obs_z = PauliObservable::pauli_z(&[0]);
        assert_eq!(obs_z.coefficient, 1.0);
        assert_eq!(obs_z.operators.len(), 1);
        let obs_x = PauliObservable::pauli_x(&[0, 1]);
        assert_eq!(obs_x.operators.len(), 2);
    }
    #[test]
    fn test_pauli_z_expectation_value() {
        let state = QulacsStateVector::new(1).unwrap();
        let obs = PauliObservable::pauli_z(&[0]);
        let exp_val = obs.expectation_value(&state);
        assert!((exp_val - 1.0).abs() < 1e-10);
    }
    #[test]
    fn test_pauli_z_expectation_value_excited() {
        let mut state = QulacsStateVector::new(1).unwrap();
        gates::pauli_x(&mut state, 0).unwrap();
        let obs = PauliObservable::pauli_z(&[0]);
        let exp_val = obs.expectation_value(&state);
        assert!((exp_val - (-1.0)).abs() < 1e-10);
    }
    #[test]
    fn test_pauli_z_expectation_value_superposition() {
        let mut state = QulacsStateVector::new(1).unwrap();
        gates::hadamard(&mut state, 0).unwrap();
        let obs = PauliObservable::pauli_z(&[0]);
        let exp_val = obs.expectation_value(&state);
        assert!(exp_val.abs() < 1e-10);
    }
    #[test]
    fn test_hermitian_observable() {
        use observable::HermitianObservable;
        let mut matrix = Array2::zeros((2, 2));
        matrix[[0, 0]] = Complex64::new(1.0, 0.0);
        matrix[[1, 1]] = Complex64::new(-1.0, 0.0);
        let obs = HermitianObservable::new(matrix).unwrap();
        assert_eq!(obs.num_qubits, 1);
        let state = QulacsStateVector::new(1).unwrap();
        let exp_val = obs.expectation_value(&state).unwrap();
        assert!((exp_val - 1.0).abs() < 1e-10);
    }
    #[test]
    fn test_composite_observable() {
        use observable::{CompositeObservable, PauliObservable};
        let obs1 = PauliObservable::pauli_z(&[0]).with_coefficient(0.5);
        let obs2 = PauliObservable::pauli_z(&[1]).with_coefficient(0.3);
        let composite = CompositeObservable::new().add_term(obs1).add_term(obs2);
        assert_eq!(composite.num_terms(), 2);
        let state = QulacsStateVector::new(2).unwrap();
        let exp_val = composite.expectation_value(&state);
        assert!((exp_val - 0.8).abs() < 1e-10);
    }
    #[test]
    fn test_observable_weight() {
        use observable::{PauliObservable, PauliOperator};
        use std::collections::HashMap;
        let mut operators = HashMap::new();
        operators.insert(0, PauliOperator::X);
        operators.insert(1, PauliOperator::Y);
        operators.insert(2, PauliOperator::I);
        let obs = PauliObservable::new(operators, 1.0);
        assert_eq!(obs.weight(), 2);
    }
    #[test]
    fn test_circuit_api_basic() {
        use circuit_api::QulacsCircuit;
        let mut circuit = QulacsCircuit::new(2).unwrap();
        assert_eq!(circuit.num_qubits(), 2);
        assert_eq!(circuit.gate_count(), 0);
        circuit.h(0).x(1);
        assert_eq!(circuit.gate_count(), 2);
    }
    #[test]
    fn test_circuit_api_bell_state() {
        let mut circuit = QulacsCircuit::new(2).unwrap();
        circuit.bell_pair(0, 1);
        assert_eq!(circuit.gate_count(), 2);
        let probs = circuit.probabilities();
        assert!((probs[0] - 0.5).abs() < 1e-10);
        assert!(probs[1].abs() < 1e-10);
        assert!(probs[2].abs() < 1e-10);
        assert!((probs[3] - 0.5).abs() < 1e-10);
    }
    #[test]
    fn test_circuit_api_run_shots() {
        let mut circuit = QulacsCircuit::new(2).unwrap();
        circuit.bell_pair(0, 1);
        let counts = circuit.run(100).unwrap();
        assert!(counts.contains_key("00") || counts.contains_key("11"));
        let total: usize = counts.values().sum();
        assert_eq!(total, 100);
    }
    #[test]
    fn test_circuit_api_reset() {
        let mut circuit = QulacsCircuit::new(2).unwrap();
        circuit.h(0).cnot(0, 1);
        assert_eq!(circuit.gate_count(), 2);
        circuit.reset().unwrap();
        assert_eq!(circuit.gate_count(), 0);
        let probs = circuit.probabilities();
        assert!((probs[0] - 1.0).abs() < 1e-10);
    }
    #[test]
    fn test_circuit_api_rotation_gates() {
        let mut circuit = QulacsCircuit::new(1).unwrap();
        circuit.rx(0, PI);
        let probs = circuit.probabilities();
        assert!(probs[0].abs() < 1e-10);
        assert!((probs[1] - 1.0).abs() < 1e-10);
    }
    #[test]
    fn test_circuit_api_phase_gates() {
        let mut circuit = QulacsCircuit::new(1).unwrap();
        circuit.s(0).s(0);
        assert_eq!(circuit.gate_count(), 2);
        let probs = circuit.probabilities();
        assert!((probs[0] - 1.0).abs() < 1e-10);
    }
    #[test]
    fn test_circuit_api_two_qubit_gates() {
        let mut circuit = QulacsCircuit::new(2).unwrap();
        circuit.x(0).cnot(0, 1);
        let probs = circuit.probabilities();
        assert!(probs[0].abs() < 1e-10);
        assert!(probs[1].abs() < 1e-10);
        assert!(probs[2].abs() < 1e-10);
        assert!((probs[3] - 1.0).abs() < 1e-10);
    }
    #[test]
    fn test_circuit_api_observable() {
        use circuit_api::{Observable, QulacsCircuit};
        let mut circuit = QulacsCircuit::new(2).unwrap();
        circuit.h(0).h(1);
        let obs = PauliObservable::pauli_z(&[0]);
        let exp_val = circuit.expectation(&obs).unwrap();
        assert!(exp_val.abs() < 1e-10);
    }
    #[test]
    fn test_circuit_api_gate_record() {
        let mut circuit = QulacsCircuit::new(2).unwrap();
        circuit.h(0).cnot(0, 1).rx(1, 1.5);
        let gates = circuit.gates();
        assert_eq!(gates.len(), 3);
        assert_eq!(gates[0].name, "H");
        assert_eq!(gates[0].qubits, vec![0]);
        assert_eq!(gates[1].name, "CNOT");
        assert_eq!(gates[1].qubits, vec![0, 1]);
        assert_eq!(gates[2].name, "RX");
        assert_eq!(gates[2].params[0], 1.5);
    }
    #[test]
    fn test_circuit_api_noise_model() {
        use crate::noise_models::{DepolarizingNoise, NoiseModel as KrausNoiseModel};
        use std::sync::Arc;
        let mut circuit = QulacsCircuit::new(2).unwrap();
        assert!(!circuit.has_noise_model());
        let mut noise_model = KrausNoiseModel::new();
        noise_model.add_channel(Arc::new(DepolarizingNoise::new(0.01)));
        circuit.set_noise_model(noise_model);
        assert!(circuit.has_noise_model());
        circuit.h(0).cnot(0, 1);
        assert_eq!(circuit.gate_count(), 2);
        circuit.clear_noise_model();
        assert!(!circuit.has_noise_model());
    }
    #[test]
    fn test_circuit_api_run_with_noise() {
        use crate::noise_models::{BitFlipNoise, NoiseModel as KrausNoiseModel};
        let mut circuit = QulacsCircuit::new(1).unwrap();
        let mut noise_model = KrausNoiseModel::new();
        noise_model.add_channel(Arc::new(BitFlipNoise::new(0.1)));
        circuit.set_noise_model(noise_model);
        let counts = circuit.run_with_noise(100).unwrap();
        let total: usize = counts.values().sum();
        assert_eq!(total, 100);
        assert!(counts.contains_key("0"));
    }
}