quantrs2-device 0.1.3

Quantum device connectors for the QuantRS2 framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
//! OpenQASM 3.0 support for QuantRS2.
//!
//! This module provides QASM 3.0 circuit representation and conversion,
//! compatible with IBM Quantum and other modern quantum systems.
//!
//! ## Features
//!
//! - Full OpenQASM 3.0 syntax support
//! - Classical control flow (if/else, while, for)
//! - Dynamic circuits with mid-circuit measurements
//! - Gate modifiers (ctrl, inv, pow)
//! - Type system (qubit, bit, int, float, angle)
//! - Subroutine definitions
//!
//! ## Example
//!
//! ```rust,ignore
//! use quantrs2_device::qasm3::{Qasm3Builder, Qasm3Circuit};
//!
//! let mut builder = Qasm3Builder::new(5);
//! builder.gate("h", &[0])?;
//! builder.gate("cx", &[0, 1])?;
//! builder.measure(0, "c", 0)?;
//! builder.if_statement("c[0] == 1", |b| {
//!     b.gate("x", &[1])
//! })?;
//!
//! let qasm3 = builder.build()?;
//! println!("{}", qasm3.to_string());
//! ```

use std::collections::HashMap;
use std::fmt;

use crate::{DeviceError, DeviceResult};

/// OpenQASM 3.0 version
pub const QASM3_VERSION: &str = "3.0";

/// QASM 3.0 data types
#[derive(Debug, Clone, PartialEq)]
pub enum Qasm3Type {
    /// Quantum bit
    Qubit,
    /// Array of quantum bits
    QubitArray(usize),
    /// Classical bit
    Bit,
    /// Array of classical bits
    BitArray(usize),
    /// Integer type
    Int(Option<usize>),
    /// Unsigned integer
    Uint(Option<usize>),
    /// Floating point
    Float(Option<usize>),
    /// Angle type
    Angle(Option<usize>),
    /// Boolean
    Bool,
    /// Duration type
    Duration,
    /// Stretch (timing constraint)
    Stretch,
    /// Complex number
    Complex(Option<usize>),
}

impl fmt::Display for Qasm3Type {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Qubit => write!(f, "qubit"),
            Self::QubitArray(n) => write!(f, "qubit[{}]", n),
            Self::Bit => write!(f, "bit"),
            Self::BitArray(n) => write!(f, "bit[{}]", n),
            Self::Int(Some(n)) => write!(f, "int[{}]", n),
            Self::Int(None) => write!(f, "int"),
            Self::Uint(Some(n)) => write!(f, "uint[{}]", n),
            Self::Uint(None) => write!(f, "uint"),
            Self::Float(Some(n)) => write!(f, "float[{}]", n),
            Self::Float(None) => write!(f, "float"),
            Self::Angle(Some(n)) => write!(f, "angle[{}]", n),
            Self::Angle(None) => write!(f, "angle"),
            Self::Bool => write!(f, "bool"),
            Self::Duration => write!(f, "duration"),
            Self::Stretch => write!(f, "stretch"),
            Self::Complex(Some(n)) => write!(f, "complex[float[{}]]", n),
            Self::Complex(None) => write!(f, "complex[float]"),
        }
    }
}

/// QASM 3.0 gate modifier
#[derive(Debug, Clone)]
pub enum GateModifier {
    /// Control modifier: ctrl @ gate
    Ctrl(usize),
    /// Negated control: negctrl @ gate
    NegCtrl(usize),
    /// Inverse modifier: inv @ gate
    Inv,
    /// Power modifier: pow(n) @ gate
    Pow(f64),
}

impl fmt::Display for GateModifier {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Ctrl(n) if *n == 1 => write!(f, "ctrl @"),
            Self::Ctrl(n) => write!(f, "ctrl({}) @", n),
            Self::NegCtrl(n) if *n == 1 => write!(f, "negctrl @"),
            Self::NegCtrl(n) => write!(f, "negctrl({}) @", n),
            Self::Inv => write!(f, "inv @"),
            Self::Pow(n) => write!(f, "pow({}) @", n),
        }
    }
}

