def test_no_designator_type(): p = """ duration a; stretch b; """.strip() program = parse(p) assert program == Program(statements=[ ClassicalDeclaration( DurationType(), Identifier("a"), None, ), ClassicalDeclaration(StretchType(), Identifier("b"), None), ]) SpanGuard().visit(program)
def visitNoDesignatorDeclaration( self, ctx: qasm3Parser.NoDesignatorDeclarationContext): return ClassicalDeclaration( type=self.visit(ctx.noDesignatorType()), identifier=add_span(Identifier(ctx.Identifier().getText()), get_span(ctx.Identifier())), init_expression=self.visit(ctx.equalsExpression().expression()) if ctx.equalsExpression() else None, )
def test_qubit_and_bit_declaration(): p = """ bit c; qubit a; """.strip() program = parse(p) assert program == Program(statements=[ ClassicalDeclaration(BitType(None), Identifier("c"), None), QubitDeclaration(qubit=Identifier(name="a"), size=None), ]) SpanGuard().visit(program)
def test_bit_declaration(): p = """ bit c; """.strip() program = parse(p) assert program == Program(statements=[ ClassicalDeclaration(BitType(None), Identifier("c"), None) ]) SpanGuard().visit(program) classical_declaration = program.statements[0] assert classical_declaration.span == Span(1, 0, 1, 5)
def visitComplexDeclaration(self, ctx: qasm3Parser.ComplexDeclarationContext): return ClassicalDeclaration( add_span( ComplexType(base_type=self.visit(ctx.numericType())), get_span(ctx), ), add_span(Identifier(ctx.Identifier().getText()), get_span(ctx.Identifier())), self.visit(ctx.equalsExpression().expression()) if ctx.equalsExpression() else None, )
def visitArrayDeclaration(self, ctx: qasm3Parser.ArrayDeclarationContext): if ctx.arrayInitializer(): initializer = self.visit(ctx.arrayInitializer()) elif ctx.expression(): initializer = self.visit(ctx.expression()) else: initializer = None return ClassicalDeclaration( self.visit(ctx.arrayType()), add_span(Identifier(ctx.Identifier().getText()), get_span(ctx.Identifier())), initializer, )
def test_complex_declaration(): p = """ complex[int[24]] iq; """.strip() program = parse(p) assert program == Program(statements=[ ClassicalDeclaration( ComplexType(base_type=IntType(IntegerLiteral(24))), Identifier("iq"), None, ), ]) SpanGuard().visit(program) context_declaration = program.statements[0] assert context_declaration.span == Span(1, 0, 1, 19)
def visitBitDeclaration(self, ctx: qasm3Parser.BitDeclarationContext): equals_expression = ctx.equalsExpression() init_expression = self.visit( equals_expression.expression()) if equals_expression else None desinator = ctx.designator() desinator_expression = self.visit(desinator) if desinator else None return ClassicalDeclaration( add_span( BitType(desinator_expression), get_span(ctx), ), add_span(Identifier(ctx.Identifier().getText()), get_span(ctx.Identifier())), init_expression, )
def test_array_declaration(): p = """ array[uint[8], 2] a; array[int[8], 2] a = {1, 1}; array[bit, 2] a = b; array[float[32], 2, 2] a; array[complex[float[64]], 2, 2] a = {{1, 1}, {2, 2}}; array[uint[8], 2, 2] a = {b, b}; """.strip() program = parse(p) a, b = Identifier("a"), Identifier("b") one, two, eight = IntegerLiteral(1), IntegerLiteral(2), IntegerLiteral(8) SpanGuard().visit(program) assert program == Program(statements=[ ClassicalDeclaration( type=ArrayType(base_type=UintType(eight), dimensions=[two]), identifier=a, init_expression=None, ), ClassicalDeclaration( type=ArrayType(base_type=IntType(eight), dimensions=[two]), identifier=a, init_expression=ArrayLiteral([one, one]), ), ClassicalDeclaration( type=ArrayType(base_type=BitType(size=None), dimensions=[two]), identifier=a, init_expression=b, ), ClassicalDeclaration( type=ArrayType( base_type=FloatType(IntegerLiteral(32)), dimensions=[two, two], ), identifier=a, init_expression=None, ), ClassicalDeclaration( type=ArrayType( base_type=ComplexType(FloatType(IntegerLiteral(64))), dimensions=[two, two], ), identifier=a, init_expression=ArrayLiteral( [ArrayLiteral([one, one]), ArrayLiteral([two, two])], ), ), ClassicalDeclaration( type=ArrayType(base_type=UintType(eight), dimensions=[two, two]), identifier=a, init_expression=ArrayLiteral([b, b]), ), ], )
def visitSingleDesignatorDeclaration( self, ctx: qasm3Parser.SingleDesignatorDeclarationContext): equals_expression = ctx.equalsExpression() init_expression = self.visit( equals_expression.expression()) if equals_expression else None type_name = ctx.singleDesignatorType().getText() if type_name in _TYPE_NODE_INIT: type_size = self.visit(ctx.designator()) type_node = _TYPE_NODE_INIT[type_name](type_size) else: # To capture potential parser errors. raise ValueError(f"Type name {type_name} not found.") return ClassicalDeclaration( add_span( type_node, combine_span(get_span(ctx.singleDesignatorType()), get_span(ctx.designator())), ), add_span(Identifier(ctx.Identifier().getText()), get_span(ctx.Identifier())), init_expression, )