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
//! Photonic quantum device implementation
//!
//! This module provides the core implementation of photonic quantum computing devices,
//! supporting continuous variable, gate-based, and measurement-based quantum computing.

use async_trait::async_trait;
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use std::time::{Duration, Instant};
use thiserror::Error;

use quantrs2_circuit::prelude::Circuit;
use quantrs2_core::qubit::QubitId;

use super::{
    validate_photonic_config, PhotonicCircuitResult, PhotonicClient, PhotonicDeviceConfig,
    PhotonicExecutionMetadata, PhotonicMeasurementData, PhotonicQuantumDevice, PhotonicSystemType,
};
use crate::{CircuitExecutor, CircuitResult, DeviceError, DeviceResult, QuantumDevice};
use scirs2_core::random::prelude::*;

/// Photonic quantum device implementation
#[derive(Debug, Clone)]
pub struct PhotonicQuantumDeviceImpl {
    /// Device identifier
    pub device_id: String,
    /// Client for hardware communication
    pub client: PhotonicClient,
    /// Device configuration
    pub config: PhotonicDeviceConfig,
    /// Device capabilities cache
    capabilities: Arc<RwLock<Option<PhotonicCapabilities>>>,
    /// Calibration data
    calibration: Arc<RwLock<PhotonicCalibrationData>>,
    /// Performance metrics
    metrics: Arc<RwLock<PhotonicPerformanceMetrics>>,
}

/// Photonic device capabilities
#[derive(Debug, Clone)]
pub struct PhotonicCapabilities {
    /// Supported system types
    pub supported_systems: Vec<PhotonicSystemType>,
    /// Maximum number of modes
    pub max_modes: usize,
    /// Maximum cutoff dimension
    pub max_cutoff: usize,
    /// Supported gate operations
    pub supported_gates: Vec<String>,
    /// Maximum squeezing parameter
    pub max_squeezing: f64,
    /// Minimum detection efficiency
    pub min_detection_efficiency: f64,
    /// Supported measurement types
    pub supported_measurements: Vec<String>,
    /// Hardware-specific features
    pub hardware_features: HashMap<String, bool>,
}

/// Photonic calibration data
#[derive(Debug, Clone)]
pub struct PhotonicCalibrationData {
    /// Mode-specific loss rates
    pub mode_losses: HashMap<usize, f64>,
    /// Beamsplitter transmittances
    pub beamsplitter_transmittances: HashMap<(usize, usize), f64>,
    /// Detector efficiencies
    pub detector_efficiencies: HashMap<usize, f64>,
    /// Phase shifter accuracies
    pub phase_accuracies: HashMap<usize, f64>,
    /// Squeezing parameters
    pub squeezing_calibration: HashMap<usize, (f64, f64)>,
    /// Cross-talk measurements
    pub crosstalk_matrix: HashMap<(usize, usize), f64>,
    /// Last calibration time
    pub last_calibration: Instant,
    /// Calibration validity duration
    pub validity_duration: Duration,
}

/// Performance metrics for photonic devices
#[derive(Debug, Clone)]
pub struct PhotonicPerformanceMetrics {
    /// Total circuits executed
    pub circuits_executed: u64,
    /// Average execution time
    pub avg_execution_time: Duration,
    /// Success rate
    pub success_rate: f64,
    /// Average fidelity
    pub avg_fidelity: f64,
    /// Mode utilization statistics
    pub mode_utilization: HashMap<usize, f64>,
    /// Gate operation counts
    pub gate_counts: HashMap<String, u64>,
    /// Error rates by operation type
    pub error_rates: HashMap<String, f64>,
}

impl PhotonicQuantumDeviceImpl {
    /// Create a new photonic quantum device
    pub async fn new(
        device_id: String,
        client: PhotonicClient,
        config: PhotonicDeviceConfig,
    ) -> DeviceResult<Self> {
        // Validate configuration
        validate_photonic_config(&config)?;

        let device = Self {
            device_id,
            client,
            config,
            capabilities: Arc::new(RwLock::new(None)),
            calibration: Arc::new(RwLock::new(PhotonicCalibrationData::default())),
            metrics: Arc::new(RwLock::new(PhotonicPerformanceMetrics::default())),
        };

        // Initialize device
        device.initialize().await?;

        Ok(device)
    }