/// QASM 3.0 statement
#[derive(Debug, Clone)]
pub enum Qasm3Statement {
    /// Include directive
    Include(String),
    /// Variable declaration
    Declaration {
        var_type: Qasm3Type,
        name: String,
        init_value: Option<String>,
    },
    /// Gate definition
    GateDef {
        name: String,
        params: Vec<String>,
        qubits: Vec<String>,
        body: Vec<Qasm3Statement>,
    },
    /// Gate application
    Gate {
        name: String,
        modifiers: Vec<GateModifier>,
        params: Vec<String>,
        qubits: Vec<String>,
    },
    /// Measurement
    Measure { qubit: String, classical: String },
    /// Reset
    Reset(String),
    /// Barrier
    Barrier(Vec<String>),
    /// If statement
    If {
        condition: String,
        then_body: Vec<Qasm3Statement>,
        else_body: Option<Vec<Qasm3Statement>>,
    },
    /// Switch statement (QASM 3.0 dynamic circuits)
    Switch {
        expression: String,
        cases: Vec<(Vec<i64>, Vec<Qasm3Statement>)>,
        default_case: Option<Vec<Qasm3Statement>>,
    },
    /// While loop
    While {
        condition: String,
        body: Vec<Qasm3Statement>,
    },
    /// For loop
    For {
        var_name: String,
        range: String,
        body: Vec<Qasm3Statement>,
    },
    /// Classical assignment
    Assignment { target: String, value: String },
    /// Delay
    Delay {
        duration: String,
        qubits: Vec<String>,
    },
    /// Box (timing block)
    Box { body: Vec<Qasm3Statement> },
    /// Subroutine definition
    DefCal {
        name: String,
        params: Vec<String>,
        qubits: Vec<String>,
        body: String,
    },
    /// Comment
    Comment(String),
    /// Pragma
    Pragma(String),
}

impl fmt::Display for Qasm3Statement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Include(file) => writeln!(f, "include \"{}\";", file),
            Self::Declaration {
                var_type,
                name,
                init_value,
            } => {
                if let Some(val) = init_value {
                    writeln!(f, "{} {} = {};", var_type, name, val)
                } else {
                    writeln!(f, "{} {};", var_type, name)
                }
            }
            Self::GateDef {
                name,
                params,
                qubits,
                body,
            } => {
                let params_str = if params.is_empty() {
                    String::new()
                } else {
                    format!("({})", params.join(", "))
                };
                writeln!(f, "gate {}{} {} {{", name, params_str, qubits.join(", "))?;
                for stmt in body {
                    write!(f, "  {}", stmt)?;
                }
                writeln!(f, "}}")
            }
            Self::Gate {
                name,
                modifiers,
                params,
                qubits,
            } => {
                let mod_str = if modifiers.is_empty() {
                    String::new()
                } else {
                    modifiers
                        .iter()
                        .map(|m| m.to_string())
                        .collect::<Vec<_>>()
                        .join(" ")
                        + " "
                };
                let params_str = if params.is_empty() {
                    String::new()
                } else {
                    format!("({})", params.join(", "))
                };
                writeln!(
                    f,
                    "{}{}{} {};",
                    mod_str,
                    name,
                    params_str,
                    qubits.join(", ")
                )
            }
            Self::Measure { qubit, classical } => {
                writeln!(f, "{} = measure {};", classical, qubit)
            }
            Self::Reset(qubit) => writeln!(f, "reset {};", qubit),
            Self::Barrier(qubits) => writeln!(f, "barrier {};", qubits.join(", ")),
            Self::If {
                condition,
                then_body,
                else_body,
            } => {
                writeln!(f, "if ({}) {{", condition)?;
                for stmt in then_body {
                    write!(f, "  {}", stmt)?;
                }
                if let Some(else_stmts) = else_body {
                    writeln!(f, "}} else {{")?;
                    for stmt in else_stmts {
                        write!(f, "  {}", stmt)?;
                    }
                }
                writeln!(f, "}}")
            }
            Self::Switch {
                expression,
                cases,
                default_case,
            } => {
                writeln!(f, "switch ({}) {{", expression)?;
                for (values, body) in cases {
                    let values_str = values
                        .iter()
                        .map(|v| v.to_string())
                        .collect::<Vec<_>>()
                        .join(", ");
                    writeln!(f, "  case {}: {{", values_str)?;
                    for stmt in body {
                        write!(f, "    {}", stmt)?;
                    }
                    writeln!(f, "  }}")?;
                }
                if let Some(default_body) = default_case {
                    writeln!(f, "  default: {{")?;
                    for stmt in default_body {
                        write!(f, "    {}", stmt)?;
                    }
                    writeln!(f, "  }}")?;
                }
                writeln!(f, "}}")
            }
            Self::While { condition, body } => {
                writeln!(f, "while ({}) {{", condition)?;
                for stmt in body {
                    write!(f, "  {}", stmt)?;
                }
                writeln!(f, "}}")
            }
            Self::For {
                var_name,
                range,
                body,
            } => {
                writeln!(f, "for {} in {} {{", var_name, range)?;
                for stmt in body {
                    write!(f, "  {}", stmt)?;
                }
                writeln!(f, "}}")
            }
            Self::Assignment { target, value } => {
                writeln!(f, "{} = {};", target, value)
            }
            Self::Delay { duration, qubits } => {
                writeln!(f, "delay[{}] {};", duration, qubits.join(", "))
            }
            Self::Box { body } => {
                writeln!(f, "box {{")?;
                for stmt in body {
                    write!(f, "  {}", stmt)?;
                }
                writeln!(f, "}}")
            }
            Self::DefCal {
                name,
                params,
                qubits,
                body,
            } => {
                let params_str = if params.is_empty() {
                    String::new()
                } else {
                    format!("({})", params.join(", "))
                };
                writeln!(f, "defcal {}{} {} {{", name, params_str, qubits.join(", "))?;
                writeln!(f, "  {}", body)?;
                writeln!(f, "}}")
            }
            Self::Comment(text) => writeln!(f, "// {}", text),
            Self::Pragma(text) => writeln!(f, "#pragma {}", text),
        }
    }
}

