qasm-ts 2.1.1 → 2.1.3

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