trustformers-core 0.1.1

Core traits and utilities 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
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
//! Optical Computing Preparation Framework
//!
//! This module provides experimental support for optical and photonic computing
//! including photonic neural networks, optical signal processing, and coherent computing.

pub mod coherent_computing;
pub mod interference_patterns;
pub mod optical_encoding;
pub mod optical_operations;
pub mod photonic_devices;
pub mod photonic_networks;

pub use photonic_networks::*;

use crate::tensor::Tensor;
use anyhow::Result;
use scirs2_core::random::*;
use std::collections::HashMap;

/// Optical computing platforms
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OpticalPlatform {
    Simulation,
    MachZehnder,     // Mach-Zehnder interferometers
    RingResonator,   // Ring resonator networks
    PhotonicCrystal, // Photonic crystal devices
    CoherentIsing,   // Coherent Ising machines
    QuantumPhotonic, // Quantum photonic processors
}

/// Optical signal representation
#[derive(Debug, Clone)]
pub struct OpticalSignal {
    pub amplitude: Vec<f64>,
    pub phase: Vec<f64>,
    pub wavelength: f64,
    pub power: f64,
    pub polarization: Polarization,
}

/// Light polarization states
#[derive(Debug, Clone, Copy)]
pub enum Polarization {
    Linear {
        angle: f64,
    },
    Circular {
        handedness: Handedness,
    },
    Elliptical {
        major_axis: f64,
        minor_axis: f64,
        angle: f64,
    },
    Unpolarized,
}

#[derive(Debug, Clone, Copy)]
pub enum Handedness {
    Left,
    Right,
}

/// Photonic device configuration
#[derive(Debug, Clone)]
pub struct PhotonicConfig {
    pub platform: OpticalPlatform,
    pub wavelength_range: (f64, f64), // in nanometers
    pub bandwidth: f64,
    pub power_budget: f64, // in watts
    pub num_waveguides: usize,
    pub coupling_strength: f64,
    pub loss_coefficient: f64,
    pub temperature: f64, // in Kelvin
}

/// Optical matrix unit (equivalent to MAC operation)
#[derive(Debug, Clone)]
pub struct OpticalMatrixUnit {
    pub input_ports: usize,
    pub output_ports: usize,
    pub coupling_matrix: Vec<Vec<Complex>>,
    pub phase_shifters: Vec<f64>,
    pub attenuation: Vec<f64>,
}

/// Complex number for optical amplitudes
#[derive(Debug, Clone, Copy)]
pub struct Complex {
    pub real: f64,
    pub imag: f64,
}

/// Photonic processor simulation
#[derive(Debug)]
pub struct PhotonicProcessor {
    config: PhotonicConfig,
    networks: HashMap<String, PhotonicNeuralNetwork>,
    device_library: HashMap<String, Box<dyn OpticalDevice>>,
    #[allow(dead_code)]
    signal_registry: Vec<OpticalSignal>,
    _interference_engine: InterferenceEngine,
}

/// Optical device trait
pub trait OpticalDevice: std::fmt::Debug {
    fn process_signal(&self, input: &OpticalSignal) -> Result<OpticalSignal>;
    fn get_transfer_function(&self) -> OpticalTransferFunction;
    fn get_power_consumption(&self) -> f64;
    fn calibrate(&mut self, reference: &OpticalSignal) -> Result<()>;
}

/// Optical transfer function
#[derive(Debug, Clone)]
pub struct OpticalTransferFunction {
    pub amplitude_response: Vec<f64>,
    pub phase_response: Vec<f64>,
    pub frequency_range: (f64, f64),
}

/// Interference computation engine
#[derive(Debug, Clone)]
pub struct InterferenceEngine {
    pub coherence_length: f64,
    pub decoherence_time: f64,
    pub noise_level: f64,
}

impl Complex {
    pub fn new(real: f64, imag: f64) -> Self {
        Self { real, imag }
    }

    pub fn magnitude(&self) -> f64 {
        (self.real * self.real + self.imag * self.imag).sqrt()
    }

    pub fn phase(&self) -> f64 {
        self.imag.atan2(self.real)
    }

    pub fn conjugate(&self) -> Self {
        Self::new(self.real, -self.imag)
    }