    /// Initialize the device
    async fn initialize(&self) -> DeviceResult<()> {
        // Load capabilities
        let capabilities = self.load_capabilities().await?;
        *self
            .capabilities
            .write()
            .map_err(|e| DeviceError::LockError(format!("Capabilities lock poisoned: {e}")))? =
            Some(capabilities);

        // Load calibration data
        let calibration = self.load_calibration_data().await?;
        *self
            .calibration
            .write()
            .map_err(|e| DeviceError::LockError(format!("Calibration lock poisoned: {e}")))? =
            calibration;

        Ok(())
    }

    /// Load device capabilities
    async fn load_capabilities(&self) -> DeviceResult<PhotonicCapabilities> {
        let mut supported_systems = vec![PhotonicSystemType::ContinuousVariable];

        // Check system-specific capabilities
        match self.config.system_type {
            PhotonicSystemType::ContinuousVariable => {
                supported_systems.push(PhotonicSystemType::ContinuousVariable);
            }
            PhotonicSystemType::GateBased => {
                supported_systems.push(PhotonicSystemType::GateBased);
            }
            PhotonicSystemType::MeasurementBased => {
                supported_systems.push(PhotonicSystemType::MeasurementBased);
            }
            PhotonicSystemType::Hybrid => {
                supported_systems.extend(&[
                    PhotonicSystemType::ContinuousVariable,
                    PhotonicSystemType::GateBased,
                    PhotonicSystemType::MeasurementBased,
                ]);
            }
        }

        let supported_gates = vec![
            "displacement".to_string(),
            "squeezing".to_string(),
            "two_mode_squeezing".to_string(),
            "beamsplitter".to_string(),
            "phase_rotation".to_string(),
            "kerr".to_string(),
            "cross_kerr".to_string(),
            "homodyne".to_string(),
            "heterodyne".to_string(),
        ];

        let supported_measurements = vec![
            "homodyne".to_string(),
            "heterodyne".to_string(),
            "photon_counting".to_string(),
            "quadrature".to_string(),
        ];

        let mut hardware_features = HashMap::new();
        hardware_features.insert("squeezed_light_source".to_string(), true);
        hardware_features.insert("programmable_beamsplitters".to_string(), true);
        hardware_features.insert("high_efficiency_detectors".to_string(), true);
        hardware_features.insert(
            "real_time_feedback".to_string(),
            self.config.hardware_acceleration,
        );

        Ok(PhotonicCapabilities {
            supported_systems,
            max_modes: self.config.mode_count * 2, // Allow for expansion
            max_cutoff: self.config.cutoff_dimension.unwrap_or(20),
            supported_gates,
            max_squeezing: 10.0, // dB
            min_detection_efficiency: 0.8,
            supported_measurements,
            hardware_features,
        })
    }

    /// Load calibration data
    async fn load_calibration_data(&self) -> DeviceResult<PhotonicCalibrationData> {
        let mut mode_losses = HashMap::new();
        let mut detector_efficiencies = HashMap::new();
        let mut phase_accuracies = HashMap::new();
        let mut squeezing_calibration = HashMap::new();

        // Initialize default calibration values
        for mode in 0..self.config.mode_count {
            mode_losses.insert(mode, self.config.loss_rate.unwrap_or(0.01));
            detector_efficiencies.insert(mode, self.config.detection_efficiency.unwrap_or(0.9));
            phase_accuracies.insert(mode, 0.001); // 0.1% accuracy
            squeezing_calibration.insert(mode, (0.0, 0.0)); // No squeezing by default
        }

        Ok(PhotonicCalibrationData {
            mode_losses,
            beamsplitter_transmittances: HashMap::new(),
            detector_efficiencies,
            phase_accuracies,
            squeezing_calibration,
            crosstalk_matrix: HashMap::new(),
            last_calibration: Instant::now(),
            validity_duration: Duration::from_secs(3600), // 1 hour
        })
    }

