quantrs2-device 0.1.3

Quantum device connectors for the QuantRS2 framework
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
//! Optical tweezer array management for neutral atom quantum computing
//!
//! This module provides implementations for managing optical tweezer arrays,
//! including atom loading, positioning, manipulation, and optimization.

use crate::{DeviceError, DeviceResult};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::Duration;

/// Optical tweezer configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TweezerArrayConfig {
    /// Array dimensions (x, y, z)
    pub array_dimensions: (usize, usize, usize),
    /// Tweezer spacing (μm)
    pub tweezer_spacing: (f64, f64, f64),
    /// Laser wavelength (nm)
    pub laser_wavelength: f64,
    /// Beam waist (μm)
    pub beam_waist: f64,
    /// Maximum laser power (mW)
    pub max_laser_power: f64,
    /// Trap depth (μK)
    pub trap_depth: f64,
    /// Loading efficiency
    pub loading_efficiency: f64,
    /// Movement precision (nm)
    pub movement_precision: f64,
}

/// Tweezer position in 3D space
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct TweezerPosition {
    /// X coordinate (μm)
    pub x: f64,
    /// Y coordinate (μm)
    pub y: f64,
    /// Z coordinate (μm)
    pub z: f64,
}

/// Atom state in a tweezer
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum AtomState {
    /// No atom present
    Empty,
    /// Atom loaded successfully
    Loaded,
    /// Atom loading failed
    LoadingFailed,
    /// Atom lost during operation
    Lost,
    /// Atom in unknown state
    Unknown,
}

/// Individual tweezer information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TweezerInfo {
    /// Tweezer ID
    pub tweezer_id: usize,
    /// Current position
    pub position: TweezerPosition,
    /// Target position (for movement operations)
    pub target_position: Option<TweezerPosition>,
    /// Current laser power (mW)
    pub laser_power: f64,
    /// Atom state
    pub atom_state: AtomState,
    /// Loading attempt count
    pub loading_attempts: usize,
    /// Last update timestamp
    pub last_update: std::time::SystemTime,
}

/// Tweezer array state
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TweezerArrayState {
    /// Configuration
    pub config: TweezerArrayConfig,
    /// Individual tweezers
    pub tweezers: HashMap<usize, TweezerInfo>,
    /// Array loading statistics
    pub loading_stats: LoadingStatistics,
    /// Movement operations in progress
    pub active_movements: Vec<MovementOperation>,
}

/// Loading statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoadingStatistics {
    /// Total loading attempts
    pub total_attempts: usize,
    /// Successful loads
    pub successful_loads: usize,
    /// Failed loads
    pub failed_loads: usize,
    /// Current fill factor
    pub fill_factor: f64,
    /// Average loading time
    pub average_loading_time: Duration,
}

/// Movement operation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MovementOperation {
    /// Operation ID
    pub operation_id: String,
    /// Tweezer ID
    pub tweezer_id: usize,
    /// Start position
    pub start_position: TweezerPosition,
    /// End position
    pub end_position: TweezerPosition,
    /// Movement parameters
    pub parameters: MovementParameters,
    /// Start time
    pub start_time: std::time::SystemTime,
    /// Expected completion time
    pub expected_completion: std::time::SystemTime,
    /// Current status
    pub status: MovementStatus,
}

/// Movement parameters
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MovementParameters {
    /// Movement speed (μm/s)
    pub speed: f64,
    /// Acceleration (μm/s²)
    pub acceleration: f64,
    /// Movement trajectory
    pub trajectory: MovementTrajectory,
    /// Power ramping during movement
    pub power_ramping: PowerRampingConfig,
}

/// Movement trajectory types
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum MovementTrajectory {
    /// Direct linear path
    Linear,
    /// Smooth S-curve trajectory
    SCurve,
    /// Parabolic trajectory
    Parabolic,
    /// Custom waypoint-based trajectory
    Waypoints(Vec<TweezerPosition>),
}

/// Power ramping configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PowerRampingConfig {
    /// Enable power ramping
    pub enabled: bool,
    /// Initial power factor
    pub initial_factor: f64,
    /// Final power factor
    pub final_factor: f64,
    /// Ramping duration
    pub ramping_duration: Duration,
}

/// Movement operation status
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum MovementStatus {
    /// Movement is queued
    Queued,
    /// Movement is in progress
    InProgress,
    /// Movement completed successfully
    Completed,
    /// Movement failed
    Failed,
    /// Movement was cancelled
    Cancelled,
}

