quantrs2_core/optimizations_stable/
mod.rs1pub mod adaptive_precision;
8pub mod circuit_optimization;
9pub mod gate_fusion;
10pub mod quantum_cache;
11pub mod simd_gates;
12
13pub use adaptive_precision::{
14 adapt_precision_for_circuit, AdaptivePrecisionManager, PrecisionLevel,
15};
16pub use circuit_optimization::{
17 optimize_circuit, CircuitMetrics, CircuitOptimizer, OptimizationLevel,
18};
19pub use gate_fusion::{apply_gate_fusion, FusedGateSequence, FusionRule, GateFusionEngine};
20pub use quantum_cache::{get_global_cache, CacheKey, CachedResult, StableQuantumCache};
21pub use simd_gates::{process_gates_simd, SIMDGateProcessor, VectorizedOperation};
22
23pub fn initialize_stable_optimizations() -> crate::error::QuantRS2Result<()> {
25 let _cache = get_global_cache();
27
28 let _simd = SIMDGateProcessor::new();
30
31 precompute_common_gates()?;
33
34 Ok(())
35}
36
37fn precompute_common_gates() -> crate::error::QuantRS2Result<()> {
39 use num_complex::Complex64;
40 use std::f64::consts::{FRAC_1_SQRT_2, PI};
41
42 let cache = get_global_cache();
43
44 cache.insert(
46 CacheKey::new("pauli_x", vec![], 1),
47 CachedResult::Matrix(vec![
48 Complex64::new(0.0, 0.0),
49 Complex64::new(1.0, 0.0),
50 Complex64::new(1.0, 0.0),
51 Complex64::new(0.0, 0.0),
52 ]),
53 );
54
55 cache.insert(
56 CacheKey::new("pauli_y", vec![], 1),
57 CachedResult::Matrix(vec![
58 Complex64::new(0.0, 0.0),
59 Complex64::new(0.0, -1.0),
60 Complex64::new(0.0, 1.0),
61 Complex64::new(0.0, 0.0),
62 ]),
63 );
64
65 cache.insert(
66 CacheKey::new("pauli_z", vec![], 1),
67 CachedResult::Matrix(vec![
68 Complex64::new(1.0, 0.0),
69 Complex64::new(0.0, 0.0),
70 Complex64::new(0.0, 0.0),
71 Complex64::new(-1.0, 0.0),
72 ]),
73 );
74
75 cache.insert(
77 CacheKey::new("hadamard", vec![], 1),
78 CachedResult::Matrix(vec![
79 Complex64::new(FRAC_1_SQRT_2, 0.0),
80 Complex64::new(FRAC_1_SQRT_2, 0.0),
81 Complex64::new(FRAC_1_SQRT_2, 0.0),
82 Complex64::new(-FRAC_1_SQRT_2, 0.0),
83 ]),
84 );
85
86 cache.insert(
88 CacheKey::new("cnot", vec![], 2),
89 CachedResult::Matrix(vec![
90 Complex64::new(1.0, 0.0),
91 Complex64::new(0.0, 0.0),
92 Complex64::new(0.0, 0.0),
93 Complex64::new(0.0, 0.0),
94 Complex64::new(0.0, 0.0),
95 Complex64::new(1.0, 0.0),
96 Complex64::new(0.0, 0.0),
97 Complex64::new(0.0, 0.0),
98 Complex64::new(0.0, 0.0),
99 Complex64::new(0.0, 0.0),
100 Complex64::new(0.0, 0.0),
101 Complex64::new(1.0, 0.0),
102 Complex64::new(0.0, 0.0),
103 Complex64::new(0.0, 0.0),
104 Complex64::new(1.0, 0.0),
105 Complex64::new(0.0, 0.0),
106 ]),
107 );
108
109 for &angle in &[PI / 4.0, PI / 2.0, 3.0 * PI / 4.0, PI] {
111 let cos_half = (angle / 2.0).cos();
112 let sin_half = (angle / 2.0).sin();
113
114 cache.insert(
116 CacheKey::new("rx", vec![angle], 1),
117 CachedResult::Matrix(vec![
118 Complex64::new(cos_half, 0.0),
119 Complex64::new(0.0, -sin_half),
120 Complex64::new(0.0, -sin_half),
121 Complex64::new(cos_half, 0.0),
122 ]),
123 );
124
125 cache.insert(
127 CacheKey::new("ry", vec![angle], 1),
128 CachedResult::Matrix(vec![
129 Complex64::new(cos_half, 0.0),
130 Complex64::new(-sin_half, 0.0),
131 Complex64::new(sin_half, 0.0),
132 Complex64::new(cos_half, 0.0),
133 ]),
134 );
135
136 cache.insert(
138 CacheKey::new("rz", vec![angle], 1),
139 CachedResult::Matrix(vec![
140 Complex64::new(cos_half, -sin_half),
141 Complex64::new(0.0, 0.0),
142 Complex64::new(0.0, 0.0),
143 Complex64::new(cos_half, sin_half),
144 ]),
145 );
146 }
147
148 Ok(())
149}
150
151pub fn get_optimization_statistics() -> OptimizationStatistics {
153 OptimizationStatistics {
154 cache_stats: get_global_cache().get_statistics(),
155 fusion_stats: GateFusionEngine::get_global_statistics(),
156 simd_stats: SIMDGateProcessor::get_global_statistics(),
157 }
158}
159
160#[derive(Debug, Clone)]
162pub struct OptimizationStatistics {
163 pub cache_stats: quantum_cache::CacheStatistics,
164 pub fusion_stats: gate_fusion::FusionStatistics,
165 pub simd_stats: simd_gates::SIMDStatistics,
166}