qasm-ts
Version:
QASM, the low-level programming language for quantum circuit specification, implemented in TypeScript.
161 lines (160 loc) • 5.32 kB
JavaScript
;
/**
* OpenQASM 2.0 Token Definitions and Utilities
*
* This module defines the token types used in OpenQASM 2.0 syntax. OpenQASM 2.0
* has a simpler token set compared to 3.0, focusing on basic quantum operations
* and classical registers without advanced control flow or data types.
*
* Key differences from OpenQASM 3.0:
* - Limited to `qreg` and `creg` declarations (no advanced types)
* - No control flow tokens (if/else/for/while)
* - No subroutine or function definitions
* - Simpler expression and operator support
*
* @module
*
* @example OpenQASM 2.0 token usage
* ```typescript
* import { lookup, Token } from './qasm2/token';
*
* console.log(lookup('qreg')); // Token.QReg
* console.log(lookup('barrier')); // Token.Barrier
* console.log(lookup('measure')); // Token.Measure
* ```
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Token = void 0;
exports.notParam = notParam;
exports.lookup = lookup;
exports.inverseLookup = inverseLookup;
/**
* Enumeration of OpenQASM 2.0 token types.
*
* This simplified token set reflects OpenQASM 2.0's focus on basic quantum
* circuit description without the advanced features of version 3.0.
*/
var Token;
(function (Token) {
// 0; invalid or unrecognized token
Token[Token["Illegal"] = 0] = "Illegal";
// 1; end of file character
Token[Token["EndOfFile"] = 1] = "EndOfFile";
// 2; real number (floating point)
Token[Token["Real"] = 2] = "Real";
// 3; non-negative integer
Token[Token["NNInteger"] = 3] = "NNInteger";
// 4; identifier (variables names, function names, etc.)
Token[Token["Id"] = 4] = "Id";
// 5; OPENQASM version declaration
Token[Token["OpenQASM"] = 5] = "OpenQASM";
// 6; semicolon to terminate statements
Token[Token["Semicolon"] = 6] = "Semicolon";
// 7; comma
Token[Token["Comma"] = 7] = "Comma";
// 8; left paren (
Token[Token["LParen"] = 8] = "LParen";
// 9; left square bracket [
Token[Token["LSParen"] = 9] = "LSParen";
// 10; left curly brakcet {
Token[Token["LCParen"] = 10] = "LCParen";
// 11; right paren )
Token[Token["RParen"] = 11] = "RParen";
// 12; right square paren ]
Token[Token["RSParen"] = 12] = "RSParen";
// 13; right curly bracket }
Token[Token["RCParen"] = 13] = "RCParen";
// 14; arrow (->) used in measurement operations
Token[Token["Arrow"] = 14] = "Arrow";
// 15; equality operator (==)
Token[Token["Equals"] = 15] = "Equals";
// 16; addition operator (+)
Token[Token["Plus"] = 16] = "Plus";
// 17; subtraction operator (-)
Token[Token["Minus"] = 17] = "Minus";
// 18; multiplication operator (*)
Token[Token["Times"] = 18] = "Times";
// 19; division operator (/)
Token[Token["Divide"] = 19] = "Divide";
// 20; exponentiation operator (^)
Token[Token["Power"] = 20] = "Power";
// 21; sine function
Token[Token["Sin"] = 21] = "Sin";
// 22; cosine function
Token[Token["Cos"] = 22] = "Cos";
// 23; tangent function
Token[Token["Tan"] = 23] = "Tan";
// 24; exponential function
Token[Token["Exp"] = 24] = "Exp";
// 25; natural logarithm function
Token[Token["Ln"] = 25] = "Ln";
// 26; square root function
Token[Token["Sqrt"] = 26] = "Sqrt";
// 27; mathematical constant pi
Token[Token["Pi"] = 27] = "Pi";
// 28; quantum register declaration
Token[Token["QReg"] = 28] = "QReg";
// 29; classical register declaration
Token[Token["CReg"] = 29] = "CReg";
// 30; barrier operation
Token[Token["Barrier"] = 30] = "Barrier";
// 31; gate declaration or application
Token[Token["Gate"] = 31] = "Gate";
// 32; measurement operation
Token[Token["Measure"] = 32] = "Measure";
// 33; qubit reset operation
Token[Token["Reset"] = 33] = "Reset";
// 34; include statement
Token[Token["Include"] = 34] = "Include";
// 35; if statement conditional
Token[Token["If"] = 35] = "If";
// 36; string literal
Token[Token["String"] = 36] = "String";
// 37; opaque keyword
Token[Token["Opaque"] = 37] = "Opaque";
})(Token || (exports.Token = Token = {}));
var lookupMap = {
if: Token.If,
sin: Token.Sin,
cos: Token.Cos,
tan: Token.Tan,
exp: Token.Exp,
ln: Token.Ln,
sqrt: Token.Sqrt,
pi: Token.Pi,
"+": Token.Plus,
"-": Token.Minus,
"/": Token.Divide,
"*": Token.Times,
"^": Token.Power,
};
/**
* Returns the token that represents a given string.
* @param ident - The string.
* @return The corresponding token.
*/
function lookup(ident) {
return ident in lookupMap ? lookupMap[ident] : Token.Id;
}
/**
* Returns the string representation of a token.
* @param tokens - The token.
* @return The string representation of the token.
*/
function inverseLookup(token) {
return Object.keys(lookupMap).find(function (ident) { return lookupMap[ident] == token; });
}
/**
* Determines whether a token denotes a parameter.
* @param tokens - The token.
* @return Whether the token does NOT denote a parameter.
*/
function notParam(token) {
if (token == Token.NNInteger ||
token == Token.Real ||
token == Token.Id ||
inverseLookup(token)) {
return false;
}
return true;
}