cqlib-core 0.1.0-beta.5

The high-performance core foundation for the CQLib quantum computing ecosystem.
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
// This code is part of Cqlib.
//
// (C) Copyright China Telecom Quantum Group 2026
//
// This code is licensed under the Apache License, Version 2.0. You may
// obtain a copy of this license in the LICENSE.txt file in the root directory
// of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
//
// Any modifications or derivative works of this code must retain this
// copyright notice, and modified files need to carry a notice indicating
// that they have been altered from the originals.

use super::*;
use crate::circuit::circuit_to_matrix;
use crate::circuit::gate::StandardGate;
use crate::circuit::{Circuit, Instruction};
use crate::qis::Hamiltonian;
use crate::qis::evolution::TrotterMode;
use crate::qis::pauli::PauliString;
use approx::assert_abs_diff_eq;
use ndarray::array;
use num_complex::Complex64;
use std::f64::consts::PI;

#[test]
fn test_pauli_evolution_x() {
    let mut circuit = Circuit::new(1);
    let qubits = circuit.qubits();

    // X rotation should be equivalent to RX
    let pauli: PauliString = "X".parse().unwrap();
    circuit
        .pauli_evolution(&pauli, std::f64::consts::PI, &qubits)
        .unwrap();

    // Check that we have the right gates: H - RZ - H
    let ops = circuit.operations();
    assert_eq!(ops.len(), 3);
}

#[test]
fn test_pauli_evolution_z() {
    let mut circuit = Circuit::new(1);
    let qubits = circuit.qubits();

    // Z rotation should be just RZ
    let pauli: PauliString = "Z".parse().unwrap();
    circuit
        .pauli_evolution(&pauli, std::f64::consts::PI, &qubits)
        .unwrap();

    // Check that we have just one gate: RZ
    let ops = circuit.operations();
    assert_eq!(ops.len(), 1);
}

#[test]
fn test_pauli_evolution_multi_qubit() {
    let mut circuit = Circuit::new(2);
    let qubits = circuit.qubits();

    // XX evolution: e^(-iθ/2 * X⊗X)
    // Chain structure: H(q0), H(q1), CNOT(0->1), RZ(1), CNOT(0->1), H(q0), H(q1)
    let pauli: PauliString = "XX".parse().unwrap();
    circuit
        .pauli_evolution(&pauli, std::f64::consts::PI / 2.0, &qubits)
        .unwrap();

    // Gate sequence: H - H - CNOT(0->1) - RZ(1) - CNOT(0->1) - H - H
    let ops = circuit.operations();

    // Check total gate count: 2 H (X->Z) + 2 CNOTs + 1 RZ + 2 H (reverse)
    assert_eq!(
        ops.len(),
        7,
        "Expected 7 gates for XX evolution: H, H, CNOT, RZ, CNOT, H, H"
    );

    // Verify the structure
    // First two should be H gates (basis transformation for X)
    assert!(matches!(
        ops[0].instruction,
        Instruction::Standard(StandardGate::H)
    ));
    assert!(matches!(
        ops[1].instruction,
        Instruction::Standard(StandardGate::H)
    ));

    // Third should be CNOT
    assert!(matches!(
        ops[2].instruction,
        Instruction::Standard(StandardGate::CX)
    ));

    // Fourth should be RZ on last qubit (qubit 1)
    assert!(matches!(
        ops[3].instruction,
        Instruction::Standard(StandardGate::RZ)
    ));
    assert_eq!(ops[3].qubits[0], qubits[1]);

    // Fifth should be CNOT (reverse chain)
    assert!(matches!(
        ops[4].instruction,
        Instruction::Standard(StandardGate::CX)
    ));
    assert_eq!(ops[4].qubits[0], qubits[0]); // control
    assert_eq!(ops[4].qubits[1], qubits[1]); // target

    // Last two should be H gates (reverse basis)
    assert!(matches!(
        ops[5].instruction,
        Instruction::Standard(StandardGate::H)
    ));
    assert!(matches!(
        ops[6].instruction,
        Instruction::Standard(StandardGate::H)
    ));
}

