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
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
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
//! Photonic Quantum Communication Protocols
//!
//! This module implements quantum communication protocols specifically designed for
//! photonic systems, including quantum key distribution, teleportation, and networking.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::{Duration, Instant};
use thiserror::Error;

use super::continuous_variable::{CVResult, Complex, GaussianState};
use super::gate_based::{PhotonicQubitEncoding, PhotonicQubitState};
use super::{PhotonicMode, PhotonicSystemType};
use crate::DeviceResult;
use scirs2_core::random::prelude::*;

/// Photonic protocol errors
#[derive(Error, Debug)]
pub enum PhotonicProtocolError {
    #[error("Protocol execution failed: {0}")]
    ExecutionFailed(String),
    #[error("Authentication failed: {0}")]
    AuthenticationFailed(String),
    #[error("Security violation: {0}")]
    SecurityViolation(String),
    #[error("Network communication error: {0}")]
    NetworkError(String),
    #[error("Protocol not supported: {0}")]
    UnsupportedProtocol(String),
}

/// Types of photonic quantum protocols
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum PhotonicProtocolType {
    /// Quantum Key Distribution
    QKD { variant: QKDVariant },
    /// Quantum Teleportation
    Teleportation,
    /// Quantum State Distribution
    StateDistribution,
    /// Quantum Clock Synchronization
    ClockSynchronization,
    /// Quantum Sensing Networks
    SensingNetwork,
    /// Quantum Internet Protocols
    QuantumInternet { protocol_version: String },
}

/// QKD protocol variants
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum QKDVariant {
    /// BB84 protocol
    BB84,
    /// B92 protocol
    B92,
    /// SARG04 protocol
    SARG04,
    /// Continuous Variable QKD
    CVQKD { modulation: CVModulation },
    /// Measurement Device Independent QKD
    MDIQKD,
    /// Twin Field QKD
    TwinField,
}

/// CV QKD modulation schemes
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum CVModulation {
    /// Gaussian modulation
    Gaussian { variance: f64 },
    /// Discrete modulation
    Discrete { constellation_size: usize },
    /// Heterodyne detection
    Heterodyne,
    /// Homodyne detection
    Homodyne,
}

/// Protocol execution context
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProtocolContext {
    /// Protocol identifier
    pub protocol_id: String,
    /// Participating parties
    pub parties: Vec<ProtocolParty>,
    /// Security parameters
    pub security_params: SecurityParameters,
    /// Network configuration
    pub network_config: NetworkConfiguration,
    /// Start time
    #[serde(with = "instant_serde")]
    pub start_time: Instant,
}

/// Party in a quantum protocol
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProtocolParty {
    /// Party identifier
    pub party_id: String,
    /// Role in protocol
    pub role: PartyRole,
    /// Communication endpoints
    pub endpoints: Vec<String>,
    /// Capabilities
    pub capabilities: PartyCapabilities,
}

/// Role of a party in quantum protocols
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum PartyRole {
    /// Alice (sender)
    Alice,
    /// Bob (receiver)
    Bob,
    /// Charlie (trusted third party)
    Charlie,
    /// Eve (eavesdropper - for security analysis)
    Eve,
    /// Network node
    NetworkNode { node_id: String },
}

/// Capabilities of a protocol party
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PartyCapabilities {
    /// Supported encodings
    pub encodings: Vec<PhotonicQubitEncoding>,
    /// Detection efficiency
    pub detection_efficiency: f64,
    /// Maximum transmission rate
    pub max_rate: f64,
    /// Supported wavelengths
    pub wavelengths: Vec<f64>,
    /// Distance limitations
    pub max_distance: Option<f64>,
}

/// Security parameters for protocols
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SecurityParameters {
    /// Required security level (bits)
    pub security_level: usize,
    /// Error tolerance
    pub error_tolerance: f64,
    /// Privacy amplification parameters
    pub privacy_amplification: PrivacyAmplificationParams,
    /// Authentication method
    pub authentication: AuthenticationMethod,
}

/// Privacy amplification parameters
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PrivacyAmplificationParams {
    /// Hash function family
    pub hash_family: String,
    /// Compression ratio
    pub compression_ratio: f64,
    /// Number of rounds
    pub rounds: usize,
}