/// QASM 3.0 circuit representation
#[derive(Debug, Clone)]
pub struct Qasm3Circuit {
    /// Version (always "3.0")
    pub version: String,
    /// Statements in the circuit
    pub statements: Vec<Qasm3Statement>,
    /// Input parameters
    pub inputs: HashMap<String, Qasm3Type>,
    /// Output parameters
    pub outputs: HashMap<String, Qasm3Type>,
}

impl Qasm3Circuit {
    /// Create a new empty QASM 3.0 circuit
    pub fn new() -> Self {
        Self {
            version: QASM3_VERSION.to_string(),
            statements: Vec::new(),
            inputs: HashMap::new(),
            outputs: HashMap::new(),
        }
    }

    /// Add an input parameter
    pub fn add_input(&mut self, name: &str, var_type: Qasm3Type) {
        self.inputs.insert(name.to_string(), var_type);
    }

    /// Add an output parameter
    pub fn add_output(&mut self, name: &str, var_type: Qasm3Type) {
        self.outputs.insert(name.to_string(), var_type);
    }

    /// Add a statement
    pub fn add_statement(&mut self, stmt: Qasm3Statement) {
        self.statements.push(stmt);
    }

    /// Get the number of qubits used
    pub fn num_qubits(&self) -> usize {
        let mut max_qubit = 0;
        for stmt in &self.statements {
            if let Qasm3Statement::Declaration {
                var_type: Qasm3Type::QubitArray(n),
                ..
            } = stmt
            {
                max_qubit = max_qubit.max(*n);
            }
        }
        max_qubit
    }

    /// Convert to QASM 3.0 string
    pub fn to_qasm3(&self) -> String {
        self.to_string()
    }