#[test]
fn test_pauli_evolution_identity() {
    let mut circuit = Circuit::new(2);
    let qubits = circuit.qubits();

    // Identity evolution should add global phase
    let pauli: PauliString = "II".parse().unwrap();
    circuit
        .pauli_evolution(&pauli, std::f64::consts::PI, &qubits)
        .unwrap();

    // No gates should be added, only global phase
    let ops = circuit.operations();
    assert_eq!(ops.len(), 0);
}

#[test]
fn test_pauli_evolution_qubit_mismatch() {
    let mut circuit = Circuit::new(2);
    let qubits = circuit.qubits();

    // Mismatched qubit count
    let pauli: PauliString = "XXX".parse().unwrap();
    let result = circuit.pauli_evolution(&pauli, 1.0, &qubits);

    assert!(result.is_err());
}

#[test]
fn test_pauli_evolution_with_phase() {
    let mut circuit = Circuit::new(1);
    let qubits = circuit.qubits();

    // Pauli string with -1 phase
    let pauli: PauliString = "-X".parse().unwrap();
    circuit
        .pauli_evolution(&pauli, std::f64::consts::PI, &qubits)
        .unwrap();

    // The phase should flip the sign of the angle
    // So e^(-i * π/2 * (-X)) = e^(i * π/2 * X)
    // This should work without error
    let ops = circuit.operations();
    assert_eq!(ops.len(), 3);
}

#[test]
fn test_pauli_evolution_y() {
    let mut circuit = Circuit::new(1);
    let qubits = circuit.qubits();

    // Y rotation: S† - H - RZ - H - S
    let pauli: PauliString = "Y".parse().unwrap();
    circuit
        .pauli_evolution(&pauli, std::f64::consts::PI / 2.0, &qubits)
        .unwrap();

    let ops = circuit.operations();
    assert_eq!(ops.len(), 5);
}

#[test]
fn test_pauli_evolution_mixed() {
    let mut circuit = Circuit::new(3);
    let qubits = circuit.qubits();

    // XZY evolution
    let pauli: PauliString = "XZY".parse().unwrap();
    circuit.pauli_evolution(&pauli, 1.0, &qubits).unwrap();

    // Structure: basis_transforms + cnot_ladder + rz + reverse_cnot_ladder + reverse_basis
    // X -> H, Z -> nothing, Y -> H·S†
    // CNOT ladder: CNOT(2->0), CNOT(1->0) assuming 0 is first non-I
    let ops = circuit.operations();

    // Should have gates
    assert!(
        ops.len() >= 7,
        "Expected at least 7 gates for 3-qubit XZY evolution"
    );

    // Verify we have CNOTs in the circuit
    let cnot_count = ops
        .iter()
        .filter(|op| {
            matches!(
                op.instruction,
                crate::circuit::Instruction::Standard(StandardGate::CX)
            )
        })
        .count();
    assert!(
        cnot_count >= 2,
        "Expected at least 2 CNOTs for 3-qubit evolution, got {}",
        cnot_count
    );
}

#[test]
fn test_pauli_evolution_zz_two_qubit() {
    let mut circuit = Circuit::new(2);
    let qubits = circuit.qubits();

    // ZZ evolution: e^(-iθ/2 * Z⊗Z)
    // Chain structure: CNOT(0->1) - RZ(1) - CNOT(0->1)
    let pauli: PauliString = "ZZ".parse().unwrap();
    circuit
        .pauli_evolution(&pauli, std::f64::consts::PI / 2.0, &qubits)
        .unwrap();

    let ops = circuit.operations();
    assert_eq!(
        ops.len(),
        3,
        "Expected 3 gates for ZZ evolution: CNOT, RZ, CNOT"
    );

    // Verify chain structure: CNOT(q0,q1), RZ(q1), CNOT(q0,q1)
    assert!(matches!(
        ops[0].instruction,
        Instruction::Standard(StandardGate::CX)
    ));
    assert_eq!(ops[0].qubits[0], qubits[0]); // control
    assert_eq!(ops[0].qubits[1], qubits[1]); // target

    assert!(matches!(
        ops[1].instruction,
        Instruction::Standard(StandardGate::RZ)
    ));
    assert_eq!(ops[1].qubits[0], qubits[1]); // RZ on last qubit

    assert!(matches!(
        ops[2].instruction,
        Instruction::Standard(StandardGate::CX)
    ));
    assert_eq!(ops[2].qubits[0], qubits[0]); // control
    assert_eq!(ops[2].qubits[1], qubits[1]); // target
}

