quantrs2-core 0.1.3

Core types and traits for the QuantRS2 quantum computing 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
//! Quantum Gate Fusion Engine
//!
//! Optimizes quantum circuits by fusing adjacent gates into efficient sequences,
//! reducing the total number of matrix multiplications and improving performance.

use crate::error::{QuantRS2Error, QuantRS2Result};
use scirs2_core::Complex64;
use std::collections::HashMap;
use std::sync::{Arc, OnceLock, RwLock};

/// Types of gates that can be fused
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum GateType {
    // Single-qubit gates
    PauliX,
    PauliY,
    PauliZ,
    Hadamard,
    Phase(u64), // Quantized angle
    RX(u64),    // Quantized angle
    RY(u64),    // Quantized angle
    RZ(u64),    // Quantized angle
    S,
    T,

    // Two-qubit gates
    CNOT,
    CZ,
    SWAP,
    CRZ(u64), // Controlled RZ with quantized angle

    // Multi-qubit gates
    Toffoli,
    Fredkin,
}

/// A quantum gate with target qubits
#[derive(Debug, Clone)]
pub struct QuantumGate {
    pub gate_type: GateType,
    pub qubits: Vec<usize>,
    pub matrix: Vec<Complex64>,
}

impl QuantumGate {
    /// Create a new quantum gate
    pub fn new(gate_type: GateType, qubits: Vec<usize>) -> QuantRS2Result<Self> {
        let matrix = Self::compute_matrix(&gate_type)?;
        Ok(Self {
            gate_type,
            qubits,
            matrix,
        })
    }

    /// Compute the unitary matrix for a gate type
    fn compute_matrix(gate_type: &GateType) -> QuantRS2Result<Vec<Complex64>> {
        use std::f64::consts::{FRAC_1_SQRT_2, PI};

        let matrix = match gate_type {
            GateType::PauliX => vec![
                Complex64::new(0.0, 0.0),
                Complex64::new(1.0, 0.0),
                Complex64::new(1.0, 0.0),
                Complex64::new(0.0, 0.0),
            ],
            GateType::PauliY => vec![
                Complex64::new(0.0, 0.0),
                Complex64::new(0.0, -1.0),
                Complex64::new(0.0, 1.0),
                Complex64::new(0.0, 0.0),
            ],
            GateType::PauliZ => vec![
                Complex64::new(1.0, 0.0),
                Complex64::new(0.0, 0.0),
                Complex64::new(0.0, 0.0),
                Complex64::new(-1.0, 0.0),
            ],
            GateType::Hadamard => vec![
                Complex64::new(FRAC_1_SQRT_2, 0.0),
                Complex64::new(FRAC_1_SQRT_2, 0.0),
                Complex64::new(FRAC_1_SQRT_2, 0.0),
                Complex64::new(-FRAC_1_SQRT_2, 0.0),
            ],
            GateType::S => vec![
                Complex64::new(1.0, 0.0),
                Complex64::new(0.0, 0.0),
                Complex64::new(0.0, 0.0),
                Complex64::new(0.0, 1.0),
            ],
            GateType::T => vec![
                Complex64::new(1.0, 0.0),
                Complex64::new(0.0, 0.0),
                Complex64::new(0.0, 0.0),
                Complex64::new(FRAC_1_SQRT_2, FRAC_1_SQRT_2),
            ],
            GateType::RX(quantized_angle) => {
                let angle = (*quantized_angle as f64) / 1_000_000.0;
                let cos_half = (angle / 2.0).cos();
                let sin_half = (angle / 2.0).sin();
                vec![
                    Complex64::new(cos_half, 0.0),
                    Complex64::new(0.0, -sin_half),
                    Complex64::new(0.0, -sin_half),
                    Complex64::new(cos_half, 0.0),
                ]
            }
            GateType::RY(quantized_angle) => {
                let angle = (*quantized_angle as f64) / 1_000_000.0;
                let cos_half = (angle / 2.0).cos();
                let sin_half = (angle / 2.0).sin();
                vec![
                    Complex64::new(cos_half, 0.0),
                    Complex64::new(-sin_half, 0.0),
                    Complex64::new(sin_half, 0.0),
                    Complex64::new(cos_half, 0.0),
                ]
            }
            GateType::RZ(quantized_angle) => {
                let angle = (*quantized_angle as f64) / 1_000_000.0;
                let cos_half = (angle / 2.0).cos();
                let sin_half = (angle / 2.0).sin();
                vec![
                    Complex64::new(cos_half, -sin_half),
                    Complex64::new(0.0, 0.0),
                    Complex64::new(0.0, 0.0),
                    Complex64::new(cos_half, sin_half),
                ]
            }
            GateType::Phase(quantized_angle) => {
                let angle = (*quantized_angle as f64) / 1_000_000.0;
                vec![
                    Complex64::new(1.0, 0.0),
                    Complex64::new(0.0, 0.0),
                    Complex64::new(0.0, 0.0),
                    Complex64::new(angle.cos(), angle.sin()),
                ]
            }
            GateType::CNOT => vec![
                Complex64::new(1.0, 0.0),
                Complex64::new(0.0, 0.0),
                Complex64::new(0.0, 0.0),
                Complex64::new(0.0, 0.0),
                Complex64::new(0.0, 0.0),
                Complex64::new(1.0, 0.0),
                Complex64::new(0.0, 0.0),
                Complex64::new(0.0, 0.0),
                Complex64::new(0.0, 0.0),
                Complex64::new(0.0, 0.0),
                Complex64::new(0.0, 0.0),
                Complex64::new(1.0, 0.0),
                Complex64::new(0.0, 0.0),
                Complex64::new(0.0, 0.0),
                Complex64::new(1.0, 0.0),
                Complex64::new(0.0, 0.0),
            ],
            _ => return Err(QuantRS2Error::UnsupportedGate(format!("{gate_type:?}"))),
        };

        Ok(matrix)
    }