    /// Convert to QASM 2.0 string (for backward compatibility)
    pub fn to_qasm2(&self) -> DeviceResult<String> {
        let mut qasm2 = String::from("OPENQASM 2.0;\ninclude \"qelib1.inc\";\n\n");

        // Find qubit and bit declarations
        let mut num_qubits = 0;
        let mut num_bits = 0;

        for stmt in &self.statements {
            match stmt {
                Qasm3Statement::Declaration {
                    var_type: Qasm3Type::QubitArray(n),
                    ..
                } => {
                    num_qubits = num_qubits.max(*n);
                }
                Qasm3Statement::Declaration {
                    var_type: Qasm3Type::BitArray(n),
                    ..
                } => {
                    num_bits = num_bits.max(*n);
                }
                _ => {}
            }
        }

        if num_qubits > 0 {
            qasm2.push_str(&format!("qreg q[{}];\n", num_qubits));
        }
        if num_bits > 0 {
            qasm2.push_str(&format!("creg c[{}];\n", num_bits));
        }
        qasm2.push('\n');

        // Convert statements
        for stmt in &self.statements {
            match stmt {
                Qasm3Statement::Gate {
                    name,
                    modifiers,
                    params,
                    qubits,
                } => {
                    if !modifiers.is_empty() {
                        return Err(DeviceError::QasmError(
                            "Gate modifiers not supported in QASM 2.0".to_string(),
                        ));
                    }
                    let params_str = if params.is_empty() {
                        String::new()
                    } else {
                        format!("({})", params.join(", "))
                    };
                    qasm2.push_str(&format!("{}{} {};\n", name, params_str, qubits.join(", ")));
                }
                Qasm3Statement::Measure { qubit, classical } => {
                    qasm2.push_str(&format!("measure {} -> {};\n", qubit, classical));
                }
                Qasm3Statement::Reset(qubit) => {
                    qasm2.push_str(&format!("reset {};\n", qubit));
                }
                Qasm3Statement::Barrier(qubits) => {
                    qasm2.push_str(&format!("barrier {};\n", qubits.join(", ")));
                }
                Qasm3Statement::If {
                    condition,
                    then_body,
                    else_body,
                } => {
                    if else_body.is_some() {
                        return Err(DeviceError::QasmError(
                            "If-else not supported in QASM 2.0".to_string(),
                        ));
                    }
                    // QASM 2.0 only supports simple if statements
                    qasm2.push_str(&format!("if ({}) ", condition));
                    if let Some(stmt) = then_body.first() {
                        if let Qasm3Statement::Gate {
                            name,
                            params,
                            qubits,
                            ..
                        } = stmt
                        {
                            let params_str = if params.is_empty() {
                                String::new()
                            } else {
                                format!("({})", params.join(", "))
                            };
                            qasm2.push_str(&format!(
                                "{}{} {};\n",
                                name,
                                params_str,
                                qubits.join(", ")
                            ));
                        }
                    }
                }
                Qasm3Statement::While { .. } | Qasm3Statement::For { .. } => {
                    return Err(DeviceError::QasmError(
                        "Loops not supported in QASM 2.0".to_string(),
                    ));
                }
                Qasm3Statement::Declaration { .. }
                | Qasm3Statement::Include(_)
                | Qasm3Statement::Comment(_) => {
                    // Skip declarations (handled above) and comments
                }
                _ => {}
            }
        }

        Ok(qasm2)
    }
}

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

impl fmt::Display for Qasm3Circuit {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "OPENQASM {};", self.version)?;
        writeln!(f)?;

        // Include standard library
        writeln!(f, "include \"stdgates.inc\";")?;
        writeln!(f)?;

        // Input declarations
        for (name, var_type) in &self.inputs {
            writeln!(f, "input {} {};", var_type, name)?;
        }
        if !self.inputs.is_empty() {
            writeln!(f)?;
        }

        // Output declarations
        for (name, var_type) in &self.outputs {
            writeln!(f, "output {} {};", var_type, name)?;
        }
        if !self.outputs.is_empty() {
            writeln!(f)?;
        }

        // Statements
        for stmt in &self.statements {
            write!(f, "{}", stmt)?;
        }

        Ok(())
    }
}

/// Builder for QASM 3.0 circuits
pub struct Qasm3Builder {
    /// The circuit being built
    pub circuit: Qasm3Circuit,
    /// Number of qubits in the circuit
    pub num_qubits: usize,
    /// Number of classical bits in the circuit
    pub num_bits: usize,
}