#[test]
fn test_pauli_evolution_three_qubit_zzz() {
    let mut circuit = Circuit::new(3);
    let qubits = circuit.qubits();

    // ZZZ evolution: e^(-iθ/2 * Z⊗Z⊗Z)
    // Chain structure: CNOT(0->1) - CNOT(1->2) - RZ(2) - CNOT(1->2) - CNOT(0->1)
    let pauli: PauliString = "ZZZ".parse().unwrap();
    circuit.pauli_evolution(&pauli, 1.0, &qubits).unwrap();

    let ops = circuit.operations();
    assert_eq!(
        ops.len(),
        5,
        "Expected 5 gates for ZZZ evolution: 2 CNOTs + RZ + 2 CNOTs"
    );

    // Verify chain structure: CNOT(q0,q1), CNOT(q1,q2), RZ(q2), CNOT(q1,q2), CNOT(q0,q1)
    assert!(matches!(
        ops[0].instruction,
        Instruction::Standard(StandardGate::CX)
    ));
    assert_eq!(ops[0].qubits[0], qubits[0]); // control
    assert_eq!(ops[0].qubits[1], qubits[1]); // target

    assert!(matches!(
        ops[1].instruction,
        Instruction::Standard(StandardGate::CX)
    ));
    assert_eq!(ops[1].qubits[0], qubits[1]); // control
    assert_eq!(ops[1].qubits[1], qubits[2]); // target

    assert!(matches!(
        ops[2].instruction,
        Instruction::Standard(StandardGate::RZ)
    ));
    assert_eq!(ops[2].qubits[0], qubits[2]); // RZ on last qubit

    assert!(matches!(
        ops[3].instruction,
        Instruction::Standard(StandardGate::CX)
    ));
    assert_eq!(ops[3].qubits[0], qubits[1]); // control
    assert_eq!(ops[3].qubits[1], qubits[2]); // target

    assert!(matches!(
        ops[4].instruction,
        Instruction::Standard(StandardGate::CX)
    ));
    assert_eq!(ops[4].qubits[0], qubits[0]); // control
    assert_eq!(ops[4].qubits[1], qubits[1]); // target
}

#[test]
fn test_trotter_first_order_basic() {
    // H = 0.5 * ZZ
    let mut h = Hamiltonian::new(2);
    let pauli: PauliString = "ZZ".parse().unwrap();
    h.add_term(pauli, 0.5.into()).unwrap();

    // Create Trotter circuit: t=1.0, steps=2
    let circuit = h
        .to_trotter_circuit(1.0, 2, TrotterMode::FirstOrder)
        .unwrap();

    // Should have 2 steps * (CNOT + RZ + CNOT) = 6 gates
    let ops = circuit.operations();
    assert_eq!(ops.len(), 6, "Expected 6 gates for 2-step ZZ Trotter");
}

#[test]
fn test_trotter_two_terms() {
    // H = 0.5 * ZZ + 0.3 * XX
    let mut h = Hamiltonian::new(2);
    h.add_term("ZZ".parse().unwrap(), 0.5.into()).unwrap();
    h.add_term("XX".parse().unwrap(), 0.3.into()).unwrap();

    // Create Trotter circuit: t=1.0, steps=1
    let circuit = h
        .to_trotter_circuit(1.0, 1, TrotterMode::FirstOrder)
        .unwrap();

    // For ZZ: CNOT-RZ-CNOT (3 gates)
    // For XX: H-H-CNOT-RZ-CNOT-H-H (7 gates)
    // Total: 10 gates
    let ops = circuit.operations();
    assert_eq!(ops.len(), 10, "Expected 10 gates for 1-step ZZ+XX Trotter");
}