    /// Get the number of qubits this gate acts on
    pub const fn num_qubits(&self) -> usize {
        match self.gate_type {
            GateType::PauliX
            | GateType::PauliY
            | GateType::PauliZ
            | GateType::Hadamard
            | GateType::Phase(_)
            | GateType::RX(_)
            | GateType::RY(_)
            | GateType::RZ(_)
            | GateType::S
            | GateType::T => 1,

            GateType::CNOT | GateType::CZ | GateType::SWAP | GateType::CRZ(_) => 2,

            GateType::Toffoli | GateType::Fredkin => 3,
        }
    }
}

/// Rule for fusing gates
#[derive(Debug, Clone)]
pub struct FusionRule {
    pub pattern: Vec<GateType>,
    pub replacement: Vec<GateType>,
    pub efficiency_gain: f64, // Expected speedup
}

impl FusionRule {
    /// Create common fusion rules
    pub fn common_rules() -> Vec<Self> {
        vec![
            // X * X = I (eliminate double X)
            Self {
                pattern: vec![GateType::PauliX, GateType::PauliX],
                replacement: vec![], // Identity = no gates
                efficiency_gain: 2.0,
            },
            // Y * Y = I
            Self {
                pattern: vec![GateType::PauliY, GateType::PauliY],
                replacement: vec![],
                efficiency_gain: 2.0,
            },
            // Z * Z = I
            Self {
                pattern: vec![GateType::PauliZ, GateType::PauliZ],
                replacement: vec![],
                efficiency_gain: 2.0,
            },
            // H * H = I
            Self {
                pattern: vec![GateType::Hadamard, GateType::Hadamard],
                replacement: vec![],
                efficiency_gain: 2.0,
            },
            // S * S = Z
            Self {
                pattern: vec![GateType::S, GateType::S],
                replacement: vec![GateType::PauliZ],
                efficiency_gain: 2.0,
            },
            // T * T * T * T = I
            Self {
                pattern: vec![GateType::T, GateType::T, GateType::T, GateType::T],
                replacement: vec![],
                efficiency_gain: 4.0,
            },
            // Commute Z and RZ (can be parallelized)
            // This would be handled by specialized logic
        ]
    }
}

