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
//! Cross-Compilation with SciRS2 IR Tools
//!
//! This module provides quantum circuit cross-compilation using SciRS2's intermediate
//! representation (IR) tools for multi-backend optimization and code generation.

use crate::{DeviceError, DeviceResult};
use scirs2_core::ndarray::{Array1, Array2};
use scirs2_core::Complex64;
use std::collections::HashMap;

/// Target backend for cross-compilation
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TargetBackend {
    /// IBM Quantum (OpenQASM 2.0)
    IBMQuantum,
    /// AWS Braket
    AWSBraket,
    /// Azure Quantum
    AzureQuantum,
    /// Generic QASM
    GenericQASM,
    /// Custom backend
    Custom,
}

/// Optimization level for IR compilation
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OptimizationLevel {
    /// No optimization
    O0,
    /// Basic optimization
    O1,
    /// Moderate optimization
    O2,
    /// Aggressive optimization
    O3,
}

/// Cross-compiler configuration
#[derive(Debug, Clone)]
pub struct CrossCompilerConfig {
    /// Target backend
    pub target: TargetBackend,
    /// Optimization level
    pub optimization_level: OptimizationLevel,
    /// Enable gate fusion
    pub enable_gate_fusion: bool,
    /// Enable commutation-based optimization
    pub enable_commutation_opt: bool,
    /// Enable backend-specific optimizations
    pub enable_backend_opt: bool,
    /// Maximum circuit depth (None for unlimited)
    pub max_circuit_depth: Option<usize>,
}

impl Default for CrossCompilerConfig {
    fn default() -> Self {
        Self {
            target: TargetBackend::GenericQASM,
            optimization_level: OptimizationLevel::O2,
            enable_gate_fusion: true,
            enable_commutation_opt: true,
            enable_backend_opt: true,
            max_circuit_depth: None,
        }
    }
}

/// Quantum circuit intermediate representation
#[derive(Debug, Clone)]
pub struct QuantumIR {
    /// Number of qubits
    pub num_qubits: usize,
    /// IR instructions
    pub instructions: Vec<IRInstruction>,
    /// Metadata
    pub metadata: IRMetadata,
}

/// IR instruction representing quantum operations
#[derive(Debug, Clone)]
pub enum IRInstruction {
    /// Single-qubit gate
    SingleQubitGate {
        gate_type: SingleQubitGateType,
        target: usize,
        parameters: Vec<f64>,
    },
    /// Two-qubit gate
    TwoQubitGate {
        gate_type: TwoQubitGateType,
        control: usize,
        target: usize,
        parameters: Vec<f64>,
    },
    /// Measurement
    Measure { qubit: usize, classical_bit: usize },
    /// Barrier (prevents optimization across it)
    Barrier { qubits: Vec<usize> },
    /// Custom gate
    Custom {
        name: String,
        qubits: Vec<usize>,
        parameters: Vec<f64>,
    },
}

/// Single-qubit gate types
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SingleQubitGateType {
    Hadamard,
    PauliX,
    PauliY,
    PauliZ,
    Phase,
    T,
    TDagger,
    S,
    SDagger,
    Rx,
    Ry,
    Rz,
    U3,
}

/// Two-qubit gate types
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TwoQubitGateType {
    CNOT,
    CZ,
    SWAP,
    Controlled,
}

/// IR metadata
#[derive(Debug, Clone)]
pub struct IRMetadata {
    /// Circuit name
    pub name: String,
    /// Creation timestamp
    pub created: std::time::SystemTime,
    /// Source backend (if converted)
    pub source_backend: Option<TargetBackend>,
    /// Optimization passes applied
    pub optimization_passes: Vec<String>,
}

impl Default for IRMetadata {
    fn default() -> Self {
        Self {
            name: "quantum_circuit".to_string(),
            created: std::time::SystemTime::now(),
            source_backend: None,
            optimization_passes: Vec::new(),
        }
    }
}

/// Compilation result
#[derive(Debug, Clone)]
pub struct CompilationResult {
    /// Compiled code for target backend
    pub code: String,
    /// Optimized IR
    pub optimized_ir: QuantumIR,
    /// Compilation statistics
    pub statistics: CompilationStatistics,
}