/// Tweezer array manager
pub struct TweezerArrayManager {
    state: TweezerArrayState,
    movement_queue: Vec<MovementOperation>,
    optimization_settings: OptimizationSettings,
}

/// Array optimization settings
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OptimizationSettings {
    /// Enable automatic reloading
    pub auto_reload: bool,
    /// Maximum reloading attempts
    pub max_reload_attempts: usize,
    /// Enable position optimization
    pub position_optimization: bool,
    /// Enable power optimization
    pub power_optimization: bool,
    /// Optimization interval
    pub optimization_interval: Duration,
}

impl TweezerArrayManager {
    /// Create a new tweezer array manager
    pub fn new(config: TweezerArrayConfig) -> Self {
        let total_tweezers =
            config.array_dimensions.0 * config.array_dimensions.1 * config.array_dimensions.2;
        let mut tweezers = HashMap::new();

        // Initialize tweezers
        for i in 0..total_tweezers {
            let (x_idx, y_idx, z_idx) = Self::index_to_coordinates(i, &config.array_dimensions);
            let position = TweezerPosition {
                x: x_idx as f64 * config.tweezer_spacing.0,
                y: y_idx as f64 * config.tweezer_spacing.1,
                z: z_idx as f64 * config.tweezer_spacing.2,
            };

            let tweezer_info = TweezerInfo {
                tweezer_id: i,
                position,
                target_position: None,
                laser_power: 0.0,
                atom_state: AtomState::Empty,
                loading_attempts: 0,
                last_update: std::time::SystemTime::now(),
            };

            tweezers.insert(i, tweezer_info);
        }

        let state = TweezerArrayState {
            config,
            tweezers,
            loading_stats: LoadingStatistics::default(),
            active_movements: Vec::new(),
        };

        Self {
            state,
            movement_queue: Vec::new(),
            optimization_settings: OptimizationSettings::default(),
        }
    }

    /// Convert linear index to 3D coordinates
    const fn index_to_coordinates(
        index: usize,
        dimensions: &(usize, usize, usize),
    ) -> (usize, usize, usize) {
        let z_idx = index / (dimensions.0 * dimensions.1);
        let y_idx = (index % (dimensions.0 * dimensions.1)) / dimensions.0;
        let x_idx = index % dimensions.0;
        (x_idx, y_idx, z_idx)
    }

    /// Convert 3D coordinates to linear index
    const fn coordinates_to_index(
        x: usize,
        y: usize,
        z: usize,
        dimensions: &(usize, usize, usize),
    ) -> usize {
        z * dimensions.0 * dimensions.1 + y * dimensions.0 + x
    }

    /// Load atoms into specified tweezers
    pub async fn load_atoms(&mut self, tweezer_ids: &[usize]) -> DeviceResult<LoadingResult> {
        let mut results = HashMap::new();
        let start_time = std::time::SystemTime::now();

        for &tweezer_id in tweezer_ids {
            let loading_result = self.load_single_atom(tweezer_id).await?;
            results.insert(tweezer_id, loading_result);
        }

        // Update loading statistics
        self.update_loading_statistics(&results, start_time);

        Ok(LoadingResult {
            attempted_tweezers: tweezer_ids.to_vec(),
            successful_loads: results.values().filter(|&&success| success).count(),
            failed_loads: results.values().filter(|&&success| !success).count(),
            individual_results: results,
            loading_time: start_time.elapsed().unwrap_or(Duration::ZERO),
        })
    }

    /// Load a single atom into a tweezer
    async fn load_single_atom(&mut self, tweezer_id: usize) -> DeviceResult<bool> {
        // First, get the tweezer info for loading calculation
        let (loading_attempts, base_success_rate) = {
            let tweezer = self.state.tweezers.get(&tweezer_id).ok_or_else(|| {
                DeviceError::InvalidInput(format!("Tweezer {tweezer_id} not found"))
            })?;
            (
                tweezer.loading_attempts,
                self.state.config.loading_efficiency,
            )
        };

        // Calculate loading success
        let loading_success =
            self.simulate_atom_loading_from_params(loading_attempts, base_success_rate);

        // Now update the tweezer
        let tweezer =
            self.state.tweezers.get_mut(&tweezer_id).ok_or_else(|| {
                DeviceError::InvalidInput(format!("Tweezer {tweezer_id} not found"))
            })?;

        tweezer.loading_attempts += 1;
        tweezer.last_update = std::time::SystemTime::now();
        tweezer.atom_state = if loading_success {
            AtomState::Loaded
        } else {
            AtomState::LoadingFailed
        };

        Ok(loading_success)
    }