/// A sequence of fused gates
#[derive(Debug, Clone)]
pub struct FusedGateSequence {
    pub gates: Vec<QuantumGate>,
    pub fused_matrix: Vec<Complex64>,
    pub target_qubits: Vec<usize>,
    pub efficiency_gain: f64,
}

impl FusedGateSequence {
    /// Create a fused sequence from individual gates
    pub fn from_gates(gates: Vec<QuantumGate>) -> QuantRS2Result<Self> {
        if gates.is_empty() {
            return Err(QuantRS2Error::InvalidInput(
                "Empty gate sequence".to_string(),
            ));
        }

        // All gates must act on the same qubits for fusion
        let target_qubits = gates[0].qubits.clone();
        for gate in &gates {
            if gate.qubits != target_qubits {
                return Err(QuantRS2Error::InvalidInput(
                    "All gates must act on the same qubits for fusion".to_string(),
                ));
            }
        }

        // Compute fused matrix by multiplying individual matrices
        let matrix_size = gates[0].matrix.len();
        let sqrt_size = (matrix_size as f64).sqrt() as usize;

        let mut fused_matrix = Self::identity_matrix(sqrt_size);

        // Multiply matrices in reverse order (gates are applied left to right)
        for gate in gates.iter().rev() {
            fused_matrix = Self::matrix_multiply(&fused_matrix, &gate.matrix, sqrt_size)?;
        }

        let efficiency_gain = gates.len() as f64; // Each gate fusion saves one matrix multiplication

        Ok(Self {
            gates,
            fused_matrix,
            target_qubits,
            efficiency_gain,
        })
    }

    /// Create identity matrix
    fn identity_matrix(size: usize) -> Vec<Complex64> {
        let mut matrix = vec![Complex64::new(0.0, 0.0); size * size];
        for i in 0..size {
            matrix[i * size + i] = Complex64::new(1.0, 0.0);
        }
        matrix
    }

    /// Check if matrix is approximately identity
    fn is_identity_matrix(&self) -> bool {
        let size = (self.fused_matrix.len() as f64).sqrt() as usize;
        let identity = Self::identity_matrix(size);

        for (a, b) in self.fused_matrix.iter().zip(identity.iter()) {
            if (a - b).norm() > 1e-10 {
                return false;
            }
        }
        true
    }

    /// Multiply two matrices
    fn matrix_multiply(
        a: &[Complex64],
        b: &[Complex64],
        size: usize,
    ) -> QuantRS2Result<Vec<Complex64>> {
        if a.len() != size * size || b.len() != size * size {
            return Err(QuantRS2Error::InvalidInput(
                "Matrix size mismatch".to_string(),
            ));
        }

        let mut result = vec![Complex64::new(0.0, 0.0); size * size];

        for i in 0..size {
            for j in 0..size {
                for k in 0..size {
                    result[i * size + j] += a[i * size + k] * b[k * size + j];
                }
            }
        }

        Ok(result)
    }
}

/// Gate fusion engine
pub struct GateFusionEngine {
    rules: Vec<FusionRule>,
    statistics: Arc<RwLock<FusionStatistics>>,
}

/// Fusion performance statistics
#[derive(Debug, Clone, Default)]
pub struct FusionStatistics {
    pub total_fusions: u64,
    pub gates_eliminated: u64,
    pub total_efficiency_gain: f64,
    pub fusion_types: HashMap<String, u64>,
}

impl GateFusionEngine {
    /// Create a new fusion engine
    pub fn new() -> Self {
        Self {
            rules: FusionRule::common_rules(),
            statistics: Arc::new(RwLock::new(FusionStatistics::default())),
        }
    }

    /// Add a custom fusion rule
    pub fn add_rule(&mut self, rule: FusionRule) {
        self.rules.push(rule);
    }