#[test]
fn test_trotter_second_order() {
    // H = 0.5 * ZZ
    let mut h = Hamiltonian::new(2);
    h.add_term("ZZ".parse().unwrap(), 0.5.into()).unwrap();

    // Create 2nd-order Trotter circuit: t=1.0, steps=1
    let circuit = h
        .to_trotter_circuit(1.0, 1, TrotterMode::SecondOrder)
        .unwrap();

    // Second order: forward half + backward half
    // ZZ forward half: CNOT-RZ_half-CNOT (3 gates)
    // ZZ backward half: CNOT-RZ_half-CNOT (3 gates)
    // Total: 6 gates
    let ops = circuit.operations();
    assert_eq!(
        ops.len(),
        6,
        "Expected 6 gates for 1-step 2nd-order Trotter"
    );
}

#[test]
fn test_trotter_empty_hamiltonian_error() {
    let h = Hamiltonian::new(2);

    let result = h.to_trotter_circuit(1.0, 1, TrotterMode::FirstOrder);
    assert!(result.is_err(), "Should error on empty Hamiltonian");
}

#[test]
fn test_trotter_zero_steps_error() {
    let mut h = Hamiltonian::new(2);
    h.add_term("ZZ".parse().unwrap(), 0.5.into()).unwrap();

    let result = h.to_trotter_circuit(1.0, 0, TrotterMode::FirstOrder);
    assert!(result.is_err(), "Should error on zero steps");
}

#[test]
fn test_trotter_scale_factor_overflow_returns_error() {
    let mut h = Hamiltonian::new(1);
    h.add_term("Z".parse().unwrap(), f64::MAX.into()).unwrap();

    for mode in [TrotterMode::FirstOrder, TrotterMode::Randomized(42)] {
        // All inputs are finite, but doubling the coefficient overflows.
        let result = h.to_trotter_circuit(0.25, 2, mode);
        assert!(matches!(result, Err(QisError::UnsupportedOperation(_))));
    }
}

#[test]
fn test_trotter_merged_coefficient_overflow_returns_error() {
    let mut h = Hamiltonian::new(1);
    for _ in 0..2 {
        h.add_term("Z".parse().unwrap(), f64::MAX.into()).unwrap();
    }

    for mode in [
        TrotterMode::FirstOrder,
        TrotterMode::SecondOrder,
        TrotterMode::Randomized(42),
    ] {
        // Simplification can overflow even before the scale factor is computed.
        let result = h.to_trotter_circuit(0.25, 2, mode);
        assert!(matches!(result, Err(QisError::UnsupportedOperation(_))));
    }
}

#[test]
fn test_trotter_randomized() {
    // H = 0.5 * ZZ + 0.3 * XX
    let mut h = Hamiltonian::new(2);
    h.add_term("ZZ".parse().unwrap(), 0.5.into()).unwrap();
    h.add_term("XX".parse().unwrap(), 0.3.into()).unwrap();

    // Create randomized Trotter circuit
    let circuit = h
        .to_trotter_circuit(1.0, 2, TrotterMode::Randomized(42))
        .unwrap();

    // Should have the same number of gates as first order
    // 2 steps * (3 for ZZ + 7 for XX) = 20 gates
    let ops = circuit.operations();
    assert_eq!(
        ops.len(),
        20,
        "Expected 20 gates for 2-step randomized Trotter"
    );
}

#[test]
fn test_trotter_multiple_steps() {
    // H = 0.5 * Z
    let mut h = Hamiltonian::new(1);
    h.add_term("Z".parse().unwrap(), 0.5.into()).unwrap();

    // 5 steps
    let circuit = h
        .to_trotter_circuit(1.0, 5, TrotterMode::FirstOrder)
        .unwrap();

    // Each step has 1 RZ gate
    let ops = circuit.operations();
    assert_eq!(
        ops.len(),
        5,
        "Expected 5 RZ gates for 5-step single-qubit Trotter"
    );
}

