quantrs2 0.1.3

Comprehensive Rust quantum computing framework - unified entry point for quantum simulation, algorithm development, and hardware interaction
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
#![allow(clippy::pedantic, clippy::assertions_on_constants)]
//! Cross-Subcrate Integration Tests for QuantRS2
//!
//! These tests verify that different QuantRS2 subcrates work correctly together,
//! ensuring seamless integration across the ecosystem.

// Test circuit + sim integration
#[cfg(all(feature = "circuit", feature = "sim"))]
mod circuit_sim_integration {
    use quantrs2::prelude::circuits::Simulator;
    use quantrs2::prelude::simulation::*;

    #[test]
    fn test_basic_circuit_simulation() {
        // Create a simple Bell state circuit
        let mut circuit = Circuit::<2>::new();
        circuit.h(0).unwrap();
        circuit.cnot(0, 1).unwrap();

        // Simulate with state vector simulator
        let simulator = StateVectorSimulator::new();
        let result = Simulator::run(&simulator, &circuit);

        // Should succeed
        assert!(result.is_ok(), "Circuit simulation should succeed");
    }

    #[test]
    fn test_circuit_optimization_and_simulation() {
        // Create a circuit with redundant gates
        let mut circuit = Circuit::<2>::new();
        circuit.h(0).unwrap();
        circuit.x(0).unwrap();
        circuit.x(0).unwrap(); // Redundant - two X gates cancel

        // Simulate the circuit
        let simulator = StateVectorSimulator::new();
        let result = Simulator::run(&simulator, &circuit);

        assert!(
            result.is_ok(),
            "Circuit with redundant gates should still simulate"
        );
    }

    #[test]
    fn test_multi_qubit_circuit_simulation() {
        // Create a larger circuit
        let mut circuit = Circuit::<4>::new();
        circuit.h(0).unwrap();
        circuit.cnot(0, 1).unwrap();
        circuit.cnot(1, 2).unwrap();
        circuit.cnot(2, 3).unwrap();

        // Simulate
        let simulator = StateVectorSimulator::new();
        let result = Simulator::run(&simulator, &circuit);

        assert!(result.is_ok(), "Multi-qubit circuit should simulate");
    }
}

// Test ml + sim integration
#[cfg(all(feature = "ml", feature = "sim"))]
mod ml_sim_integration {
    #[test]
    fn test_ml_requires_sim() {
        // This test verifies that ML features can access simulation capabilities
        // The fact that this compiles proves the integration works
        assert!(true, "ML and sim integration is available");
    }
}

// Test ml + anneal integration
#[cfg(all(feature = "ml", feature = "anneal"))]
mod ml_anneal_integration {
    #[test]
    fn test_ml_requires_anneal() {
        // This test verifies that ML features can access annealing capabilities
        // The fact that this compiles proves the integration works
        assert!(true, "ML and anneal integration is available");
    }
}

// Test device + circuit integration
#[cfg(all(feature = "device", feature = "circuit"))]
mod device_circuit_integration {
    use quantrs2::prelude::hardware::*;

    #[test]
    fn test_circuit_for_device() {
        // Create a circuit suitable for hardware execution
        let mut circuit = Circuit::<2>::new();
        circuit.h(0).unwrap();
        circuit.cnot(0, 1).unwrap();

        // Verify circuit properties
        assert_eq!(circuit.num_qubits(), 2);
        // Circuit creation should succeed (verified by the fact we got here)
        assert!(true, "Circuit created successfully for hardware");
    }

    #[test]
    fn test_device_circuit_validation() {
        // Create a valid hardware circuit
        let mut circuit = Circuit::<2>::new();
        circuit.h(0).unwrap();
        circuit.cnot(0, 1).unwrap();

        // Circuit should be valid for basic hardware constraints
        assert!(
            circuit.num_qubits() <= 100,
            "Circuit should be reasonable size for hardware"
        );
    }
}

// Test tytan + anneal integration
#[cfg(all(feature = "tytan", feature = "anneal"))]
mod tytan_anneal_integration {
    #[test]
    fn test_tytan_requires_anneal() {
        // This test verifies that Tytan features can access annealing capabilities
        // The fact that this compiles proves the integration works
        assert!(true, "Tytan and anneal integration is available");
    }
}

// Test full stack integration (when all features are enabled)
#[cfg(all(
    feature = "circuit",
    feature = "sim",
    feature = "ml",
    feature = "device"
))]
mod full_stack_integration {
    use quantrs2::circuit::builder::Simulator;
    use quantrs2::prelude::full::*;

