/** * OpenQASM 3.0 Abstract Syntax Tree Node Definitions * * This module defines all the AST node classes that represent the parsed structure * of OpenQASM 3.0 programs. Each class corresponds to a specific language construct * and contains the necessary information to represent that construct in memory. * * The AST provides a hierarchical representation where: * - **AstNode**: Base class for all nodes * - **Statement**: Executable instructions (declarations, assignments, control flow) * - **Expression**: Value-producing constructs (literals, identifiers, operations) * - **ClassicalType**: Type system for classical data * * Key node categories: * - **Declarations**: Variable, type, and quantum register declarations * - **Quantum Operations**: Gate calls, measurements, barriers, delays * - **Control Flow**: If/else, loops, switch statements * - **Expressions**: Arithmetic, logical, and function call expressions * - **Types**: Classical type specifications (int, float, bool, etc.) * * @module * * @example Building a quantum declaration * ```typescript * import { QuantumDeclaration, Identifier, IntegerLiteral } from './ast'; * * // Represents: qubit[2] q; * const qubitDecl = new QuantumDeclaration( * new Identifier('q'), * new IntegerLiteral(2) * ); * ``` * * @example Creating expressions * ```typescript * import { Arithmetic, ArithmeticOp, Identifier, FloatLiteral } from './ast'; * * // Represents: theta * 2.0 * const expr = new Arithmetic( * ArithmeticOp.TIMES, * new Identifier('theta'), * new FloatLiteral(2.0) * ); * ``` */ import { OpenQASMVersion } from "../version"; /** Base class representing a basic AST node. */ declare class AstNode { } /** * Base class representing an instruction which performs an action. * * statement * : expressionStatement * | assignmentStatement * | classicalDeclarationStatement * | branchingStatement * | loopStatement * | endStatement * | aliasStatement * | quantumStatement */ declare class Statement extends AstNode { } /** * Class representing a custom grammar for specifying calibrations. * * calibrationGrammarStatement: * | DEFCALGRAMMAR StringLiteral SEMICOLON */ declare class CalibrationGrammarDeclaration extends Statement { name: string; constructor(name: string); } /** * Class representing an include statement. * * includeStatement: * INCLUDE StringLiteral SEMICOLON */ declare class Include extends AstNode { filename: string; constructor(filename: string); } /** * Class representing the version statement. * * version * : 'OPENQASM'(Integer | RealNumber) SEMICOLON */ declare class Version extends AstNode { version: OpenQASMVersion; constructor(version: OpenQASMVersion); } /** * Base class representing a quantum instruction. * * quantumInstruction * : quantumGateCall * | quantumPhase * | quantumMeasurement * | quantumReset * | quantumBarrier */ declare class QuantumInstruction extends AstNode { } /** Base class representing a classical computing type. */ declare class ClassicalType extends AstNode { } /** * Class representing a classical float type. * * scalarType: * FLOAT designator? */ declare class FloatType extends ClassicalType { width: number | Expression | null; constructor(width?: number | Expression); } /** * Class representing a complex number type. * * scalarType: * COMPLEX (LBRACKET scalarType RBRACKET)? */ declare class ComplexType extends ClassicalType { float: FloatType; constructor(float: FloatType); } /** Class representing a classical boolean type. */ declare class BoolType extends ClassicalType { } /** Class representing a classical signed integer type. */ declare class IntType extends ClassicalType { size: number | Expression | null; constructor(size?: number | Expression); } /** Class representing a classical unsigned integer type. */ declare class UIntType extends ClassicalType { size: number | Expression | null; constructor(size?: number | Expression); } /** Class representing a classical bit type. */ declare class BitType extends ClassicalType { size: number | Expression | null; constructor(size?: number | Expression); } /** Class representing an angle type. */ declare class AngleType extends ClassicalType { size: number | Expression | null; constructor(size?: number | Expression); } /** Class representing the stretch type. */ declare class StretchType extends ClassicalType { } /** Class representing a duration type. */ declare class DurationType extends ClassicalType { } /** Base class representing an expression. */ declare class Expression extends AstNode { } /** Class representing a list of parameters. */ declare class Parameters extends Expression { args: Array; constructor(args: Array); } /** Class representing a range. */ declare class Range extends Expression { start: Expression | null; end: Expression | null; step: Expression | null; constructor(start?: Expression, end?: Expression, step?: Expression); } /** Class representing an expression identifier. */ declare class Identifier extends Expression { name: string; constructor(name: string); } /** Class representing an identifier with subscripted access. */ declare class SubscriptedIdentifier extends Identifier { subscript: Range | Expression; constructor(name: string, subscript: Range | Expression); } /** Class representing the Pi constant. */ declare class Pi extends Expression { } /** Class representing the Euler constant. */ declare class Euler extends Expression { } /** Class representing the Tau constant. */ declare class Tau extends Expression { } /** Enum representing the available mathematical functions. */ declare enum MathFunctionTypes { CEILING = "ceiling", EXP = "exponential", FLOOR = "floor", LOG = "logarithm", MOD = "modulus", POPCOUNT = "popcount", POW = "power", SQRT = "sqrt", ROTR = "rotr", ROTL = "rotl" } /** Class representing a mathematical function. */ declare class MathFunction extends Expression { mathType: MathFunctionTypes; operands: Expression; constructor(mathType: MathFunctionTypes, operands: Expression | Array); } /** Enum representing the available trigonometric functions. */ declare enum TrigFunctionTypes { ARCCOS = "ArcCos", ARCSIN = "ArcSin", ARCTAN = "ArcTan", COS = "Cos", SIN = "Sin", TAN = "Tan" } /** Class representing a trigonometric function. */ declare class TrigFunction extends Expression { trigType: TrigFunctionTypes; operand: Expression; constructor(trigType: TrigFunctionTypes, operand: Expression); } /** Class representing an integer literal. */ declare class IntegerLiteral extends Expression { value: number | string; constructor(value: number | string); } /** Class representing a scientific notation, binary, octal, or hex literal. */ declare class NumericLiteral extends Expression { value: string; constructor(value: string); } /** Class representing a float literal. */ declare class FloatLiteral extends Expression { value: number | string; constructor(value: number | string); } /** Class representing an imaginary number literal. */ declare class ImaginaryLiteral extends Expression { value: string; constructor(value: string); } /** Class representing a boolean literal. */ declare class BooleanLiteral extends Expression { value: boolean; constructor(value: boolean); } /** Class representing a bit string literal. */ declare class BitstringLiteral extends Expression { value: string; constructor(value: string); } /** Enum representing the supported duration units. */ declare enum DurationUnit { NANOSECOND = "ns", MICROSECOND = "us", MILLISECOND = "ms", SECOND = "s", SAMPLE = "dt" } /** Class representing a duration literal. */ declare class DurationLiteral extends Expression { value: number | Expression; unit: DurationUnit; constructor(value: number | Expression, unit: DurationUnit); } /** Class representing a durationof function call. */ declare class DurationOf extends Expression { scope: ProgramBlock; constructor(scope: ProgramBlock); } /** Class representing a sizeof function call. */ declare class SizeOf extends Expression { array: Identifier; dimensionIndex: Expression; constructor(array: Identifier, dimensionIndex?: Expression | null); } /** Enum representing Unary operands. */ declare enum UnaryOp { LOGIC_NOT = "!", BIT_NOT = "~", MINUS = "-" } /** Class representing a unary operator. */ declare class Unary extends Expression { op: UnaryOp; operand: Expression; constructor(op: UnaryOp, operand: Expression); } /** Enum representing arithmetic operations. */ declare enum ArithmeticOp { POWER = "**", TIMES = "*", DIVISION = "/", MOD = "%", PLUS = "+", MINUS = "-", CONCAT = "++" } /** Class representing an arithmetic operator expression. */ declare class Arithmetic extends Expression { op: ArithmeticOp; left: Expression; right: Expression; constructor(op: ArithmeticOp, left: Expression, right: Expression); } /** Enum representing binary operands. */ declare enum BinaryOp { BIT_AND = "&", BIT_OR = "|", BIT_XOR = "^", LOGIC_AND = "&&", LOGIC_OR = "||", LESS = "<", LESS_EQUAL = "<=", GREATER = ">", GREATER_EQUAL = ">=", EQUAL = "==", NOT_EQUAL = "!=", SHIFT_LEFT = "<<", SHIFT_RIGHT = ">>" } /** Class representing binary operator expressions. */ declare class Binary extends Expression { op: BinaryOp; left: Expression; right: Expression; constructor(op: BinaryOp, left: Expression, right: Expression); } /** Class representing an cast expression. */ declare class Cast extends Expression { type: ClassicalType; operand: Expression; constructor(type: ClassicalType, operand: Expression); } /** * Class representing a literal index set of values. * * { Expression (, Expression )* } */ declare class IndexSet extends Expression { values: Array; constructor(values: Array); } /** Enum representing the supported array reference modifiers. */ declare enum ArrayReferenceModifier { READONLY = "readonly", MUTABLE = "mutable" } /** Class representing an array reference. */ declare class ArrayReference extends Expression { array: ArrayDeclaration; modifier: ArrayReferenceModifier; constructor(array: ArrayDeclaration, modifier: ArrayReferenceModifier); } /** Class representing a statically sized array. */ declare class ArrayDeclaration extends Statement { baseType: ClassicalType; dimensions: Array | number; identifier: Identifier; initializer: ArrayInitializer | Expression | null; constructor(baseType: ClassicalType, dimensions: Array | number, identifier: Identifier, initializer?: ArrayInitializer | Expression | null); } /** Class representing an initial value for an ArrayDeclaration. */ declare class ArrayInitializer extends Expression { values: Array; constructor(values: Array); } /** Class representing an array access */ declare class ArrayAccess extends Expression { array: Identifier; indices: Array | Range | null; constructor(array: Identifier, indices?: Array | Range); } /** * Class representing a quantum measurement. * * quantumMeasurement * : `measure` identifierList */ declare class QuantumMeasurement extends AstNode { identifierList: Array; constructor(identifierList: Array); } /** * Class representing a quantum measurement assignment statement. * * QuantumMeasurementAssignment * : quantumMeasurement ARROW indexIdentifierList * | indexIdentifier EQUALS quantumMeasurement */ declare class QuantumMeasurementAssignment extends Statement { identifier: Identifier | SubscriptedIdentifier; quantumMeasurement: QuantumMeasurement; constructor(identifier: Identifier | SubscriptedIdentifier, quantumMeasurement: QuantumMeasurement); } /** Class representing the declaration of a classical type, optionally initializing it to a value. */ declare class ClassicalDeclaration extends Statement { classicalType: ClassicalType; identifier: Identifier | null; initializer: any | null; isConst: boolean; constructor(classicalType: ClassicalType, identifier?: Identifier, initializer?: any, isConst?: boolean); } /** Class representing an expression to a left value. */ declare class AssignmentStatement extends Statement { leftValue: SubscriptedIdentifier | Identifier | ArrayAccess; rightValue: Expression | SubroutineCall | ArrayAccess; constructor(leftValue: SubscriptedIdentifier | Identifier | ArrayAccess, rightValue: Expression | SubroutineCall | ArrayAccess); } /** * Class representing a quantum declaration. * * quantumDeclaration * : `qreg` Identifier designator? * `qubit` designator? Identifier */ declare class QuantumDeclaration extends AstNode { identifier: Identifier; size: Expression | null; constructor(identifier: Identifier, size?: Expression | null); } /** * Class representing a hardware qubit. * * $[NUM] */ declare class HardwareQubit extends AstNode { number: number; constructor(number: number); } /** Class representing an alias statement. */ declare class AliasStatement extends AstNode { identifier: Identifier; value: Expression; constructor(identifier: Identifier, value: Expression); } /** Enum representing the available quantum gate modifiers. */ declare enum QuantumGateModifierName { CTRL = 0, NRGCTRL = 1, INV = 2, POW = 3 } /** Class representing a modifier of a gate. */ declare class QuantumGateModifier extends AstNode { modifier: QuantumGateModifierName; argument: Expression | null; constructor(modifier: QuantumGateModifierName, argument?: Expression); } /** * Class representing a quantum gate call. * * quantumGateCall * : quantumGateModifier* quantumGateName ( LPAREN expressionList? RPAREN )? indexIdentifierList */ declare class QuantumGateCall extends QuantumInstruction { quantumGateName: Identifier; qubits: Array; parameters: Parameters | null; modifiers: Array | null; constructor(quantumGateCall: Identifier, qubits: Array, parameters?: Parameters, modifiers?: Array); } /** * Class representing a quantum barrier. * * quantumBarrier * : `barrier` indexIdentifierList */ declare class QuantumBarrier extends QuantumInstruction { qubits: Array; constructor(qubits: Array); } /** Class representing a quantum reset instruction. */ declare class QuantumReset extends QuantumInstruction { identifier: Identifier; constructor(identifier: Identifier); } /** Class representing a quantum delay instruction. */ declare class QuantumDelay extends QuantumInstruction { duration: Expression; qubits: Array; constructor(duration: Expression, qubits: Array); } /** Class representing a return statement. */ declare class ReturnStatement extends Statement { expression: Expression | QuantumMeasurement | null; constructor(expression?: Expression | QuantumMeasurement); } /** * Base class representing a program block. * * programBlock * : statement | controlDirective * | LBRACE(statement | controlDirective) * RBRACEj */ declare class ProgramBlock extends AstNode { statements: Array; constructor(statements: Array); } /** * Class representing a block of quantum operation statements. * * quantumBlock * : LBRACE (quantumStatement | quantumLoop) * RBRACE */ declare class QuantumBlock extends ProgramBlock { constructor(statements: Array); } /** * Class representing a block of statements in a subroutine. * * subroutineBlock * : LBRACE statement* returnStatement? RBRACE */ declare class SubroutineBlock extends ProgramBlock { constructor(statements: Array); } /** * Class representing a quantum gate definition. * * quantumGateDefinition * : `gate` quantumGateDefinition quantumBlock */ declare class QuantumGateDefinition extends Statement { name: Identifier; params: Parameters; qubits: Array; body: QuantumBlock; constructor(name: Identifier, params: Parameters, qubits: Array, body: QuantumBlock); } /** * Class representing an extern function signature. * * externStatement * : EXTERN Identifier LPAREN externArgumentList? RPAREN returnSignature? SEMICOLON; */ declare class ExternSignature extends Statement { name: Identifier; args: Parameters | null; returnType: ClassicalType | null; constructor(name: Identifier, args: Parameters | null, returnType?: ClassicalType); } /** * Class representing a subroutine. * * subroutineDefinition * : `def` Identifier LPAREN anyTypeArgumentList? RPAREN * returnSignature? subroutineBlock */ declare class SubroutineDefinition extends Statement { name: Identifier; subroutineBlock: SubroutineBlock; args: Parameters | null; returnType: ClassicalType | null; constructor(name: Identifier, subroutineBlock: SubroutineBlock, args: Parameters | null, returnType?: ClassicalType); } /** * Class representing a box scoping statement. * * boxStatement * : BOX designator? scope; */ declare class BoxDefinition extends Statement { designator: Expression | null; scope: ProgramBlock; constructor(scope: ProgramBlock, designator?: Expression); } /** Class representing a subroutine call. */ declare class SubroutineCall extends Statement { subroutineName: Identifier; parameters: Parameters | null; constructor(subroutineName: Identifier, parameters?: Parameters); } /** * Class representing a branching if statement. * * branchingStatement * : `if` LPAREN booleanExpression RPAREN programBlock (`else` programBlock)? */ declare class BranchingStatement extends Statement { condition: Expression; trueBody: ProgramBlock; falseBody: ProgramBlock | null; constructor(condition: Expression, trueBody: ProgramBlock, falseBody?: ProgramBlock); } /** * Class representing a for loop statement. * * ForLoop: "for" Identifier "in" SetDeclaration ProgramBlock * SetDeclaration: * | Identifier * | "{" Expression ("," Expression)* "}" * | "[" Range "]" */ declare class ForLoopStatement extends Statement { indexSet: IndexSet | Range | Expression; loopVarType: ClassicalType; parameter: Identifier; body: ProgramBlock; constructor(indexSet: IndexSet | Range | Expression, loopVarType: ClassicalType, parameter: Identifier, body: ProgramBlock); } /** * Class representing a while loop statement. * * WhileLoop: "while" "(" Expression ")" ProgramBlock */ declare class WhileLoopStatement extends Statement { condition: Expression; body: ProgramBlock; constructor(condition: Expression, body: ProgramBlock); } /** Class representing a break loop statement. */ declare class BreakStatement extends Statement { } /** Class representing a continue loop statement. */ declare class ContinueStatement extends Statement { } /** Enum representing the available IO modifiers. */ declare enum IOModifier { INPUT = "input", OUTPUT = "output" } /** Class representing a declaration of an IO variable. */ declare class IODeclaration extends Statement { modifier: IOModifier; classicalType: ClassicalDeclaration; constructor(modifier: IOModifier, classicalType: ClassicalDeclaration); } /** Class representing a switch statement. */ declare class SwitchStatement extends Statement { controlExpression: Expression; cases: Array; defaultBlock: DefaultStatement | null; constructor(controlExpression: Expression, cases: Array, defaultBlock?: DefaultStatement); } /** Class representing a single case in a switch statement. */ declare class CaseStatement extends Statement { labels: Array; body: ProgramBlock; constructor(labels: Array, body: ProgramBlock); } /** Class representing the default case in a swtich statement. */ declare class DefaultStatement extends Statement { body: ProgramBlock; constructor(body: ProgramBlock); } export { AstNode, Statement, Expression, Parameters, ArrayReferenceModifier, ArrayReference, CalibrationGrammarDeclaration, Include, Version, FloatType, ComplexType, BoolType, IntType, UIntType, BitType, AngleType, StretchType, DurationType, Range, Identifier, SubscriptedIdentifier, Pi, Euler, Tau, MathFunctionTypes, MathFunction, TrigFunctionTypes, TrigFunction, IntegerLiteral, NumericLiteral, FloatLiteral, ImaginaryLiteral, BooleanLiteral, BitstringLiteral, DurationUnit, DurationLiteral, DurationOf, SizeOf, UnaryOp, Unary, ArithmeticOp, Arithmetic, BinaryOp, Binary, Cast, IndexSet, ArrayDeclaration, ArrayInitializer, ArrayAccess, QuantumMeasurement, QuantumMeasurementAssignment, ClassicalDeclaration, AssignmentStatement, QuantumDeclaration, HardwareQubit, AliasStatement, QuantumGateModifierName, QuantumGateModifier, QuantumGateCall, QuantumBarrier, QuantumReset, QuantumDelay, ReturnStatement, ProgramBlock, QuantumBlock, SubroutineBlock, QuantumGateDefinition, BoxDefinition, ExternSignature, SubroutineDefinition, SubroutineCall, BranchingStatement, ForLoopStatement, WhileLoopStatement, BreakStatement, ContinueStatement, IOModifier, IODeclaration, SwitchStatement, CaseStatement, DefaultStatement, ClassicalType, };