/// Authentication methods
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum AuthenticationMethod {
    /// Classical authentication
    Classical { algorithm: String },
    /// Quantum authentication
    Quantum { protocol: String },
    /// Post-quantum cryptography
    PostQuantum { algorithm: String },
}

/// Network configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NetworkConfiguration {
    /// Topology type
    pub topology: NetworkTopology,
    /// Channel characteristics
    pub channels: Vec<QuantumChannel>,
    /// Routing configuration
    pub routing: RoutingConfig,
}

/// Network topology types
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum NetworkTopology {
    /// Point-to-point
    PointToPoint,
    /// Star network
    Star { hub: String },
    /// Ring network
    Ring,
    /// Mesh network
    Mesh,
    /// Tree network
    Tree { root: String },
}

/// Quantum communication channel
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QuantumChannel {
    /// Channel identifier
    pub channel_id: String,
    /// Source party
    pub source: String,
    /// Destination party
    pub destination: String,
    /// Channel characteristics
    pub characteristics: ChannelCharacteristics,
}

/// Physical characteristics of quantum channels
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChannelCharacteristics {
    /// Loss rate (dB/km)
    pub loss_rate: f64,
    /// Dark count rate
    pub dark_count_rate: f64,
    /// Channel length (km)
    pub length: f64,
    /// Wavelength (nm)
    pub wavelength: f64,
    /// Detector efficiency
    pub detector_efficiency: f64,
}

/// Routing configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RoutingConfig {
    /// Routing algorithm
    pub algorithm: RoutingAlgorithm,
    /// Maximum hops
    pub max_hops: usize,
    /// Load balancing enabled
    pub load_balancing: bool,
}

/// Routing algorithms
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum RoutingAlgorithm {
    /// Shortest path
    ShortestPath,
    /// Minimum loss
    MinimumLoss,
    /// Load balanced
    LoadBalanced,
    /// Quantum-aware routing
    QuantumAware,
}

/// Protocol execution result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProtocolResult {
    /// Whether protocol succeeded
    pub success: bool,
    /// Generated key (for QKD)
    pub key: Option<Vec<u8>>,
    /// Transmitted state (for teleportation)
    pub transmitted_state: Option<PhotonicQubitState>,
    /// Protocol metrics
    pub metrics: ProtocolMetrics,
    /// Security analysis
    pub security_analysis: SecurityAnalysis,
}

/// Protocol performance metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProtocolMetrics {
    /// Execution time
    pub execution_time: Duration,
    /// Key rate (bits/second)
    pub key_rate: Option<f64>,
    /// Error rate
    pub error_rate: f64,
    /// Fidelity
    pub fidelity: f64,
    /// Throughput
    pub throughput: f64,
}

/// Security analysis results
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SecurityAnalysis {
    /// Information leakage estimate
    pub information_leakage: f64,
    /// Eavesdropping detection
    pub eavesdropping_detected: bool,
    /// Security proof validity
    pub security_proof_valid: bool,
    /// Achieved security level
    pub achieved_security: f64,
}

/// Photonic protocol engine
pub struct PhotonicProtocolEngine {
    /// Active protocols
    pub active_protocols: HashMap<String, ProtocolContext>,
    /// Protocol statistics
    pub statistics: ProtocolStatistics,
    /// Security monitor
    pub security_monitor: SecurityMonitor,
}

/// Protocol execution statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProtocolStatistics {
    /// Total protocols executed
    pub total_protocols: usize,
    /// Success rate
    pub success_rate: f64,
    /// Average key rate
    pub average_key_rate: f64,
    /// Average fidelity
    pub average_fidelity: f64,
    /// Security violations detected
    pub security_violations: usize,
}

/// Security monitoring system
#[derive(Debug, Clone)]
pub struct SecurityMonitor {
    /// Threat detection enabled
    pub threat_detection: bool,
    /// Anomaly detection threshold
    pub anomaly_threshold: f64,
    /// Security events
    pub security_events: Vec<SecurityEvent>,
}

