numrs2 0.3.2

A Rust implementation inspired by NumPy for numerical computing (NumRS2)
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
//! Quantum Gate Unitarity Verification
//!
//! This module provides functions to verify the unitarity of quantum gate matrices.
//! A matrix U is unitary if U * U† = I, where U† is the conjugate transpose of U.
//!
//! # Overview
//!
//! Unitarity is a fundamental property of quantum gates. Any valid quantum gate
//! must be represented by a unitary matrix, meaning:
//!
//! - U†U = I (preserves inner products)
//! - ||U|ψ⟩|| = |||ψ⟩|| for all states |ψ⟩ (preserves norms)
//! - All eigenvalues have absolute value 1
//!
//! # Functions
//!
//! - [`is_unitary`]: Check whether a gate matrix is unitary within a given tolerance
//! - [`unitarity_error`]: Compute the Frobenius norm of the deviation from unitarity
//! - [`validate_gate_unitarity`]: Validate a gate and return a descriptive error if not unitary
//! - [`validate_all_standard_gates`]: Validate all built-in standard gates
//!
//! # Runtime Validation
//!
//! The module provides a global flag to enable or disable runtime unitarity
//! checks during gate creation. When enabled, custom gate creation functions
//! will verify unitarity before accepting the gate matrix.
//!
//! # Examples
//!
//! ```
//! use numrs2::new_modules::quantum::unitarity::{is_unitary, unitarity_error};
//! use numrs2::new_modules::quantum::gates::hadamard;
//!
//! let h = hadamard::<f64>().unwrap();
//!
//! // Check if the Hadamard gate is unitary
//! assert!(is_unitary(&h, 1e-10).unwrap());
//!
//! // Compute the deviation from unitarity
//! let error = unitarity_error(&h).unwrap();
//! assert!(error < 1e-10);
//! ```
//!
//! # References
//!
//! - Nielsen & Chuang, "Quantum Computation and Quantum Information" (2010), Chapter 2

use crate::array::Array;
use crate::error::{NumRs2Error, Result};
use num_traits::Float;
use scirs2_core::Complex;
use std::fmt::Debug;
use std::sync::atomic::{AtomicBool, Ordering};

/// Global flag to enable or disable runtime unitarity validation.
///
/// When set to `true`, custom gate creation functions will verify that
/// the provided matrix is unitary before accepting it.
///
/// Default: `false` (disabled for performance)
static RUNTIME_UNITARITY_CHECK: AtomicBool = AtomicBool::new(false);

/// Enable runtime unitarity validation for gate creation.
///
/// When enabled, [`create_validated_gate`] will check that matrices
/// satisfy the unitarity condition U†U ≈ I before accepting them.
///
/// # Examples
///
/// ```
/// use numrs2::new_modules::quantum::unitarity::{enable_runtime_validation, is_runtime_validation_enabled};
///
/// enable_runtime_validation();
/// assert!(is_runtime_validation_enabled());
/// ```
pub fn enable_runtime_validation() {
    RUNTIME_UNITARITY_CHECK.store(true, Ordering::SeqCst);
}

/// Disable runtime unitarity validation for gate creation.
///
/// When disabled, gate creation functions will skip the unitarity check
/// for better performance.
///
/// # Examples
///
/// ```
/// use numrs2::new_modules::quantum::unitarity::{disable_runtime_validation, is_runtime_validation_enabled};
///
/// disable_runtime_validation();
/// assert!(!is_runtime_validation_enabled());
/// ```
pub fn disable_runtime_validation() {
    RUNTIME_UNITARITY_CHECK.store(false, Ordering::SeqCst);
}

/// Check whether runtime unitarity validation is currently enabled.
///
/// # Returns
///
/// `true` if runtime validation is enabled, `false` otherwise.
pub fn is_runtime_validation_enabled() -> bool {
    RUNTIME_UNITARITY_CHECK.load(Ordering::SeqCst)
}