    /// Update performance metrics
    fn update_metrics(&self, execution_time: Duration, success: bool, fidelity: Option<f64>) {
        let Ok(mut metrics) = self.metrics.write() else {
            // If lock is poisoned, skip metrics update rather than panic
            return;
        };

        metrics.circuits_executed += 1;

        // Update average execution time
        let total_time =
            metrics.avg_execution_time * (metrics.circuits_executed - 1) as u32 + execution_time;
        metrics.avg_execution_time = total_time / metrics.circuits_executed as u32;

        // Update success rate
        let total_success = metrics.success_rate.mul_add(
            (metrics.circuits_executed - 1) as f64,
            if success { 1.0 } else { 0.0 },
        );
        metrics.success_rate = total_success / metrics.circuits_executed as f64;

        // Update average fidelity if provided
        if let Some(fid) = fidelity {
            let total_fidelity = metrics
                .avg_fidelity
                .mul_add((metrics.circuits_executed - 1) as f64, fid);
            metrics.avg_fidelity = total_fidelity / metrics.circuits_executed as f64;
        }
    }

    /// Check if calibration is valid
    fn is_calibration_valid(&self) -> bool {
        let Ok(calibration) = self.calibration.read() else {
            // If lock is poisoned, assume calibration is invalid
            return false;
        };
        calibration.last_calibration.elapsed() < calibration.validity_duration
    }

    /// Recalibrate device if needed
    async fn ensure_calibrated(&self) -> DeviceResult<()> {
        if !self.is_calibration_valid() {
            let new_calibration = self.load_calibration_data().await?;
            *self
                .calibration
                .write()
                .map_err(|e| DeviceError::LockError(format!("Calibration lock poisoned: {e}")))? =
                new_calibration;
        }
        Ok(())
    }
}

#[async_trait]
impl QuantumDevice for PhotonicQuantumDeviceImpl {
    async fn is_available(&self) -> DeviceResult<bool> {
        // Check client connection and device status
        self.client.check_availability().await
    }

    async fn qubit_count(&self) -> DeviceResult<usize> {
        // For photonic systems, return mode count as effective qubit count
        Ok(self.config.mode_count)
    }

    async fn properties(&self) -> DeviceResult<HashMap<String, String>> {
        let mut properties = HashMap::new();

        properties.insert(
            "system_type".to_string(),
            format!("{:?}", self.config.system_type),
        );
        properties.insert("mode_count".to_string(), self.config.mode_count.to_string());

        if let Some(cutoff) = self.config.cutoff_dimension {
            properties.insert("cutoff_dimension".to_string(), cutoff.to_string());
        }

        if let Some(loss_rate) = self.config.loss_rate {
            properties.insert("loss_rate".to_string(), loss_rate.to_string());
        }

        if let Some(efficiency) = self.config.detection_efficiency {
            properties.insert("detection_efficiency".to_string(), efficiency.to_string());
        }

        // Add calibration status
        properties.insert(
            "calibration_valid".to_string(),
            self.is_calibration_valid().to_string(),
        );

        // Add performance metrics
        let metrics = self
            .metrics
            .read()
            .map_err(|e| DeviceError::LockError(format!("Metrics lock poisoned: {e}")))?;
        properties.insert(
            "circuits_executed".to_string(),
            metrics.circuits_executed.to_string(),
        );
        properties.insert("success_rate".to_string(), metrics.success_rate.to_string());
        properties.insert("avg_fidelity".to_string(), metrics.avg_fidelity.to_string());

        Ok(properties)
    }

    async fn is_simulator(&self) -> DeviceResult<bool> {
        // Check if this is a hardware device or simulator
        self.client.is_simulator().await
    }
}

#[async_trait]
impl CircuitExecutor for PhotonicQuantumDeviceImpl {
    async fn execute_circuit<const N: usize>(
        &self,
        circuit: &Circuit<N>,
        shots: usize,
    ) -> DeviceResult<CircuitResult> {
        let photonic_result = self.execute_photonic_circuit(circuit, shots, None).await?;
        Ok(photonic_result.circuit_result)
    }