/// Compilation statistics
#[derive(Debug, Clone)]
pub struct CompilationStatistics {
    /// Original instruction count
    pub original_instruction_count: usize,
    /// Optimized instruction count
    pub optimized_instruction_count: usize,
    /// Circuit depth before optimization
    pub original_depth: usize,
    /// Circuit depth after optimization
    pub optimized_depth: usize,
    /// Number of optimization passes applied
    pub num_optimization_passes: usize,
    /// Compilation time (milliseconds)
    pub compilation_time_ms: u128,
}

/// Cross-compiler using SciRS2 IR tools
pub struct CrossCompiler {
    config: CrossCompilerConfig,
}

impl CrossCompiler {
    /// Create a new cross-compiler
    pub fn new(config: CrossCompilerConfig) -> Self {
        Self { config }
    }

    /// Create compiler with default configuration
    pub fn default() -> Self {
        Self::new(CrossCompilerConfig::default())
    }

    /// Compile quantum IR to target backend
    ///
    /// # Arguments
    /// * `ir` - Quantum circuit in IR format
    ///
    /// # Returns
    /// Compilation result with backend-specific code
    pub fn compile(&self, ir: QuantumIR) -> DeviceResult<CompilationResult> {
        let start_time = std::time::Instant::now();

        let original_count = ir.instructions.len();
        let original_depth = self.compute_circuit_depth(&ir);

        // Apply optimization passes based on configuration
        let mut optimized_ir = ir.clone();
        let mut num_passes = 0;

        if self.config.optimization_level != OptimizationLevel::O0 {
            // Gate fusion pass
            if self.config.enable_gate_fusion {
                optimized_ir = self.apply_gate_fusion(optimized_ir)?;
                num_passes += 1;
            }

            // Commutation-based optimization
            if self.config.enable_commutation_opt {
                optimized_ir = self.apply_commutation_optimization(optimized_ir)?;
                num_passes += 1;
            }

            // Backend-specific optimizations
            if self.config.enable_backend_opt {
                optimized_ir = self.apply_backend_optimization(optimized_ir)?;
                num_passes += 1;
            }

            // Remove identity gates
            optimized_ir = self.remove_identity_gates(optimized_ir)?;
            num_passes += 1;
        }

        let optimized_count = optimized_ir.instructions.len();
        let optimized_depth = self.compute_circuit_depth(&optimized_ir);

        // Generate code for target backend
        let code = self.generate_backend_code(&optimized_ir)?;

        let compilation_time = start_time.elapsed().as_millis();

        Ok(CompilationResult {
            code,
            optimized_ir,
            statistics: CompilationStatistics {
                original_instruction_count: original_count,
                optimized_instruction_count: optimized_count,
                original_depth,
                optimized_depth,
                num_optimization_passes: num_passes,
                compilation_time_ms: compilation_time,
            },
        })
    }

    /// Compute circuit depth
    fn compute_circuit_depth(&self, ir: &QuantumIR) -> usize {
        let mut qubit_depths = vec![0; ir.num_qubits];

        for instr in &ir.instructions {
            match instr {
                IRInstruction::SingleQubitGate { target, .. } => {
                    qubit_depths[*target] += 1;
                }
                IRInstruction::TwoQubitGate {
                    control, target, ..
                } => {
                    let max_depth = qubit_depths[*control].max(qubit_depths[*target]);
                    qubit_depths[*control] = max_depth + 1;
                    qubit_depths[*target] = max_depth + 1;
                }
                IRInstruction::Measure { qubit, .. } => {
                    qubit_depths[*qubit] += 1;
                }
                IRInstruction::Barrier { qubits } => {
                    if !qubits.is_empty() {
                        let max_depth = qubits.iter().map(|&q| qubit_depths[q]).max().unwrap_or(0);
                        for &q in qubits {
                            qubit_depths[q] = max_depth + 1;
                        }
                    }
                }
                IRInstruction::Custom { qubits, .. } => {
                    if !qubits.is_empty() {
                        let max_depth = qubits.iter().map(|&q| qubit_depths[q]).max().unwrap_or(0);
                        for &q in qubits {
                            qubit_depths[q] = max_depth + 1;
                        }
                    }
                }
            }
        }

        qubit_depths.into_iter().max().unwrap_or(0)
    }