    pub fn exp_i(phase: f64) -> Self {
        Self::new(phase.cos(), phase.sin())
    }
}

impl std::ops::Add for Complex {
    type Output = Self;

    fn add(self, other: Self) -> Self {
        Self::new(self.real + other.real, self.imag + other.imag)
    }
}

impl std::ops::Mul for Complex {
    type Output = Self;

    fn mul(self, other: Self) -> Self {
        Self::new(
            self.real * other.real - self.imag * other.imag,
            self.real * other.imag + self.imag * other.real,
        )
    }
}

impl OpticalSignal {
    /// Create a new optical signal
    pub fn new(wavelength: f64, power: f64) -> Self {
        Self {
            amplitude: vec![1.0],
            phase: vec![0.0],
            wavelength,
            power,
            polarization: Polarization::Linear { angle: 0.0 },
        }
    }

    /// Create a coherent signal with specified amplitude and phase
    pub fn coherent(amplitude: f64, phase: f64, wavelength: f64) -> Self {
        Self {
            amplitude: vec![amplitude],
            phase: vec![phase],
            wavelength,
            power: amplitude * amplitude,
            polarization: Polarization::Linear { angle: 0.0 },
        }
    }

    /// Create a multi-mode signal
    pub fn multi_mode(amplitudes: Vec<f64>, phases: Vec<f64>, wavelength: f64) -> Self {
        let power = amplitudes.iter().map(|a| a * a).sum();
        Self {
            amplitude: amplitudes,
            phase: phases,
            wavelength,
            power,
            polarization: Polarization::Linear { angle: 0.0 },
        }
    }

    /// Get the complex amplitude
    pub fn complex_amplitude(&self) -> Vec<Complex> {
        self.amplitude
            .iter()
            .zip(&self.phase)
            .map(|(&amp, &phase)| Complex::new(amp * phase.cos(), amp * phase.sin()))
            .collect()
    }

    /// Calculate signal intensity
    pub fn intensity(&self) -> Vec<f64> {
        self.amplitude.iter().map(|a| a * a).collect()
    }

    /// Add noise to the signal
    pub fn add_noise(&mut self, noise_power: f64) {
        let mut rng = thread_rng();
        for amp in &mut self.amplitude {
            let noise = noise_power.sqrt() * (rng.random::<f64>() - 0.5);
            *amp += noise;
        }
    }

    /// Apply phase shift
    pub fn apply_phase_shift(&mut self, phase_shift: f64) {
        for phase in &mut self.phase {
            *phase += phase_shift;
        }
    }

    /// Apply attenuation
    pub fn apply_attenuation(&mut self, attenuation_db: f64) {
        let factor = 10.0_f64.powf(-attenuation_db / 20.0);
        for amp in &mut self.amplitude {
            *amp *= factor;
        }
        self.power *= factor * factor;
    }
}

impl OpticalMatrixUnit {
    /// Create a new optical matrix unit
    pub fn new(input_ports: usize, output_ports: usize) -> Self {
        let coupling_matrix = vec![vec![Complex::new(0.0, 0.0); input_ports]; output_ports];
        let phase_shifters = vec![0.0; input_ports];
        let attenuation = vec![0.0; input_ports];

        Self {
            input_ports,
            output_ports,
            coupling_matrix,
            phase_shifters,
            attenuation,
        }
    }

    /// Configure as a Mach-Zehnder interferometer
    pub fn configure_mach_zehnder(
        &mut self,
        phase_diff: f64,
        beam_splitter_ratio: f64,
    ) -> Result<()> {
        if self.input_ports != 2 || self.output_ports != 2 {
            return Err(anyhow::anyhow!("Mach-Zehnder requires 2x2 configuration"));
        }

        let sqrt_ratio = beam_splitter_ratio.sqrt();
        let sqrt_ratio_conj = (1.0 - beam_splitter_ratio).sqrt();

        // Input beam splitter
        self.coupling_matrix[0][0] = Complex::new(sqrt_ratio, 0.0);
        self.coupling_matrix[0][1] = Complex::new(0.0, sqrt_ratio_conj);
        self.coupling_matrix[1][0] = Complex::new(0.0, sqrt_ratio_conj);
        self.coupling_matrix[1][1] = Complex::new(sqrt_ratio, 0.0);

        // Phase shift in one arm
        self.phase_shifters[1] = phase_diff;

        Ok(())
    }