impl Qasm3Builder {
    /// Create a new builder with specified number of qubits
    pub fn new(num_qubits: usize) -> Self {
        let mut builder = Self {
            circuit: Qasm3Circuit::new(),
            num_qubits,
            num_bits: num_qubits, // Default same number of classical bits
        };

        // Add qubit and bit declarations
        builder.circuit.add_statement(Qasm3Statement::Declaration {
            var_type: Qasm3Type::QubitArray(num_qubits),
            name: "q".to_string(),
            init_value: None,
        });
        builder.circuit.add_statement(Qasm3Statement::Declaration {
            var_type: Qasm3Type::BitArray(num_qubits),
            name: "c".to_string(),
            init_value: None,
        });

        builder
    }

    /// Set number of classical bits
    pub fn with_bits(mut self, num_bits: usize) -> Self {
        self.num_bits = num_bits;
        // Update the bit declaration
        for stmt in &mut self.circuit.statements {
            if let Qasm3Statement::Declaration {
                var_type: Qasm3Type::BitArray(n),
                name,
                ..
            } = stmt
            {
                if name == "c" {
                    *n = num_bits;
                }
            }
        }
        self
    }

    /// Add a gate operation
    pub fn gate(&mut self, name: &str, qubits: &[usize]) -> DeviceResult<&mut Self> {
        self.gate_with_params(name, &[], qubits)
    }

    /// Add a gate with parameters
    pub fn gate_with_params(
        &mut self,
        name: &str,
        params: &[f64],
        qubits: &[usize],
    ) -> DeviceResult<&mut Self> {
        for &q in qubits {
            if q >= self.num_qubits {
                return Err(DeviceError::InvalidInput(format!(
                    "Qubit {} out of range (max {})",
                    q,
                    self.num_qubits - 1
                )));
            }
        }

        let qubit_strs: Vec<String> = qubits.iter().map(|q| format!("q[{}]", q)).collect();
        let param_strs: Vec<String> = params.iter().map(|p| format!("{}", p)).collect();

        self.circuit.add_statement(Qasm3Statement::Gate {
            name: name.to_string(),
            modifiers: Vec::new(),
            params: param_strs,
            qubits: qubit_strs,
        });

        Ok(self)
    }

    /// Add a controlled gate
    pub fn ctrl_gate(
        &mut self,
        name: &str,
        control: usize,
        target: usize,
        params: &[f64],
    ) -> DeviceResult<&mut Self> {
        if control >= self.num_qubits || target >= self.num_qubits {
            return Err(DeviceError::InvalidInput("Qubit out of range".to_string()));
        }

        let param_strs: Vec<String> = params.iter().map(|p| format!("{}", p)).collect();

        self.circuit.add_statement(Qasm3Statement::Gate {
            name: name.to_string(),
            modifiers: vec![GateModifier::Ctrl(1)],
            params: param_strs,
            qubits: vec![format!("q[{}]", control), format!("q[{}]", target)],
        });

        Ok(self)
    }

    /// Add an inverse gate
    pub fn inv_gate(&mut self, name: &str, qubits: &[usize]) -> DeviceResult<&mut Self> {
        for &q in qubits {
            if q >= self.num_qubits {
                return Err(DeviceError::InvalidInput("Qubit out of range".to_string()));
            }
        }

        let qubit_strs: Vec<String> = qubits.iter().map(|q| format!("q[{}]", q)).collect();

        self.circuit.add_statement(Qasm3Statement::Gate {
            name: name.to_string(),
            modifiers: vec![GateModifier::Inv],
            params: Vec::new(),
            qubits: qubit_strs,
        });

        Ok(self)
    }

    /// Add measurement
    pub fn measure(&mut self, qubit: usize, bit: usize) -> DeviceResult<&mut Self> {
        if qubit >= self.num_qubits {
            return Err(DeviceError::InvalidInput("Qubit out of range".to_string()));
        }
        if bit >= self.num_bits {
            return Err(DeviceError::InvalidInput("Bit out of range".to_string()));
        }

        self.circuit.add_statement(Qasm3Statement::Measure {
            qubit: format!("q[{}]", qubit),
            classical: format!("c[{}]", bit),
        });

        Ok(self)
    }

    /// Measure all qubits
    pub fn measure_all(&mut self) -> DeviceResult<&mut Self> {
        for i in 0..self.num_qubits.min(self.num_bits) {
            self.measure(i, i)?;
        }
        Ok(self)
    }