/// Security event record
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SecurityEvent {
    /// Event timestamp
    #[serde(with = "instant_serde")]
    pub timestamp: Instant,
    /// Event type
    pub event_type: SecurityEventType,
    /// Severity level
    pub severity: SecuritySeverity,
    /// Description
    pub description: String,
    /// Affected protocols
    pub affected_protocols: Vec<String>,
}

mod instant_serde {
    use serde::{Deserialize, Deserializer, Serialize, Serializer};
    use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};

    pub fn serialize<S>(instant: &Instant, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let duration_since_epoch = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default();
        duration_since_epoch.as_secs().serialize(serializer)
    }

    pub fn deserialize<'de, D>(deserializer: D) -> Result<Instant, D::Error>
    where
        D: Deserializer<'de>,
    {
        let secs = u64::deserialize(deserializer)?;
        Ok(Instant::now()) // Simplified
    }
}

/// Types of security events
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum SecurityEventType {
    /// Eavesdropping attempt detected
    EavesdroppingDetected,
    /// Anomalous error rate
    AnomalousErrorRate,
    /// Authentication failure
    AuthenticationFailure,
    /// Protocol violation
    ProtocolViolation,
    /// Network intrusion
    NetworkIntrusion,
}

/// Security event severity levels
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum SecuritySeverity {
    Low,
    Medium,
    High,
    Critical,
}

impl PhotonicProtocolEngine {
    pub fn new() -> Self {
        Self {
            active_protocols: HashMap::new(),
            statistics: ProtocolStatistics {
                total_protocols: 0,
                success_rate: 0.0,
                average_key_rate: 0.0,
                average_fidelity: 0.0,
                security_violations: 0,
            },
            security_monitor: SecurityMonitor {
                threat_detection: true,
                anomaly_threshold: 0.05,
                security_events: Vec::new(),
            },
        }
    }

    /// Execute a quantum protocol
    pub fn execute_protocol(
        &mut self,
        protocol_type: PhotonicProtocolType,
        context: ProtocolContext,
    ) -> Result<ProtocolResult, PhotonicProtocolError> {
        let start_time = Instant::now();

        // Register protocol
        self.active_protocols
            .insert(context.protocol_id.clone(), context.clone());

        // Execute based on protocol type
        let result = match protocol_type {
            PhotonicProtocolType::QKD { variant } => self.execute_qkd_protocol(variant, &context),
            PhotonicProtocolType::Teleportation => self.execute_teleportation_protocol(&context),
            PhotonicProtocolType::StateDistribution => {
                self.execute_state_distribution_protocol(&context)
            }
            PhotonicProtocolType::ClockSynchronization => {
                self.execute_clock_sync_protocol(&context)
            }
            PhotonicProtocolType::SensingNetwork => self.execute_sensing_protocol(&context),
            PhotonicProtocolType::QuantumInternet { protocol_version } => {
                self.execute_quantum_internet_protocol(&context, &protocol_version)
            }
        }?;

        // Update statistics
        self.update_statistics(&result);

        // Remove from active protocols
        self.active_protocols.remove(&context.protocol_id);

        Ok(result)
    }

    /// Execute QKD protocol
    fn execute_qkd_protocol(
        &mut self,
        variant: QKDVariant,
        context: &ProtocolContext,
    ) -> Result<ProtocolResult, PhotonicProtocolError> {
        match variant {
            QKDVariant::BB84 => self.execute_bb84(context),
            QKDVariant::CVQKD { modulation } => self.execute_cvqkd(context, modulation),
            _ => Err(PhotonicProtocolError::UnsupportedProtocol(format!(
                "QKD variant {variant:?} not implemented"
            ))),
        }
    }

