quantrs2-circuit 0.1.3

Quantum circuit representation and DSL 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
//! Photonic quantum circuit support
//!
//! This module provides specialized support for photonic quantum computing,
//! including linear optical elements, measurement-based computation,
//! and continuous variable quantum computation.

use crate::builder::Circuit;
use quantrs2_core::{
    error::{QuantRS2Error, QuantRS2Result},
    gate::GateOp,
    qubit::QubitId,
};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::f64::consts::PI;

/// Photonic mode representing optical field modes
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct PhotonicMode {
    pub id: u32,
    pub polarization: Polarization,
    pub frequency: Option<f64>, // Optional frequency specification
}

impl PhotonicMode {
    #[must_use]
    pub const fn new(id: u32) -> Self {
        Self {
            id,
            polarization: Polarization::Horizontal,
            frequency: None,
        }
    }

    #[must_use]
    pub const fn with_polarization(mut self, polarization: Polarization) -> Self {
        self.polarization = polarization;
        self
    }

    #[must_use]
    pub const fn with_frequency(mut self, frequency: f64) -> Self {
        self.frequency = Some(frequency);
        self
    }
}

/// Polarization states for photonic modes
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Polarization {
    Horizontal,
    Vertical,
    Diagonal,
    AntiDiagonal,
    LeftCircular,
    RightCircular,
}

/// Linear optical elements for photonic circuits
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum PhotonicGate {
    /// Beam splitter with reflectivity parameter
    BeamSplitter {
        mode1: PhotonicMode,
        mode2: PhotonicMode,
        reflectivity: f64, // 0.0 = fully transmissive, 1.0 = fully reflective
        phase: f64,
    },
    /// Phase shifter
    PhaseShifter { mode: PhotonicMode, phase: f64 },
    /// Polarization rotator
    PolarizationRotator {
        mode: PhotonicMode,
        angle: f64, // Rotation angle
    },
    /// Half-wave plate
    HalfWavePlate {
        mode: PhotonicMode,
        angle: f64, // Fast axis angle
    },
    /// Quarter-wave plate
    QuarterWavePlate { mode: PhotonicMode, angle: f64 },
    /// Polarizing beam splitter
    PolarizingBeamSplitter {
        input: PhotonicMode,
        h_output: PhotonicMode, // Horizontal polarization output
        v_output: PhotonicMode, // Vertical polarization output
    },
    /// Mach-Zehnder interferometer
    MachZehnder {
        input1: PhotonicMode,
        input2: PhotonicMode,
        output1: PhotonicMode,
        output2: PhotonicMode,
        phase_shift: f64,
    },
    /// Hong-Ou-Mandel effect (two-photon interference)
    HongOuMandel {
        mode1: PhotonicMode,
        mode2: PhotonicMode,
    },
    /// Photonic controlled gate (using ancilla photons)
    PhotonicCNOT {
        control: PhotonicMode,
        target: PhotonicMode,
        ancilla: Vec<PhotonicMode>,
    },
    /// Kerr effect (nonlinear phase shift)
    KerrGate { mode: PhotonicMode, strength: f64 },
}

impl PhotonicGate {
    /// Get the modes involved in this gate
    #[must_use]
    pub fn modes(&self) -> Vec<PhotonicMode> {
        match self {
            Self::BeamSplitter { mode1, mode2, .. } => vec![*mode1, *mode2],
            Self::PhaseShifter { mode, .. }
            | Self::PolarizationRotator { mode, .. }
            | Self::HalfWavePlate { mode, .. }
            | Self::QuarterWavePlate { mode, .. } => vec![*mode],
            Self::PolarizingBeamSplitter {
                input,
                h_output,
                v_output,
                ..
            } => {
                vec![*input, *h_output, *v_output]
            }
            Self::MachZehnder {
                input1,
                input2,
                output1,
                output2,
                ..
            } => {
                vec![*input1, *input2, *output1, *output2]
            }
            Self::HongOuMandel { mode1, mode2, .. } => vec![*mode1, *mode2],
            Self::PhotonicCNOT {
                control,
                target,
                ancilla,
                ..
            } => {
                let mut modes = vec![*control, *target];
                modes.extend(ancilla);
                modes
            }
            Self::KerrGate { mode, .. } => vec![*mode],
        }
    }