    /// Fuse a sequence of gates
    pub fn fuse_gates(&self, gates: Vec<QuantumGate>) -> QuantRS2Result<Vec<FusedGateSequence>> {
        if gates.is_empty() {
            return Ok(vec![]);
        }

        let mut fused_sequences = Vec::new();
        let mut i = 0;

        while i < gates.len() {
            let gate = &gates[i];

            // Try to find fusable patterns
            if let Some(fusion_length) = self.find_fusion_pattern(&gates[i..]) {
                // Found a fusable pattern
                let fusion_gates = gates[i..i + fusion_length].to_vec();
                let fused_sequence = FusedGateSequence::from_gates(fusion_gates)?;

                // Only add non-identity sequences
                if fused_sequence.is_identity_matrix() {
                    // Identity matrix - gates cancelled out, count them as eliminated
                    if let Ok(mut stats) = self.statistics.write() {
                        stats.total_fusions += 1;
                        stats.gates_eliminated += fusion_length as u64; // All gates eliminated
                    }
                } else {
                    // Update statistics
                    if let Ok(mut stats) = self.statistics.write() {
                        stats.total_fusions += 1;
                        stats.gates_eliminated += (fusion_length - 1) as u64;
                        stats.total_efficiency_gain += fused_sequence.efficiency_gain;

                        let fusion_type = format!("{:?}_fusion", gate.gate_type);
                        *stats.fusion_types.entry(fusion_type).or_insert(0) += 1;
                    }

                    fused_sequences.push(fused_sequence);
                }
                i += fusion_length;
            } else {
                // No fusion pattern found, group consecutive gates on the same qubit
                let mut gate_group = vec![gate.clone()];
                let mut j = i + 1;

                // Collect consecutive gates on the same qubit
                while j < gates.len() && gates[j].qubits == gate.qubits {
                    gate_group.push(gates[j].clone());
                    j += 1;
                }

                // Create a fused sequence for the group
                let fused_sequence = FusedGateSequence::from_gates(gate_group)?;
                fused_sequences.push(fused_sequence);
                i = j;
            }
        }

        Ok(fused_sequences)
    }

    /// Find fusion patterns in gate sequence
    fn find_fusion_pattern(&self, gates: &[QuantumGate]) -> Option<usize> {
        for rule in &self.rules {
            if gates.len() >= rule.pattern.len() {
                let matches = gates[..rule.pattern.len()]
                    .iter()
                    .zip(&rule.pattern)
                    .all(|(gate, pattern_gate)| gate.gate_type == *pattern_gate);

                // Also check that all gates in the pattern act on the same qubits
                let same_qubits = if rule.pattern.len() > 1 {
                    let first_qubits = &gates[0].qubits;
                    gates[1..rule.pattern.len()]
                        .iter()
                        .all(|gate| gate.qubits == *first_qubits)
                } else {
                    true // Single gate patterns always match
                };

                if matches && same_qubits {
                    return Some(rule.pattern.len());
                }
            }
        }

        // Check for consecutive identical single-qubit gates on the same qubits (can be optimized)
        if gates.len() >= 2 {
            let first = &gates[0];
            if first.num_qubits() == 1 {
                let mut count = 1;
                for gate in gates.iter().skip(1) {
                    if gate.gate_type == first.gate_type && gate.qubits == first.qubits {
                        count += 1;
                    } else {
                        break;
                    }
                }
                if count > 1 {
                    return Some(count); // Found consecutive identical gates on same qubits
                }
            }
        }

        None
    }

    /// Get fusion statistics
    pub fn get_statistics(&self) -> FusionStatistics {
        self.statistics
            .read()
            .map(|guard| guard.clone())
            .unwrap_or_default()
    }

    /// Get global fusion statistics
    pub fn get_global_statistics() -> FusionStatistics {
        if let Some(engine) = GLOBAL_FUSION_ENGINE.get() {
            engine.get_statistics()
        } else {
            FusionStatistics::default()
        }
    }

    /// Reset statistics
    pub fn reset_statistics(&self) {
        if let Ok(mut stats) = self.statistics.write() {
            *stats = FusionStatistics::default();
        }
    }
}

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

/// Global gate fusion engine
static GLOBAL_FUSION_ENGINE: OnceLock<GateFusionEngine> = OnceLock::new();

/// Get the global gate fusion engine
pub fn get_global_fusion_engine() -> &'static GateFusionEngine {
    GLOBAL_FUSION_ENGINE.get_or_init(GateFusionEngine::new)
}