    #[test]
    fn test_full_prelude_available() {
        // Test that full prelude provides all necessary types
        let _q = QubitId::new(0);

        // Circuit building
        let mut circuit = Circuit::<2>::new();
        circuit.h(0).unwrap();
        circuit.cnot(0, 1).unwrap();

        // Simulation - use the Simulator<N> trait directly
        let simulator = StateVectorSimulator::new();
        let result = simulator.run(&circuit);

        assert!(result.is_ok(), "Full stack integration should work");
    }

    #[test]
    fn test_workflow_circuit_to_simulation() {
        // Complete workflow: build circuit -> optimize -> simulate
        let mut circuit = Circuit::<3>::new();

        // Build a simple circuit
        circuit.h(0).unwrap();
        circuit.cnot(0, 1).unwrap();
        circuit.cnot(1, 2).unwrap();

        // Simulate - use the Simulator<N> trait directly
        let simulator = StateVectorSimulator::new();
        let result = simulator.run(&circuit);

        assert!(result.is_ok(), "Full workflow should succeed");
    }
}

// Test error propagation across subcrates
#[cfg(feature = "circuit")]
mod error_propagation {
    use quantrs2::error::{with_context, QuantRS2Result};
    use quantrs2::prelude::circuits::*;

    #[test]
    fn test_error_conversion_circuit() {
        fn build_valid_circuit() -> Circuit<2> {
            Circuit::<2>::new()
        }

        let circuit = build_valid_circuit();
        // Circuit should be valid
        assert!(
            circuit.gates().is_empty(),
            "New circuit should have no gates"
        );
    }

    #[test]
    fn test_error_context_propagation() {
        let error = QuantRS2Error::InvalidQubitId(100);
        let contextualized = with_context(error, "building GHZ state");

        // Error should retain its type (with_context preserves InvalidQubitId variant)
        assert!(matches!(contextualized, QuantRS2Error::InvalidQubitId(100)));
    }
}

// Test prelude hierarchy consistency
mod prelude_hierarchy {
    use quantrs2::prelude::essentials::*;

    #[test]
    fn test_essentials_always_available() {
        // Essential types should always be available
        let _q = QubitId::new(0);
        let version = VERSION;
        assert!(!version.is_empty());
    }

    #[cfg(feature = "circuit")]
    #[test]
    fn test_circuits_includes_essentials() {
        use quantrs2::prelude::circuits::*;

        // Circuits prelude should include essentials
        let _q = QubitId::new(0);
        let _circuit = Circuit::<2>::new();
        assert!(!VERSION.is_empty());
    }

    #[cfg(feature = "sim")]
    #[test]
    fn test_simulation_includes_circuits() {
        use quantrs2::prelude::simulation::*;

        // Simulation prelude should include circuits and essentials
        let _q = QubitId::new(0);
        let _circuit = Circuit::<2>::new();
        let _simulator = StateVectorSimulator::new();
        assert!(!VERSION.is_empty());
    }

    #[test]
    fn test_full_includes_all() {
        use quantrs2::prelude::full::*;
        // Disambiguate VERSION - use essentials
        use quantrs2::prelude::essentials::VERSION;

        // Full prelude should include essentials at minimum
        let _q = QubitId::new(0);
        assert!(!VERSION.is_empty());

        // Additional features available when enabled
        #[cfg(feature = "circuit")]
        {
            let _circuit = Circuit::<2>::new();
        }

        #[cfg(feature = "sim")]
        {
            let _simulator = StateVectorSimulator::new();
        }
    }
}

// Test version compatibility across subcrates
mod version_compatibility {
    use quantrs2::version;

    #[test]
    fn test_version_consistency() {
        let info = version::VersionInfo::current();

        // All version strings should be non-empty
        assert!(!info.quantrs2.is_empty());
        assert!(!info.scirs2.is_empty());
        assert!(!info.rustc.is_empty());
        assert!(!info.target.is_empty());
    }

    #[test]
    fn test_compatibility_check() {
        // Compatibility check should pass for valid builds
        let result = version::check_compatibility();

        // In development, this might have warnings but should not fail
        match result {
            Ok(()) => {
                // Perfect - no compatibility issues
            }
            Err(issues) => {
                // Some issues detected, but this is acceptable in development
                // Just ensure we can enumerate them
                assert!(!issues.is_empty(), "If check fails, there should be issues");
            }
        }
    }
}

// Test configuration management across subcrates
mod configuration_integration {
    use quantrs2::config;

