qasm-ts 2.1.3 → 2.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/errors.d.ts +88 -0
- package/dist/errors.js +162 -0
- package/dist/lexer.d.ts +78 -0
- package/dist/lexer.js +116 -0
- package/dist/main.d.ts +87 -0
- package/dist/main.js +122 -0
- package/dist/parser.d.ts +75 -0
- package/dist/parser.js +121 -0
- package/dist/qasm2/ast.d.ts +159 -0
- package/dist/qasm2/ast.js +186 -0
- package/dist/qasm2/lexer.d.ts +135 -0
- package/dist/qasm2/lexer.js +483 -0
- package/dist/qasm2/parser.d.ts +187 -0
- package/dist/qasm2/parser.js +655 -0
- package/dist/qasm2/token.d.ts +89 -0
- package/dist/qasm2/token.js +155 -0
- package/dist/qasm3/ast.d.ts +652 -0
- package/dist/qasm3/ast.js +746 -0
- package/dist/qasm3/lexer.d.ts +156 -0
- package/dist/qasm3/lexer.js +667 -0
- package/dist/qasm3/parser.d.ts +407 -0
- package/dist/qasm3/parser.js +2079 -0
- package/dist/qasm3/token.d.ts +197 -0
- package/dist/qasm3/token.js +420 -0
- package/dist/version.d.ts +41 -0
- package/dist/version.js +53 -0
- package/package.json +1 -1
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenQASM 3.0 Token Definitions and Utilities
|
|
3
|
+
*
|
|
4
|
+
* This module defines all the token types used in OpenQASM 3.0 syntax, which
|
|
5
|
+
* significantly extends OpenQASM 2.0 with modern programming language features.
|
|
6
|
+
*
|
|
7
|
+
* Major additions in OpenQASM 3.0:
|
|
8
|
+
* - **Classical types**: int, uint, float, bool, bit, complex
|
|
9
|
+
* - **Control flow**: if/else, for/while loops, switch/case
|
|
10
|
+
* - **Functions**: def, return, extern declarations
|
|
11
|
+
* - **Advanced features**: arrays, timing (delay, durationof), calibration
|
|
12
|
+
* - **Quantum extensions**: qubit declarations, gate modifiers, hardware qubits
|
|
13
|
+
*
|
|
14
|
+
* @module
|
|
15
|
+
*
|
|
16
|
+
* @example OpenQASM 3.0 advanced tokens
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import { lookup, Token } from './qasm3/token';
|
|
19
|
+
*
|
|
20
|
+
* console.log(lookup('qubit')); // Token.Qubit
|
|
21
|
+
* console.log(lookup('if')); // Token.If
|
|
22
|
+
* console.log(lookup('def')); // Token.Def
|
|
23
|
+
* console.log(lookup('complex')); // Token.Complex
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
/**
|
|
27
|
+
* Enumeration of all OpenQASM 3.0 token types.
|
|
28
|
+
*
|
|
29
|
+
* Each token represents a specific syntactic element in OpenQASM 3.0 code.
|
|
30
|
+
* The enum values correspond to different categories:
|
|
31
|
+
* - Literals: NNInteger, Real, String, BoolLiteral
|
|
32
|
+
* - Identifiers: Id
|
|
33
|
+
* - Keywords: Qubit, Gate, Measure, If, For, While, etc.
|
|
34
|
+
* - Operators: ArithmeticOp, BinaryOp, UnaryOp
|
|
35
|
+
* - Punctuation: Semicolon, Comma, LParen, RParen, etc.
|
|
36
|
+
* - Special: OpenQASM, Include, EndOfFile, Illegal
|
|
37
|
+
*/
|
|
38
|
+
declare enum Token {
|
|
39
|
+
Illegal = 0,
|
|
40
|
+
EndOfFile = 1,
|
|
41
|
+
Real = 2,
|
|
42
|
+
NNInteger = 3,
|
|
43
|
+
Integer = 4,
|
|
44
|
+
Id = 5,
|
|
45
|
+
OpenQASM = 6,
|
|
46
|
+
Include = 7,
|
|
47
|
+
Semicolon = 8,
|
|
48
|
+
Colon = 9,
|
|
49
|
+
Comma = 10,
|
|
50
|
+
LParen = 11,
|
|
51
|
+
LSParen = 12,
|
|
52
|
+
LCParen = 13,
|
|
53
|
+
RParen = 14,
|
|
54
|
+
RSParen = 15,
|
|
55
|
+
RCParen = 16,
|
|
56
|
+
EqualsAssmt = 17,
|
|
57
|
+
Arrow = 18,
|
|
58
|
+
QReg = 19,
|
|
59
|
+
Qubit = 20,
|
|
60
|
+
CReg = 21,
|
|
61
|
+
Bit = 22,
|
|
62
|
+
Barrier = 23,
|
|
63
|
+
Gate = 24,
|
|
64
|
+
Measure = 25,
|
|
65
|
+
Reset = 26,
|
|
66
|
+
String = 27,
|
|
67
|
+
Opaque = 28,
|
|
68
|
+
DefcalGrammar = 29,
|
|
69
|
+
Float = 30,
|
|
70
|
+
Bool = 31,
|
|
71
|
+
Int = 32,
|
|
72
|
+
UInt = 33,
|
|
73
|
+
Pi = 34,
|
|
74
|
+
Euler = 35,
|
|
75
|
+
Tau = 36,
|
|
76
|
+
BinaryLiteral = 37,
|
|
77
|
+
OctalLiteral = 38,
|
|
78
|
+
HexLiteral = 39,
|
|
79
|
+
ArithmeticOp = 40,
|
|
80
|
+
CompoundArithmeticOp = 41,
|
|
81
|
+
BoolLiteral = 42,
|
|
82
|
+
Duration = 43,
|
|
83
|
+
UnaryOp = 44,
|
|
84
|
+
BinaryOp = 45,
|
|
85
|
+
Let = 46,
|
|
86
|
+
QuantumModifier = 47,
|
|
87
|
+
Delay = 48,
|
|
88
|
+
Return = 49,
|
|
89
|
+
Def = 50,
|
|
90
|
+
For = 51,
|
|
91
|
+
In = 52,
|
|
92
|
+
While = 53,
|
|
93
|
+
Break = 54,
|
|
94
|
+
Continue = 55,
|
|
95
|
+
Input = 56,
|
|
96
|
+
Output = 57,
|
|
97
|
+
Switch = 58,
|
|
98
|
+
Case = 59,
|
|
99
|
+
Default = 60,
|
|
100
|
+
Defcal = 61,
|
|
101
|
+
Const = 62,
|
|
102
|
+
If = 63,
|
|
103
|
+
Else = 64,
|
|
104
|
+
End = 65,
|
|
105
|
+
Arccos = 66,
|
|
106
|
+
Arcsin = 67,
|
|
107
|
+
Arctan = 68,
|
|
108
|
+
Ceiling = 69,
|
|
109
|
+
Cos = 70,
|
|
110
|
+
Exp = 71,
|
|
111
|
+
Floor = 72,
|
|
112
|
+
Log = 73,
|
|
113
|
+
Mod = 74,
|
|
114
|
+
Popcount = 75,
|
|
115
|
+
Pow = 76,
|
|
116
|
+
Rotl = 77,
|
|
117
|
+
Rotr = 78,
|
|
118
|
+
Sin = 79,
|
|
119
|
+
Sqrt = 80,
|
|
120
|
+
Tan = 81,
|
|
121
|
+
Angle = 82,
|
|
122
|
+
Ctrl = 83,
|
|
123
|
+
NegCtrl = 84,
|
|
124
|
+
Inv = 85,
|
|
125
|
+
PowM = 86,
|
|
126
|
+
At = 87,
|
|
127
|
+
Complex = 88,
|
|
128
|
+
Dollar = 89,
|
|
129
|
+
Array = 90,
|
|
130
|
+
DurationOf = 91,
|
|
131
|
+
Stretch = 92,
|
|
132
|
+
Box = 93,
|
|
133
|
+
ReadOnly = 94,
|
|
134
|
+
Mutable = 95,
|
|
135
|
+
Dim = 96,
|
|
136
|
+
ScientificNotation = 97,
|
|
137
|
+
SizeOf = 98,
|
|
138
|
+
Extern = 99,
|
|
139
|
+
CompoundBinaryOp = 100
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Returns the token type that corresponds to a given string.
|
|
143
|
+
*
|
|
144
|
+
* This function is used by the lexer to classify identifiers and keywords.
|
|
145
|
+
* If the string is a reserved keyword, it returns the appropriate token type.
|
|
146
|
+
* Otherwise, it returns Token.Id to indicate a user-defined identifier.
|
|
147
|
+
*
|
|
148
|
+
* @param ident - The string to look up
|
|
149
|
+
* @returns The corresponding token type
|
|
150
|
+
*
|
|
151
|
+
* @example Keyword lookup
|
|
152
|
+
* ```typescript
|
|
153
|
+
* lookup('qubit'); // Returns Token.Qubit
|
|
154
|
+
* lookup('measure'); // Returns Token.Measure
|
|
155
|
+
* lookup('myVar'); // Returns Token.Id
|
|
156
|
+
* lookup('π'); // Returns Token.Pi
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
159
|
+
declare function lookup(ident: string): Token;
|
|
160
|
+
/**
|
|
161
|
+
* Returns the string representation of a token type.
|
|
162
|
+
*
|
|
163
|
+
* This is useful for debugging and error reporting, allowing you to
|
|
164
|
+
* convert token enum values back to their string representations.
|
|
165
|
+
*
|
|
166
|
+
* @param token - The token type to convert
|
|
167
|
+
* @returns The string representation of the token, or undefined if not found
|
|
168
|
+
*
|
|
169
|
+
* @example Token to string conversion
|
|
170
|
+
* ```typescript
|
|
171
|
+
* inverseLookup(Token.Qubit); // Returns 'qubit'
|
|
172
|
+
* inverseLookup(Token.If); // Returns 'if'
|
|
173
|
+
* inverseLookup(Token.Pi); // Returns 'pi'
|
|
174
|
+
* ```
|
|
175
|
+
*/
|
|
176
|
+
declare function inverseLookup(token: Token): string;
|
|
177
|
+
/**
|
|
178
|
+
* Determines whether a token can be used as a parameter in expressions.
|
|
179
|
+
*
|
|
180
|
+
* This function helps the parser validate parameter lists by checking if
|
|
181
|
+
* a token type is allowed in parameter contexts. Parameters can include
|
|
182
|
+
* identifiers, numbers, and other value-bearing tokens, but not structural
|
|
183
|
+
* tokens like semicolons or braces.
|
|
184
|
+
*
|
|
185
|
+
* @param token - The token type to check
|
|
186
|
+
* @returns true if the token CANNOT be used as a parameter, false otherwise
|
|
187
|
+
*
|
|
188
|
+
* @example Parameter validation
|
|
189
|
+
* ```typescript
|
|
190
|
+
* notParam(Token.Id); // false - identifiers can be parameters
|
|
191
|
+
* notParam(Token.NNInteger); // false - numbers can be parameters
|
|
192
|
+
* notParam(Token.Semicolon); // true - semicolons cannot be parameters
|
|
193
|
+
* notParam(Token.LParen); // true - parentheses cannot be parameters
|
|
194
|
+
* ```
|
|
195
|
+
*/
|
|
196
|
+
declare function notParam(token: Token): boolean;
|
|
197
|
+
export { Token, notParam, lookup, inverseLookup };
|
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenQASM 3.0 Token Definitions and Utilities
|
|
3
|
+
*
|
|
4
|
+
* This module defines all the token types used in OpenQASM 3.0 syntax, which
|
|
5
|
+
* significantly extends OpenQASM 2.0 with modern programming language features.
|
|
6
|
+
*
|
|
7
|
+
* Major additions in OpenQASM 3.0:
|
|
8
|
+
* - **Classical types**: int, uint, float, bool, bit, complex
|
|
9
|
+
* - **Control flow**: if/else, for/while loops, switch/case
|
|
10
|
+
* - **Functions**: def, return, extern declarations
|
|
11
|
+
* - **Advanced features**: arrays, timing (delay, durationof), calibration
|
|
12
|
+
* - **Quantum extensions**: qubit declarations, gate modifiers, hardware qubits
|
|
13
|
+
*
|
|
14
|
+
* @module
|
|
15
|
+
*
|
|
16
|
+
* @example OpenQASM 3.0 advanced tokens
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import { lookup, Token } from './qasm3/token';
|
|
19
|
+
*
|
|
20
|
+
* console.log(lookup('qubit')); // Token.Qubit
|
|
21
|
+
* console.log(lookup('if')); // Token.If
|
|
22
|
+
* console.log(lookup('def')); // Token.Def
|
|
23
|
+
* console.log(lookup('complex')); // Token.Complex
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
/**
|
|
27
|
+
* Enumeration of all OpenQASM 3.0 token types.
|
|
28
|
+
*
|
|
29
|
+
* Each token represents a specific syntactic element in OpenQASM 3.0 code.
|
|
30
|
+
* The enum values correspond to different categories:
|
|
31
|
+
* - Literals: NNInteger, Real, String, BoolLiteral
|
|
32
|
+
* - Identifiers: Id
|
|
33
|
+
* - Keywords: Qubit, Gate, Measure, If, For, While, etc.
|
|
34
|
+
* - Operators: ArithmeticOp, BinaryOp, UnaryOp
|
|
35
|
+
* - Punctuation: Semicolon, Comma, LParen, RParen, etc.
|
|
36
|
+
* - Special: OpenQASM, Include, EndOfFile, Illegal
|
|
37
|
+
*/
|
|
38
|
+
var Token;
|
|
39
|
+
(function (Token) {
|
|
40
|
+
// 0; invalid or unrecognized token
|
|
41
|
+
Token[Token["Illegal"] = 0] = "Illegal";
|
|
42
|
+
// 1; end of file character
|
|
43
|
+
Token[Token["EndOfFile"] = 1] = "EndOfFile";
|
|
44
|
+
// 2; real number (floating point)
|
|
45
|
+
Token[Token["Real"] = 2] = "Real";
|
|
46
|
+
// 3; non-negative integer
|
|
47
|
+
Token[Token["NNInteger"] = 3] = "NNInteger";
|
|
48
|
+
// 4; integer that supports underscores and negatives
|
|
49
|
+
Token[Token["Integer"] = 4] = "Integer";
|
|
50
|
+
// 5; identifier (variables names, function names, etc.)
|
|
51
|
+
Token[Token["Id"] = 5] = "Id";
|
|
52
|
+
// 6; OPENQASM version declaration
|
|
53
|
+
Token[Token["OpenQASM"] = 6] = "OpenQASM";
|
|
54
|
+
// 7; include statement
|
|
55
|
+
Token[Token["Include"] = 7] = "Include";
|
|
56
|
+
// 8; semicolon to terminate statements
|
|
57
|
+
Token[Token["Semicolon"] = 8] = "Semicolon";
|
|
58
|
+
// 9; colon
|
|
59
|
+
Token[Token["Colon"] = 9] = "Colon";
|
|
60
|
+
// 10; comma
|
|
61
|
+
Token[Token["Comma"] = 10] = "Comma";
|
|
62
|
+
// 11; left paren (
|
|
63
|
+
Token[Token["LParen"] = 11] = "LParen";
|
|
64
|
+
// 12; left square bracket [
|
|
65
|
+
Token[Token["LSParen"] = 12] = "LSParen";
|
|
66
|
+
// 13; left curly brakcet {
|
|
67
|
+
Token[Token["LCParen"] = 13] = "LCParen";
|
|
68
|
+
// 14; right paren )
|
|
69
|
+
Token[Token["RParen"] = 14] = "RParen";
|
|
70
|
+
// 15; right square paren ]
|
|
71
|
+
Token[Token["RSParen"] = 15] = "RSParen";
|
|
72
|
+
// 16; right curly bracket }
|
|
73
|
+
Token[Token["RCParen"] = 16] = "RCParen";
|
|
74
|
+
// 17; assignment operator (=)
|
|
75
|
+
Token[Token["EqualsAssmt"] = 17] = "EqualsAssmt";
|
|
76
|
+
// 18; arrow (->) used in measurement operations
|
|
77
|
+
Token[Token["Arrow"] = 18] = "Arrow";
|
|
78
|
+
// 19; quantum register declaration
|
|
79
|
+
Token[Token["QReg"] = 19] = "QReg";
|
|
80
|
+
// 20; quantum register declaration (functionally equivalent to QReg but for OpenQASM version 3)
|
|
81
|
+
Token[Token["Qubit"] = 20] = "Qubit";
|
|
82
|
+
// 21; classical register declaration
|
|
83
|
+
Token[Token["CReg"] = 21] = "CReg";
|
|
84
|
+
// 22; classical register declaration (functionally equivalent to Creg but for OpenQASM version 3)
|
|
85
|
+
Token[Token["Bit"] = 22] = "Bit";
|
|
86
|
+
// 23; barrier operation
|
|
87
|
+
Token[Token["Barrier"] = 23] = "Barrier";
|
|
88
|
+
// 24; gate declaration or application
|
|
89
|
+
Token[Token["Gate"] = 24] = "Gate";
|
|
90
|
+
// 25; measurement operation
|
|
91
|
+
Token[Token["Measure"] = 25] = "Measure";
|
|
92
|
+
// 26; qubit reset operation
|
|
93
|
+
Token[Token["Reset"] = 26] = "Reset";
|
|
94
|
+
// 27; string literal
|
|
95
|
+
Token[Token["String"] = 27] = "String";
|
|
96
|
+
// 28; opaque keyword
|
|
97
|
+
Token[Token["Opaque"] = 28] = "Opaque";
|
|
98
|
+
// 29; defcalgrammar keyword
|
|
99
|
+
Token[Token["DefcalGrammar"] = 29] = "DefcalGrammar";
|
|
100
|
+
// 30; float type keyword
|
|
101
|
+
Token[Token["Float"] = 30] = "Float";
|
|
102
|
+
// 31; bool type keyword,
|
|
103
|
+
Token[Token["Bool"] = 31] = "Bool";
|
|
104
|
+
// 32; int type keyword
|
|
105
|
+
Token[Token["Int"] = 32] = "Int";
|
|
106
|
+
// 33; uint type keyword
|
|
107
|
+
Token[Token["UInt"] = 33] = "UInt";
|
|
108
|
+
// 34; mathematical constant pi
|
|
109
|
+
Token[Token["Pi"] = 34] = "Pi";
|
|
110
|
+
// 35; euler constant
|
|
111
|
+
Token[Token["Euler"] = 35] = "Euler";
|
|
112
|
+
// 36; tau constant
|
|
113
|
+
Token[Token["Tau"] = 36] = "Tau";
|
|
114
|
+
// 37; binary literal
|
|
115
|
+
Token[Token["BinaryLiteral"] = 37] = "BinaryLiteral";
|
|
116
|
+
// 38; octal literal
|
|
117
|
+
Token[Token["OctalLiteral"] = 38] = "OctalLiteral";
|
|
118
|
+
// 39; hex literal
|
|
119
|
+
Token[Token["HexLiteral"] = 39] = "HexLiteral";
|
|
120
|
+
// 40; arithmetic operators
|
|
121
|
+
Token[Token["ArithmeticOp"] = 40] = "ArithmeticOp";
|
|
122
|
+
// 41; compound arithmetic operators
|
|
123
|
+
Token[Token["CompoundArithmeticOp"] = 41] = "CompoundArithmeticOp";
|
|
124
|
+
// 42; boolean literal value
|
|
125
|
+
Token[Token["BoolLiteral"] = 42] = "BoolLiteral";
|
|
126
|
+
// 43; duration keyword
|
|
127
|
+
Token[Token["Duration"] = 43] = "Duration";
|
|
128
|
+
// 44; unary operator
|
|
129
|
+
Token[Token["UnaryOp"] = 44] = "UnaryOp";
|
|
130
|
+
// 45; binary operator
|
|
131
|
+
Token[Token["BinaryOp"] = 45] = "BinaryOp";
|
|
132
|
+
// 46; let keyword for aliasing
|
|
133
|
+
Token[Token["Let"] = 46] = "Let";
|
|
134
|
+
// 47; quantum gate modifier
|
|
135
|
+
Token[Token["QuantumModifier"] = 47] = "QuantumModifier";
|
|
136
|
+
// 48; delay keyword
|
|
137
|
+
Token[Token["Delay"] = 48] = "Delay";
|
|
138
|
+
// 49; return keyword
|
|
139
|
+
Token[Token["Return"] = 49] = "Return";
|
|
140
|
+
// 50; def keyword for subroutines
|
|
141
|
+
Token[Token["Def"] = 50] = "Def";
|
|
142
|
+
// 51; for loop keyword
|
|
143
|
+
Token[Token["For"] = 51] = "For";
|
|
144
|
+
// 52; in keyword
|
|
145
|
+
Token[Token["In"] = 52] = "In";
|
|
146
|
+
// 53; while loop keyword
|
|
147
|
+
Token[Token["While"] = 53] = "While";
|
|
148
|
+
// 54; break keyword
|
|
149
|
+
Token[Token["Break"] = 54] = "Break";
|
|
150
|
+
// 55; continue keyword
|
|
151
|
+
Token[Token["Continue"] = 55] = "Continue";
|
|
152
|
+
// 56; input keyword
|
|
153
|
+
Token[Token["Input"] = 56] = "Input";
|
|
154
|
+
// 57; output keyword
|
|
155
|
+
Token[Token["Output"] = 57] = "Output";
|
|
156
|
+
// 58; switch statement keyword
|
|
157
|
+
Token[Token["Switch"] = 58] = "Switch";
|
|
158
|
+
// 59; switch case keyword
|
|
159
|
+
Token[Token["Case"] = 59] = "Case";
|
|
160
|
+
// 60; switch default keyword
|
|
161
|
+
Token[Token["Default"] = 60] = "Default";
|
|
162
|
+
// 61; defcal keyword
|
|
163
|
+
Token[Token["Defcal"] = 61] = "Defcal";
|
|
164
|
+
// 62; constant keywork
|
|
165
|
+
Token[Token["Const"] = 62] = "Const";
|
|
166
|
+
// 63; if statement conditional
|
|
167
|
+
Token[Token["If"] = 63] = "If";
|
|
168
|
+
// 64; else keyword
|
|
169
|
+
Token[Token["Else"] = 64] = "Else";
|
|
170
|
+
// 65; end keyword
|
|
171
|
+
Token[Token["End"] = 65] = "End";
|
|
172
|
+
// 66; inverse cosine
|
|
173
|
+
Token[Token["Arccos"] = 66] = "Arccos";
|
|
174
|
+
// 67; inverse sine
|
|
175
|
+
Token[Token["Arcsin"] = 67] = "Arcsin";
|
|
176
|
+
// 68; inverse tangent
|
|
177
|
+
Token[Token["Arctan"] = 68] = "Arctan";
|
|
178
|
+
// 69; ceiling function
|
|
179
|
+
Token[Token["Ceiling"] = 69] = "Ceiling";
|
|
180
|
+
// 70; cosine function
|
|
181
|
+
Token[Token["Cos"] = 70] = "Cos";
|
|
182
|
+
// 71; exp keyword
|
|
183
|
+
Token[Token["Exp"] = 71] = "Exp";
|
|
184
|
+
// 72; floor function
|
|
185
|
+
Token[Token["Floor"] = 72] = "Floor";
|
|
186
|
+
// 73; logarithm base e
|
|
187
|
+
Token[Token["Log"] = 73] = "Log";
|
|
188
|
+
// 74; modulus
|
|
189
|
+
Token[Token["Mod"] = 74] = "Mod";
|
|
190
|
+
// 75; popcount function
|
|
191
|
+
Token[Token["Popcount"] = 75] = "Popcount";
|
|
192
|
+
// 76; power function
|
|
193
|
+
Token[Token["Pow"] = 76] = "Pow";
|
|
194
|
+
// 77; rotate bits left function
|
|
195
|
+
Token[Token["Rotl"] = 77] = "Rotl";
|
|
196
|
+
// 78; rotate bits right function
|
|
197
|
+
Token[Token["Rotr"] = 78] = "Rotr";
|
|
198
|
+
// 79; sine
|
|
199
|
+
Token[Token["Sin"] = 79] = "Sin";
|
|
200
|
+
// 80; sqaure root
|
|
201
|
+
Token[Token["Sqrt"] = 80] = "Sqrt";
|
|
202
|
+
// 81; tangent
|
|
203
|
+
Token[Token["Tan"] = 81] = "Tan";
|
|
204
|
+
// 82; angle type
|
|
205
|
+
Token[Token["Angle"] = 82] = "Angle";
|
|
206
|
+
// 83; ctrl gate modifier
|
|
207
|
+
Token[Token["Ctrl"] = 83] = "Ctrl";
|
|
208
|
+
// 84; negctrl gate modifier
|
|
209
|
+
Token[Token["NegCtrl"] = 84] = "NegCtrl";
|
|
210
|
+
// 85; inv gate modifier
|
|
211
|
+
Token[Token["Inv"] = 85] = "Inv";
|
|
212
|
+
// 86; pow gate modifier
|
|
213
|
+
Token[Token["PowM"] = 86] = "PowM";
|
|
214
|
+
// 87; @ symbol
|
|
215
|
+
Token[Token["At"] = 87] = "At";
|
|
216
|
+
// 88; complex number keyword
|
|
217
|
+
Token[Token["Complex"] = 88] = "Complex";
|
|
218
|
+
// 89; $ symbol
|
|
219
|
+
Token[Token["Dollar"] = 89] = "Dollar";
|
|
220
|
+
// 90; array keyword
|
|
221
|
+
Token[Token["Array"] = 90] = "Array";
|
|
222
|
+
// 91; durationof function keyword
|
|
223
|
+
Token[Token["DurationOf"] = 91] = "DurationOf";
|
|
224
|
+
// 92; stretch type keyword
|
|
225
|
+
Token[Token["Stretch"] = 92] = "Stretch";
|
|
226
|
+
// 93; box keyword
|
|
227
|
+
Token[Token["Box"] = 93] = "Box";
|
|
228
|
+
// 94; readonly keyword
|
|
229
|
+
Token[Token["ReadOnly"] = 94] = "ReadOnly";
|
|
230
|
+
// 95; mutable keyword
|
|
231
|
+
Token[Token["Mutable"] = 95] = "Mutable";
|
|
232
|
+
// 96; #dim array dimensions
|
|
233
|
+
Token[Token["Dim"] = 96] = "Dim";
|
|
234
|
+
// 97; scientific notation literal
|
|
235
|
+
Token[Token["ScientificNotation"] = 97] = "ScientificNotation";
|
|
236
|
+
// 98; sizeof function
|
|
237
|
+
Token[Token["SizeOf"] = 98] = "SizeOf";
|
|
238
|
+
// 99; extern keyword
|
|
239
|
+
Token[Token["Extern"] = 99] = "Extern";
|
|
240
|
+
// 100; compound binary operators
|
|
241
|
+
Token[Token["CompoundBinaryOp"] = 100] = "CompoundBinaryOp";
|
|
242
|
+
})(Token || (Token = {}));
|
|
243
|
+
/**
|
|
244
|
+
* Mapping of string keywords to their corresponding token types.
|
|
245
|
+
*
|
|
246
|
+
* This lookup table enables the lexer to quickly determine if a string
|
|
247
|
+
* represents a reserved keyword or should be treated as an identifier.
|
|
248
|
+
*
|
|
249
|
+
* @internal
|
|
250
|
+
*/
|
|
251
|
+
const lookupMap = {
|
|
252
|
+
pi: Token.Pi,
|
|
253
|
+
π: Token.Pi,
|
|
254
|
+
qreg: Token.QReg,
|
|
255
|
+
qubit: Token.Qubit,
|
|
256
|
+
creg: Token.CReg,
|
|
257
|
+
bit: Token.Bit,
|
|
258
|
+
barrier: Token.Barrier,
|
|
259
|
+
gate: Token.Gate,
|
|
260
|
+
measure: Token.Measure,
|
|
261
|
+
reset: Token.Reset,
|
|
262
|
+
include: Token.Include,
|
|
263
|
+
if: Token.If,
|
|
264
|
+
opaque: Token.Opaque,
|
|
265
|
+
defcalgrammar: Token.DefcalGrammar,
|
|
266
|
+
float: Token.Float,
|
|
267
|
+
bool: Token.Bool,
|
|
268
|
+
true: Token.BoolLiteral,
|
|
269
|
+
false: Token.BoolLiteral,
|
|
270
|
+
int: Token.Int,
|
|
271
|
+
uint: Token.UInt,
|
|
272
|
+
euler: Token.Euler,
|
|
273
|
+
ℇ: Token.Euler,
|
|
274
|
+
tau: Token.Tau,
|
|
275
|
+
τ: Token.Tau,
|
|
276
|
+
duration: Token.Duration,
|
|
277
|
+
let: Token.Let,
|
|
278
|
+
delay: Token.Delay,
|
|
279
|
+
return: Token.Return,
|
|
280
|
+
def: Token.Def,
|
|
281
|
+
for: Token.For,
|
|
282
|
+
in: Token.In,
|
|
283
|
+
while: Token.While,
|
|
284
|
+
break: Token.Break,
|
|
285
|
+
continue: Token.Continue,
|
|
286
|
+
input: Token.Input,
|
|
287
|
+
output: Token.Output,
|
|
288
|
+
switch: Token.Switch,
|
|
289
|
+
case: Token.Case,
|
|
290
|
+
default: Token.Default,
|
|
291
|
+
defcal: Token.Defcal,
|
|
292
|
+
const: Token.Const,
|
|
293
|
+
else: Token.Else,
|
|
294
|
+
end: Token.End,
|
|
295
|
+
"!": Token.UnaryOp,
|
|
296
|
+
"~": Token.UnaryOp,
|
|
297
|
+
"**": Token.ArithmeticOp,
|
|
298
|
+
"*": Token.ArithmeticOp,
|
|
299
|
+
"/": Token.ArithmeticOp,
|
|
300
|
+
"%": Token.ArithmeticOp,
|
|
301
|
+
"+": Token.ArithmeticOp,
|
|
302
|
+
"++": Token.ArithmeticOp,
|
|
303
|
+
"&": Token.BinaryOp,
|
|
304
|
+
"|": Token.BinaryOp,
|
|
305
|
+
"^": Token.BinaryOp,
|
|
306
|
+
"&&": Token.BinaryOp,
|
|
307
|
+
"||": Token.BinaryOp,
|
|
308
|
+
"<": Token.BinaryOp,
|
|
309
|
+
"<=": Token.BinaryOp,
|
|
310
|
+
">": Token.BinaryOp,
|
|
311
|
+
">=": Token.BinaryOp,
|
|
312
|
+
"==": Token.BinaryOp,
|
|
313
|
+
"!=": Token.BinaryOp,
|
|
314
|
+
"<<": Token.BinaryOp,
|
|
315
|
+
">>": Token.BinaryOp,
|
|
316
|
+
"**=": Token.CompoundArithmeticOp,
|
|
317
|
+
"/=": Token.CompoundArithmeticOp,
|
|
318
|
+
"%=": Token.CompoundArithmeticOp,
|
|
319
|
+
"+=": Token.CompoundArithmeticOp,
|
|
320
|
+
"-=": Token.CompoundArithmeticOp,
|
|
321
|
+
"*=": Token.CompoundArithmeticOp,
|
|
322
|
+
"^=": Token.CompoundArithmeticOp,
|
|
323
|
+
arccos: Token.Arccos,
|
|
324
|
+
arcsin: Token.Arcsin,
|
|
325
|
+
arctan: Token.Arctan,
|
|
326
|
+
ceiling: Token.Ceiling,
|
|
327
|
+
cos: Token.Cos,
|
|
328
|
+
exp: Token.Exp,
|
|
329
|
+
floor: Token.Floor,
|
|
330
|
+
log: Token.Log,
|
|
331
|
+
mod: Token.Mod,
|
|
332
|
+
popcount: Token.Popcount,
|
|
333
|
+
pow: Token.Pow,
|
|
334
|
+
rotl: Token.Rotl,
|
|
335
|
+
rotr: Token.Rotr,
|
|
336
|
+
sin: Token.Sin,
|
|
337
|
+
sqrt: Token.Sqrt,
|
|
338
|
+
tan: Token.Tan,
|
|
339
|
+
angle: Token.Angle,
|
|
340
|
+
"@": Token.At,
|
|
341
|
+
complex: Token.Complex,
|
|
342
|
+
$: Token.Dollar,
|
|
343
|
+
array: Token.Array,
|
|
344
|
+
durationof: Token.DurationOf,
|
|
345
|
+
stretch: Token.Stretch,
|
|
346
|
+
box: Token.Box,
|
|
347
|
+
readonly: Token.ReadOnly,
|
|
348
|
+
mutable: Token.Mutable,
|
|
349
|
+
sizeof: Token.SizeOf,
|
|
350
|
+
extern: Token.Extern,
|
|
351
|
+
};
|
|
352
|
+
/**
|
|
353
|
+
* Returns the token type that corresponds to a given string.
|
|
354
|
+
*
|
|
355
|
+
* This function is used by the lexer to classify identifiers and keywords.
|
|
356
|
+
* If the string is a reserved keyword, it returns the appropriate token type.
|
|
357
|
+
* Otherwise, it returns Token.Id to indicate a user-defined identifier.
|
|
358
|
+
*
|
|
359
|
+
* @param ident - The string to look up
|
|
360
|
+
* @returns The corresponding token type
|
|
361
|
+
*
|
|
362
|
+
* @example Keyword lookup
|
|
363
|
+
* ```typescript
|
|
364
|
+
* lookup('qubit'); // Returns Token.Qubit
|
|
365
|
+
* lookup('measure'); // Returns Token.Measure
|
|
366
|
+
* lookup('myVar'); // Returns Token.Id
|
|
367
|
+
* lookup('π'); // Returns Token.Pi
|
|
368
|
+
* ```
|
|
369
|
+
*/
|
|
370
|
+
function lookup(ident) {
|
|
371
|
+
return ident in lookupMap ? lookupMap[ident] : Token.Id;
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* Returns the string representation of a token type.
|
|
375
|
+
*
|
|
376
|
+
* This is useful for debugging and error reporting, allowing you to
|
|
377
|
+
* convert token enum values back to their string representations.
|
|
378
|
+
*
|
|
379
|
+
* @param token - The token type to convert
|
|
380
|
+
* @returns The string representation of the token, or undefined if not found
|
|
381
|
+
*
|
|
382
|
+
* @example Token to string conversion
|
|
383
|
+
* ```typescript
|
|
384
|
+
* inverseLookup(Token.Qubit); // Returns 'qubit'
|
|
385
|
+
* inverseLookup(Token.If); // Returns 'if'
|
|
386
|
+
* inverseLookup(Token.Pi); // Returns 'pi'
|
|
387
|
+
* ```
|
|
388
|
+
*/
|
|
389
|
+
function inverseLookup(token) {
|
|
390
|
+
return Object.keys(lookupMap).find((ident) => lookupMap[ident] == token);
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* Determines whether a token can be used as a parameter in expressions.
|
|
394
|
+
*
|
|
395
|
+
* This function helps the parser validate parameter lists by checking if
|
|
396
|
+
* a token type is allowed in parameter contexts. Parameters can include
|
|
397
|
+
* identifiers, numbers, and other value-bearing tokens, but not structural
|
|
398
|
+
* tokens like semicolons or braces.
|
|
399
|
+
*
|
|
400
|
+
* @param token - The token type to check
|
|
401
|
+
* @returns true if the token CANNOT be used as a parameter, false otherwise
|
|
402
|
+
*
|
|
403
|
+
* @example Parameter validation
|
|
404
|
+
* ```typescript
|
|
405
|
+
* notParam(Token.Id); // false - identifiers can be parameters
|
|
406
|
+
* notParam(Token.NNInteger); // false - numbers can be parameters
|
|
407
|
+
* notParam(Token.Semicolon); // true - semicolons cannot be parameters
|
|
408
|
+
* notParam(Token.LParen); // true - parentheses cannot be parameters
|
|
409
|
+
* ```
|
|
410
|
+
*/
|
|
411
|
+
function notParam(token) {
|
|
412
|
+
if (token == Token.NNInteger ||
|
|
413
|
+
token == Token.Real ||
|
|
414
|
+
token == Token.Id ||
|
|
415
|
+
inverseLookup(token)) {
|
|
416
|
+
return false;
|
|
417
|
+
}
|
|
418
|
+
return true;
|
|
419
|
+
}
|
|
420
|
+
export { Token, notParam, lookup, inverseLookup };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenQASM version detection and management utilities
|
|
3
|
+
*
|
|
4
|
+
* Handles version detection from QASM source code and provides utilities
|
|
5
|
+
* for working with different OpenQASM versions. Supports automatic version
|
|
6
|
+
* detection from OPENQASM statements and manual version specification.
|
|
7
|
+
*
|
|
8
|
+
* @module Version Management
|
|
9
|
+
*
|
|
10
|
+
* @example Version detection
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const version = new OpenQASMVersion(3, 0);
|
|
13
|
+
* console.log(version.toString()); // "3.0"
|
|
14
|
+
* console.log(version.isVersion3()); // true
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
/** Enum representing the major OpenQASM versions. */
|
|
18
|
+
declare enum OpenQASMMajorVersion {
|
|
19
|
+
Version2 = 2,
|
|
20
|
+
Version3 = 3
|
|
21
|
+
}
|
|
22
|
+
/** Class representing the OpenQASM version. */
|
|
23
|
+
declare class OpenQASMVersion {
|
|
24
|
+
/** The major OpenQASM version. */
|
|
25
|
+
major: OpenQASMMajorVersion;
|
|
26
|
+
/** The minor OpenQASM version. */
|
|
27
|
+
minor: number;
|
|
28
|
+
/**
|
|
29
|
+
* Creates an OpenQASMVersion instance.
|
|
30
|
+
* @param major - The OpenQASM major version. (optional)
|
|
31
|
+
* @param minor - The OpenQASM minor version (optional)
|
|
32
|
+
*/
|
|
33
|
+
constructor(major?: OpenQASMMajorVersion, minor?: number);
|
|
34
|
+
/** Returns the version as a formatted string. */
|
|
35
|
+
toString(): string;
|
|
36
|
+
/** Returns whether the version is 3.x */
|
|
37
|
+
isVersion3(): boolean;
|
|
38
|
+
/** Returns whether the version is 2.x */
|
|
39
|
+
isVersion2(): boolean;
|
|
40
|
+
}
|
|
41
|
+
export { OpenQASMMajorVersion, OpenQASMVersion };
|