    /// Add reset operation
    pub fn reset(&mut self, qubit: usize) -> DeviceResult<&mut Self> {
        if qubit >= self.num_qubits {
            return Err(DeviceError::InvalidInput("Qubit out of range".to_string()));
        }

        self.circuit
            .add_statement(Qasm3Statement::Reset(format!("q[{}]", qubit)));

        Ok(self)
    }

    /// Add barrier
    pub fn barrier(&mut self, qubits: &[usize]) -> DeviceResult<&mut Self> {
        for &q in qubits {
            if q >= self.num_qubits {
                return Err(DeviceError::InvalidInput("Qubit out of range".to_string()));
            }
        }

        let qubit_strs: Vec<String> = qubits.iter().map(|q| format!("q[{}]", q)).collect();
        self.circuit
            .add_statement(Qasm3Statement::Barrier(qubit_strs));

        Ok(self)
    }

    /// Add if statement (dynamic circuit)
    pub fn if_statement<F>(&mut self, condition: &str, then_block: F) -> DeviceResult<&mut Self>
    where
        F: FnOnce(&mut Qasm3Builder) -> DeviceResult<()>,
    {
        let mut inner_builder = Qasm3Builder {
            circuit: Qasm3Circuit::new(),
            num_qubits: self.num_qubits,
            num_bits: self.num_bits,
        };
        inner_builder.circuit.statements.clear(); // Clear default declarations

        then_block(&mut inner_builder)?;

        self.circuit.add_statement(Qasm3Statement::If {
            condition: condition.to_string(),
            then_body: inner_builder.circuit.statements,
            else_body: None,
        });

        Ok(self)
    }

    /// Add if-else statement (dynamic circuit)
    pub fn if_else_statement<F, G>(
        &mut self,
        condition: &str,
        then_block: F,
        else_block: G,
    ) -> DeviceResult<&mut Self>
    where
        F: FnOnce(&mut Qasm3Builder) -> DeviceResult<()>,
        G: FnOnce(&mut Qasm3Builder) -> DeviceResult<()>,
    {
        let mut then_builder = Qasm3Builder {
            circuit: Qasm3Circuit::new(),
            num_qubits: self.num_qubits,
            num_bits: self.num_bits,
        };
        then_builder.circuit.statements.clear();
        then_block(&mut then_builder)?;

        let mut else_builder = Qasm3Builder {
            circuit: Qasm3Circuit::new(),
            num_qubits: self.num_qubits,
            num_bits: self.num_bits,
        };
        else_builder.circuit.statements.clear();
        else_block(&mut else_builder)?;

        self.circuit.add_statement(Qasm3Statement::If {
            condition: condition.to_string(),
            then_body: then_builder.circuit.statements,
            else_body: Some(else_builder.circuit.statements),
        });

        Ok(self)
    }

    /// Add while loop
    pub fn while_loop<F>(&mut self, condition: &str, body: F) -> DeviceResult<&mut Self>
    where
        F: FnOnce(&mut Qasm3Builder) -> DeviceResult<()>,
    {
        let mut inner_builder = Qasm3Builder {
            circuit: Qasm3Circuit::new(),
            num_qubits: self.num_qubits,
            num_bits: self.num_bits,
        };
        inner_builder.circuit.statements.clear();

        body(&mut inner_builder)?;

        self.circuit.add_statement(Qasm3Statement::While {
            condition: condition.to_string(),
            body: inner_builder.circuit.statements,
        });

        Ok(self)
    }

    /// Add for loop
    pub fn for_loop<F>(&mut self, var_name: &str, range: &str, body: F) -> DeviceResult<&mut Self>
    where
        F: FnOnce(&mut Qasm3Builder) -> DeviceResult<()>,
    {
        let mut inner_builder = Qasm3Builder {
            circuit: Qasm3Circuit::new(),
            num_qubits: self.num_qubits,
            num_bits: self.num_bits,
        };
        inner_builder.circuit.statements.clear();

        body(&mut inner_builder)?;

        self.circuit.add_statement(Qasm3Statement::For {
            var_name: var_name.to_string(),
            range: range.to_string(),
            body: inner_builder.circuit.statements,
        });

        Ok(self)
    }