    #[test]
    fn test_global_config_access() {
        let cfg = config::Config::global();
        let snapshot = cfg.snapshot();

        // Configuration should have reasonable defaults
        assert!(snapshot.enable_simd); // SIMD should be enabled by default
        assert_eq!(snapshot.log_level, config::LogLevel::Warn);
    }

    #[test]
    fn test_config_builder() {
        let config_data = config::Config::builder()
            .num_threads(8)
            .memory_limit_gb(16)
            .enable_gpu(true)
            .build();

        assert_eq!(config_data.num_threads, Some(8));
        assert_eq!(
            config_data.memory_limit_bytes,
            Some(16 * 1024 * 1024 * 1024)
        );
        assert!(config_data.enable_gpu);
    }

    #[test]
    fn test_backend_selection() {
        let backends = [
            config::DefaultBackend::Auto,
            config::DefaultBackend::Cpu,
            config::DefaultBackend::Gpu,
        ];

        // All backends should be selectable
        for backend in &backends {
            let config_data = config::Config::builder().default_backend(*backend).build();
            assert_eq!(config_data.default_backend, *backend);
        }
    }
}

// Test diagnostics integration
mod diagnostics_integration {
    use quantrs2::diagnostics;

    #[test]
    fn test_diagnostics_report() {
        let report = diagnostics::run_diagnostics();

        // Check that all sections are populated
        assert!(!report.version.quantrs2.is_empty());
        assert!(report.capabilities.cpu_cores > 0);
        // Note: total_memory_bytes may be 0 if memory detection is not implemented
        // assert!(report.capabilities.total_memory_bytes >= 0);

        // Check ready status
        let _ = report.is_ready();

        // Check summary generation
        let summary = report.summary();
        assert!(summary.contains("Diagnostic Summary"));
    }

    #[test]
    fn test_system_capabilities() {
        let report = diagnostics::run_diagnostics();
        let caps = &report.capabilities;

        // System should have reasonable capabilities
        assert!(caps.cpu_cores >= 1);
        assert!(caps.cpu_cores <= 1024); // Sanity check
                                         // Note: total_memory_bytes may be 0 if memory detection is not implemented
                                         // This is acceptable for the facade crate which uses a placeholder implementation
    }

    #[test]
    fn test_feature_detection() {
        let report = diagnostics::run_diagnostics();

        // Check that features are detected correctly
        #[cfg(feature = "circuit")]
        assert!(report.features.circuit);

        #[cfg(feature = "sim")]
        assert!(report.features.sim);

        #[cfg(feature = "ml")]
        assert!(report.features.ml);

        #[cfg(feature = "device")]
        assert!(report.features.device);

        #[cfg(feature = "anneal")]
        assert!(report.features.anneal);

        #[cfg(feature = "tytan")]
        assert!(report.features.tytan);
    }
}

// Test utility functions integration
mod utils_integration {
    use quantrs2::utils;

    #[test]
    fn test_memory_estimation() {
        // Test memory estimation for different qubit counts
        let memory_10 = utils::estimate_statevector_memory(10);
        let memory_20 = utils::estimate_statevector_memory(20);
        let memory_30 = utils::estimate_statevector_memory(30);

        // Memory should grow exponentially
        assert!(memory_20 > memory_10);
        assert!(memory_30 > memory_20);
        assert!(memory_20 >= memory_10 * 1024); // Should be ~1024x more for 10 extra qubits
    }

    #[test]
    fn test_max_qubits_calculation() {
        // Test with different memory limits
        let available_1gb = 1024 * 1024 * 1024usize;
        let max_qubits_1gb = utils::max_qubits_for_memory(available_1gb);

        let available_16gb = 16 * 1024 * 1024 * 1024usize;
        let max_qubits_16gb = utils::max_qubits_for_memory(available_16gb);

        // More memory should allow more qubits
        assert!(max_qubits_16gb > max_qubits_1gb);

        // Results should be reasonable
        assert!(max_qubits_1gb >= 15);
        assert!(max_qubits_1gb <= 30);
        assert!(max_qubits_16gb >= 20);
        assert!(max_qubits_16gb <= 35);
    }

    #[test]
    fn test_formatting_utilities() {
        use std::time::Duration;

        // Test memory formatting
        let mem_1kb = utils::format_memory(1024);
        assert!(mem_1kb.contains("KB") || mem_1kb.contains('1'));

        let mem_1mb = utils::format_memory(1024 * 1024);
        assert!(mem_1mb.contains("MB") || mem_1mb.contains('1'));

        // Test duration formatting
        let dur_1s = utils::format_duration(Duration::from_secs(1));
        assert!(dur_1s.contains('s'));

        let dur_1ms = utils::format_duration(Duration::from_millis(1));
        assert!(dur_1ms.contains("ms"));
    }

