trustformers-optim 0.1.0

Optimizers for TrustformeRS
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
//! # Quantum-Inspired Optimization Algorithms
//!
//! This module implements cutting-edge optimization algorithms inspired by quantum
//! computing principles, specifically quantum annealing and quantum tunneling effects.
//! These optimizers leverage quantum mechanical concepts to escape local minima and
//! explore the loss landscape more effectively.
//!
//! ## Available Algorithms
//!
//! - **Quantum Annealing Optimizer (QAO)**: Simulates quantum annealing process
//! - **Quantum Tunneling Adam (QT-Adam)**: Adam with quantum tunneling effects
//! - **Quantum Superposition SGD (QS-SGD)**: SGD with superposition states
//! - **Quantum Entanglement Optimizer (QEO)**: Parameter entanglement for coordination

use crate::{
    common::{OptimizerState, StateMemoryStats},
    traits::StatefulOptimizer,
};
use parking_lot::RwLock;
use scirs2_core::random::*; // Replaces rand and rand_distr - SciRS2 Integration Policy
use serde::{Deserialize, Serialize};
use std::{
    collections::HashMap,
    sync::{
        atomic::{AtomicU64, Ordering},
        Arc,
    },
};
use trustformers_core::{errors::Result, tensor::Tensor, traits::Optimizer};

/// Configuration for Quantum Annealing Optimizer
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QuantumAnnealingConfig {
    /// Base learning rate
    pub learning_rate: f32,
    /// Initial quantum temperature (controls quantum fluctuations)
    pub initial_temperature: f32,
    /// Final quantum temperature (cooling schedule end point)
    pub final_temperature: f32,
    /// Number of steps for annealing schedule
    pub annealing_steps: usize,
    /// Quantum tunneling strength
    pub tunneling_strength: f32,
    /// Classical momentum coefficient
    pub momentum: f32,
    /// Energy threshold for quantum jumps
    pub energy_threshold: f32,
    /// Number of quantum states to maintain in superposition
    pub superposition_states: usize,
    /// Decoherence rate (quantum to classical transition)
    pub decoherence_rate: f32,
    /// Weight decay coefficient
    pub weight_decay: f32,
}

impl Default for QuantumAnnealingConfig {
    fn default() -> Self {
        Self {
            learning_rate: 1e-3,
            initial_temperature: 1.0,
            final_temperature: 1e-6,
            annealing_steps: 10000,
            tunneling_strength: 0.1,
            momentum: 0.9,
            energy_threshold: 1e-4,
            superposition_states: 4,
            decoherence_rate: 0.01,
            weight_decay: 1e-4,
        }
    }
}

/// Quantum state representation for parameters
#[derive(Debug, Clone)]
struct QuantumState {
    /// Parameter amplitudes in superposition
    amplitudes: Vec<f32>,
    /// Parameter phases
    phases: Vec<f32>,
    /// Probability distribution over states
    probabilities: Vec<f32>,
    /// Energy levels of each state
    energy_levels: Vec<f32>,
    /// Entanglement connections with other parameters
    #[allow(dead_code)]
    entanglements: HashMap<String, f32>,
}

impl QuantumState {
    fn new(superposition_states: usize) -> Self {
        let mut rng = thread_rng();
        let uniform = Uniform::new(0.0, 1.0).expect("Failed to create uniform distribution");

        let amplitudes: Vec<f32> =
            (0..superposition_states).map(|_| uniform.sample(&mut rng)).collect();

        let phases: Vec<f32> = (0..superposition_states)
            .map(|_| uniform.sample(&mut rng) * 2.0 * std::f32::consts::PI)
            .collect();

        // Normalize probabilities
        let sum_squares: f32 = amplitudes.iter().map(|a| a * a).sum();
        let probabilities: Vec<f32> = amplitudes.iter().map(|a| (a * a) / sum_squares).collect();

        Self {
            amplitudes,
            phases,
            probabilities,
            energy_levels: vec![0.0; superposition_states],
            entanglements: HashMap::new(),
        }
    }

    /// Collapse quantum state to classical value
    fn collapse(&self) -> f32 {
        let mut rng = thread_rng();
        let random_val = rng.random::<f32>();

        let mut cumulative_prob = 0.0;
        for (i, &prob) in self.probabilities.iter().enumerate() {
            cumulative_prob += prob;
            if random_val <= cumulative_prob {
                return self.amplitudes[i] * self.phases[i].cos();
            }
        }

        // Fallback to first state
        self.amplitudes[0] * self.phases[0].cos()
    }