    /// Process optical signals through the matrix unit
    pub fn process(&self, inputs: &[OpticalSignal]) -> Result<Vec<OpticalSignal>> {
        if inputs.len() != self.input_ports {
            return Err(anyhow::anyhow!("Input count mismatch"));
        }

        let mut outputs = vec![OpticalSignal::new(inputs[0].wavelength, 0.0); self.output_ports];

        for (out_idx, output) in outputs.iter_mut().enumerate() {
            let mut total_amplitude = Complex::new(0.0, 0.0);

            for (in_idx, input) in inputs.iter().enumerate() {
                let coupling = self.coupling_matrix[out_idx][in_idx];
                let phase_shift = Complex::exp_i(self.phase_shifters[in_idx]);
                let attenuation = 10.0_f64.powf(-self.attenuation[in_idx] / 20.0);

                for (&amp, &phase) in input.amplitude.iter().zip(&input.phase) {
                    let input_complex = Complex::new(amp * phase.cos(), amp * phase.sin());
                    let contribution =
                        input_complex * coupling * phase_shift * Complex::new(attenuation, 0.0);
                    total_amplitude = total_amplitude + contribution;
                }
            }

            output.amplitude = vec![total_amplitude.magnitude()];
            output.phase = vec![total_amplitude.phase()];
            output.power = total_amplitude.magnitude() * total_amplitude.magnitude();
        }

        Ok(outputs)
    }
}

impl PhotonicProcessor {
    /// Create a new photonic processor
    pub fn new(config: PhotonicConfig) -> Self {
        Self {
            config,
            networks: HashMap::new(),
            device_library: HashMap::new(),
            signal_registry: Vec::new(),
            _interference_engine: InterferenceEngine {
                coherence_length: 1e-3,  // 1 mm
                decoherence_time: 1e-12, // 1 ps
                noise_level: 0.01,
            },
        }
    }

    /// Create processor for specific platform
    pub fn with_platform(platform: OpticalPlatform) -> Self {
        let config = PhotonicConfig {
            platform,
            wavelength_range: match platform {
                OpticalPlatform::MachZehnder => (1530.0, 1570.0), // C-band
                OpticalPlatform::RingResonator => (1540.0, 1560.0),
                OpticalPlatform::PhotonicCrystal => (1500.0, 1600.0),
                _ => (1550.0, 1550.0), // Single wavelength
            },
            bandwidth: 40.0,    // 40 nm
            power_budget: 1e-3, // 1 mW
            num_waveguides: match platform {
                OpticalPlatform::MachZehnder => 64,
                OpticalPlatform::RingResonator => 256,
                OpticalPlatform::PhotonicCrystal => 1024,
                _ => 16,
            },
            coupling_strength: 0.1,
            loss_coefficient: 0.01, // 0.01 dB/cm
            temperature: 300.0,     // Room temperature
        };
        Self::new(config)
    }

    /// Add photonic neural network
    pub fn add_network(&mut self, name: String, network: PhotonicNeuralNetwork) {
        self.networks.insert(name, network);
    }

    /// Convert tensor to optical signals
    pub fn tensor_to_optical(
        &self,
        input: &Tensor,
        encoding: OpticalEncoding,
    ) -> Result<Vec<OpticalSignal>> {
        let data = input.data()?;
        match encoding {
            OpticalEncoding::Amplitude => self.amplitude_encode(&data),
            OpticalEncoding::Phase => self.phase_encode(&data),
            OpticalEncoding::Coherent => self.coherent_encode(&data),
            OpticalEncoding::Wavelength => self.wavelength_encode(&data),
        }
    }

    fn amplitude_encode(&self, data: &[f32]) -> Result<Vec<OpticalSignal>> {
        data.iter()
            .map(|&value| {
                let amplitude = value.abs() as f64;
                Ok(OpticalSignal::coherent(
                    amplitude,
                    0.0,
                    self.config.wavelength_range.0,
                ))
            })
            .collect()
    }

    fn phase_encode(&self, data: &[f32]) -> Result<Vec<OpticalSignal>> {
        data.iter()
            .map(|&value| {
                let phase = (value as f64) * std::f64::consts::PI;
                Ok(OpticalSignal::coherent(
                    1.0,
                    phase,
                    self.config.wavelength_range.0,
                ))
            })
            .collect()
    }