    #[test]
    fn test_validation_utilities() {
        // Test qubit count validation with available memory
        let mem_16gb = 16 * 1024 * 1024 * 1024;
        assert!(utils::is_valid_qubit_count(1, mem_16gb));
        assert!(utils::is_valid_qubit_count(30, mem_16gb));
        assert!(!utils::is_valid_qubit_count(100, mem_16gb));

        // Test range validation
        assert!(utils::is_in_range(&0.5, &0.0, &1.0));
        assert!(!utils::is_in_range(&1.5, &0.0, &1.0));
        assert!(!utils::is_in_range(&-0.5, &0.0, &1.0));
    }
}

// Test testing utilities integration
mod testing_utilities {
    use quantrs2::testing;

    #[test]
    fn test_approximate_equality() {
        // Test floating point comparison - these functions panic on failure
        testing::assert_approx_eq(1.0, 1.0 + 1e-10, 1e-8);
        // This should pass
    }

    #[test]
    #[should_panic]
    fn test_approximate_equality_fails() {
        // This should panic
        testing::assert_approx_eq(1.0, 2.0, 1e-8);
    }

    #[test]
    fn test_vector_approximate_equality() {
        let v1 = vec![1.0, 2.0, 3.0];
        let v2 = vec![1.0 + 1e-10, 2.0 + 1e-10, 3.0 + 1e-10];

        testing::assert_vec_approx_eq(&v1, &v2, 1e-8);
        // This should pass
    }

    #[test]
    #[should_panic]
    fn test_vector_approximate_equality_fails() {
        let v1 = vec![1.0, 2.0, 3.0];
        let v3 = vec![1.0, 2.0, 4.0];

        testing::assert_vec_approx_eq(&v1, &v3, 1e-8);
        // This should panic
    }

    #[test]
    fn test_temp_directory_creation() {
        use std::fs;

        let temp_dir = testing::create_temp_test_dir();
        assert!(temp_dir.exists());
        assert!(temp_dir.is_dir());

        // Clean up
        let _ = fs::remove_dir_all(temp_dir);
    }

    #[test]
    fn test_random_data_generation() {
        let data = testing::generate_random_test_data(100, 42);
        assert_eq!(data.len(), 100);

        // All values should be in range [0, 1]
        for &val in &data {
            assert!(val >= 0.0);
            assert!(val <= 1.0);
        }
    }
}

// Test benchmarking utilities integration
mod benchmarking_integration {
    use quantrs2::bench;
    use std::time::Duration;

    #[test]
    fn test_benchmark_timer_basic() {
        let timer = bench::BenchmarkTimer::start();
        std::thread::sleep(Duration::from_millis(5));
        let elapsed = timer.stop();
        assert!(elapsed >= Duration::from_millis(4));
    }

    #[test]
    fn test_benchmark_stats_aggregation() {
        let mut stats = bench::BenchmarkStats::new("test_stats");

        // Record multiple samples
        for i in 1..=10 {
            stats.record(Duration::from_millis(i * 10));
        }

        assert_eq!(stats.count(), 10);
        assert!(stats.mean().is_some());
        assert!(stats.median().is_some());
        assert!(stats.std_dev().is_some());
        assert!(stats.min().is_some());
        assert!(stats.max().is_some());

        // Verify ordering
        assert!(stats.min().unwrap() <= stats.mean().unwrap());
        assert!(stats.mean().unwrap() <= stats.max().unwrap());
    }

    #[test]
    fn test_measure_closure() {
        // Use more substantial work to ensure measurable time in release mode
        let (result, duration) = bench::measure(|| {
            // Perform work that won't be optimized away
            let mut sum: u64 = 0;
            for i in 1..=10_000 {
                sum = sum.wrapping_add(i);
                // Prevent complete optimization with black_box
                std::hint::black_box(sum);
            }
            sum
        });

        assert_eq!(result, 50_005_000); // Sum of 1 to 10,000
                                        // In release mode, this might still be very fast, so be lenient
        assert!(duration >= Duration::ZERO);
    }

    #[test]
    fn test_measure_iterations() {
        let stats = bench::measure_iterations(5, || {
            std::thread::sleep(Duration::from_millis(1));
        });

        assert_eq!(stats.count(), 5);
    }