    /// Update quantum state based on gradient information
    fn evolve(&mut self, gradient: f32, temperature: f32, tunneling_strength: f32) {
        let mut rng = thread_rng();

        // Update energy levels based on gradient
        for i in 0..self.energy_levels.len() {
            self.energy_levels[i] += gradient * self.amplitudes[i] * 0.1;
        }

        // Quantum tunneling: allow transitions between states
        if rng.random::<f32>() < tunneling_strength * temperature {
            let state1 = rng.random_range(0..self.amplitudes.len());
            let state2 = rng.random_range(0..self.amplitudes.len());

            if state1 != state2 {
                let energy_diff = self.energy_levels[state2] - self.energy_levels[state1];
                let tunnel_prob = (-energy_diff / temperature).exp();

                if rng.random::<f32>() < tunnel_prob {
                    // Quantum tunneling occurs - exchange amplitudes
                    self.amplitudes.swap(state1, state2);
                    self.phases.swap(state1, state2);
                }
            }
        }

        // Update phases with quantum evolution
        for phase in &mut self.phases {
            *phase += gradient * temperature * 0.01;
            *phase %= 2.0 * std::f32::consts::PI;
        }

        // Renormalize probabilities
        let sum_squares: f32 = self.amplitudes.iter().map(|a| a * a).sum();
        if sum_squares > 0.0 {
            for (i, amplitude) in self.amplitudes.iter().enumerate() {
                self.probabilities[i] = (amplitude * amplitude) / sum_squares;
            }
        }
    }
}

/// Quantum Annealing Optimizer - implements quantum annealing for optimization
pub struct QuantumAnnealingOptimizer {
    config: QuantumAnnealingConfig,
    quantum_states: Arc<RwLock<HashMap<String, QuantumState>>>,
    classical_momentum: Arc<RwLock<HashMap<String, Tensor>>>,
    step_count: AtomicU64,
    current_temperature: Arc<RwLock<f32>>,
    energy_history: Arc<RwLock<Vec<f32>>>,
    state: OptimizerState,
}

impl QuantumAnnealingOptimizer {
    pub fn new(config: QuantumAnnealingConfig) -> Self {
        let initial_temperature = config.initial_temperature;
        Self {
            config,
            quantum_states: Arc::new(RwLock::new(HashMap::new())),
            classical_momentum: Arc::new(RwLock::new(HashMap::new())),
            step_count: AtomicU64::new(0),
            current_temperature: Arc::new(RwLock::new(initial_temperature)),
            energy_history: Arc::new(RwLock::new(Vec::new())),
            state: OptimizerState::default(),
        }
    }

    /// Create quantum annealing optimizer with preset configurations
    pub fn for_deep_learning() -> Self {
        let config = QuantumAnnealingConfig {
            learning_rate: 1e-3,
            initial_temperature: 0.5,
            final_temperature: 1e-5,
            annealing_steps: 20000,
            tunneling_strength: 0.05,
            superposition_states: 8,
            decoherence_rate: 0.005,
            ..Default::default()
        };
        Self::new(config)
    }

    /// Create quantum annealing optimizer for reinforcement learning
    pub fn for_reinforcement_learning() -> Self {
        let config = QuantumAnnealingConfig {
            learning_rate: 3e-4,
            initial_temperature: 2.0,
            final_temperature: 1e-4,
            annealing_steps: 50000,
            tunneling_strength: 0.15,
            superposition_states: 6,
            momentum: 0.95,
            energy_threshold: 1e-5,
            ..Default::default()
        };
        Self::new(config)
    }

    /// Update annealing temperature based on schedule
    fn update_temperature(&self) {
        let step = self.step_count.load(Ordering::Relaxed) as f32;
        let progress = (step / self.config.annealing_steps as f32).min(1.0);

        // Exponential cooling schedule
        let temp_ratio = self.config.final_temperature / self.config.initial_temperature;
        let new_temperature = self.config.initial_temperature * temp_ratio.powf(progress);

        *self.current_temperature.write() = new_temperature;
    }

    /// Calculate total system energy for quantum state monitoring
    #[allow(dead_code)]
    fn calculate_system_energy(&self, gradients: &HashMap<String, Tensor>) -> f32 {
        let quantum_states = self.quantum_states.read();
        let mut total_energy = 0.0;

        for (param_name, gradient) in gradients.iter() {
            if let Some(quantum_state) = quantum_states.get(param_name) {
                // Quantum energy contribution
                for (i, &energy) in quantum_state.energy_levels.iter().enumerate() {
                    total_energy += energy * quantum_state.probabilities[i];
                }

                // Classical gradient energy
                if let Ok(grad_data) = gradient.data() {
                    let grad_norm_squared: f32 = grad_data.iter().map(|&g| g * g).sum();
                    total_energy += grad_norm_squared * 0.5;
                }
            }
        }

        total_energy
    }