    async fn execute_circuits<const N: usize>(
        &self,
        circuits: Vec<&Circuit<N>>,
        shots: usize,
    ) -> DeviceResult<Vec<CircuitResult>> {
        let mut results = Vec::new();

        for circuit in circuits {
            let result = self.execute_circuit(circuit, shots).await?;
            results.push(result);
        }

        Ok(results)
    }

    async fn can_execute_circuit<const N: usize>(
        &self,
        circuit: &Circuit<N>,
    ) -> DeviceResult<bool> {
        // Check if circuit is compatible with photonic system

        // Check mode count
        if N > self.config.mode_count {
            return Ok(false);
        }

        // Check gate compatibility
        // TODO: Implement gate validation based on photonic capabilities

        Ok(true)
    }

    async fn estimated_queue_time<const N: usize>(
        &self,
        _circuit: &Circuit<N>,
    ) -> DeviceResult<Duration> {
        // For photonic systems, execution is typically fast
        // Queue time depends on system load
        self.client.get_queue_time().await
    }
}

#[async_trait]
impl PhotonicQuantumDevice for PhotonicQuantumDeviceImpl {
    async fn system_type(&self) -> DeviceResult<PhotonicSystemType> {
        Ok(self.config.system_type)
    }

    async fn mode_count(&self) -> DeviceResult<usize> {
        Ok(self.config.mode_count)
    }

    async fn cutoff_dimension(&self) -> DeviceResult<Option<usize>> {
        Ok(self.config.cutoff_dimension)
    }

    async fn supports_cv_operations(&self) -> DeviceResult<bool> {
        let capabilities = self
            .capabilities
            .read()
            .map_err(|e| DeviceError::LockError(format!("Capabilities lock poisoned: {e}")))?;
        Ok(capabilities.as_ref().map_or(false, |caps| {
            caps.supported_systems
                .contains(&PhotonicSystemType::ContinuousVariable)
        }))
    }

    async fn supports_gate_based(&self) -> DeviceResult<bool> {
        let capabilities = self
            .capabilities
            .read()
            .map_err(|e| DeviceError::LockError(format!("Capabilities lock poisoned: {e}")))?;
        Ok(capabilities.as_ref().map_or(false, |caps| {
            caps.supported_systems
                .contains(&PhotonicSystemType::GateBased)
        }))
    }

    async fn supports_measurement_based(&self) -> DeviceResult<bool> {
        let capabilities = self
            .capabilities
            .read()
            .map_err(|e| DeviceError::LockError(format!("Capabilities lock poisoned: {e}")))?;
        Ok(capabilities.as_ref().map_or(false, |caps| {
            caps.supported_systems
                .contains(&PhotonicSystemType::MeasurementBased)
        }))
    }

    async fn quadrature_precision(&self) -> DeviceResult<f64> {
        // Return precision based on calibration data
        let calibration = self
            .calibration
            .read()
            .map_err(|e| DeviceError::LockError(format!("Calibration lock poisoned: {e}")))?;
        let len = calibration.phase_accuracies.len();
        if len == 0 {
            return Ok(0.0);
        }
        let avg_precision =
            calibration.phase_accuracies.values().copied().sum::<f64>() / len as f64;
        Ok(avg_precision)
    }

    async fn detection_efficiency(&self) -> DeviceResult<f64> {
        Ok(self.config.detection_efficiency.unwrap_or(0.9))
    }