    /// Get gate name
    #[must_use]
    pub const fn name(&self) -> &'static str {
        match self {
            Self::BeamSplitter { .. } => "BS",
            Self::PhaseShifter { .. } => "PS",
            Self::PolarizationRotator { .. } => "PR",
            Self::HalfWavePlate { .. } => "HWP",
            Self::QuarterWavePlate { .. } => "QWP",
            Self::PolarizingBeamSplitter { .. } => "PBS",
            Self::MachZehnder { .. } => "MZ",
            Self::HongOuMandel { .. } => "HOM",
            Self::PhotonicCNOT { .. } => "PCNOT",
            Self::KerrGate { .. } => "KERR",
        }
    }
}

/// Photonic measurement operations
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum PhotonicMeasurement {
    /// Photon number measurement
    PhotonNumber {
        mode: PhotonicMode,
        detector_efficiency: f64,
    },
    /// Homodyne measurement (measures quadrature)
    Homodyne {
        mode: PhotonicMode,
        local_oscillator_phase: f64,
        detection_efficiency: f64,
    },
    /// Heterodyne measurement (measures both quadratures)
    Heterodyne {
        mode: PhotonicMode,
        detection_efficiency: f64,
    },
    /// Polarization measurement
    Polarization {
        mode: PhotonicMode,
        measurement_basis: PolarizationBasis,
    },
    /// Coincidence detection
    Coincidence {
        modes: Vec<PhotonicMode>,
        time_window: f64, // nanoseconds
    },
}

/// Polarization measurement bases
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PolarizationBasis {
    Linear,   // H/V
    Diagonal, // D/A
    Circular, // L/R
}

/// Photonic circuit representation
#[derive(Debug, Clone)]
pub struct PhotonicCircuit {
    /// Number of photonic modes
    pub num_modes: usize,
    /// Photonic gates in the circuit
    pub gates: Vec<PhotonicGate>,
    /// Measurements in the circuit
    pub measurements: Vec<PhotonicMeasurement>,
    /// Mode mapping (logical to physical modes)
    pub mode_mapping: HashMap<u32, u32>,
}

impl PhotonicCircuit {
    /// Create a new photonic circuit
    #[must_use]
    pub fn new(num_modes: usize) -> Self {
        Self {
            num_modes,
            gates: Vec::new(),
            measurements: Vec::new(),
            mode_mapping: HashMap::new(),
        }
    }

    /// Add a photonic gate
    pub fn add_gate(&mut self, gate: PhotonicGate) -> QuantRS2Result<()> {
        // Validate that all modes are within bounds
        for mode in gate.modes() {
            if mode.id as usize >= self.num_modes {
                return Err(QuantRS2Error::InvalidInput(format!(
                    "Mode {} exceeds circuit size {}",
                    mode.id, self.num_modes
                )));
            }
        }

        self.gates.push(gate);
        Ok(())
    }

    /// Add a measurement
    pub fn add_measurement(&mut self, measurement: PhotonicMeasurement) -> QuantRS2Result<()> {
        self.measurements.push(measurement);
        Ok(())
    }

    /// Get circuit depth (simplified)
    #[must_use]
    pub fn depth(&self) -> usize {
        // For photonic circuits, depth is more complex due to parallelism
        // This is a simplified calculation
        self.gates.len()
    }

    /// Validate the photonic circuit
    pub fn validate(&self) -> QuantRS2Result<()> {
        // Check for mode conflicts
        let mut mode_usage = HashMap::new();

        for (layer, gate) in self.gates.iter().enumerate() {
            for mode in gate.modes() {
                if let Some(&last_usage) = mode_usage.get(&mode.id) {
                    if last_usage == layer {
                        return Err(QuantRS2Error::InvalidInput(format!(
                            "Mode {} used multiple times in layer {}",
                            mode.id, layer
                        )));
                    }
                }
                mode_usage.insert(mode.id, layer);
            }
        }

        Ok(())
    }
}