    /// Execute BB84 protocol
    fn execute_bb84(
        &mut self,
        context: &ProtocolContext,
    ) -> Result<ProtocolResult, PhotonicProtocolError> {
        let start_time = Instant::now();

        // Simulate BB84 protocol execution
        let key_length = 256; // bits
        let mut raw_key = Vec::new();
        let mut error_count = 0;

        // Simulate photon transmission and measurement
        for _ in 0..key_length * 2 {
            // Send 2x for basis reconciliation
            let bit = thread_rng().random::<bool>();
            let basis = thread_rng().random::<bool>(); // 0: rectilinear, 1: diagonal

            // Simulate channel loss and errors
            let channel_loss = 0.05; // 5% loss
            let error_rate = 0.01; // 1% error rate

            if thread_rng().random::<f64>() > channel_loss {
                let received_bit = if thread_rng().random::<f64>() < error_rate {
                    !bit // Flip bit due to error
                } else {
                    bit
                };

                if thread_rng().random::<bool>() {
                    // Bob chooses same basis 50% of time
                    raw_key.push(received_bit as u8);
                    if received_bit != bit {
                        error_count += 1;
                    }
                }
            }
        }

        // Trim to desired key length
        raw_key.truncate(key_length);
        let final_error_rate = error_count as f64 / raw_key.len() as f64;

        // Security analysis
        let security_analysis = SecurityAnalysis {
            information_leakage: final_error_rate * 0.5, // Simplified estimate
            eavesdropping_detected: final_error_rate > 0.11, // BB84 threshold
            security_proof_valid: final_error_rate <= 0.11,
            achieved_security: if final_error_rate <= 0.11 {
                context.security_params.security_level as f64
            } else {
                0.0
            },
        };

        let metrics = ProtocolMetrics {
            execution_time: start_time.elapsed(),
            key_rate: Some(raw_key.len() as f64 / start_time.elapsed().as_secs_f64()),
            error_rate: final_error_rate,
            fidelity: 1.0 - final_error_rate,
            throughput: raw_key.len() as f64,
        };

        Ok(ProtocolResult {
            success: security_analysis.security_proof_valid,
            key: if security_analysis.security_proof_valid {
                Some(raw_key)
            } else {
                None
            },
            transmitted_state: None,
            metrics,
            security_analysis,
        })
    }

    /// Execute CV-QKD protocol
    fn execute_cvqkd(
        &mut self,
        context: &ProtocolContext,
        modulation: CVModulation,
    ) -> Result<ProtocolResult, PhotonicProtocolError> {
        let start_time = Instant::now();

        // Simulate CV-QKD protocol
        let key_length = 256;
        let mut key = Vec::new();

        // Generate coherent states for CV-QKD
        for _ in 0..key_length {
            let x_quad = match modulation {
                CVModulation::Gaussian { variance } => {
                    // Generate Gaussian distributed quadrature
                    self.generate_gaussian_random(0.0, variance)
                }
                CVModulation::Discrete { constellation_size } => {
                    // Generate discrete constellation point
                    let point = (thread_rng().random::<f64>() * constellation_size as f64) as usize;
                    (point as f64 - constellation_size as f64 / 2.0) * 0.5
                }
                _ => thread_rng().random::<f64>() - 0.5,
            };

            // Quantize to bits (simplified)
            key.push(u8::from(x_quad > 0.0));
        }

        let error_rate = 0.02; // Lower error rate for CV-QKD
        let security_analysis = SecurityAnalysis {
            information_leakage: error_rate * 0.3,
            eavesdropping_detected: error_rate > 0.05,
            security_proof_valid: error_rate <= 0.05,
            achieved_security: if error_rate <= 0.05 {
                context.security_params.security_level as f64
            } else {
                0.0
            },
        };

        let metrics = ProtocolMetrics {
            execution_time: start_time.elapsed(),
            key_rate: Some(key.len() as f64 / start_time.elapsed().as_secs_f64()),
            error_rate,
            fidelity: 1.0 - error_rate,
            throughput: key.len() as f64,
        };

        Ok(ProtocolResult {
            success: security_analysis.security_proof_valid,
            key: if security_analysis.security_proof_valid {
                Some(key)
            } else {
                None
            },
            transmitted_state: None,
            metrics,
            security_analysis,
        })
    }