    /// Perform quantum measurement and collapse to classical parameters
    fn quantum_measurement(&self, param_name: &str) -> Option<f32> {
        let quantum_states = self.quantum_states.read();
        quantum_states.get(param_name).map(|state| state.collapse())
    }

    /// Apply quantum entanglement effects between parameters
    #[allow(dead_code)]
    fn apply_entanglement_effects(&self, param_name: &str, gradient: &mut Tensor) -> Result<()> {
        let quantum_states = self.quantum_states.read();

        if let Some(quantum_state) = quantum_states.get(param_name) {
            let mut entanglement_correction = 0.0;

            // Calculate entanglement corrections
            for (entangled_param, strength) in &quantum_state.entanglements {
                if let Some(entangled_state) = quantum_states.get(entangled_param) {
                    let correlation = entangled_state.collapse() * strength;
                    entanglement_correction += correlation;
                }
            }

            // Apply entanglement correction to gradient
            if entanglement_correction.abs() > 1e-8 {
                let correction_tensor = Tensor::scalar(entanglement_correction)?;
                *gradient = gradient.add(&correction_tensor)?;
            }
        }

        Ok(())
    }
}

impl Optimizer for QuantumAnnealingOptimizer {
    fn update(&mut self, parameter: &mut Tensor, grad: &Tensor) -> Result<()> {
        // For individual parameter updates
        let param_name = "param"; // Would be provided in real implementation

        // Initialize quantum state if not exists
        {
            let mut quantum_states = self.quantum_states.write();
            quantum_states
                .entry(param_name.to_string())
                .or_insert_with(|| QuantumState::new(self.config.superposition_states));
        }

        // Apply quantum-inspired update
        let current_temp = *self.current_temperature.read();

        // Evolve quantum state based on gradient
        if let Ok(grad_data) = grad.data() {
            let grad_norm: f32 = grad_data.iter().map(|&g| g * g).sum::<f32>().sqrt();

            let mut quantum_states = self.quantum_states.write();
            if let Some(quantum_state) = quantum_states.get_mut(param_name) {
                quantum_state.evolve(grad_norm, current_temp, self.config.tunneling_strength);
            }
        }

        // Classical momentum update
        {
            let mut momentum_states = self.classical_momentum.write();

            // Initialize momentum if needed
            if !momentum_states.contains_key(param_name) {
                let zero_momentum = Tensor::zeros_like(parameter)?;
                momentum_states.insert(param_name.to_string(), zero_momentum);
            }

            let momentum = momentum_states
                .get_mut(param_name)
                .expect("Momentum state must exist after initialization");

            // Update momentum with quantum correction
            let quantum_correction = self.quantum_measurement(param_name).unwrap_or(0.0);
            let corrected_gradient = grad.scalar_mul(1.0 + quantum_correction * 0.1)?;

            *momentum = momentum
                .scalar_mul(self.config.momentum)?
                .add(&corrected_gradient.scalar_mul(1.0 - self.config.momentum)?)?;

            // Apply weight decay
            let weight_decay_term = parameter.scalar_mul(self.config.weight_decay)?;

            // Final parameter update with quantum annealing schedule
            let effective_lr =
                self.config.learning_rate * (current_temp / self.config.initial_temperature);
            let update = momentum
                .scalar_mul(effective_lr)?
                .add(&weight_decay_term.scalar_mul(effective_lr)?)?;

            *parameter = parameter.sub(&update)?;
        }

        Ok(())
    }

    fn zero_grad(&mut self) {
        self.classical_momentum.write().clear();
    }

    fn step(&mut self) {
        self.update_temperature();
        self.step_count.fetch_add(1, Ordering::Relaxed);
    }

    fn get_lr(&self) -> f32 {
        let current_temp = *self.current_temperature.read();
        self.config.learning_rate * (current_temp / self.config.initial_temperature)
    }

    fn set_lr(&mut self, _lr: f32) {
        // This would update the config learning rate if we had mutable access
        // For now, we'll use the temperature-based scaling
    }
}

impl StatefulOptimizer for QuantumAnnealingOptimizer {
    type Config = QuantumAnnealingConfig;
    type State = OptimizerState;

    fn config(&self) -> &Self::Config {
        &self.config
    }

    fn state(&self) -> &Self::State {
        &self.state
    }