/// Builder for photonic circuits
#[derive(Clone)]
pub struct PhotonicCircuitBuilder {
    circuit: PhotonicCircuit,
}

impl PhotonicCircuitBuilder {
    /// Create a new builder
    #[must_use]
    pub fn new(num_modes: usize) -> Self {
        Self {
            circuit: PhotonicCircuit::new(num_modes),
        }
    }

    /// Add a beam splitter
    pub fn beam_splitter(
        &mut self,
        mode1: u32,
        mode2: u32,
        reflectivity: f64,
        phase: f64,
    ) -> QuantRS2Result<&mut Self> {
        let gate = PhotonicGate::BeamSplitter {
            mode1: PhotonicMode::new(mode1),
            mode2: PhotonicMode::new(mode2),
            reflectivity,
            phase,
        };
        self.circuit.add_gate(gate)?;
        Ok(self)
    }

    /// Add a phase shifter
    pub fn phase_shifter(&mut self, mode: u32, phase: f64) -> QuantRS2Result<&mut Self> {
        let gate = PhotonicGate::PhaseShifter {
            mode: PhotonicMode::new(mode),
            phase,
        };
        self.circuit.add_gate(gate)?;
        Ok(self)
    }

    /// Add a Mach-Zehnder interferometer
    pub fn mach_zehnder(
        &mut self,
        input1: u32,
        input2: u32,
        output1: u32,
        output2: u32,
        phase_shift: f64,
    ) -> QuantRS2Result<&mut Self> {
        let gate = PhotonicGate::MachZehnder {
            input1: PhotonicMode::new(input1),
            input2: PhotonicMode::new(input2),
            output1: PhotonicMode::new(output1),
            output2: PhotonicMode::new(output2),
            phase_shift,
        };
        self.circuit.add_gate(gate)?;
        Ok(self)
    }

    /// Add Hong-Ou-Mandel interference
    pub fn hong_ou_mandel(&mut self, mode1: u32, mode2: u32) -> QuantRS2Result<&mut Self> {
        let gate = PhotonicGate::HongOuMandel {
            mode1: PhotonicMode::new(mode1),
            mode2: PhotonicMode::new(mode2),
        };
        self.circuit.add_gate(gate)?;
        Ok(self)
    }

    /// Add photon number measurement
    pub fn measure_photon_number(&mut self, mode: u32) -> QuantRS2Result<&mut Self> {
        let measurement = PhotonicMeasurement::PhotonNumber {
            mode: PhotonicMode::new(mode),
            detector_efficiency: 1.0,
        };
        self.circuit.add_measurement(measurement)?;
        Ok(self)
    }

    /// Build the final circuit
    pub fn build(self) -> QuantRS2Result<PhotonicCircuit> {
        self.circuit.validate()?;
        Ok(self.circuit)
    }
}

/// Conversion between photonic and standard quantum circuits
pub struct PhotonicConverter;

impl PhotonicConverter {
    /// Convert a standard quantum circuit to photonic representation
    pub fn quantum_to_photonic<const N: usize>(
        circuit: &Circuit<N>,
    ) -> QuantRS2Result<PhotonicCircuit> {
        let mut photonic_circuit = PhotonicCircuit::new(N * 2); // Dual-rail encoding

        for gate in circuit.gates() {
            let photonic_gates = Self::convert_gate(gate.as_ref())?;
            for pg in photonic_gates {
                photonic_circuit.add_gate(pg)?;
            }
        }

        Ok(photonic_circuit)
    }