    #[test]
    fn test_memory_usage_formatting() {
        let mem = bench::MemoryUsage::from_bytes(1024 * 1024);
        assert!((mem.mb() - 1.0).abs() < 0.01);
        assert!((mem.kb() - 1024.0).abs() < 0.01);
    }

    #[test]
    fn test_benchmark_config_presets() {
        let quick = bench::BenchmarkConfig::quick();
        assert_eq!(quick.warmup_iterations, 5);
        assert_eq!(quick.measure_iterations, 20);

        let thorough = bench::BenchmarkConfig::thorough();
        assert_eq!(thorough.warmup_iterations, 50);
        assert_eq!(thorough.measure_iterations, 1000);
    }

    #[test]
    fn test_throughput_calculation() {
        let mut stats = bench::BenchmarkStats::new("throughput");
        stats.set_ops_per_sample(1000);
        stats.record(Duration::from_secs(1));

        let throughput = stats.throughput().unwrap();
        assert!((throughput - 1000.0).abs() < 0.1);
    }

    #[test]
    fn test_percentile_calculation() {
        let mut stats = bench::BenchmarkStats::new("percentile");
        for i in 1..=100 {
            stats.record(Duration::from_millis(i));
        }

        let p50 = stats.percentile(50.0).unwrap();
        let p90 = stats.percentile(90.0).unwrap();
        let p99 = stats.percentile(99.0).unwrap();

        assert!(p50 < p90);
        assert!(p90 < p99);
    }
}

// Test symengine integration
#[cfg(feature = "symengine")]
mod symengine_integration {
    #[test]
    fn test_symengine_available() {
        // Test that symengine module is available when feature is enabled
        // This validates the feature flag and module integration
        use quantrs2::symengine;
        // Module should be accessible
        let type_name = std::any::type_name::<quantrs2::symengine::Expression>();
        assert!(!type_name.is_empty(), "SymEngine integration is available");
    }

    #[test]
    fn test_symengine_basic_types() {
        // Test basic symbolic types availability
        use quantrs2::symengine::Expression;

        // Expression type should be available
        // Note: Actual operations depend on SymEngine C library
        let type_name = std::any::type_name::<Expression>();
        assert!(!type_name.is_empty(), "SymEngine types are accessible");
    }
}

// Test symengine + circuit integration (parametric circuits)
#[cfg(all(feature = "symengine", feature = "circuit"))]
mod symengine_circuit_integration {
    #[test]
    fn test_parametric_gates() {
        // Test that symbolic parameters can be used with circuits
        // This integration enables variational circuit construction
        use quantrs2::symengine::Expression;
        let type_name = std::any::type_name::<Expression>();
        assert!(
            !type_name.is_empty(),
            "SymEngine can be used with circuits for parametric gates"
        );
    }

    #[test]
    fn test_circuit_symbolic_optimization() {
        // Test that circuits with symbolic parameters can be created
        // and potentially optimized symbolically
        use quantrs2::symengine::Expression;
        let type_name = std::any::type_name::<Expression>();
        assert!(
            !type_name.is_empty(),
            "SymEngine enables symbolic circuit optimization workflows"
        );
    }
}

// Test quantum math utilities integration
mod quantum_math_integration {
    use quantrs2::utils;

    #[test]
    fn test_quantum_constants() {
        // Verify quantum computing constants
        assert!(utils::SQRT_2.mul_add(utils::INV_SQRT_2, -1.0).abs() < 1e-15);
        assert!(utils::PI_OVER_2.mul_add(2.0, -utils::PI_CONST).abs() < 1e-15);
        assert!(utils::PI_OVER_4.mul_add(4.0, -utils::PI_CONST).abs() < 1e-15);
        assert!(utils::PI_OVER_8.mul_add(8.0, -utils::PI_CONST).abs() < 1e-15);
    }

    #[test]
    fn test_probability_normalization() {
        let mut probs = vec![2.0, 3.0, 5.0];
        assert!(utils::normalize_probabilities(&mut probs));
        assert!(utils::is_normalized(&probs, 1e-10));

        // Verify individual probabilities
        assert!((probs[0] - 0.2).abs() < 1e-10);
        assert!((probs[1] - 0.3).abs() < 1e-10);
        assert!((probs[2] - 0.5).abs() < 1e-10);
    }