#[test]
fn test_trotter_circuit_num_qubits() {
    // H on 3 qubits
    let mut h = Hamiltonian::new(3);
    h.add_term("ZZZ".parse().unwrap(), 1.0.into()).unwrap();

    let circuit = h
        .to_trotter_circuit(1.0, 1, TrotterMode::FirstOrder)
        .unwrap();

    assert_eq!(
        circuit.num_qubits(),
        3,
        "Circuit should have same qubits as Hamiltonian"
    );
}

/// Test that Trotter evolution direction is correct (e^{-iHt}, not e^{+iHt})
///
/// For H = Z, U(t) = e^{-i Z t}.
/// When applied to |0⟩, Z|0⟩ = +|0⟩, so U(t)|0⟩ = e^{-it}|0⟩.
/// The phase should be e^{-it} (clockwise rotation in Bloch sphere).
///
/// The pauli_evolution angle should be θ = 2*c*t (positive for positive c and t).
#[test]
fn test_trotter_time_evolution_direction() {
    // H = 0.5 * Z
    let mut h = Hamiltonian::new(1);
    h.add_term("Z".parse().unwrap(), 0.5.into()).unwrap();

    // Time t = 2.0, steps = 1
    // Expected: θ = 2 * c * t = 2 * 0.5 * 2.0 = 2.0
    // Bug: θ = -2 * c * t = -2.0 would give wrong direction
    let circuit = h
        .to_trotter_circuit(2.0, 1, TrotterMode::FirstOrder)
        .unwrap();

    let ops = circuit.operations();
    assert_eq!(ops.len(), 1);

    // Check RZ gate has positive angle
    if let Instruction::Standard(StandardGate::RZ) = ops[0].instruction {
        // Get the parameter - should be Fixed(2.0) not Fixed(-2.0)
        match &ops[0].params[0] {
            crate::circuit::CircuitParam::Fixed(val) => {
                // θ = 2 * 0.5 * 2.0 = 2.0
                assert!(
                    *val > 0.0,
                    "RZ angle should be positive for positive coefficient and time, got {}",
                    val
                );
                assert!(
                    (val - 2.0).abs() < 1e-10,
                    "RZ angle should be 2.0, got {}",
                    val
                );
            }
            _ => panic!("Expected Fixed parameter for RZ angle"),
        }
    } else {
        panic!("Expected RZ gate");
    }
}

/// Test that negative time gives opposite angle (time reversal)
#[test]
fn test_trotter_negative_time() {
    // H = 0.5 * Z
    let mut h = Hamiltonian::new(1);
    h.add_term("Z".parse().unwrap(), 0.5.into()).unwrap();

    // Time t = -1.0 (backward evolution)
    // Expected: θ = 2 * c * t = 2 * 0.5 * (-1.0) = -1.0
    let circuit = h
        .to_trotter_circuit(-1.0, 1, TrotterMode::FirstOrder)
        .unwrap();

    let ops = circuit.operations();
    if let Instruction::Standard(StandardGate::RZ) = ops[0].instruction {
        match &ops[0].params[0] {
            crate::circuit::CircuitParam::Fixed(val) => {
                assert!(*val < 0.0, "RZ angle should be negative for negative time");
                assert!(
                    (val + 1.0).abs() < 1e-10,
                    "RZ angle should be -1.0, got {}",
                    val
                );
            }
            _ => panic!("Expected Fixed parameter"),
        }
    }
}

/// Assert that two complex matrices are approximately equal
fn assert_matrix_approx_eq(
    actual: &ndarray::Array2<num_complex::Complex64>,
    expected: &ndarray::Array2<num_complex::Complex64>,
    eps: f64,
) {
    assert_eq!(
        actual.shape(),
        expected.shape(),
        "Matrix shapes differ: {:?} vs {:?}",
        actual.shape(),
        expected.shape()
    );
    for (a, e) in actual.iter().zip(expected.iter()) {
        if (a - e).norm() > eps {
            panic!(
                "Matrices differ.\nActual:\n{:?}\nExpected:\n{:?}",
                actual, expected
            );
        }
    }
}