    /// Convert a quantum gate to photonic representation
    fn convert_gate(gate: &dyn GateOp) -> QuantRS2Result<Vec<PhotonicGate>> {
        let mut photonic_gates = Vec::new();
        let gate_name = gate.name();
        let qubits = gate.qubits();

        match gate_name {
            "H" => {
                // Hadamard gate using beam splitters and phase shifters
                let qubit = qubits[0].id();
                let mode0 = qubit * 2; // |0⟩ rail
                let mode1 = qubit * 2 + 1; // |1⟩ rail

                // Beam splitter with 50:50 ratio
                photonic_gates.push(PhotonicGate::BeamSplitter {
                    mode1: PhotonicMode::new(mode0),
                    mode2: PhotonicMode::new(mode1),
                    reflectivity: 0.5,
                    phase: 0.0,
                });
            }
            "X" => {
                // Pauli-X swaps the rails
                let qubit = qubits[0].id();
                let mode0 = qubit * 2;
                let mode1 = qubit * 2 + 1;

                // Swap using beam splitters
                photonic_gates.push(PhotonicGate::BeamSplitter {
                    mode1: PhotonicMode::new(mode0),
                    mode2: PhotonicMode::new(mode1),
                    reflectivity: 1.0, // Full reflection = swap
                    phase: 0.0,
                });
            }
            "Z" => {
                // Pauli-Z adds phase to |1⟩ rail
                let qubit = qubits[0].id();
                let mode1 = qubit * 2 + 1;

                photonic_gates.push(PhotonicGate::PhaseShifter {
                    mode: PhotonicMode::new(mode1),
                    phase: PI,
                });
            }
            "CNOT" => {
                // CNOT using photonic controlled gates (requires ancilla photons)
                let control_qubit = qubits[0].id();
                let target_qubit = qubits[1].id();

                let control_mode = control_qubit * 2 + 1; // Control on |1⟩ rail
                let target_mode0 = target_qubit * 2;
                let target_mode1 = target_qubit * 2 + 1;

                // Simplified photonic CNOT (would need more complex implementation)
                photonic_gates.push(PhotonicGate::PhotonicCNOT {
                    control: PhotonicMode::new(control_mode),
                    target: PhotonicMode::new(target_mode0),
                    ancilla: vec![PhotonicMode::new(target_mode1)],
                });
            }
            _ => {
                return Err(QuantRS2Error::InvalidInput(format!(
                    "Gate {gate_name} not supported in photonic conversion"
                )));
            }
        }

        Ok(photonic_gates)
    }
}

/// Continuous variable quantum computation support
#[derive(Debug, Clone)]
pub struct CVCircuit {
    /// Number of modes
    pub num_modes: usize,
    /// CV gates
    pub gates: Vec<CVGate>,
    /// Position/momentum measurements
    pub measurements: Vec<CVMeasurement>,
}

/// Continuous variable gates
#[derive(Debug, Clone, PartialEq)]
pub enum CVGate {
    /// Displacement operator D(α)
    Displacement {
        mode: u32,
        amplitude: f64,
        phase: f64,
    },
    /// Squeezing operator S(r)
    Squeezing {
        mode: u32,
        squeezing_parameter: f64,
        squeezing_angle: f64,
    },
    /// Two-mode squeezing
    TwoModeSqueezing {
        mode1: u32,
        mode2: u32,
        squeezing_parameter: f64,
    },
    /// Rotation gate (phase space rotation)
    Rotation { mode: u32, angle: f64 },
    /// Beam splitter (linear transformation)
    CVBeamSplitter {
        mode1: u32,
        mode2: u32,
        theta: f64, // Beam splitter angle
        phi: f64,   // Phase shift
    },
    /// Kerr gate (cubic phase)
    CVKerr { mode: u32, strength: f64 },
    /// Controlled displacement
    ControlledDisplacement {
        control_mode: u32,
        target_mode: u32,
        strength: f64,
    },
}

/// CV measurements
#[derive(Debug, Clone, PartialEq)]
pub enum CVMeasurement {
    /// Homodyne detection (position/momentum)
    CVHomodyne {
        mode: u32,
        angle: f64, // 0 = position, π/2 = momentum
    },
    /// Heterodyne detection
    CVHeterodyne { mode: u32 },
    /// Photon number measurement
    CVPhotonNumber { mode: u32 },
}

impl CVCircuit {
    /// Create new CV circuit
    #[must_use]
    pub const fn new(num_modes: usize) -> Self {
        Self {
            num_modes,
            gates: Vec::new(),
            measurements: Vec::new(),
        }
    }

    /// Add a displacement gate
    pub fn displacement(&mut self, mode: u32, amplitude: f64, phase: f64) -> QuantRS2Result<()> {
        self.gates.push(CVGate::Displacement {
            mode,
            amplitude,
            phase,
        });
        Ok(())
    }