/// Check whether a gate matrix is unitary within a given tolerance.
///
/// A matrix U is unitary if U†U ≈ I, where the deviation of each
/// element from the identity matrix is within the specified tolerance.
///
/// # Arguments
///
/// * `gate_matrix` - The square complex matrix to check
/// * `tolerance` - Maximum allowed absolute deviation per element from identity
///
/// # Returns
///
/// * `Ok(true)` if the matrix is unitary within tolerance
/// * `Ok(false)` if the matrix is not unitary
/// * `Err(...)` if the matrix is not square or indexing fails
///
/// # Examples
///
/// ```
/// use numrs2::new_modules::quantum::unitarity::is_unitary;
/// use numrs2::new_modules::quantum::gates::{hadamard, pauli_x};
///
/// let h = hadamard::<f64>().unwrap();
/// assert!(is_unitary(&h, 1e-10).unwrap());
///
/// let x = pauli_x::<f64>().unwrap();
/// assert!(is_unitary(&x, 1e-10).unwrap());
/// ```
pub fn is_unitary<T>(gate_matrix: &Array<Complex<T>>, tolerance: f64) -> Result<bool>
where
    T: Float + Clone + Debug + Into<f64> + From<f64>,
{
    validate_square_matrix(gate_matrix)?;

    let shape = gate_matrix.shape();
    let n = shape[0];

    // Compute U†U and check each element against identity
    for i in 0..n {
        for j in 0..n {
            let mut sum = Complex::new(T::zero(), T::zero());
            for k in 0..n {
                let u_ki = gate_matrix.get(&[k, i]).map_err(|e| {
                    NumRs2Error::ComputationError(format!(
                        "Failed to access gate element [{}, {}]: {}",
                        k, i, e
                    ))
                })?;
                let u_kj = gate_matrix.get(&[k, j]).map_err(|e| {
                    NumRs2Error::ComputationError(format!(
                        "Failed to access gate element [{}, {}]: {}",
                        k, j, e
                    ))
                })?;
                // U†[i,k] * U[k,j] = conj(U[k,i]) * U[k,j]
                sum = sum + u_ki.conj() * u_kj;
            }

            let expected_re = if i == j { T::one() } else { T::zero() };
            let diff_re: f64 = (sum.re - expected_re).abs().into();
            let diff_im: f64 = sum.im.abs().into();

            if diff_re > tolerance || diff_im > tolerance {
                return Ok(false);
            }
        }
    }

    Ok(true)
}

/// Compute the unitarity error of a gate matrix.
///
/// Calculates ||U†U - I||_F, the Frobenius norm of the deviation from unitarity.
/// A perfectly unitary matrix will have an error of 0.0.
///
/// The Frobenius norm is computed as:
///   ||M||_F = sqrt(sum_{i,j} |M_{i,j}|²)
///
/// # Arguments
///
/// * `gate_matrix` - The square complex matrix to evaluate
///
/// # Returns
///
/// * `Ok(f64)` - The Frobenius norm of (U†U - I)
/// * `Err(...)` if the matrix is not square or indexing fails
///
/// # Examples
///
/// ```
/// use numrs2::new_modules::quantum::unitarity::unitarity_error;
/// use numrs2::new_modules::quantum::gates::hadamard;
///
/// let h = hadamard::<f64>().unwrap();
/// let error = unitarity_error(&h).unwrap();
/// assert!(error < 1e-14);
/// ```
pub fn unitarity_error<T>(gate_matrix: &Array<Complex<T>>) -> Result<f64>
where
    T: Float + Clone + Debug + Into<f64> + From<f64>,
{
    validate_square_matrix(gate_matrix)?;

    let shape = gate_matrix.shape();
    let n = shape[0];

    let mut frobenius_sq: f64 = 0.0;

    // Compute ||U†U - I||_F²
    for i in 0..n {
        for j in 0..n {
            let mut sum = Complex::new(T::zero(), T::zero());
            for k in 0..n {
                let u_ki = gate_matrix.get(&[k, i]).map_err(|e| {
                    NumRs2Error::ComputationError(format!(
                        "Failed to access gate element [{}, {}]: {}",
                        k, i, e
                    ))
                })?;
                let u_kj = gate_matrix.get(&[k, j]).map_err(|e| {
                    NumRs2Error::ComputationError(format!(
                        "Failed to access gate element [{}, {}]: {}",
                        k, j, e
                    ))
                })?;
                sum = sum + u_ki.conj() * u_kj;
            }

            // Subtract identity element
            let identity_val = if i == j { T::one() } else { T::zero() };
            let diff_re: f64 = (sum.re - identity_val).into();
            let diff_im: f64 = sum.im.into();

            frobenius_sq += diff_re * diff_re + diff_im * diff_im;
        }
    }

    Ok(frobenius_sq.sqrt())
}