/// Apply gate fusion to a circuit
pub fn apply_gate_fusion(gates: Vec<QuantumGate>) -> QuantRS2Result<Vec<FusedGateSequence>> {
    let engine = get_global_fusion_engine();
    engine.fuse_gates(gates)
}

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

    #[test]
    fn test_pauli_x_fusion() {
        let gates = vec![
            QuantumGate::new(GateType::PauliX, vec![0]).expect("failed to create PauliX gate"),
            QuantumGate::new(GateType::PauliX, vec![0]).expect("failed to create PauliX gate"),
        ];

        let engine = GateFusionEngine::new();
        let fused = engine.fuse_gates(gates).expect("failed to fuse gates");

        // Should eliminate both X gates (X*X = I)
        assert_eq!(fused.len(), 0);

        let stats = engine.get_statistics();
        assert_eq!(stats.gates_eliminated, 2);
    }

    #[test]
    fn test_hadamard_fusion() {
        let gates = vec![
            QuantumGate::new(GateType::Hadamard, vec![0]).expect("failed to create Hadamard gate"),
            QuantumGate::new(GateType::Hadamard, vec![0]).expect("failed to create Hadamard gate"),
        ];

        let engine = GateFusionEngine::new();
        let fused = engine.fuse_gates(gates).expect("failed to fuse gates");

        // Should eliminate both H gates (H*H = I)
        assert_eq!(fused.len(), 0);
    }

    #[test]
    fn test_mixed_gate_fusion() {
        let gates = vec![
            QuantumGate::new(GateType::PauliX, vec![0]).expect("failed to create PauliX gate"),
            QuantumGate::new(GateType::PauliY, vec![0]).expect("failed to create PauliY gate"),
            QuantumGate::new(GateType::PauliZ, vec![0]).expect("failed to create PauliZ gate"),
        ];

        let engine = GateFusionEngine::new();
        let fused = engine.fuse_gates(gates).expect("failed to fuse gates");

        // Should create one fused sequence with all three gates
        assert_eq!(fused.len(), 1);
        assert_eq!(fused[0].gates.len(), 3);
    }

    #[test]
    fn test_no_fusion_different_qubits() {
        let gates = vec![
            QuantumGate::new(GateType::PauliX, vec![0]).expect("failed to create PauliX gate"),
            QuantumGate::new(GateType::PauliX, vec![1]).expect("failed to create PauliX gate"), // Different qubit
        ];

        let engine = GateFusionEngine::new();
        let fused = engine.fuse_gates(gates).expect("failed to fuse gates");

        // Should create two separate sequences
        assert_eq!(fused.len(), 2);
    }

    #[test]
    fn test_matrix_multiplication() {
        // Test identity multiplication
        let identity = vec![
            Complex64::new(1.0, 0.0),
            Complex64::new(0.0, 0.0),
            Complex64::new(0.0, 0.0),
            Complex64::new(1.0, 0.0),
        ];
        let pauli_x = vec![
            Complex64::new(0.0, 0.0),
            Complex64::new(1.0, 0.0),
            Complex64::new(1.0, 0.0),
            Complex64::new(0.0, 0.0),
        ];

        let result = FusedGateSequence::matrix_multiply(&identity, &pauli_x, 2)
            .expect("matrix multiplication failed");

        // I * X should equal X
        for (a, b) in result.iter().zip(pauli_x.iter()) {
            assert!((a - b).norm() < 1e-10);
        }
    }

    #[test]
    fn test_efficiency_gain_calculation() {
        let gates = vec![
            QuantumGate::new(GateType::S, vec![0]).expect("failed to create S gate"),
            QuantumGate::new(GateType::T, vec![0]).expect("failed to create T gate"),
            QuantumGate::new(GateType::Hadamard, vec![0]).expect("failed to create Hadamard gate"),
        ];

        let fused = FusedGateSequence::from_gates(gates).expect("failed to create fused sequence");
        assert_eq!(fused.efficiency_gain, 3.0); // Three gates fused into one
    }
}