    /// Add squeezing
    pub fn squeezing(&mut self, mode: u32, r: f64, angle: f64) -> QuantRS2Result<()> {
        self.gates.push(CVGate::Squeezing {
            mode,
            squeezing_parameter: r,
            squeezing_angle: angle,
        });
        Ok(())
    }

    /// Add beam splitter
    pub fn beam_splitter(
        &mut self,
        mode1: u32,
        mode2: u32,
        theta: f64,
        phi: f64,
    ) -> QuantRS2Result<()> {
        self.gates.push(CVGate::CVBeamSplitter {
            mode1,
            mode2,
            theta,
            phi,
        });
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use quantrs2_core::gate::multi::CNOT;
    use quantrs2_core::gate::single::Hadamard;

    #[test]
    fn test_photonic_circuit_creation() {
        let mut circuit = PhotonicCircuit::new(4);

        let bs_gate = PhotonicGate::BeamSplitter {
            mode1: PhotonicMode::new(0),
            mode2: PhotonicMode::new(1),
            reflectivity: 0.5,
            phase: 0.0,
        };

        assert!(circuit.add_gate(bs_gate).is_ok());
        assert_eq!(circuit.gates.len(), 1);
    }

    #[test]
    fn test_photonic_builder() {
        let mut builder = PhotonicCircuitBuilder::new(4);

        builder
            .beam_splitter(0, 1, 0.5, 0.0)
            .expect("Failed to add beam splitter");
        builder
            .phase_shifter(1, PI / 2.0)
            .expect("Failed to add phase shifter");
        builder
            .mach_zehnder(0, 1, 2, 3, PI / 4.0)
            .expect("Failed to add Mach-Zehnder interferometer");
        let result = builder.build();

        assert!(result.is_ok());
        let circuit = result.expect("Failed to build photonic circuit");
        assert_eq!(circuit.gates.len(), 3);
    }

    #[test]
    fn test_quantum_to_photonic_conversion() {
        let mut quantum_circuit = Circuit::<2>::new();
        quantum_circuit
            .add_gate(Hadamard { target: QubitId(0) })
            .expect("Failed to add Hadamard gate");

        let photonic_result = PhotonicConverter::quantum_to_photonic(&quantum_circuit);
        assert!(photonic_result.is_ok());

        let photonic_circuit = photonic_result.expect("Failed to convert to photonic circuit");
        assert_eq!(photonic_circuit.num_modes, 4); // Dual-rail encoding
        assert!(!photonic_circuit.gates.is_empty());
    }

    #[test]
    fn test_cv_circuit() {
        let mut cv_circuit = CVCircuit::new(2);

        assert!(cv_circuit.displacement(0, 1.0, 0.0).is_ok());
        assert!(cv_circuit.squeezing(1, 0.5, PI / 4.0).is_ok());
        assert!(cv_circuit.beam_splitter(0, 1, PI / 4.0, 0.0).is_ok());

        assert_eq!(cv_circuit.gates.len(), 3);
    }

    #[test]
    fn test_photonic_modes() {
        let mode = PhotonicMode::new(0)
            .with_polarization(Polarization::Vertical)
            .with_frequency(532e12); // Green light

        assert_eq!(mode.id, 0);
        assert_eq!(mode.polarization, Polarization::Vertical);
        assert_eq!(mode.frequency, Some(532e12));
    }

    #[test]
    fn test_hong_ou_mandel() {
        let mut builder = PhotonicCircuitBuilder::new(2);
        builder
            .hong_ou_mandel(0, 1)
            .expect("Failed to add Hong-Ou-Mandel gate");
        let result = builder.build();

        assert!(result.is_ok());
        let circuit = result.expect("Failed to build photonic circuit");
        assert_eq!(circuit.gates.len(), 1);

        match &circuit.gates[0] {
            PhotonicGate::HongOuMandel { mode1, mode2 } => {
                assert_eq!(mode1.id, 0);
                assert_eq!(mode2.id, 1);
            }
            _ => panic!("Expected HongOuMandel gate"),
        }
    }
}