    /// Add switch statement (QASM 3.0 dynamic circuit feature)
    ///
    /// # Arguments
    /// * `expression` - The expression to switch on (e.g., "c")
    /// * `case_builder` - A closure that builds the switch cases
    ///
    /// # Example
    /// ```ignore
    /// builder.switch_statement("c", |sw| {
    ///     sw.case(&[0], |b| b.gate("x", &[0]))?;
    ///     sw.case(&[1], |b| b.gate("y", &[0]))?;
    ///     sw.default(|b| b.gate("z", &[0]))?;
    ///     Ok(())
    /// })?;
    /// ```
    pub fn switch_statement<F>(
        &mut self,
        expression: &str,
        case_builder: F,
    ) -> DeviceResult<&mut Self>
    where
        F: FnOnce(&mut SwitchBuilder) -> DeviceResult<()>,
    {
        let mut switch_builder = SwitchBuilder::new(self.num_qubits, self.num_bits);
        case_builder(&mut switch_builder)?;

        self.circuit.add_statement(Qasm3Statement::Switch {
            expression: expression.to_string(),
            cases: switch_builder.cases,
            default_case: switch_builder.default_case,
        });

        Ok(self)
    }

    /// Add classical assignment
    ///
    /// # Arguments
    /// * `target` - Target variable (e.g., `"c[0]"`)
    /// * `value` - Value expression (e.g., `"c[0] + 1"`, `"c[0] & c[1]"`)
    pub fn assign(&mut self, target: &str, value: &str) -> &mut Self {
        self.circuit.add_statement(Qasm3Statement::Assignment {
            target: target.to_string(),
            value: value.to_string(),
        });
        self
    }

    /// Add comment
    pub fn comment(&mut self, text: &str) -> &mut Self {
        self.circuit
            .add_statement(Qasm3Statement::Comment(text.to_string()));
        self
    }

    /// Build the QASM 3.0 circuit
    pub fn build(self) -> DeviceResult<Qasm3Circuit> {
        Ok(self.circuit)
    }
}

/// Builder for switch statement cases
pub struct SwitchBuilder {
    num_qubits: usize,
    num_bits: usize,
    cases: Vec<(Vec<i64>, Vec<Qasm3Statement>)>,
    default_case: Option<Vec<Qasm3Statement>>,
}

impl SwitchBuilder {
    fn new(num_qubits: usize, num_bits: usize) -> Self {
        Self {
            num_qubits,
            num_bits,
            cases: Vec::new(),
            default_case: None,
        }
    }

    /// Add a case to the switch statement
    ///
    /// # Arguments
    /// * `values` - The case values (can be multiple for fallthrough)
    /// * `body` - A closure that builds the case body
    pub fn case<F>(&mut self, values: &[i64], body: F) -> DeviceResult<&mut Self>
    where
        F: FnOnce(&mut Qasm3Builder) -> DeviceResult<()>,
    {
        let mut inner_builder = Qasm3Builder {
            circuit: Qasm3Circuit::new(),
            num_qubits: self.num_qubits,
            num_bits: self.num_bits,
        };
        inner_builder.circuit.statements.clear();

        body(&mut inner_builder)?;

        self.cases
            .push((values.to_vec(), inner_builder.circuit.statements));
        Ok(self)
    }

    /// Add a default case to the switch statement
    pub fn default<F>(&mut self, body: F) -> DeviceResult<&mut Self>
    where
        F: FnOnce(&mut Qasm3Builder) -> DeviceResult<()>,
    {
        let mut inner_builder = Qasm3Builder {
            circuit: Qasm3Circuit::new(),
            num_qubits: self.num_qubits,
            num_bits: self.num_bits,
        };
        inner_builder.circuit.statements.clear();

        body(&mut inner_builder)?;

        self.default_case = Some(inner_builder.circuit.statements);
        Ok(self)
    }
}