    /// Apply gate fusion optimization
    fn apply_gate_fusion(&self, mut ir: QuantumIR) -> DeviceResult<QuantumIR> {
        let mut optimized_instructions = Vec::new();
        let mut i = 0;

        while i < ir.instructions.len() {
            // Look for consecutive rotation gates on the same qubit
            if i + 1 < ir.instructions.len() {
                match (&ir.instructions[i], &ir.instructions[i + 1]) {
                    (
                        IRInstruction::SingleQubitGate {
                            gate_type: SingleQubitGateType::Rz,
                            target: t1,
                            parameters: p1,
                        },
                        IRInstruction::SingleQubitGate {
                            gate_type: SingleQubitGateType::Rz,
                            target: t2,
                            parameters: p2,
                        },
                    ) if t1 == t2 && p1.len() == 1 && p2.len() == 1 => {
                        // Fuse consecutive Rz gates
                        optimized_instructions.push(IRInstruction::SingleQubitGate {
                            gate_type: SingleQubitGateType::Rz,
                            target: *t1,
                            parameters: vec![p1[0] + p2[0]],
                        });
                        i += 2;
                        continue;
                    }
                    _ => {}
                }
            }

            optimized_instructions.push(ir.instructions[i].clone());
            i += 1;
        }

        ir.instructions = optimized_instructions;
        ir.metadata
            .optimization_passes
            .push("gate_fusion".to_string());
        Ok(ir)
    }

    /// Apply commutation-based optimization
    fn apply_commutation_optimization(&self, mut ir: QuantumIR) -> DeviceResult<QuantumIR> {
        // Simplified commutation optimization: move measurements to the end
        let mut measurements = Vec::new();
        let mut other_instructions = Vec::new();

        for instr in ir.instructions {
            match instr {
                IRInstruction::Measure { .. } => measurements.push(instr),
                _ => other_instructions.push(instr),
            }
        }

        other_instructions.extend(measurements);
        ir.instructions = other_instructions;
        ir.metadata
            .optimization_passes
            .push("commutation_opt".to_string());
        Ok(ir)
    }

    /// Apply backend-specific optimizations
    fn apply_backend_optimization(&self, mut ir: QuantumIR) -> DeviceResult<QuantumIR> {
        match self.config.target {
            TargetBackend::IBMQuantum => {
                // IBM prefers U3 gates and CX
                ir = self.decompose_to_ibm_basis(ir)?;
            }
            TargetBackend::AWSBraket => {
                // AWS Braket supports various gate sets
                // Keep standard gates
            }
            TargetBackend::AzureQuantum => {
                // Azure supports standard gates
                // Keep standard gates
            }
            _ => {}
        }

        ir.metadata
            .optimization_passes
            .push("backend_specific".to_string());
        Ok(ir)
    }

    /// Decompose gates to IBM basis (U3, CX)
    fn decompose_to_ibm_basis(&self, mut ir: QuantumIR) -> DeviceResult<QuantumIR> {
        let mut decomposed = Vec::new();

        for instr in ir.instructions {
            match instr {
                IRInstruction::SingleQubitGate {
                    gate_type: SingleQubitGateType::Hadamard,
                    target,
                    ..
                } => {
                    // H = U3(π/2, 0, π)
                    decomposed.push(IRInstruction::SingleQubitGate {
                        gate_type: SingleQubitGateType::U3,
                        target,
                        parameters: vec![std::f64::consts::PI / 2.0, 0.0, std::f64::consts::PI],
                    });
                }
                IRInstruction::TwoQubitGate {
                    gate_type: TwoQubitGateType::CNOT,
                    control,
                    target,
                    ..
                } => {
                    // CNOT is native, keep as is
                    decomposed.push(IRInstruction::TwoQubitGate {
                        gate_type: TwoQubitGateType::CNOT,
                        control,
                        target,
                        parameters: vec![],
                    });
                }
                other => decomposed.push(other),
            }
        }

        ir.instructions = decomposed;
        Ok(ir)
    }

    /// Remove identity gates and no-ops
    fn remove_identity_gates(&self, mut ir: QuantumIR) -> DeviceResult<QuantumIR> {
        ir.instructions.retain(|instr| {
            !matches!(
                instr,
                IRInstruction::SingleQubitGate {
                    gate_type: SingleQubitGateType::Rz,
                    parameters,
                    ..
                } if parameters.len() == 1 && parameters[0].abs() < 1e-10
            )
        });

        ir.metadata
            .optimization_passes
            .push("identity_removal".to_string());
        Ok(ir)
    }