    /// Simulate atom loading (mock implementation)
    fn simulate_atom_loading(&self, tweezer: &TweezerInfo) -> bool {
        self.simulate_atom_loading_from_params(
            tweezer.loading_attempts,
            self.state.config.loading_efficiency,
        )
    }

    /// Simulate atom loading from parameters
    fn simulate_atom_loading_from_params(
        &self,
        loading_attempts: usize,
        base_success_rate: f64,
    ) -> bool {
        // Simple mock based on loading efficiency and number of attempts
        let attempt_penalty = 0.05 * (loading_attempts as f64).max(1.0);
        let effective_success_rate = (base_success_rate - attempt_penalty).max(0.1);

        // Mock random decision
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let mut hasher = DefaultHasher::new();
        loading_attempts.hash(&mut hasher);
        let hash = hasher.finish();
        let random_value = (hash % 1000) as f64 / 1000.0;

        random_value < effective_success_rate
    }

    /// Move an atom from one position to another
    pub async fn move_atom(
        &mut self,
        tweezer_id: usize,
        target_position: TweezerPosition,
        parameters: MovementParameters,
    ) -> DeviceResult<String> {
        let tweezer =
            self.state.tweezers.get(&tweezer_id).ok_or_else(|| {
                DeviceError::InvalidInput(format!("Tweezer {tweezer_id} not found"))
            })?;

        if tweezer.atom_state != AtomState::Loaded {
            return Err(DeviceError::InvalidInput(
                "Cannot move atom: no atom loaded in tweezer".to_string(),
            ));
        }

        let movement_time =
            self.calculate_movement_time(&tweezer.position, &target_position, &parameters);
        let operation_id = format!("move_{}", uuid::Uuid::new_v4());

        let movement_op = MovementOperation {
            operation_id: operation_id.clone(),
            tweezer_id,
            start_position: tweezer.position,
            end_position: target_position,
            parameters,
            start_time: std::time::SystemTime::now(),
            expected_completion: std::time::SystemTime::now() + movement_time,
            status: MovementStatus::Queued,
        };

        self.movement_queue.push(movement_op);
        Ok(operation_id)
    }

    /// Calculate movement time based on distance and parameters
    fn calculate_movement_time(
        &self,
        start: &TweezerPosition,
        end: &TweezerPosition,
        parameters: &MovementParameters,
    ) -> Duration {
        let distance = Self::calculate_distance(start, end);
        let time_seconds = distance / parameters.speed;
        Duration::from_secs_f64(time_seconds)
    }

    /// Calculate distance between two positions
    fn calculate_distance(pos1: &TweezerPosition, pos2: &TweezerPosition) -> f64 {
        let dx = pos2.x - pos1.x;
        let dy = pos2.y - pos1.y;
        let dz = pos2.z - pos1.z;
        dz.mul_add(dz, dx.mul_add(dx, dy * dy)).sqrt()
    }

    /// Process movement operations
    pub async fn process_movements(&mut self) -> DeviceResult<()> {
        let mut operations_to_start = Vec::new();
        let mut operations_to_complete = Vec::new();
        let mut completed_indices = Vec::new();

        // First pass: identify operations that need processing
        for (i, movement) in self.movement_queue.iter_mut().enumerate() {
            match movement.status {
                MovementStatus::Queued => {
                    movement.status = MovementStatus::InProgress;
                    operations_to_start.push(movement.clone());
                }
                MovementStatus::InProgress => {
                    if std::time::SystemTime::now() >= movement.expected_completion {
                        movement.status = MovementStatus::Completed;
                        operations_to_complete.push(movement.clone());
                        completed_indices.push(i);
                    }
                }
                _ => {}
            }
        }

        // Second pass: execute the operations
        for operation in operations_to_start {
            self.start_movement_execution(&operation).await?;
        }

        for operation in operations_to_complete {
            self.complete_movement(&operation)?;
        }

        // Remove completed operations
        for &i in completed_indices.iter().rev() {
            self.movement_queue.remove(i);
        }

        Ok(())
    }

    /// Start executing a movement operation
    async fn start_movement_execution(&mut self, movement: &MovementOperation) -> DeviceResult<()> {
        // In real implementation, this would start the actual hardware movement
        // For now, just update the target position
        if let Some(tweezer) = self.state.tweezers.get_mut(&movement.tweezer_id) {
            tweezer.target_position = Some(movement.end_position);
        }
        Ok(())
    }

    /// Complete a movement operation
    fn complete_movement(&mut self, movement: &MovementOperation) -> DeviceResult<()> {
        if let Some(tweezer) = self.state.tweezers.get_mut(&movement.tweezer_id) {
            tweezer.position = movement.end_position;
            tweezer.target_position = None;
            tweezer.last_update = std::time::SystemTime::now();
        }
        Ok(())
    }