    async fn execute_photonic_circuit<const N: usize>(
        &self,
        circuit: &Circuit<N>,
        shots: usize,
        config: Option<PhotonicDeviceConfig>,
    ) -> DeviceResult<PhotonicCircuitResult> {
        let start_time = Instant::now();

        // Ensure device is calibrated
        self.ensure_calibrated().await?;

        // Use provided config or default
        let exec_config = config.unwrap_or_else(|| self.config.clone());

        // Create a simple circuit representation for API
        let circuit_str = format!(
            "{{\"gates\":{},\"qubits\":{}}}",
            circuit.gates().len(),
            circuit.num_qubits()
        );

        // Convert config to JSON
        let config_json = serde_json::to_value(&exec_config).map_err(|e| {
            DeviceError::CircuitConversion(format!("Failed to serialize config: {e}"))
        })?;
        let mut config_map = std::collections::HashMap::new();
        if let serde_json::Value::Object(map) = config_json {
            for (k, v) in map {
                config_map.insert(k, v);
            }
        }

        // Execute circuit using client
        let circuit_result = self
            .client
            .execute_photonic_circuit(&circuit_str, shots, &config_map)
            .await?;

        // Generate photonic-specific measurement data
        let photonic_data = self.generate_photonic_measurements(circuit, shots).await?;

        // Create execution metadata
        let execution_time = start_time.elapsed();
        let metadata = PhotonicExecutionMetadata {
            system_type: exec_config.system_type,
            modes_used: N.min(exec_config.mode_count),
            execution_time,
            measured_loss_rate: self.config.loss_rate,
            thermal_noise: self.config.thermal_photons,
            gate_sequence: vec![],         // TODO: Extract from circuit
            optimizations_applied: vec![], // TODO: Track optimizations
        };

        // Update performance metrics
        let fidelity = photonic_data.fidelities.get("overall").copied();
        self.update_metrics(execution_time, true, fidelity);

        // Convert PhotonicJobResult to CircuitResult
        let circuit_result_converted = CircuitResult {
            counts: circuit_result
                .results
                .get("counts")
                .and_then(|v| serde_json::from_value(v.clone()).ok())
                .unwrap_or_else(|| {
                    let mut counts = HashMap::new();
                    counts.insert("0".repeat(circuit.num_qubits()), shots);
                    counts
                }),
            shots: circuit_result.shots_completed,
            metadata: circuit_result.metadata,
        };

        Ok(PhotonicCircuitResult {
            circuit_result: circuit_result_converted,
            photonic_data,
            execution_metadata: metadata,
        })
    }

    async fn measure_quadratures(
        &self,
        modes: &[usize],
        angles: &[f64],
    ) -> DeviceResult<Vec<(f64, f64)>> {
        self.client
            .measure_quadratures(&self.device_id, modes, angles)
            .await
    }

    async fn measure_photon_numbers(&self, modes: &[usize]) -> DeviceResult<Vec<usize>> {
        self.client
            .measure_photon_numbers(&self.device_id, modes)
            .await
    }

    async fn homodyne_detection(
        &self,
        mode: usize,
        phase: f64,
        shots: usize,
    ) -> DeviceResult<Vec<f64>> {
        self.client
            .homodyne_detection(&self.device_id, mode, phase, shots)
            .await
    }

    async fn heterodyne_detection(
        &self,
        mode: usize,
        shots: usize,
    ) -> DeviceResult<Vec<(f64, f64)>> {
        self.client
            .heterodyne_detection(&self.device_id, mode, shots)
            .await
    }

    async fn calculate_correlations(
        &self,
        modes: &[(usize, usize)],
        correlation_type: &str,
    ) -> DeviceResult<HashMap<String, f64>> {
        self.client
            .calculate_correlations(modes, correlation_type)
            .await
    }

    async fn estimate_fidelity(
        &self,
        target_state: &str,
        measurement_data: &PhotonicMeasurementData,
    ) -> DeviceResult<f64> {
        self.client
            .estimate_fidelity(target_state, measurement_data)
            .await
    }
}