/// Validate a gate matrix for unitarity and return a descriptive error if not unitary.
///
/// This function performs a complete unitarity check and returns a detailed
/// error message if the matrix fails the test.
///
/// # Arguments
///
/// * `gate_matrix` - The square complex matrix to validate
/// * `tolerance` - Maximum allowed Frobenius norm deviation from identity
/// * `gate_name` - Optional name of the gate for error reporting
///
/// # Returns
///
/// * `Ok(())` if the gate is unitary within tolerance
/// * `Err(NumRs2Error::InvalidInput(...))` if the gate is not unitary
///
/// # Examples
///
/// ```
/// use numrs2::new_modules::quantum::unitarity::validate_gate_unitarity;
/// use numrs2::new_modules::quantum::gates::pauli_z;
///
/// let z = pauli_z::<f64>().unwrap();
/// validate_gate_unitarity(&z, 1e-10, Some("Pauli-Z")).unwrap();
/// ```
pub fn validate_gate_unitarity<T>(
    gate_matrix: &Array<Complex<T>>,
    tolerance: f64,
    gate_name: Option<&str>,
) -> Result<()>
where
    T: Float + Clone + Debug + Into<f64> + From<f64>,
{
    let error = unitarity_error(gate_matrix)?;

    if error > tolerance {
        let name = gate_name.unwrap_or("unnamed");
        return Err(NumRs2Error::InvalidInput(format!(
            "Gate '{}' is not unitary: ||U†U - I||_F = {:.6e} (tolerance: {:.6e})",
            name, error, tolerance
        )));
    }

    Ok(())
}

/// Create a validated custom gate with optional runtime unitarity checking.
///
/// If runtime validation is enabled (via [`enable_runtime_validation`]),
/// this function will verify the matrix is unitary before returning it.
/// Otherwise, it only checks that the matrix is square.
///
/// # Arguments
///
/// * `matrix` - The square complex matrix representing the gate
/// * `tolerance` - Tolerance for unitarity check (used only if runtime validation is enabled)
/// * `gate_name` - Optional name for error reporting
///
/// # Returns
///
/// * `Ok(Array<Complex<T>>)` - The validated gate matrix
/// * `Err(...)` if validation fails
///
/// # Examples
///
/// ```
/// use numrs2::new_modules::quantum::unitarity::{
///     create_validated_gate, enable_runtime_validation, disable_runtime_validation,
/// };
/// use numrs2::new_modules::quantum::gates::hadamard;
///
/// // Get the Hadamard gate matrix
/// let h = hadamard::<f64>().unwrap();
///
/// // With runtime validation enabled
/// enable_runtime_validation();
/// let validated = create_validated_gate(h.clone(), 1e-10, Some("Custom-H"));
/// assert!(validated.is_ok());
///
/// // Clean up
/// disable_runtime_validation();
/// ```
pub fn create_validated_gate<T>(
    matrix: Array<Complex<T>>,
    tolerance: f64,
    gate_name: Option<&str>,
) -> Result<Array<Complex<T>>>
where
    T: Float + Clone + Debug + Into<f64> + From<f64>,
{
    validate_square_matrix(&matrix)?;

    if is_runtime_validation_enabled() {
        validate_gate_unitarity(&matrix, tolerance, gate_name)?;
    }

    Ok(matrix)
}

/// Result of validating all standard gates.
///
/// Contains per-gate validation results including gate name,
/// whether it passed, and the unitarity error.
#[derive(Debug, Clone)]
pub struct GateValidationResult {
    /// Name of the gate
    pub gate_name: String,
    /// Whether the gate passed the unitarity check
    pub is_unitary: bool,
    /// The Frobenius norm of U†U - I
    pub unitarity_error: f64,
    /// Dimension of the gate matrix
    pub dimension: usize,
}

/// Result of validating all standard gates.
#[derive(Debug, Clone)]
pub struct StandardGatesValidation {
    /// Per-gate validation results
    pub results: Vec<GateValidationResult>,
    /// Whether all gates passed
    pub all_passed: bool,
    /// Number of gates that passed
    pub num_passed: usize,
    /// Number of gates that failed
    pub num_failed: usize,
}