    fn state_mut(&mut self) -> &mut Self::State {
        &mut self.state
    }

    fn state_dict(&self) -> Result<HashMap<String, Tensor>> {
        let mut state = HashMap::new();

        // Save quantum states as classical approximations
        let quantum_states = self.quantum_states.read();
        for (name, q_state) in quantum_states.iter() {
            if let Ok(tensor) =
                Tensor::from_vec(q_state.amplitudes.clone(), &[q_state.amplitudes.len()])
            {
                state.insert(format!("{}_quantum_amplitudes", name), tensor);
            }
            if let Ok(tensor) = Tensor::from_vec(q_state.phases.clone(), &[q_state.phases.len()]) {
                state.insert(format!("{}_quantum_phases", name), tensor);
            }
        }

        // Save classical momentum
        let momentum_states = self.classical_momentum.read();
        for (name, momentum) in momentum_states.iter() {
            state.insert(format!("{}_momentum", name), momentum.clone());
        }

        // Save temperature and step count
        if let Ok(temp_tensor) = Tensor::scalar(*self.current_temperature.read()) {
            state.insert("current_temperature".to_string(), temp_tensor);
        }
        if let Ok(step_tensor) = Tensor::scalar(self.step_count.load(Ordering::Relaxed) as f32) {
            state.insert("step_count".to_string(), step_tensor);
        }

        Ok(state)
    }

    fn load_state_dict(&mut self, state: HashMap<String, Tensor>) -> Result<()> {
        // Load temperature and step count
        if let Some(temp_tensor) = state.get("current_temperature") {
            if let Ok(temp_data) = temp_tensor.data() {
                if !temp_data.is_empty() {
                    *self.current_temperature.write() = temp_data[0];
                }
            }
        }

        if let Some(step_tensor) = state.get("step_count") {
            if let Ok(step_data) = step_tensor.data() {
                if !step_data.is_empty() {
                    self.step_count.store(step_data[0] as u64, Ordering::Relaxed);
                }
            }
        }

        // Load classical momentum states
        let mut momentum_states = self.classical_momentum.write();
        for (key, tensor) in &state {
            if let Some(param_name) = key.strip_suffix("_momentum") {
                momentum_states.insert(param_name.to_string(), tensor.clone());
            }
        }

        // Load quantum states (simplified reconstruction)
        let mut quantum_states = self.quantum_states.write();
        for (key, tensor) in &state {
            if let Some(param_name) = key.strip_suffix("_quantum_amplitudes") {
                if let Ok(amplitudes) = tensor.data() {
                    let q_state = quantum_states
                        .entry(param_name.to_string())
                        .or_insert_with(|| QuantumState::new(self.config.superposition_states));

                    if amplitudes.len() <= q_state.amplitudes.len() {
                        q_state.amplitudes[..amplitudes.len()].copy_from_slice(&amplitudes);
                    }
                }
            }
        }

        Ok(())
    }

    fn memory_usage(&self) -> StateMemoryStats {
        let quantum_states = self.quantum_states.read();
        let momentum_states = self.classical_momentum.read();

        let quantum_memory = quantum_states.len() * self.config.superposition_states * 4 * 4; // 4 vectors of f32
        let momentum_memory = momentum_states.len() * 1000; // Estimate
        let _total_bytes = quantum_memory + momentum_memory + 1024; // Base overhead

        StateMemoryStats {
            momentum_elements: momentum_memory / std::mem::size_of::<f32>(),
            variance_elements: 0,
            third_moment_elements: 0,
            total_bytes: momentum_memory + quantum_memory,
            num_parameters: self.state.momentum.len(),
        }
    }

    fn reset_state(&mut self) {
        self.quantum_states.write().clear();
        self.classical_momentum.write().clear();
        self.step_count.store(0, Ordering::Relaxed);
        *self.current_temperature.write() = self.config.initial_temperature;
        self.energy_history.write().clear();
    }

    fn num_parameters(&self) -> usize {
        self.quantum_states.read().len()
    }
}

/// Statistics for quantum annealing optimizer
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QuantumAnnealingStats {
    pub current_temperature: f32,
    pub step_count: u64,
    pub system_energy: f32,
    pub quantum_coherence: f32,
    pub tunneling_events: u64,
    pub entanglement_strength: f32,
}

impl QuantumAnnealingOptimizer {
    /// Get the learning rate
    pub fn learning_rate(&self) -> f32 {
        self.config.learning_rate
    }