    /// Execute teleportation protocol
    fn execute_teleportation_protocol(
        &self,
        context: &ProtocolContext,
    ) -> Result<ProtocolResult, PhotonicProtocolError> {
        let start_time = Instant::now();

        // Simulate quantum teleportation
        let input_state = PhotonicQubitState::plus(PhotonicQubitEncoding::Polarization);
        let fidelity: f64 = 0.95; // Typical teleportation fidelity

        let mut output_state = input_state;
        output_state.amplitude_0 *= fidelity.sqrt();
        output_state.amplitude_1 *= fidelity.sqrt();

        let metrics = ProtocolMetrics {
            execution_time: start_time.elapsed(),
            key_rate: None,
            error_rate: 1.0 - fidelity,
            fidelity,
            throughput: 1.0, // One qubit teleported
        };

        let security_analysis = SecurityAnalysis {
            information_leakage: 0.0, // Perfect security in principle
            eavesdropping_detected: false,
            security_proof_valid: true,
            achieved_security: f64::INFINITY, // Information-theoretic security
        };

        Ok(ProtocolResult {
            success: fidelity > 0.9,
            key: None,
            transmitted_state: Some(output_state),
            metrics,
            security_analysis,
        })
    }

    /// Execute other protocols (placeholder implementations)
    const fn execute_state_distribution_protocol(
        &self,
        context: &ProtocolContext,
    ) -> Result<ProtocolResult, PhotonicProtocolError> {
        // Placeholder implementation
        Ok(self.create_placeholder_result(context))
    }

    const fn execute_clock_sync_protocol(
        &self,
        context: &ProtocolContext,
    ) -> Result<ProtocolResult, PhotonicProtocolError> {
        // Placeholder implementation
        Ok(self.create_placeholder_result(context))
    }

    const fn execute_sensing_protocol(
        &self,
        context: &ProtocolContext,
    ) -> Result<ProtocolResult, PhotonicProtocolError> {
        // Placeholder implementation
        Ok(self.create_placeholder_result(context))
    }

    const fn execute_quantum_internet_protocol(
        &self,
        context: &ProtocolContext,
        _protocol_version: &str,
    ) -> Result<ProtocolResult, PhotonicProtocolError> {
        // Placeholder implementation
        Ok(self.create_placeholder_result(context))
    }

    /// Create placeholder result for unimplemented protocols
    const fn create_placeholder_result(&self, _context: &ProtocolContext) -> ProtocolResult {
        ProtocolResult {
            success: true,
            key: None,
            transmitted_state: None,
            metrics: ProtocolMetrics {
                execution_time: Duration::from_millis(100),
                key_rate: None,
                error_rate: 0.01,
                fidelity: 0.99,
                throughput: 1.0,
            },
            security_analysis: SecurityAnalysis {
                information_leakage: 0.01,
                eavesdropping_detected: false,
                security_proof_valid: true,
                achieved_security: 128.0,
            },
        }
    }

    /// Update protocol statistics
    fn update_statistics(&mut self, result: &ProtocolResult) {
        self.statistics.total_protocols += 1;

        let success_count = if result.success { 1.0 } else { 0.0 };
        self.statistics.success_rate = self
            .statistics
            .success_rate
            .mul_add((self.statistics.total_protocols - 1) as f64, success_count)
            / self.statistics.total_protocols as f64;

        if let Some(key_rate) = result.metrics.key_rate {
            self.statistics.average_key_rate = self
                .statistics
                .average_key_rate
                .mul_add((self.statistics.total_protocols - 1) as f64, key_rate)
                / self.statistics.total_protocols as f64;
        }

        self.statistics.average_fidelity = self.statistics.average_fidelity.mul_add(
            (self.statistics.total_protocols - 1) as f64,
            result.metrics.fidelity,
        ) / self.statistics.total_protocols as f64;

        if result.security_analysis.eavesdropping_detected {
            self.statistics.security_violations += 1;
        }
    }

    /// Generate Gaussian random number (simplified)
    fn generate_gaussian_random(&self, mean: f64, variance: f64) -> f64 {
        // Simple Box-Muller transform
        let u1 = thread_rng().random::<f64>();
        let u2 = thread_rng().random::<f64>();
        let z = (-2.0 * u1.ln()).sqrt() * (2.0 * std::f64::consts::PI * u2).cos();
        variance.sqrt().mul_add(z, mean)
    }

    /// Get protocol statistics
    pub const fn get_statistics(&self) -> &ProtocolStatistics {
        &self.statistics
    }