    /// Generate backend-specific code
    fn generate_backend_code(&self, ir: &QuantumIR) -> DeviceResult<String> {
        match self.config.target {
            TargetBackend::IBMQuantum | TargetBackend::GenericQASM => self.generate_qasm_code(ir),
            TargetBackend::AWSBraket => self.generate_braket_code(ir),
            TargetBackend::AzureQuantum => self.generate_azure_code(ir),
            TargetBackend::Custom => {
                Ok("// Custom backend code generation not implemented\n".to_string())
            }
        }
    }

    /// Generate OpenQASM 2.0 code
    fn generate_qasm_code(&self, ir: &QuantumIR) -> DeviceResult<String> {
        let mut code = String::new();
        code.push_str("OPENQASM 2.0;\n");
        code.push_str("include \"qelib1.inc\";\n");
        code.push_str(&format!("qreg q[{}];\n", ir.num_qubits));
        code.push_str(&format!("creg c[{}];\n", ir.num_qubits));

        for instr in &ir.instructions {
            match instr {
                IRInstruction::SingleQubitGate {
                    gate_type,
                    target,
                    parameters,
                } => {
                    let gate_str = match gate_type {
                        SingleQubitGateType::Hadamard => format!("h q[{}];", target),
                        SingleQubitGateType::PauliX => format!("x q[{}];", target),
                        SingleQubitGateType::PauliY => format!("y q[{}];", target),
                        SingleQubitGateType::PauliZ => format!("z q[{}];", target),
                        SingleQubitGateType::T => format!("t q[{}];", target),
                        SingleQubitGateType::TDagger => format!("tdg q[{}];", target),
                        SingleQubitGateType::S => format!("s q[{}];", target),
                        SingleQubitGateType::SDagger => format!("sdg q[{}];", target),
                        SingleQubitGateType::Rx if parameters.len() == 1 => {
                            format!("rx({}) q[{}];", parameters[0], target)
                        }
                        SingleQubitGateType::Ry if parameters.len() == 1 => {
                            format!("ry({}) q[{}];", parameters[0], target)
                        }
                        SingleQubitGateType::Rz if parameters.len() == 1 => {
                            format!("rz({}) q[{}];", parameters[0], target)
                        }
                        SingleQubitGateType::U3 if parameters.len() == 3 => {
                            format!(
                                "u3({},{},{}) q[{}];",
                                parameters[0], parameters[1], parameters[2], target
                            )
                        }
                        _ => format!("// Unknown gate on q[{}]", target),
                    };
                    code.push_str(&gate_str);
                    code.push('\n');
                }
                IRInstruction::TwoQubitGate {
                    gate_type,
                    control,
                    target,
                    ..
                } => {
                    let gate_str = match gate_type {
                        TwoQubitGateType::CNOT => format!("cx q[{}],q[{}];", control, target),
                        TwoQubitGateType::CZ => format!("cz q[{}],q[{}];", control, target),
                        TwoQubitGateType::SWAP => format!("swap q[{}],q[{}];", control, target),
                        _ => "// Unknown two-qubit gate".to_string(),
                    };
                    code.push_str(&gate_str);
                    code.push('\n');
                }
                IRInstruction::Measure {
                    qubit,
                    classical_bit,
                } => {
                    code.push_str(&format!("measure q[{}] -> c[{}];\n", qubit, classical_bit));
                }
                IRInstruction::Barrier { qubits } => {
                    if qubits.is_empty() {
                        code.push_str("barrier q;\n");
                    } else {
                        let qubit_list = qubits
                            .iter()
                            .map(|q| format!("q[{}]", q))
                            .collect::<Vec<_>>()
                            .join(",");
                        code.push_str(&format!("barrier {};\n", qubit_list));
                    }
                }
                IRInstruction::Custom {
                    name,
                    qubits,
                    parameters,
                } => {
                    let qubit_list = qubits
                        .iter()
                        .map(|q| format!("q[{}]", q))
                        .collect::<Vec<_>>()
                        .join(",");
                    if parameters.is_empty() {
                        code.push_str(&format!("{} {};\n", name, qubit_list));
                    } else {
                        let param_list = parameters
                            .iter()
                            .map(|p| p.to_string())
                            .collect::<Vec<_>>()
                            .join(",");
                        code.push_str(&format!("{}({}) {};\n", name, param_list, qubit_list));
                    }
                }
            }
        }

        Ok(code)
    }

