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
// This code is part of Cqlib.
//
// (C) Copyright China Telecom Quantum Group 2026
//
// This code is licensed under the Apache License, Version 2.0. You may
// obtain a copy of this license in the LICENSE.txt file in the root directory
// of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
//
// Any modifications or derivative works of this code must retain this
// copyright notice, and modified files need to carry a notice indicating
// that they have been altered from the originals.
//! # OpenQASM 2.0 Abstract Syntax Tree (AST)
//!
//! This module defines the intermediate representation (IR) for parsing OpenQASM 2.0 quantum
//! programs. It provides data structures that represent the grammatical elements of the
//! OpenQASM 2.0 language specification.
//!
//! ## Overview
//!
//! The AST is structured as a tree of statements that correspond to the quantum circuit
//! description language defined in the OpenQASM 2.0 specification. Key elements include:
//!
//! - **Expressions**: Mathematical expressions for gate parameters (real numbers, integers,
//! identifiers, binary/ unary operations)
//! - **Arguments**: Quantum/classical register references (named or indexed)
//! - **Statements**: Quantum operations, gate declarations, classical operations
//! - **Program**: Complete OpenQASM program containing version and statements
//!
//! ## Example
//!
//! ```rust
//! use cqlib_core::ir::qasm2::ast::{Statement, Argument, Expression};
//!
//! // Represents: qreg q[2];
//! let qreg_stmt = Statement::QReg("q".to_string(), 2);
//!
//! // Represents: h q[0];
//! let h_gate = Statement::CustomGate(
//! "h".to_string(),
//! vec![],
//! vec![Argument::IndexedId("q".to_string(), 0)]
//! );
//! ```
/// Represents literal values and expressions in OpenQASM 2.0.
///
/// This enum covers all expression types defined in the OpenQASM 2.0 specification:
/// - Real and integer constants
/// - Identifier references (variables/parameters)
/// - Built-in constant `pi`
/// - Binary operations (arithmetic)
/// - Unary operations (trigonometric, exponential, etc.)
/// Binary arithmetic operators supported in OpenQASM 2.0 expressions.
///
/// These operators follow standard precedence rules and are used within
/// gate parameter expressions.
/// Unary operators (functions) supported in OpenQASM 2.0 expressions.
///
/// These include trigonometric functions, exponential/logarithmic functions,
/// and the negation operator.
/// Represents a quantum or classical register argument.
///
/// Arguments can be either:
/// - Named references (entire register): `q` refers to all qubits in register `q`
/// - Indexed references (single element): `q[0]` refers to the first qubit in register `q`
/// Top-level statements in an OpenQASM 2.0 program.
///
/// Each variant represents a distinct type of statement as defined in the
/// OpenQASM 2.0 specification, including declarations, quantum operations,
/// gate definitions, and control flow.
/// Data structure holding gate definition content.
///
/// This represents the complete gate declaration including:
/// - Gate name
/// - Formal parameters (may be empty)
/// - Formal qubits (the qubits the gate operates on)
/// - Gate body (sequence of statements)
/// Represents a complete OpenQASM 2.0 program.
///
/// This is the root structure containing the version number and
/// all top-level statements in the program.