    fn coherent_encode(&self, data: &[f32]) -> Result<Vec<OpticalSignal>> {
        data.chunks(2)
            .map(|chunk| {
                let amplitude = chunk[0] as f64;
                let phase = chunk.get(1).map(|&x| x as f64).unwrap_or(0.0);
                Ok(OpticalSignal::coherent(
                    amplitude,
                    phase,
                    self.config.wavelength_range.0,
                ))
            })
            .collect()
    }

    fn wavelength_encode(&self, data: &[f32]) -> Result<Vec<OpticalSignal>> {
        let (min_wl, max_wl) = self.config.wavelength_range;
        data.iter()
            .map(|&value| {
                let normalized = (value + 1.0) / 2.0; // Normalize to [0, 1]
                let wavelength = min_wl + normalized as f64 * (max_wl - min_wl);
                Ok(OpticalSignal::coherent(1.0, 0.0, wavelength))
            })
            .collect()
    }

    /// Convert optical signals back to tensor
    pub fn optical_to_tensor(
        &self,
        signals: &[OpticalSignal],
        decoding: OpticalDecoding,
    ) -> Result<Tensor> {
        let decoded_values = match decoding {
            OpticalDecoding::Intensity => signals.iter().map(|s| s.intensity()[0] as f32).collect(),
            OpticalDecoding::Phase => signals.iter().map(|s| s.phase[0] as f32).collect(),
            OpticalDecoding::Amplitude => signals.iter().map(|s| s.amplitude[0] as f32).collect(),
        };

        Ok(Tensor::from_vec(decoded_values, &[signals.len()])?)
    }

    /// Simulate optical computation
    pub fn compute_optical_matmul(&self, input: &Tensor, weights: &Tensor) -> Result<Tensor> {
        // Convert inputs to optical signals
        let input_signals = self.tensor_to_optical(input, OpticalEncoding::Amplitude)?;

        // Create optical matrix unit
        let mut omu = OpticalMatrixUnit::new(input.shape()[0], weights.shape()[0]);

        // Configure coupling matrix from weights
        let weight_data = weights.data()?;
        for i in 0..weights.shape()[0] {
            for j in 0..weights.shape()[1] {
                let weight_idx = i * weights.shape()[1] + j;
                let weight_value = weight_data[weight_idx] as f64;
                omu.coupling_matrix[i][j] = Complex::new(weight_value, 0.0);
            }
        }

        // Process through optical matrix unit
        let output_signals = omu.process(&input_signals)?;

        // Convert back to tensor
        self.optical_to_tensor(&output_signals, OpticalDecoding::Intensity)
    }

    /// Get energy efficiency of optical computation
    pub fn get_energy_efficiency(&self) -> f64 {
        // Optical computing can be very energy efficient
        // Operations per joule (placeholder calculation)
        1e15 // 1 petaop per joule
    }

    /// Calibrate optical devices
    pub fn calibrate_devices(&mut self) -> Result<()> {
        let reference_signal = OpticalSignal::coherent(1.0, 0.0, self.config.wavelength_range.0);

        for device in self.device_library.values_mut() {
            device.calibrate(&reference_signal)?;
        }

        Ok(())
    }
}

/// Optical encoding schemes
#[derive(Debug, Clone, Copy)]
pub enum OpticalEncoding {
    Amplitude,
    Phase,
    Coherent,
    Wavelength,
}

/// Optical decoding schemes
#[derive(Debug, Clone, Copy)]
pub enum OpticalDecoding {
    Intensity,
    Phase,
    Amplitude,
}

impl Default for PhotonicConfig {
    fn default() -> Self {
        Self {
            platform: OpticalPlatform::Simulation,
            wavelength_range: (1550.0, 1550.0),
            bandwidth: 1.0,
            power_budget: 1e-3,
            num_waveguides: 16,
            coupling_strength: 0.1,
            loss_coefficient: 0.01,
            temperature: 300.0,
        }
    }
}