    pub fn get_stats(&self) -> QuantumAnnealingStats {
        let quantum_states = self.quantum_states.read();
        let energy_history = self.energy_history.read();

        // Calculate quantum coherence (measure of quantum effects)
        let mut total_coherence = 0.0;
        let mut total_states = 0;

        for q_state in quantum_states.values() {
            let coherence: f32 = q_state
                .amplitudes
                .iter()
                .zip(q_state.phases.iter())
                .map(|(&amp, &phase)| amp * phase.cos())
                .sum();
            total_coherence += coherence.abs();
            total_states += 1;
        }

        let avg_coherence =
            if total_states > 0 { total_coherence / total_states as f32 } else { 0.0 };

        QuantumAnnealingStats {
            current_temperature: *self.current_temperature.read(),
            step_count: self.step_count.load(Ordering::Relaxed),
            system_energy: energy_history.last().copied().unwrap_or(0.0),
            quantum_coherence: avg_coherence,
            tunneling_events: 0,        // Would be tracked in implementation
            entanglement_strength: 0.0, // Would be calculated from entanglement map
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_quantum_annealing_creation() {
        let optimizer = QuantumAnnealingOptimizer::new(QuantumAnnealingConfig::default());
        assert_eq!(optimizer.get_lr(), 1e-3);

        let stats = optimizer.get_stats();
        assert_eq!(stats.step_count, 0);
        assert!((stats.current_temperature - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_quantum_state_creation() {
        let q_state = QuantumState::new(4);
        assert_eq!(q_state.amplitudes.len(), 4);
        assert_eq!(q_state.phases.len(), 4);
        assert_eq!(q_state.probabilities.len(), 4);

        // Check probability normalization
        let sum: f32 = q_state.probabilities.iter().sum();
        assert!((sum - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_quantum_state_collapse() {
        let q_state = QuantumState::new(4);

        // Test multiple collapses
        for _ in 0..100 {
            let value = q_state.collapse();
            assert!(value.is_finite());
        }
    }

    #[test]
    fn test_presets() {
        let dl_optimizer = QuantumAnnealingOptimizer::for_deep_learning();
        assert_eq!(dl_optimizer.config.superposition_states, 8);
        assert!((dl_optimizer.config.initial_temperature - 0.5).abs() < 1e-6);

        let rl_optimizer = QuantumAnnealingOptimizer::for_reinforcement_learning();
        assert_eq!(rl_optimizer.config.superposition_states, 6);
        assert!((rl_optimizer.config.momentum - 0.95).abs() < 1e-6);
    }

    #[test]
    fn test_temperature_annealing() {
        let optimizer = QuantumAnnealingOptimizer::new(QuantumAnnealingConfig {
            initial_temperature: 1.0,
            final_temperature: 0.1,
            annealing_steps: 1000,
            ..Default::default()
        });

        let initial_temp = *optimizer.current_temperature.read();

        // Simulate some steps
        optimizer.step_count.store(500, Ordering::Relaxed);
        optimizer.update_temperature();

        let mid_temp = *optimizer.current_temperature.read();
        assert!(mid_temp < initial_temp);
        assert!(mid_temp > optimizer.config.final_temperature);
    }

    #[test]
    fn test_state_dict_operations() {
        let optimizer = QuantumAnnealingOptimizer::new(QuantumAnnealingConfig::default());

        // Set some state
        optimizer.step_count.store(42, Ordering::Relaxed);
        *optimizer.current_temperature.write() = 0.5;

        // Save state
        let state_dict = optimizer.state_dict().expect("Failed to get state dict");
        assert!(state_dict.contains_key("step_count"));
        assert!(state_dict.contains_key("current_temperature"));

        // Create new optimizer and load state
        let mut new_optimizer = QuantumAnnealingOptimizer::new(QuantumAnnealingConfig::default());
        assert!(new_optimizer.load_state_dict(state_dict).is_ok());

        assert_eq!(new_optimizer.step_count.load(Ordering::Relaxed), 42);
        assert!(((*new_optimizer.current_temperature.read()) - 0.5).abs() < 1e-6);
    }

    #[test]
    fn test_stateful_optimizer_traits() {
        let optimizer = QuantumAnnealingOptimizer::for_deep_learning();

        // Test config access
        assert_eq!(optimizer.config().superposition_states, 8);

        // Test memory usage
        let memory_stats = optimizer.memory_usage();
        // Verify memory stats are accessible (usize values are always non-negative)
        let _total = memory_stats.total_bytes;
        let _params = memory_stats.num_parameters;

        // Test num parameters
        assert_eq!(optimizer.num_parameters(), 0); // No parameters registered yet
    }
}