    /// Get active protocols
    pub fn get_active_protocols(&self) -> Vec<String> {
        self.active_protocols.keys().cloned().collect()
    }
}

impl Default for PhotonicProtocolEngine {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_protocol_engine_creation() {
        let engine = PhotonicProtocolEngine::new();
        assert_eq!(engine.statistics.total_protocols, 0);
        assert_eq!(engine.active_protocols.len(), 0);
    }

    #[test]
    fn test_bb84_protocol() {
        let mut engine = PhotonicProtocolEngine::new();

        let context = ProtocolContext {
            protocol_id: "test_bb84".to_string(),
            parties: vec![],
            security_params: SecurityParameters {
                security_level: 128,
                error_tolerance: 0.11,
                privacy_amplification: PrivacyAmplificationParams {
                    hash_family: "SHA256".to_string(),
                    compression_ratio: 0.5,
                    rounds: 1,
                },
                authentication: AuthenticationMethod::Classical {
                    algorithm: "HMAC".to_string(),
                },
            },
            network_config: NetworkConfiguration {
                topology: NetworkTopology::PointToPoint,
                channels: vec![],
                routing: RoutingConfig {
                    algorithm: RoutingAlgorithm::ShortestPath,
                    max_hops: 1,
                    load_balancing: false,
                },
            },
            start_time: Instant::now(),
        };

        let result = engine
            .execute_protocol(
                PhotonicProtocolType::QKD {
                    variant: QKDVariant::BB84,
                },
                context,
            )
            .expect("BB84 protocol execution should succeed");

        assert!(result.metrics.fidelity > 0.0);
        assert!(result.metrics.execution_time > Duration::ZERO);
    }

    #[test]
    fn test_teleportation_protocol() {
        let mut engine = PhotonicProtocolEngine::new();

        let context = ProtocolContext {
            protocol_id: "test_teleport".to_string(),
            parties: vec![],
            security_params: SecurityParameters {
                security_level: 128,
                error_tolerance: 0.05,
                privacy_amplification: PrivacyAmplificationParams {
                    hash_family: "SHA256".to_string(),
                    compression_ratio: 0.5,
                    rounds: 1,
                },
                authentication: AuthenticationMethod::Quantum {
                    protocol: "QAUTH".to_string(),
                },
            },
            network_config: NetworkConfiguration {
                topology: NetworkTopology::PointToPoint,
                channels: vec![],
                routing: RoutingConfig {
                    algorithm: RoutingAlgorithm::QuantumAware,
                    max_hops: 1,
                    load_balancing: false,
                },
            },
            start_time: Instant::now(),
        };

        let result = engine
            .execute_protocol(PhotonicProtocolType::Teleportation, context)
            .expect("teleportation protocol execution should succeed");

        assert!(result.success);
        assert!(result.transmitted_state.is_some());
        assert!(result.metrics.fidelity > 0.9);
    }

    #[test]
    fn test_cvqkd_protocol() {
        let mut engine = PhotonicProtocolEngine::new();

        let context = ProtocolContext {
            protocol_id: "test_cvqkd".to_string(),
            parties: vec![],
            security_params: SecurityParameters {
                security_level: 256,
                error_tolerance: 0.05,
                privacy_amplification: PrivacyAmplificationParams {
                    hash_family: "SHA3".to_string(),
                    compression_ratio: 0.7,
                    rounds: 2,
                },
                authentication: AuthenticationMethod::PostQuantum {
                    algorithm: "Dilithium".to_string(),
                },
            },
            network_config: NetworkConfiguration {
                topology: NetworkTopology::PointToPoint,
                channels: vec![],
                routing: RoutingConfig {
                    algorithm: RoutingAlgorithm::MinimumLoss,
                    max_hops: 1,
                    load_balancing: false,
                },
            },
            start_time: Instant::now(),
        };

        let result = engine
            .execute_protocol(
                PhotonicProtocolType::QKD {
                    variant: QKDVariant::CVQKD {
                        modulation: CVModulation::Gaussian { variance: 1.0 },
                    },
                },
                context,
            )
            .expect("CV-QKD protocol execution should succeed");

        assert!(result.key.is_some());
        assert!(result.metrics.key_rate.is_some());
    }
}