impl PhotonicQuantumDeviceImpl {
    /// Generate photonic measurement data from circuit execution
    async fn generate_photonic_measurements<const N: usize>(
        &self,
        _circuit: &Circuit<N>,
        shots: usize,
    ) -> DeviceResult<PhotonicMeasurementData> {
        // Simulate photonic measurements
        let mut quadratures = Vec::new();
        let mut photon_numbers = Vec::new();
        let mut homodyne_results = Vec::new();
        let mut heterodyne_results = Vec::new();
        let mut correlations = HashMap::new();
        let mut fidelities = HashMap::new();

        // Generate mock measurements for testing
        for _ in 0..shots.min(100) {
            // Limit for demonstration
            // Random quadrature values
            quadratures.push((
                thread_rng().random::<f64>() - 0.5,
                thread_rng().random::<f64>() - 0.5,
            ));

            // Random photon numbers (small numbers typical for CV systems)
            photon_numbers.push((thread_rng().random::<f64>() * 5.0) as usize);

            // Homodyne detection results
            homodyne_results.push(thread_rng().random::<f64>() - 0.5);

            // Heterodyne detection results
            heterodyne_results.push((
                thread_rng().random::<f64>() - 0.5,
                thread_rng().random::<f64>() - 0.5,
            ));
        }

        // Calculate correlations
        correlations.insert(
            "g2".to_string(),
            thread_rng().random::<f64>().mul_add(0.1, 1.0),
        );
        correlations.insert(
            "visibility".to_string(),
            thread_rng().random::<f64>().mul_add(0.09, 0.9),
        );

        // Estimate fidelities
        fidelities.insert(
            "overall".to_string(),
            thread_rng().random::<f64>().mul_add(0.04, 0.95),
        );
        fidelities.insert(
            "gate_fidelity".to_string(),
            self.config.gate_fidelity.unwrap_or(0.99),
        );

        Ok(PhotonicMeasurementData {
            quadratures,
            photon_numbers,
            homodyne_results,
            heterodyne_results,
            correlations,
            fidelities,
        })
    }
}

impl Default for PhotonicCalibrationData {
    fn default() -> Self {
        Self {
            mode_losses: HashMap::new(),
            beamsplitter_transmittances: HashMap::new(),
            detector_efficiencies: HashMap::new(),
            phase_accuracies: HashMap::new(),
            squeezing_calibration: HashMap::new(),
            crosstalk_matrix: HashMap::new(),
            last_calibration: Instant::now(),
            validity_duration: Duration::from_secs(3600),
        }
    }
}

impl Default for PhotonicPerformanceMetrics {
    fn default() -> Self {
        Self {
            circuits_executed: 0,
            avg_execution_time: Duration::from_millis(0),
            success_rate: 0.0,
            avg_fidelity: 0.0,
            mode_utilization: HashMap::new(),
            gate_counts: HashMap::new(),
            error_rates: HashMap::new(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::photonic::{PhotonicClient, PhotonicConfig};

    #[tokio::test]
    async fn test_photonic_device_creation() {
        let client = PhotonicClient::new(
            "http://localhost:8080".to_string(),
            "test_token".to_string(),
        )
        .expect("Failed to create photonic client");
        let config = PhotonicDeviceConfig::default();

        let device =
            PhotonicQuantumDeviceImpl::new("test_device".to_string(), client, config).await;
        assert!(device.is_ok());
    }

    #[tokio::test]
    async fn test_device_properties() {
        let client = PhotonicClient::new(
            "http://localhost:8080".to_string(),
            "test_token".to_string(),
        )
        .expect("Failed to create photonic client");
        let config = PhotonicDeviceConfig::default();
        let device = PhotonicQuantumDeviceImpl::new("test_device".to_string(), client, config)
            .await
            .expect("Failed to create photonic device");

        let properties = device
            .properties()
            .await
            .expect("Failed to get device properties");
        assert!(properties.contains_key("system_type"));
        assert!(properties.contains_key("mode_count"));
    }

    #[tokio::test]
    async fn test_capabilities() {
        let client = PhotonicClient::new(
            "http://localhost:8080".to_string(),
            "test_token".to_string(),
        )
        .expect("Failed to create photonic client");
        let config = PhotonicDeviceConfig::default();
        let device = PhotonicQuantumDeviceImpl::new("test_device".to_string(), client, config)
            .await
            .expect("Failed to create photonic device");

        assert!(device
            .supports_cv_operations()
            .await
            .expect("Failed to check CV operations support"));
        assert_eq!(
            device.mode_count().await.expect("Failed to get mode count"),
            8
        );
    }
}