/// Validate all built-in standard quantum gates.
///
/// Tests the following gates for unitarity:
/// - Pauli gates: X, Y, Z
/// - Hadamard gate: H
/// - Phase gates: S, T
/// - Rotation gates: Rx(pi/4), Ry(pi/4), Rz(pi/4)
/// - Two-qubit gates: CNOT, SWAP, CZ, CY
/// - Multi-qubit gates: Toffoli, Fredkin
/// - Identity gates: 1-qubit, 2-qubit
///
/// # Arguments
///
/// * `tolerance` - Maximum allowed Frobenius norm deviation from identity
///
/// # Returns
///
/// * `Ok(StandardGatesValidation)` - Comprehensive validation results
/// * `Err(...)` if gate creation or validation fails unexpectedly
///
/// # Examples
///
/// ```
/// use numrs2::new_modules::quantum::unitarity::validate_all_standard_gates;
///
/// let validation = validate_all_standard_gates(1e-10).unwrap();
/// assert!(validation.all_passed);
/// ```
pub fn validate_all_standard_gates(tolerance: f64) -> Result<StandardGatesValidation> {
    use super::gates;

    let mut results = Vec::new();

    // Helper closure to validate a single gate
    let mut validate_one = |name: &str, gate_result: Result<Array<Complex<f64>>>| -> Result<()> {
        let gate = gate_result?;
        let dim = gate.shape()[0];
        let error = unitarity_error(&gate)?;
        let passed = error <= tolerance;

        results.push(GateValidationResult {
            gate_name: name.to_string(),
            is_unitary: passed,
            unitarity_error: error,
            dimension: dim,
        });

        Ok(())
    };

    // Single-qubit gates
    validate_one("Pauli-X", gates::pauli_x::<f64>())?;
    validate_one("Pauli-Y", gates::pauli_y::<f64>())?;
    validate_one("Pauli-Z", gates::pauli_z::<f64>())?;
    validate_one("Hadamard", gates::hadamard::<f64>())?;
    validate_one("Phase (S)", gates::phase_gate::<f64>())?;
    validate_one("T gate", gates::t_gate::<f64>())?;

    // Rotation gates at pi/4
    let theta = std::f64::consts::PI / 4.0;
    validate_one("Rx(pi/4)", gates::rx(theta))?;
    validate_one("Ry(pi/4)", gates::ry(theta))?;
    validate_one("Rz(pi/4)", gates::rz(theta))?;

    // Two-qubit gates
    validate_one("CNOT", gates::cnot::<f64>())?;
    validate_one("SWAP", gates::swap::<f64>())?;
    validate_one("CZ", gates::cz::<f64>())?;
    validate_one("CY", gates::cy::<f64>())?;

    // Multi-qubit gates
    validate_one("Toffoli", gates::toffoli::<f64>())?;
    validate_one("Fredkin", gates::fredkin::<f64>())?;

    // Identity gates
    validate_one("Identity-1q", gates::identity::<f64>(1))?;
    validate_one("Identity-2q", gates::identity::<f64>(2))?;

    let num_passed = results.iter().filter(|r| r.is_unitary).count();
    let num_failed = results.len() - num_passed;
    let all_passed = num_failed == 0;

    Ok(StandardGatesValidation {
        results,
        all_passed,
        num_passed,
        num_failed,
    })
}

/// Validate that a matrix is square and at least 1x1.
///
/// # Arguments
///
/// * `matrix` - The matrix to validate
///
/// # Returns
///
/// * `Ok(())` if the matrix is square
/// * `Err(NumRs2Error::DimensionMismatch(...))` if the matrix is not square or not 2D
fn validate_square_matrix<T>(matrix: &Array<T>) -> Result<()>
where
    T: Clone,
{
    let shape = matrix.shape();

    if shape.len() != 2 {
        return Err(NumRs2Error::DimensionMismatch(format!(
            "Gate matrix must be 2-dimensional, got {}-dimensional",
            shape.len()
        )));
    }

    if shape[0] != shape[1] {
        return Err(NumRs2Error::DimensionMismatch(format!(
            "Gate matrix must be square, got {}x{}",
            shape[0], shape[1]
        )));
    }

    if shape[0] == 0 {
        return Err(NumRs2Error::DimensionMismatch(
            "Gate matrix must have at least dimension 1x1".to_string(),
        ));
    }

    Ok(())
}