/// Convert classical neural network to photonic neural network
pub fn convert_to_photonic(
    classical_weights: &Tensor,
    conversion_method: PhotonicConversion,
) -> Result<PhotonicNeuralNetwork> {
    match conversion_method {
        PhotonicConversion::DirectMapping => convert_direct_mapping(classical_weights),
        PhotonicConversion::InterferometricMapping => {
            convert_interferometric_mapping(classical_weights)
        },
        PhotonicConversion::ResonatorMapping => convert_resonator_mapping(classical_weights),
    }
}

#[derive(Debug, Clone, Copy)]
pub enum PhotonicConversion {
    DirectMapping,
    InterferometricMapping,
    ResonatorMapping,
}

fn convert_direct_mapping(weights: &Tensor) -> Result<PhotonicNeuralNetwork> {
    // Simplified direct mapping
    let weight_data = weights.data()?;
    let mut network = PhotonicNeuralNetwork::new(weights.shape()[0], weights.shape()[1]);

    // Map weights to optical coupling coefficients
    for (i, &weight) in weight_data.iter().enumerate() {
        let layer = i / weights.shape()[1];
        let neuron = i % weights.shape()[1];
        network.set_coupling(layer, neuron, weight as f64)?;
    }

    Ok(network)
}

fn convert_interferometric_mapping(weights: &Tensor) -> Result<PhotonicNeuralNetwork> {
    // Use Mach-Zehnder interferometers for weights
    convert_direct_mapping(weights) // Placeholder
}