    /// Update loading statistics
    fn update_loading_statistics(
        &mut self,
        results: &HashMap<usize, bool>,
        start_time: std::time::SystemTime,
    ) {
        let successful = results.values().filter(|&&success| success).count();
        let failed = results.values().filter(|&&success| !success).count();
        let loading_time = start_time.elapsed().unwrap_or(Duration::ZERO);

        self.state.loading_stats.total_attempts += results.len();
        self.state.loading_stats.successful_loads += successful;
        self.state.loading_stats.failed_loads += failed;

        // Update average loading time
        let total_ops = self.state.loading_stats.total_attempts;
        let current_avg = self.state.loading_stats.average_loading_time;
        let new_avg = (current_avg * (total_ops - 1) as u32 + loading_time) / total_ops as u32;
        self.state.loading_stats.average_loading_time = new_avg;

        // Update fill factor
        let loaded_count = self
            .state
            .tweezers
            .values()
            .filter(|t| t.atom_state == AtomState::Loaded)
            .count();
        self.state.loading_stats.fill_factor =
            loaded_count as f64 / self.state.tweezers.len() as f64;
    }

    /// Get current array state
    pub const fn get_array_state(&self) -> &TweezerArrayState {
        &self.state
    }

    /// Get loading statistics
    pub const fn get_loading_statistics(&self) -> &LoadingStatistics {
        &self.state.loading_stats
    }

    /// Get atom positions
    pub fn get_atom_positions(&self) -> Vec<(usize, TweezerPosition)> {
        self.state
            .tweezers
            .iter()
            .filter(|(_, tweezer)| tweezer.atom_state == AtomState::Loaded)
            .map(|(&id, tweezer)| (id, tweezer.position))
            .collect()
    }

    /// Optimize array configuration
    pub async fn optimize_array(&mut self) -> DeviceResult<OptimizationResult> {
        let mut optimizations_applied = Vec::new();

        if self.optimization_settings.auto_reload {
            let reload_count = self.auto_reload_failed_tweezers().await?;
            if reload_count > 0 {
                optimizations_applied.push(format!("Reloaded {reload_count} failed tweezers"));
            }
        }

        if self.optimization_settings.position_optimization {
            let position_adjustments = self.optimize_positions().await?;
            if position_adjustments > 0 {
                optimizations_applied.push(format!("Adjusted {position_adjustments} positions"));
            }
        }

        if self.optimization_settings.power_optimization {
            let power_adjustments = self.optimize_power_levels().await?;
            if power_adjustments > 0 {
                optimizations_applied.push(format!("Optimized {power_adjustments} power levels"));
            }
        }

        Ok(OptimizationResult {
            optimizations_applied,
            fill_factor_improvement: 0.0, // Would calculate actual improvement
            loading_efficiency_improvement: 0.0,
        })
    }

    /// Auto-reload failed tweezers
    async fn auto_reload_failed_tweezers(&mut self) -> DeviceResult<usize> {
        let failed_tweezer_ids: Vec<usize> = self
            .state
            .tweezers
            .iter()
            .filter(|(_, tweezer)| {
                tweezer.atom_state == AtomState::LoadingFailed
                    && tweezer.loading_attempts < self.optimization_settings.max_reload_attempts
            })
            .map(|(&id, _)| id)
            .collect();

        if !failed_tweezer_ids.is_empty() {
            self.load_atoms(&failed_tweezer_ids).await?;
        }

        Ok(failed_tweezer_ids.len())
    }

    /// Optimize atom positions
    async fn optimize_positions(&mut self) -> DeviceResult<usize> {
        // Mock implementation - would implement actual position optimization
        Ok(0)
    }

    /// Optimize laser power levels
    async fn optimize_power_levels(&mut self) -> DeviceResult<usize> {
        // Mock implementation - would implement actual power optimization
        Ok(0)
    }
}

/// Loading operation result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoadingResult {
    /// Tweezers that were attempted
    pub attempted_tweezers: Vec<usize>,
    /// Number of successful loads
    pub successful_loads: usize,
    /// Number of failed loads
    pub failed_loads: usize,
    /// Individual results for each tweezer
    pub individual_results: HashMap<usize, bool>,
    /// Total loading time
    pub loading_time: Duration,
}

/// Optimization result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OptimizationResult {
    /// Optimizations that were applied
    pub optimizations_applied: Vec<String>,
    /// Fill factor improvement
    pub fill_factor_improvement: f64,
    /// Loading efficiency improvement
    pub loading_efficiency_improvement: f64,
}