/// Compute the product of two square complex matrices.
///
/// Used internally for checking that the product of unitary matrices is unitary.
///
/// # Arguments
///
/// * `a` - First matrix
/// * `b` - Second matrix
///
/// # Returns
///
/// * `Ok(Array<Complex<T>>)` - The matrix product A * B
/// * `Err(...)` if dimensions are incompatible or access fails
pub fn matrix_product<T>(a: &Array<Complex<T>>, b: &Array<Complex<T>>) -> Result<Array<Complex<T>>>
where
    T: Float + Clone + Debug + Into<f64> + From<f64>,
{
    let shape_a = a.shape();
    let shape_b = b.shape();

    if shape_a.len() != 2 || shape_b.len() != 2 {
        return Err(NumRs2Error::DimensionMismatch(
            "Both matrices must be 2-dimensional".to_string(),
        ));
    }

    if shape_a[1] != shape_b[0] {
        return Err(NumRs2Error::DimensionMismatch(format!(
            "Matrix dimensions incompatible for multiplication: {}x{} * {}x{}",
            shape_a[0], shape_a[1], shape_b[0], shape_b[1]
        )));
    }

    let m = shape_a[0];
    let n = shape_b[1];
    let p = shape_a[1];

    let mut result_data = vec![Complex::new(T::zero(), T::zero()); m * n];

    for i in 0..m {
        for j in 0..n {
            let mut sum = Complex::new(T::zero(), T::zero());
            for k in 0..p {
                let a_ik = a.get(&[i, k]).map_err(|e| {
                    NumRs2Error::ComputationError(format!(
                        "Failed to access matrix A element [{}, {}]: {}",
                        i, k, e
                    ))
                })?;
                let b_kj = b.get(&[k, j]).map_err(|e| {
                    NumRs2Error::ComputationError(format!(
                        "Failed to access matrix B element [{}, {}]: {}",
                        k, j, e
                    ))
                })?;
                sum = sum + a_ik * b_kj;
            }
            result_data[i * n + j] = sum;
        }
    }

    Ok(Array::from_vec(result_data).reshape(&[m, n]))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::new_modules::quantum::gates;

    /// Test 1: Verify all standard single-qubit gates are unitary
    #[test]
    fn test_single_qubit_gates_unitary() {
        let tolerance = 1e-12;

        let x = gates::pauli_x::<f64>().expect("pauli_x should succeed");
        assert!(
            is_unitary(&x, tolerance).expect("is_unitary check should not fail"),
            "Pauli-X should be unitary"
        );

        let y = gates::pauli_y::<f64>().expect("pauli_y should succeed");
        assert!(
            is_unitary(&y, tolerance).expect("is_unitary check should not fail"),
            "Pauli-Y should be unitary"
        );

        let z = gates::pauli_z::<f64>().expect("pauli_z should succeed");
        assert!(
            is_unitary(&z, tolerance).expect("is_unitary check should not fail"),
            "Pauli-Z should be unitary"
        );

        let h = gates::hadamard::<f64>().expect("hadamard should succeed");
        assert!(
            is_unitary(&h, tolerance).expect("is_unitary check should not fail"),
            "Hadamard should be unitary"
        );

        let s = gates::phase_gate::<f64>().expect("phase_gate should succeed");
        assert!(
            is_unitary(&s, tolerance).expect("is_unitary check should not fail"),
            "Phase (S) gate should be unitary"
        );

        let t = gates::t_gate::<f64>().expect("t_gate should succeed");
        assert!(
            is_unitary(&t, tolerance).expect("is_unitary check should not fail"),
            "T gate should be unitary"
        );
    }

    /// Test 2: Verify multi-qubit gates (CNOT, SWAP, Toffoli, Fredkin) are unitary
    #[test]
    fn test_multi_qubit_gates_unitary() {
        let tolerance = 1e-12;

        let cnot_gate = gates::cnot::<f64>().expect("cnot should succeed");
        assert!(
            is_unitary(&cnot_gate, tolerance).expect("is_unitary check should not fail"),
            "CNOT should be unitary"
        );

        let swap_gate = gates::swap::<f64>().expect("swap should succeed");
        assert!(
            is_unitary(&swap_gate, tolerance).expect("is_unitary check should not fail"),
            "SWAP should be unitary"
        );

        let cz_gate = gates::cz::<f64>().expect("cz should succeed");
        assert!(
            is_unitary(&cz_gate, tolerance).expect("is_unitary check should not fail"),
            "CZ should be unitary"
        );

        let cy_gate = gates::cy::<f64>().expect("cy should succeed");
        assert!(
            is_unitary(&cy_gate, tolerance).expect("is_unitary check should not fail"),
            "CY should be unitary"
        );

        let toffoli_gate = gates::toffoli::<f64>().expect("toffoli should succeed");
        assert!(
            is_unitary(&toffoli_gate, tolerance).expect("is_unitary check should not fail"),
            "Toffoli should be unitary"
        );

        let fredkin_gate = gates::fredkin::<f64>().expect("fredkin should succeed");
        assert!(
            is_unitary(&fredkin_gate, tolerance).expect("is_unitary check should not fail"),
            "Fredkin should be unitary"
        );
    }

    /// Test 3: Detect a non-unitary matrix
    #[test]
    fn test_non_unitary_matrix_detected() {
        // A non-unitary matrix (scaling matrix)
        let data = vec![
            Complex::new(2.0, 0.0),
            Complex::new(0.0, 0.0),
            Complex::new(0.0, 0.0),
            Complex::new(2.0, 0.0),
        ];
        let non_unitary = Array::from_vec(data).reshape(&[2, 2]);

        let result = is_unitary(&non_unitary, 1e-10).expect("is_unitary check should not fail");
        assert!(!result, "Scaling matrix should NOT be detected as unitary");

        let error = unitarity_error(&non_unitary).expect("unitarity_error should not fail");
        assert!(
            error > 1.0,
            "Scaling matrix should have a large unitarity error, got {}",
            error
        );
    }

    /// Test 4: Tolerance handling - tight vs loose tolerance
    #[test]
    fn test_tolerance_handling() {
        let h = gates::hadamard::<f64>().expect("hadamard should succeed");

        // Very tight tolerance should still pass for exact gates
        assert!(
            is_unitary(&h, 1e-15).expect("is_unitary check should not fail"),
            "Hadamard should pass with very tight tolerance"
        );

        // Create a slightly non-unitary matrix (Hadamard with small perturbation)
        let data = vec![
            Complex::new(1.0 / 2.0_f64.sqrt() + 1e-6, 0.0),
            Complex::new(1.0 / 2.0_f64.sqrt(), 0.0),
            Complex::new(1.0 / 2.0_f64.sqrt(), 0.0),
            Complex::new(-1.0 / 2.0_f64.sqrt(), 0.0),
        ];
        let perturbed = Array::from_vec(data).reshape(&[2, 2]);

        // Should fail with tight tolerance
        assert!(
            !is_unitary(&perturbed, 1e-10).expect("is_unitary check should not fail"),
            "Perturbed Hadamard should fail with tight tolerance"
        );

        // Should pass with loose tolerance
        assert!(
            is_unitary(&perturbed, 1e-3).expect("is_unitary check should not fail"),
            "Perturbed Hadamard should pass with loose tolerance"
        );
    }

    /// Test 5: Identity gate is unitary
    #[test]
    fn test_identity_gate_unitary() {
        let tolerance = 1e-14;

        // 1-qubit identity
        let id1 = gates::identity::<f64>(1).expect("identity(1) should succeed");
        assert!(
            is_unitary(&id1, tolerance).expect("is_unitary check should not fail"),
            "1-qubit identity should be unitary"
        );
        let error1 = unitarity_error(&id1).expect("unitarity_error should not fail");
        assert!(
            error1 < 1e-15,
            "Identity unitarity error should be essentially zero, got {}",
            error1
        );

        // 2-qubit identity
        let id2 = gates::identity::<f64>(2).expect("identity(2) should succeed");
        assert!(
            is_unitary(&id2, tolerance).expect("is_unitary check should not fail"),
            "2-qubit identity should be unitary"
        );

        // 3-qubit identity
        let id3 = gates::identity::<f64>(3).expect("identity(3) should succeed");
        assert!(
            is_unitary(&id3, tolerance).expect("is_unitary check should not fail"),
            "3-qubit identity should be unitary"
        );
    }

    /// Test 6: Composed gates - product of unitary gates should be unitary
    #[test]
    fn test_composed_gates_unitary() {
        let tolerance = 1e-10;

        // H * X should be unitary (product of two unitaries)
        let h = gates::hadamard::<f64>().expect("hadamard should succeed");
        let x = gates::pauli_x::<f64>().expect("pauli_x should succeed");
        let hx = matrix_product(&h, &x).expect("matrix_product should succeed");
        assert!(
            is_unitary(&hx, tolerance).expect("is_unitary check should not fail"),
            "H*X should be unitary"
        );

        // X * Y * Z should be unitary
        let y = gates::pauli_y::<f64>().expect("pauli_y should succeed");
        let z = gates::pauli_z::<f64>().expect("pauli_z should succeed");
        let xy = matrix_product(&x, &y).expect("matrix_product should succeed");
        let xyz = matrix_product(&xy, &z).expect("matrix_product should succeed");
        assert!(
            is_unitary(&xyz, tolerance).expect("is_unitary check should not fail"),
            "X*Y*Z should be unitary"
        );

        // H * H should be approximately identity (H is self-inverse)
        let hh = matrix_product(&h, &h).expect("matrix_product should succeed");
        let hh_error = unitarity_error(&hh).expect("unitarity_error should not fail");
        assert!(
            hh_error < tolerance,
            "H*H unitarity error should be small, got {}",
            hh_error
        );
    }

    /// Test 7: Edge case - 1x1 gate (global phase)
    #[test]
    fn test_1x1_gate() {
        let tolerance = 1e-14;

        // e^(i*pi/4) as a 1x1 unitary
        let phase = std::f64::consts::PI / 4.0;
        let data = vec![Complex::new(phase.cos(), phase.sin())];
        let gate = Array::from_vec(data).reshape(&[1, 1]);

        assert!(
            is_unitary(&gate, tolerance).expect("is_unitary check should not fail"),
            "1x1 phase gate should be unitary"
        );

        let error = unitarity_error(&gate).expect("unitarity_error should not fail");
        assert!(
            error < 1e-15,
            "1x1 phase gate error should be ~0, got {}",
            error
        );

        // Non-unitary 1x1 (magnitude != 1)
        let data_non_unitary = vec![Complex::new(0.5, 0.0)];
        let non_unitary_gate = Array::from_vec(data_non_unitary).reshape(&[1, 1]);

        assert!(
            !is_unitary(&non_unitary_gate, tolerance).expect("is_unitary check should not fail"),
            "1x1 scaling gate should NOT be unitary"
        );
    }

    /// Test 8: Larger gates (4-qubit identity = 16x16)
    #[test]
    fn test_larger_gates() {
        let tolerance = 1e-12;

        // 4-qubit identity (16x16 matrix)
        let id4 = gates::identity::<f64>(4).expect("identity(4) should succeed");
        assert_eq!(id4.shape(), vec![16, 16]);
        assert!(
            is_unitary(&id4, tolerance).expect("is_unitary check should not fail"),
            "4-qubit identity should be unitary"
        );

        let error = unitarity_error(&id4).expect("unitarity_error should not fail");
        assert!(
            error < 1e-15,
            "4-qubit identity error should be ~0, got {}",
            error
        );
    }

    /// Test 9: Rotation gates at various angles are always unitary
    #[test]
    fn test_rotation_gates_various_angles() {
        let tolerance = 1e-12;

        let angles = [
            0.0,
            std::f64::consts::PI / 6.0,
            std::f64::consts::PI / 4.0,
            std::f64::consts::PI / 3.0,
            std::f64::consts::PI / 2.0,
            std::f64::consts::PI,
            std::f64::consts::TAU,
            -std::f64::consts::PI / 4.0,
            3.7, // arbitrary angle
        ];

        for &angle in &angles {
            let rx_gate = gates::rx(angle).expect("rx should succeed");
            assert!(
                is_unitary(&rx_gate, tolerance).expect("is_unitary check should not fail"),
                "Rx({}) should be unitary",
                angle
            );

            let ry_gate = gates::ry(angle).expect("ry should succeed");
            assert!(
                is_unitary(&ry_gate, tolerance).expect("is_unitary check should not fail"),
                "Ry({}) should be unitary",
                angle
            );

            let rz_gate = gates::rz(angle).expect("rz should succeed");
            assert!(
                is_unitary(&rz_gate, tolerance).expect("is_unitary check should not fail"),
                "Rz({}) should be unitary",
                angle
            );
        }
    }

    /// Test 10: validate_all_standard_gates comprehensive check
    #[test]
    fn test_validate_all_standard_gates() {
        let validation = validate_all_standard_gates(1e-10).expect("validation should not fail");

        assert!(
            validation.all_passed,
            "All standard gates should pass unitarity check. Failed gates: {:?}",
            validation
                .results
                .iter()
                .filter(|r| !r.is_unitary)
                .map(|r| format!("{}: error={:.6e}", r.gate_name, r.unitarity_error))
                .collect::<Vec<_>>()
        );

        assert_eq!(validation.num_failed, 0, "No standard gates should fail");

        assert!(
            validation.num_passed >= 17,
            "Should validate at least 17 gates, got {}",
            validation.num_passed
        );
    }

    /// Test 11: Non-square matrix is rejected
    #[test]
    fn test_non_square_matrix_rejected() {
        let data = vec![
            Complex::new(1.0, 0.0),
            Complex::new(0.0, 0.0),
            Complex::new(0.0, 0.0),
            Complex::new(1.0, 0.0),
            Complex::new(0.0, 0.0),
            Complex::new(0.0, 0.0),
        ];
        let non_square = Array::from_vec(data).reshape(&[2, 3]);

        let result = is_unitary(&non_square, 1e-10);
        assert!(result.is_err(), "Non-square matrix should return an error");

        let error_result = unitarity_error(&non_square);
        assert!(
            error_result.is_err(),
            "unitarity_error should fail for non-square matrix"
        );
    }

    /// Test 12: validate_gate_unitarity provides meaningful error messages
    #[test]
    fn test_validate_gate_unitarity_error_message() {
        // Create a non-unitary matrix
        let data = vec![
            Complex::new(2.0, 0.0),
            Complex::new(0.0, 0.0),
            Complex::new(0.0, 0.0),
            Complex::new(1.0, 0.0),
        ];
        let non_unitary = Array::from_vec(data).reshape(&[2, 2]);

        let result = validate_gate_unitarity(&non_unitary, 1e-10, Some("BadGate"));
        assert!(result.is_err(), "Non-unitary gate should fail validation");

        let err_msg = format!("{}", result.expect_err("should be error"));
        assert!(
            err_msg.contains("BadGate"),
            "Error message should contain gate name, got: {}",
            err_msg
        );
        assert!(
            err_msg.contains("not unitary"),
            "Error message should indicate non-unitarity, got: {}",
            err_msg
        );
    }

    /// Test 13: Runtime validation flag works correctly
    #[test]
    fn test_runtime_validation_flag() {
        // Ensure we start with validation disabled
        disable_runtime_validation();
        assert!(!is_runtime_validation_enabled());

        // Non-unitary matrix should pass when validation is disabled
        let data = vec![
            Complex::new(2.0, 0.0),
            Complex::new(0.0, 0.0),
            Complex::new(0.0, 0.0),
            Complex::new(2.0, 0.0),
        ];
        let non_unitary = Array::from_vec(data.clone()).reshape(&[2, 2]);
        let result = create_validated_gate(non_unitary, 1e-10, Some("Test"));
        assert!(
            result.is_ok(),
            "Non-unitary should pass when validation is disabled"
        );

        // Enable validation
        enable_runtime_validation();
        assert!(is_runtime_validation_enabled());

        // Same non-unitary matrix should fail now
        let non_unitary_2 = Array::from_vec(data).reshape(&[2, 2]);
        let result_2 = create_validated_gate(non_unitary_2, 1e-10, Some("Test"));
        assert!(
            result_2.is_err(),
            "Non-unitary should fail when validation is enabled"
        );

        // Valid unitary should still pass
        let h = gates::hadamard::<f64>().expect("hadamard should succeed");
        let result_3 = create_validated_gate(h, 1e-10, Some("Hadamard"));
        assert!(result_3.is_ok(), "Hadamard should pass validation");

        // Clean up
        disable_runtime_validation();
    }

    /// Test 14: Controlled gate preserves unitarity
    #[test]
    fn test_controlled_gate_unitarity() {
        let tolerance = 1e-12;

        let x = gates::pauli_x::<f64>().expect("pauli_x should succeed");
        let cx = gates::controlled_gate(&x).expect("controlled_gate should succeed");

        assert!(
            is_unitary(&cx, tolerance).expect("is_unitary check should not fail"),
            "Controlled-X should be unitary"
        );

        let h = gates::hadamard::<f64>().expect("hadamard should succeed");
        let ch = gates::controlled_gate(&h).expect("controlled_gate should succeed");

        assert!(
            is_unitary(&ch, tolerance).expect("is_unitary check should not fail"),
            "Controlled-H should be unitary"
        );

        let s = gates::phase_gate::<f64>().expect("phase_gate should succeed");
        let cs = gates::controlled_gate(&s).expect("controlled_gate should succeed");

        assert!(
            is_unitary(&cs, tolerance).expect("is_unitary check should not fail"),
            "Controlled-S should be unitary"
        );
    }

    /// Test 15: Unitarity error is zero for exact identity
    #[test]
    fn test_exact_identity_zero_error() {
        let id1 = gates::identity::<f64>(1).expect("identity(1) should succeed");
        let error = unitarity_error(&id1).expect("unitarity_error should not fail");
        assert!(
            error < f64::EPSILON,
            "Identity gate should have zero unitarity error, got {}",
            error
        );
    }
}