#[test]
fn test_trotter_matrix_equivalence_z() {
    // H = 0.5 * Z
    let mut h = Hamiltonian::new(1);
    h.add_term("Z".parse().unwrap(), 0.5.into()).unwrap();

    let circuit = h
        .to_trotter_circuit(1.0, 1, TrotterMode::FirstOrder)
        .unwrap();
    let matrix = circuit.to_matrix(None).unwrap();

    let c = |re: f64, im: f64| Complex64::new(re, im);

    // U = e^{-i * 0.5 * Z} = [[e^{-0.5i}, 0], [0, e^{0.5i}]]
    let expected = array![
        [c(0.5_f64.cos(), -0.5_f64.sin()), c(0.0, 0.0)],
        [c(0.0, 0.0), c(0.5_f64.cos(), 0.5_f64.sin())],
    ];

    assert_matrix_approx_eq(&matrix, &expected, 1e-10);
}

#[test]
fn test_trotter_matrix_equivalence_xx() {
    // H = 0.5 * XX
    let mut h = Hamiltonian::new(2);
    h.add_term("XX".parse().unwrap(), 0.5.into()).unwrap();

    let circuit = h
        .to_trotter_circuit(1.0, 1, TrotterMode::FirstOrder)
        .unwrap();
    let matrix = circuit.to_matrix(None).unwrap();

    let c = |re: f64, im: f64| Complex64::new(re, im);

    // U = e^{-i * 0.5 * XX} = cos(0.5)I - i sin(0.5)XX
    let cos_val = 0.5_f64.cos();
    let sin_val = 0.5_f64.sin();

    let expected = array![
        [c(cos_val, 0.0), c(0.0, 0.0), c(0.0, 0.0), c(0.0, -sin_val)],
        [c(0.0, 0.0), c(cos_val, 0.0), c(0.0, -sin_val), c(0.0, 0.0)],
        [c(0.0, 0.0), c(0.0, -sin_val), c(cos_val, 0.0), c(0.0, 0.0)],
        [c(0.0, -sin_val), c(0.0, 0.0), c(0.0, 0.0), c(cos_val, 0.0)],
    ];

    assert_matrix_approx_eq(&matrix, &expected, 1e-10);
}

#[test]
fn test_evolution_circuit_exact_for_commuting_hamiltonian() {
    // ZZ and ZI commute → exact path should be taken (1 gate per term, no Trotter steps)
    let mut h = Hamiltonian::new(2);
    h.add_term("ZZ".parse::<PauliString>().unwrap(), 0.5.into())
        .unwrap();
    h.add_term("ZI".parse::<PauliString>().unwrap(), 0.3.into())
        .unwrap();

    // steps=1 is irrelevant for commuting H; mode is also irrelevant
    let circuit = h
        .to_evolution_circuit(1.0, 1, TrotterMode::FirstOrder)
        .unwrap();

    // Exact path: one pauli_evolution per term (ZZ → 1 gate, ZI → 1 gate = 2 RZ + 2 CNOT chain)
    // The number of ops is determined by the circuit structure; we just check it builds without error.
    assert_eq!(circuit.num_qubits(), 2);
}

#[test]
fn test_evolution_circuit_exact_matches_to_trotter_circuit_for_commuting() {
    // For commuting H, to_evolution_circuit should produce the same matrix as
    // to_trotter_circuit with 1 step (which for commuting H is also exact).
    let mut h = Hamiltonian::new(1);
    h.add_term("Z".parse::<PauliString>().unwrap(), 0.7.into())
        .unwrap();

    let t = 0.4_f64;
    let exact_circ = h
        .to_evolution_circuit(t, 1, TrotterMode::FirstOrder)
        .unwrap();
    let trotter_circ = h.to_trotter_circuit(t, 1, TrotterMode::FirstOrder).unwrap();

    let m_exact = circuit_to_matrix(&exact_circ, None).unwrap();
    let m_trotter = circuit_to_matrix(&trotter_circ, None).unwrap();

    // Both should give the same 2×2 matrix
    assert_eq!(m_exact.shape(), m_trotter.shape());
    for (a, b) in m_exact.iter().zip(m_trotter.iter()) {
        assert_abs_diff_eq!(a.re, b.re, epsilon = 1e-12);
        assert_abs_diff_eq!(a.im, b.im, epsilon = 1e-12);
    }
}