fn convert_resonator_mapping(weights: &Tensor) -> Result<PhotonicNeuralNetwork> {
    // Use ring resonators for weights
    convert_direct_mapping(weights) // Placeholder
}

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

    #[test]
    fn test_complex_operations() {
        let a = Complex::new(1.0, 2.0);
        let b = Complex::new(3.0, 4.0);

        let sum = a + b;
        assert_eq!(sum.real, 4.0);
        assert_eq!(sum.imag, 6.0);

        let product = a * b;
        assert_eq!(product.real, -5.0); // 1*3 - 2*4
        assert_eq!(product.imag, 10.0); // 1*4 + 2*3

        assert!((a.magnitude() - (5.0_f64).sqrt()).abs() < 1e-10);
    }

    #[test]
    fn test_optical_signal_creation() {
        let signal = OpticalSignal::new(1550.0, 1e-3);
        assert_eq!(signal.wavelength, 1550.0);
        assert_eq!(signal.power, 1e-3);
        assert_eq!(signal.amplitude.len(), 1);
        assert_eq!(signal.phase.len(), 1);
    }

    #[test]
    fn test_coherent_signal() {
        let signal = OpticalSignal::coherent(2.0, std::f64::consts::PI / 4.0, 1550.0);
        assert_eq!(signal.amplitude[0], 2.0);
        assert_eq!(signal.phase[0], std::f64::consts::PI / 4.0);
        assert_eq!(signal.power, 4.0);
    }

    #[test]
    fn test_multi_mode_signal() {
        let amplitudes = vec![1.0, 2.0, 3.0];
        let phases = vec![0.0, std::f64::consts::PI / 2.0, std::f64::consts::PI];

        let signal = OpticalSignal::multi_mode(amplitudes.clone(), phases.clone(), 1550.0);
        assert_eq!(signal.amplitude, amplitudes);
        assert_eq!(signal.phase, phases);
        assert_eq!(signal.power, 14.0); // 1^2 + 2^2 + 3^2
    }

    #[test]
    fn test_signal_intensity() {
        let signal = OpticalSignal::multi_mode(vec![1.0, 2.0], vec![0.0, 0.0], 1550.0);
        let intensity = signal.intensity();
        assert_eq!(intensity, vec![1.0, 4.0]);
    }

    #[test]
    fn test_phase_shift() {
        let mut signal = OpticalSignal::coherent(1.0, 0.0, 1550.0);
        signal.apply_phase_shift(std::f64::consts::PI / 2.0);
        assert!((signal.phase[0] - std::f64::consts::PI / 2.0).abs() < 1e-10);
    }

    #[test]
    fn test_attenuation() {
        let mut signal = OpticalSignal::coherent(1.0, 0.0, 1550.0);
        let original_power = signal.power;

        signal.apply_attenuation(3.0); // 3 dB attenuation
        assert!(signal.power < original_power);
        assert!((signal.power / original_power - 0.5).abs() < 0.01); // ~3dB ≈ factor of 2
    }

    #[test]
    fn test_optical_matrix_unit() {
        let omu = OpticalMatrixUnit::new(2, 2);
        assert_eq!(omu.input_ports, 2);
        assert_eq!(omu.output_ports, 2);
        assert_eq!(omu.coupling_matrix.len(), 2);
        assert_eq!(omu.coupling_matrix[0].len(), 2);
    }

    #[test]
    fn test_mach_zehnder_configuration() {
        let mut omu = OpticalMatrixUnit::new(2, 2);
        let result = omu.configure_mach_zehnder(std::f64::consts::PI, 0.5);
        assert!(result.is_ok());
        assert_eq!(omu.phase_shifters[1], std::f64::consts::PI);
    }

    #[test]
    fn test_photonic_processor_creation() {
        let processor = PhotonicProcessor::with_platform(OpticalPlatform::MachZehnder);
        assert_eq!(processor.config.platform, OpticalPlatform::MachZehnder);
        assert_eq!(processor.config.num_waveguides, 64);
    }

    #[test]
    fn test_platform_configurations() {
        let platforms = [
            OpticalPlatform::MachZehnder,
            OpticalPlatform::RingResonator,
            OpticalPlatform::PhotonicCrystal,
            OpticalPlatform::Simulation,
        ];

        for &platform in &platforms {
            let processor = PhotonicProcessor::with_platform(platform);
            assert_eq!(processor.config.platform, platform);
            assert!(processor.config.num_waveguides > 0);
            assert!(processor.config.power_budget > 0.0);
        }
    }

    #[test]
    fn test_optical_encoding() {
        let processor = PhotonicProcessor::with_platform(OpticalPlatform::Simulation);
        let input = Tensor::from_vec(vec![0.5, 1.0, -0.5], &[3]).expect("Tensor from_vec failed");

        let amplitude_encoded = processor.tensor_to_optical(&input, OpticalEncoding::Amplitude);
        assert!(amplitude_encoded.is_ok());

        let phase_encoded = processor.tensor_to_optical(&input, OpticalEncoding::Phase);
        assert!(phase_encoded.is_ok());

        let signals = amplitude_encoded.expect("operation failed in test");
        assert_eq!(signals.len(), 3);
        assert_eq!(signals[0].amplitude[0], 0.5);
        assert_eq!(signals[1].amplitude[0], 1.0);
        assert_eq!(signals[2].amplitude[0], 0.5); // abs(-0.5)
    }

    #[test]
    fn test_optical_decoding() {
        let processor = PhotonicProcessor::with_platform(OpticalPlatform::Simulation);
        let signals = vec![
            OpticalSignal::coherent(1.0, 0.0, 1550.0),
            OpticalSignal::coherent(2.0, std::f64::consts::PI / 2.0, 1550.0),
            OpticalSignal::coherent(0.5, std::f64::consts::PI, 1550.0),
        ];

        let intensity_decoded = processor.optical_to_tensor(&signals, OpticalDecoding::Intensity);
        assert!(intensity_decoded.is_ok());

        let decoded = intensity_decoded.expect("operation failed in test");
        let data = decoded.data().expect("operation failed in test");
        assert_eq!(data.len(), 3);
        assert_eq!(data[0], 1.0);
        assert_eq!(data[1], 4.0);
        assert_eq!(data[2], 0.25);
    }

    #[test]
    fn test_complex_amplitude() {
        let signal = OpticalSignal::multi_mode(
            vec![1.0, 2.0],
            vec![0.0, std::f64::consts::PI / 2.0],
            1550.0,
        );

        let complex_amps = signal.complex_amplitude();
        assert_eq!(complex_amps.len(), 2);
        assert!((complex_amps[0].real - 1.0).abs() < 1e-10);
        assert!(complex_amps[0].imag.abs() < 1e-10);
        assert!(complex_amps[1].real.abs() < 1e-10);
        assert!((complex_amps[1].imag - 2.0).abs() < 1e-10);
    }

    #[test]
    fn test_energy_efficiency() {
        let processor = PhotonicProcessor::with_platform(OpticalPlatform::Simulation);
        let efficiency = processor.get_energy_efficiency();
        assert!(efficiency > 0.0);
    }
}