    /// Generate AWS Braket code (OpenQASM-like)
    fn generate_braket_code(&self, ir: &QuantumIR) -> DeviceResult<String> {
        // AWS Braket uses OpenQASM with some extensions
        self.generate_qasm_code(ir)
    }

    /// Generate Azure Quantum code
    fn generate_azure_code(&self, ir: &QuantumIR) -> DeviceResult<String> {
        // Azure Quantum supports OpenQASM
        self.generate_qasm_code(ir)
    }

    /// Create IR from gate sequence
    pub fn create_ir_from_gates(&self, num_qubits: usize, gates: Vec<IRInstruction>) -> QuantumIR {
        QuantumIR {
            num_qubits,
            instructions: gates,
            metadata: IRMetadata::default(),
        }
    }
}

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

    #[test]
    fn test_compiler_creation() {
        let config = CrossCompilerConfig::default();
        let compiler = CrossCompiler::new(config);
        assert_eq!(compiler.config.target, TargetBackend::GenericQASM);
    }

    #[test]
    fn test_ir_creation() {
        let compiler = CrossCompiler::default();
        let gates = vec![
            IRInstruction::SingleQubitGate {
                gate_type: SingleQubitGateType::Hadamard,
                target: 0,
                parameters: vec![],
            },
            IRInstruction::TwoQubitGate {
                gate_type: TwoQubitGateType::CNOT,
                control: 0,
                target: 1,
                parameters: vec![],
            },
        ];

        let ir = compiler.create_ir_from_gates(2, gates);
        assert_eq!(ir.num_qubits, 2);
        assert_eq!(ir.instructions.len(), 2);
    }

    #[test]
    fn test_circuit_depth_calculation() {
        let compiler = CrossCompiler::default();
        let gates = vec![
            IRInstruction::SingleQubitGate {
                gate_type: SingleQubitGateType::Hadamard,
                target: 0,
                parameters: vec![],
            },
            IRInstruction::SingleQubitGate {
                gate_type: SingleQubitGateType::Hadamard,
                target: 1,
                parameters: vec![],
            },
            IRInstruction::TwoQubitGate {
                gate_type: TwoQubitGateType::CNOT,
                control: 0,
                target: 1,
                parameters: vec![],
            },
        ];

        let ir = compiler.create_ir_from_gates(2, gates);
        let depth = compiler.compute_circuit_depth(&ir);
        assert_eq!(depth, 2); // H on q0, H on q1 (parallel), then CNOT
    }

    #[test]
    fn test_qasm_code_generation() {
        let config = CrossCompilerConfig {
            target: TargetBackend::GenericQASM,
            optimization_level: OptimizationLevel::O0,
            ..Default::default()
        };
        let compiler = CrossCompiler::new(config);

        let gates = vec![
            IRInstruction::SingleQubitGate {
                gate_type: SingleQubitGateType::Hadamard,
                target: 0,
                parameters: vec![],
            },
            IRInstruction::TwoQubitGate {
                gate_type: TwoQubitGateType::CNOT,
                control: 0,
                target: 1,
                parameters: vec![],
            },
            IRInstruction::Measure {
                qubit: 0,
                classical_bit: 0,
            },
        ];

        let ir = compiler.create_ir_from_gates(2, gates);
        let result = compiler.compile(ir);

        assert!(result.is_ok());
        let result = result.expect("Compilation failed");
        assert!(result.code.contains("OPENQASM 2.0"));
        assert!(result.code.contains("h q[0]"));
        assert!(result.code.contains("cx q[0],q[1]"));
        assert!(result.code.contains("measure q[0] -> c[0]"));
    }

    #[test]
    fn test_gate_fusion_optimization() {
        let config = CrossCompilerConfig {
            optimization_level: OptimizationLevel::O2,
            enable_gate_fusion: true,
            ..Default::default()
        };
        let compiler = CrossCompiler::new(config);

        let gates = vec![
            IRInstruction::SingleQubitGate {
                gate_type: SingleQubitGateType::Rz,
                target: 0,
                parameters: vec![0.5],
            },
            IRInstruction::SingleQubitGate {
                gate_type: SingleQubitGateType::Rz,
                target: 0,
                parameters: vec![0.3],
            },
        ];

        let ir = compiler.create_ir_from_gates(1, gates);
        let result = compiler.compile(ir);

        assert!(result.is_ok());
        let result = result.expect("Compilation failed");

        // Should have fused the two Rz gates into one
        assert_eq!(result.statistics.optimized_instruction_count, 1);
    }

    #[test]
    fn test_identity_gate_removal() {
        let config = CrossCompilerConfig {
            optimization_level: OptimizationLevel::O2,
            ..Default::default()
        };
        let compiler = CrossCompiler::new(config);

        let gates = vec![
            IRInstruction::SingleQubitGate {
                gate_type: SingleQubitGateType::Hadamard,
                target: 0,
                parameters: vec![],
            },
            IRInstruction::SingleQubitGate {
                gate_type: SingleQubitGateType::Rz,
                target: 0,
                parameters: vec![0.0], // Identity (no rotation)
            },
            IRInstruction::Measure {
                qubit: 0,
                classical_bit: 0,
            },
        ];

        let ir = compiler.create_ir_from_gates(1, gates);
        let result = compiler.compile(ir);

        assert!(result.is_ok());
        let result = result.expect("Compilation failed");

        // Should have removed the identity Rz gate
        assert_eq!(result.statistics.optimized_instruction_count, 2);
    }

    #[test]
    fn test_compilation_statistics() {
        let config = CrossCompilerConfig {
            optimization_level: OptimizationLevel::O2,
            ..Default::default()
        };
        let compiler = CrossCompiler::new(config);

        let gates = vec![
            IRInstruction::SingleQubitGate {
                gate_type: SingleQubitGateType::Hadamard,
                target: 0,
                parameters: vec![],
            },
            IRInstruction::SingleQubitGate {
                gate_type: SingleQubitGateType::Rz,
                target: 0,
                parameters: vec![0.0],
            },
        ];

        let ir = compiler.create_ir_from_gates(1, gates);
        let result = compiler.compile(ir);

        assert!(result.is_ok());
        let result = result.expect("Compilation failed");

        assert_eq!(result.statistics.original_instruction_count, 2);
        assert!(result.statistics.num_optimization_passes > 0);
        // Compilation time is u64, always non-negative
    }

    #[test]
    fn test_different_backends() {
        let backends = vec![
            TargetBackend::IBMQuantum,
            TargetBackend::AWSBraket,
            TargetBackend::AzureQuantum,
        ];

        for backend in backends {
            let config = CrossCompilerConfig {
                target: backend,
                optimization_level: OptimizationLevel::O0,
                ..Default::default()
            };
            let compiler = CrossCompiler::new(config);

            let gates = vec![IRInstruction::SingleQubitGate {
                gate_type: SingleQubitGateType::Hadamard,
                target: 0,
                parameters: vec![],
            }];

            let ir = compiler.create_ir_from_gates(1, gates);
            let result = compiler.compile(ir);

            assert!(result.is_ok());
        }
    }

    #[test]
    fn test_barrier_instruction() {
        let compiler = CrossCompiler::default();

        let gates = vec![
            IRInstruction::SingleQubitGate {
                gate_type: SingleQubitGateType::Hadamard,
                target: 0,
                parameters: vec![],
            },
            IRInstruction::Barrier { qubits: vec![0, 1] },
            IRInstruction::TwoQubitGate {
                gate_type: TwoQubitGateType::CNOT,
                control: 0,
                target: 1,
                parameters: vec![],
            },
        ];

        let ir = compiler.create_ir_from_gates(2, gates);
        let result = compiler.compile(ir);

        assert!(result.is_ok());
        let result = result.expect("Compilation failed");
        assert!(result.code.contains("barrier"));
    }

    #[test]
    fn test_optimization_levels() {
        let levels = vec![
            OptimizationLevel::O0,
            OptimizationLevel::O1,
            OptimizationLevel::O2,
            OptimizationLevel::O3,
        ];

        for level in levels {
            let config = CrossCompilerConfig {
                optimization_level: level,
                ..Default::default()
            };
            let compiler = CrossCompiler::new(config);

            let gates = vec![IRInstruction::SingleQubitGate {
                gate_type: SingleQubitGateType::Hadamard,
                target: 0,
                parameters: vec![],
            }];

            let ir = compiler.create_ir_from_gates(1, gates);
            let result = compiler.compile(ir);

            assert!(result.is_ok());
        }
    }
}