#[test]
fn test_evolution_circuit_noncommuting_uses_trotter() {
    // X and Z do not commute → falls back to Trotter
    let mut h = Hamiltonian::new(1);
    h.add_term("X".parse::<PauliString>().unwrap(), 1.0.into())
        .unwrap();
    h.add_term("Z".parse::<PauliString>().unwrap(), 1.0.into())
        .unwrap();

    let circuit_trotter = h
        .to_evolution_circuit(0.5, 10, TrotterMode::FirstOrder)
        .unwrap();
    let circuit_direct = h
        .to_trotter_circuit(0.5, 10, TrotterMode::FirstOrder)
        .unwrap();

    // Both should produce the same gate count since the same logic is used
    assert_eq!(
        circuit_trotter.operations().len(),
        circuit_direct.operations().len()
    );
}

#[test]
fn test_evolution_circuit_rejects_empty_hamiltonian() {
    let h = Hamiltonian::new(1);
    assert!(
        h.to_evolution_circuit(1.0, 1, TrotterMode::FirstOrder)
            .is_err()
    );
}

#[test]
fn test_evolution_circuit_rejects_zero_steps() {
    let mut h = Hamiltonian::new(1);
    h.add_term("X".parse::<PauliString>().unwrap(), 1.0.into())
        .unwrap();
    assert!(
        h.to_evolution_circuit(1.0, 0, TrotterMode::FirstOrder)
            .is_err()
    );
}

#[test]
fn test_evolution_circuit_exact_matrix_correctness() {
    // H = 0.5*Z, t = π  →  e^{-i*0.5*π*Z} = [[e^{-iπ/2}, 0], [0, e^{iπ/2}]]
    //                                        = [[-i, 0], [0, i]]
    let mut h = Hamiltonian::new(1);
    h.add_term("Z".parse::<PauliString>().unwrap(), 0.5.into())
        .unwrap();

    let circuit = h
        .to_evolution_circuit(PI, 1, TrotterMode::FirstOrder)
        .unwrap();
    let matrix = circuit_to_matrix(&circuit, None).unwrap();

    // Expected: diagonal [e^{-i π/2}, e^{i π/2}] = [-i, i]
    assert_abs_diff_eq!(matrix[[0, 0]].re, 0.0, epsilon = 1e-12);
    assert_abs_diff_eq!(matrix[[0, 0]].im, -1.0, epsilon = 1e-12);
    assert_abs_diff_eq!(matrix[[1, 1]].re, 0.0, epsilon = 1e-12);
    assert_abs_diff_eq!(matrix[[1, 1]].im, 1.0, epsilon = 1e-12);
    assert_abs_diff_eq!(matrix[[0, 1]].re, 0.0, epsilon = 1e-12);
    assert_abs_diff_eq!(matrix[[1, 0]].re, 0.0, epsilon = 1e-12);
}

#[test]
fn evolution_retains_small_coefficients_before_time_scaling() {
    for coefficients in [vec![1e-11], vec![2e-11, -1e-11], vec![1e-11, -1e-11]] {
        let mut h = Hamiltonian::new(1);
        for &c in &coefficients {
            h.add_term("Z".parse().unwrap(), c.into()).unwrap();
        }
        let angle = coefficients.iter().sum::<f64>() * 1e11;
        for mode in [TrotterMode::FirstOrder, TrotterMode::SecondOrder] {
            for circuit in [
                h.to_trotter_circuit(1e11, 3, mode).unwrap(),
                h.to_evolution_circuit(1e11, 3, mode).unwrap(),
            ] {
                let m = circuit_to_matrix(&circuit, None).unwrap();
                assert!((m[[0, 0]] - Complex64::from_polar(1.0, -angle)).norm() < 1e-10);
                assert!((m[[1, 1]] - Complex64::from_polar(1.0, angle)).norm() < 1e-10);
                assert!(m[[0, 1]].norm() < 1e-10 && m[[1, 0]].norm() < 1e-10);
            }
        }
        h.simplify();
        assert!(h.terms.is_empty());
    }
}