impl Default for TweezerArrayConfig {
    fn default() -> Self {
        Self {
            array_dimensions: (10, 10, 1),
            tweezer_spacing: (5.0, 5.0, 0.0),
            laser_wavelength: 1064.0,
            beam_waist: 1.0,
            max_laser_power: 100.0,
            trap_depth: 1000.0,
            loading_efficiency: 0.8,
            movement_precision: 10.0,
        }
    }
}

impl Default for LoadingStatistics {
    fn default() -> Self {
        Self {
            total_attempts: 0,
            successful_loads: 0,
            failed_loads: 0,
            fill_factor: 0.0,
            average_loading_time: Duration::ZERO,
        }
    }
}

impl Default for OptimizationSettings {
    fn default() -> Self {
        Self {
            auto_reload: true,
            max_reload_attempts: 3,
            position_optimization: false,
            power_optimization: false,
            optimization_interval: Duration::from_secs(60),
        }
    }
}

impl Default for MovementParameters {
    fn default() -> Self {
        Self {
            speed: 10.0,       // μm/s
            acceleration: 5.0, // μm/s²
            trajectory: MovementTrajectory::Linear,
            power_ramping: PowerRampingConfig::default(),
        }
    }
}

impl Default for PowerRampingConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            initial_factor: 1.0,
            final_factor: 1.0,
            ramping_duration: Duration::from_millis(100),
        }
    }
}

/// Create a basic tweezer array configuration
pub fn create_basic_array_config(rows: usize, cols: usize, spacing: f64) -> TweezerArrayConfig {
    TweezerArrayConfig {
        array_dimensions: (rows, cols, 1),
        tweezer_spacing: (spacing, spacing, 0.0),
        ..Default::default()
    }
}

/// Create movement parameters for fast movement
pub const fn create_fast_movement_params() -> MovementParameters {
    MovementParameters {
        speed: 50.0,
        acceleration: 20.0,
        trajectory: MovementTrajectory::SCurve,
        power_ramping: PowerRampingConfig {
            enabled: true,
            initial_factor: 1.2,
            final_factor: 1.0,
            ramping_duration: Duration::from_millis(50),
        },
    }
}

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

    #[test]
    fn test_tweezer_array_creation() {
        let config = TweezerArrayConfig::default();
        let manager = TweezerArrayManager::new(config);

        assert_eq!(manager.state.tweezers.len(), 100); // 10x10x1 array
        assert!(manager
            .state
            .tweezers
            .values()
            .all(|t| t.atom_state == AtomState::Empty));
    }

    #[test]
    fn test_coordinate_conversion() {
        let dimensions = (3, 3, 2);

        // Test index to coordinates
        let (x, y, z) = TweezerArrayManager::index_to_coordinates(10, &dimensions);
        assert_eq!((x, y, z), (1, 0, 1));

        // Test coordinates to index
        let index = TweezerArrayManager::coordinates_to_index(1, 0, 1, &dimensions);
        assert_eq!(index, 10);
    }

    #[test]
    fn test_distance_calculation() {
        let pos1 = TweezerPosition {
            x: 0.0,
            y: 0.0,
            z: 0.0,
        };
        let pos2 = TweezerPosition {
            x: 3.0,
            y: 4.0,
            z: 0.0,
        };

        let distance = TweezerArrayManager::calculate_distance(&pos1, &pos2);
        assert_eq!(distance, 5.0);
    }
}

// Mock UUID implementation
mod uuid {
    use std::fmt;

    pub struct Uuid([u8; 16]);

    impl Uuid {
        pub fn new_v4() -> Self {
            use std::collections::hash_map::DefaultHasher;
            use std::hash::{Hash, Hasher};
            use std::time::SystemTime;

            let mut hasher = DefaultHasher::new();
            SystemTime::now().hash(&mut hasher);
            let hash = hasher.finish();

            let mut bytes = [0u8; 16];
            bytes[0..8].copy_from_slice(&hash.to_le_bytes());
            bytes[8..16].copy_from_slice(&hash.to_be_bytes());

            Self(bytes)
        }
    }

    impl fmt::Display for Uuid {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "{:02x}{:02x}{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
                self.0[0], self.0[1], self.0[2], self.0[3],
                self.0[4], self.0[5],
                self.0[6], self.0[7],
                self.0[8], self.0[9],
                self.0[10], self.0[11], self.0[12], self.0[13], self.0[14], self.0[15])
        }
    }
}