/// Convert a QuantRS2 circuit to QASM 3.0
pub fn circuit_to_qasm3<const N: usize>(
    _circuit: &quantrs2_circuit::prelude::Circuit<N>,
) -> DeviceResult<Qasm3Circuit> {
    let mut builder = Qasm3Builder::new(N);

    // In a complete implementation, this would iterate through the circuit gates
    // and convert each to QASM 3.0 statements

    // For now, return a placeholder circuit
    builder.build()
}

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

    #[test]
    fn test_qasm3_type_display() {
        assert_eq!(format!("{}", Qasm3Type::Qubit), "qubit");
        assert_eq!(format!("{}", Qasm3Type::QubitArray(5)), "qubit[5]");
        assert_eq!(format!("{}", Qasm3Type::BitArray(3)), "bit[3]");
        assert_eq!(format!("{}", Qasm3Type::Int(Some(32))), "int[32]");
        assert_eq!(format!("{}", Qasm3Type::Float(None)), "float");
    }

    #[test]
    fn test_gate_modifier_display() {
        assert_eq!(format!("{}", GateModifier::Ctrl(1)), "ctrl @");
        assert_eq!(format!("{}", GateModifier::Ctrl(2)), "ctrl(2) @");
        assert_eq!(format!("{}", GateModifier::Inv), "inv @");
        assert_eq!(format!("{}", GateModifier::Pow(2.0)), "pow(2) @");
    }

    #[test]
    fn test_qasm3_builder_basic() {
        let mut builder = Qasm3Builder::new(3);
        builder.gate("h", &[0]).unwrap();
        builder.gate("cx", &[0, 1]).unwrap();
        builder.measure(0, 0).unwrap();

        let circuit = builder.build().unwrap();
        let qasm = circuit.to_string();

        assert!(qasm.contains("OPENQASM 3.0"));
        assert!(qasm.contains("qubit[3]"));
        assert!(qasm.contains("h q[0]"));
        assert!(qasm.contains("cx q[0], q[1]"));
    }

    #[test]
    fn test_qasm3_builder_params() {
        let mut builder = Qasm3Builder::new(2);
        builder
            .gate_with_params("rx", &[std::f64::consts::PI / 2.0], &[0])
            .unwrap();

        let circuit = builder.build().unwrap();
        let qasm = circuit.to_string();

        assert!(qasm.contains("rx"));
    }

    #[test]
    fn test_qasm3_if_statement() {
        let mut builder = Qasm3Builder::new(2);
        builder.gate("h", &[0]).unwrap();
        builder.measure(0, 0).unwrap();
        builder
            .if_statement("c[0] == 1", |b| {
                b.gate("x", &[1])?;
                Ok(())
            })
            .unwrap();

        let circuit = builder.build().unwrap();
        let qasm = circuit.to_string();

        assert!(qasm.contains("if (c[0] == 1)"));
        assert!(qasm.contains("x q[1]"));
    }

    #[test]
    fn test_qasm3_to_qasm2() {
        let mut builder = Qasm3Builder::new(2);
        builder.gate("h", &[0]).unwrap();
        builder.gate("cx", &[0, 1]).unwrap();
        builder.measure_all().unwrap();

        let circuit = builder.build().unwrap();
        let qasm2 = circuit.to_qasm2().unwrap();

        assert!(qasm2.contains("OPENQASM 2.0"));
        assert!(qasm2.contains("qreg q[2]"));
        assert!(qasm2.contains("creg c[2]"));
    }

    #[test]
    fn test_qasm3_ctrl_gate() {
        let mut builder = Qasm3Builder::new(3);
        builder.ctrl_gate("x", 0, 1, &[]).unwrap();

        let circuit = builder.build().unwrap();
        let qasm = circuit.to_string();

        assert!(qasm.contains("ctrl @"));
    }

    #[test]
    fn test_qasm3_inv_gate() {
        let mut builder = Qasm3Builder::new(2);
        builder.inv_gate("s", &[0]).unwrap();

        let circuit = builder.build().unwrap();
        let qasm = circuit.to_string();

        assert!(qasm.contains("inv @"));
    }

    #[test]
    fn test_qasm3_for_loop() {
        let mut builder = Qasm3Builder::new(4);
        builder
            .for_loop("i", "[0:3]", |b| {
                b.gate("h", &[0])?;
                Ok(())
            })
            .unwrap();

        let circuit = builder.build().unwrap();
        let qasm = circuit.to_string();

        assert!(qasm.contains("for i in [0:3]"));
    }
}