    #[test]
    fn test_classical_fidelity_bounds() {
        // Fidelity of identical distributions should be 1
        let p = vec![0.5, 0.5];
        let fid = utils::classical_fidelity(&p, &p).unwrap();
        assert!((fid - 1.0).abs() < 1e-10);

        // Fidelity of orthogonal distributions should be 0
        let p1 = vec![1.0, 0.0];
        let p2 = vec![0.0, 1.0];
        let fid2 = utils::classical_fidelity(&p1, &p2).unwrap();
        assert!(fid2.abs() < 1e-10);
    }

    #[test]
    fn test_trace_distance_bounds() {
        // Trace distance of identical distributions should be 0
        let p = vec![0.5, 0.5];
        let dist = utils::trace_distance(&p, &p).unwrap();
        assert!(dist.abs() < 1e-10);

        // Trace distance of orthogonal distributions should be 1
        let p1 = vec![1.0, 0.0];
        let p2 = vec![0.0, 1.0];
        let dist2 = utils::trace_distance(&p1, &p2).unwrap();
        assert!((dist2 - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_entropy_properties() {
        // Entropy of uniform distribution over n states is log2(n)
        let uniform_2 = vec![0.5, 0.5];
        let h2 = utils::entropy(&uniform_2);
        assert!((h2 - 1.0).abs() < 1e-10); // log2(2) = 1

        let uniform_4 = vec![0.25, 0.25, 0.25, 0.25];
        let h4 = utils::entropy(&uniform_4);
        assert!((h4 - 2.0).abs() < 1e-10); // log2(4) = 2

        // Entropy of certain distribution is 0
        let certain = vec![1.0, 0.0, 0.0];
        let h0 = utils::entropy(&certain);
        assert!(h0.abs() < 1e-10);
    }

    #[test]
    fn test_hilbert_space_dimension() {
        // Verify 2^n dimensions
        assert_eq!(utils::hilbert_dim(0), 1);
        assert_eq!(utils::hilbert_dim(1), 2);
        assert_eq!(utils::hilbert_dim(5), 32);
        assert_eq!(utils::hilbert_dim(10), 1024);
        assert_eq!(utils::hilbert_dim(20), 1048576);

        // Reverse calculation
        assert_eq!(utils::num_qubits_from_dim(1), Some(0));
        assert_eq!(utils::num_qubits_from_dim(2), Some(1));
        assert_eq!(utils::num_qubits_from_dim(1024), Some(10));
        assert_eq!(utils::num_qubits_from_dim(100), None); // Not power of 2
    }

    #[test]
    fn test_angle_conversions() {
        // 360 degrees = 2π radians
        let rad = utils::deg_to_rad(360.0);
        assert!(2.0f64.mul_add(-std::f64::consts::PI, rad).abs() < 1e-10);

        // π radians = 180 degrees
        let deg = utils::rad_to_deg(std::f64::consts::PI);
        assert!((deg - 180.0).abs() < 1e-10);

        // Round-trip conversion
        let original = 45.0;
        let converted = utils::rad_to_deg(utils::deg_to_rad(original));
        assert!((converted - original).abs() < 1e-10);
    }

    #[test]
    fn test_probability_validation() {
        assert!(utils::is_valid_probability(0.0));
        assert!(utils::is_valid_probability(0.5));
        assert!(utils::is_valid_probability(1.0));
        assert!(!utils::is_valid_probability(-0.001));
        assert!(!utils::is_valid_probability(1.001));
        assert!(!utils::is_valid_probability(f64::NAN));
    }

    #[test]
    fn test_probability_clamping() {
        assert_eq!(utils::clamp_probability(-1.0), 0.0);
        assert_eq!(utils::clamp_probability(0.5), 0.5);
        assert_eq!(utils::clamp_probability(2.0), 1.0);
    }

    #[test]
    fn test_cnot_requirements() {
        // Linear chain entanglement requires n-1 CNOTs
        assert_eq!(utils::min_cnots_for_entanglement(1), 0);
        assert_eq!(utils::min_cnots_for_entanglement(2), 1);
        assert_eq!(utils::min_cnots_for_entanglement(5), 4);
        assert_eq!(utils::min_cnots_for_entanglement(100), 99);
    }

    #[test]
    fn test_binomial_coefficients() {
        // Pascal's triangle properties
        assert_eq!(utils::binomial(5, 0), 1);
        assert_eq!(utils::binomial(5, 5), 1);
        assert_eq!(utils::binomial(5, 2), 10);
        assert_eq!(utils::binomial(10, 5), 252);

        // Symmetry: C(n,k) = C(n, n-k)
        assert_eq!(utils::binomial(10, 3), utils::binomial(10, 7));
    }
}