braket.default_simulator.openqasm.parser.openqasm_ast module

Abstract Syntax Tree (openqasm3.ast)

The reference abstract syntax tree (AST) for OpenQASM 3 programs.

class braket.default_simulator.openqasm.parser.openqasm_ast.AccessControl(value)

Bases: Enum

An enumeration.

const = 1
mutable = 2
class braket.default_simulator.openqasm.parser.openqasm_ast.AliasStatement(target: Identifier, value: Identifier | Concatenation)[source]

Bases: Statement

Alias statement

Example:

let a = qubits[0];
target: Identifier
value: Identifier | Concatenation
class braket.default_simulator.openqasm.parser.openqasm_ast.AngleType(size: Expression | None = None)[source]

Bases: ClassicalType

Node representing the classical angle type, with an optional precision.

Example:

angle[8]
angle[16]
size: Expression | None = None
class braket.default_simulator.openqasm.parser.openqasm_ast.Annotation(keyword: str, command: str | None = None)[source]

Bases: QASMNode

An annotation applied to a statment.

keyword: str
command: str | None = None
class braket.default_simulator.openqasm.parser.openqasm_ast.ArrayLiteral(values: List[Expression])[source]

Bases: Expression

Array literal, used to initialise declared arrays.

For example:

array[uint[8], 2] row = {1, 2};
array[uint[8], 2, 2] my_array = {{1, 2}, {3, 4}};
array[uint[8], 2, 2] my_array = {row, row};
values: List[Expression]
class braket.default_simulator.openqasm.parser.openqasm_ast.ArrayReferenceType(base_type: IntType | UintType | FloatType | AngleType | BitType | BoolType | ComplexType, dimensions: Expression | List[Expression])[source]

Bases: ClassicalType

Type of arrays that are a reference to an array with allocated storage.

This is generally any array declared as a subroutine argument. The dimensions can be either a list of expressions (one for each dimension), or a single expression, which is the number of dimensions.

For example:

// `a` will have dimensions `[IntegerLiteral(2)]` (with a list), because
// it is a 1D array, with a length of 2.
def f(const array[uint[8], 2] a) {}
// `b` will have dimension `IntegerLiteral(3)` (no list), because it is
// a 3D array, but we don't know the lengths of its dimensions.
def f(const array[uint[8], #dim=3] b) {}
base_type: IntType | UintType | FloatType | AngleType | BitType | BoolType | ComplexType
dimensions: Expression | List[Expression]
class braket.default_simulator.openqasm.parser.openqasm_ast.ArrayType(base_type: IntType | UintType | FloatType | AngleType | BitType | BoolType | ComplexType, dimensions: List[Expression])[source]

Bases: ClassicalType

Type of arrays that include allocation of the storage.

This is generally any array declared as a standard statement, but not arrays declared by being arguments to subroutines.

base_type: IntType | UintType | FloatType | AngleType | BitType | BoolType | ComplexType
dimensions: List[Expression]
class braket.default_simulator.openqasm.parser.openqasm_ast.AssignmentOperator(value)

Bases: Enum

An enumeration.

class braket.default_simulator.openqasm.parser.openqasm_ast.BinaryExpression(op: BinaryOperator, lhs: Expression, rhs: Expression)[source]

Bases: Expression

A binary expression

Example:

q1 || q2
op: BinaryOperator
lhs: Expression
rhs: Expression
class braket.default_simulator.openqasm.parser.openqasm_ast.BinaryOperator(value)

Bases: Enum

An enumeration.

class braket.default_simulator.openqasm.parser.openqasm_ast.BitType(size: Expression | None = None)[source]

Bases: ClassicalType

Node representing the classical bit type, with an optional size.

Example:

bit[8]
creg[8]
size: Expression | None = None
class braket.default_simulator.openqasm.parser.openqasm_ast.BitstringLiteral(value: int, width: int)[source]

Bases: Expression

A literal bitstring value. The value is the numerical value of the bitstring, and the width is the number of digits given.

value: int
width: int
class braket.default_simulator.openqasm.parser.openqasm_ast.BoolType[source]

Bases: ClassicalType

Leaf node representing the Boolean classical type.

class braket.default_simulator.openqasm.parser.openqasm_ast.BooleanLiteral(value: bool)[source]

Bases: Expression

A boolean expression

Example:

true
false
value: bool
class braket.default_simulator.openqasm.parser.openqasm_ast.Box(duration: Expression | None, body: List[QuantumStatement])[source]

Bases: QuantumStatement

Timing box

Example:

box [maxdur] {
    delay[start_stretch] $0;
    x $0;
}
duration: Expression | None
body: List[QuantumStatement]
class braket.default_simulator.openqasm.parser.openqasm_ast.BranchingStatement(condition: Expression, if_block: List[Statement], else_block: List[Statement])[source]

Bases: Statement

Branch (if) statement

Example:

if (temp == 1) {
    ry(-pi / 2) scratch[0];
} else continue;
condition: Expression
if_block: List[Statement]
else_block: List[Statement]
class braket.default_simulator.openqasm.parser.openqasm_ast.BreakStatement[source]

Bases: Statement

Break statement

Example:

break;
class braket.default_simulator.openqasm.parser.openqasm_ast.CalibrationDefinition(name: Identifier, arguments: List[ClassicalArgument], qubits: List[Identifier], return_type: ClassicalType | None, body: str)[source]

Bases: Statement

Calibration definition

Example:

defcal rz(angle[20] theta) q {
    shift_phase drive(q), -theta;
}
name: Identifier
arguments: List[ClassicalArgument]
qubits: List[Identifier]
return_type: ClassicalType | None
body: str
class braket.default_simulator.openqasm.parser.openqasm_ast.CalibrationGrammarDeclaration(name: str)[source]

Bases: Statement

Calibration grammar declaration

Example:

defcalgrammar "openpulse";
name: str
class braket.default_simulator.openqasm.parser.openqasm_ast.Cast(type: ClassicalType, argument: Expression)[source]

Bases: Expression

A cast call expression

Example:

counts += int[1](b);
type: ClassicalType
argument: Expression
class braket.default_simulator.openqasm.parser.openqasm_ast.ClassicalArgument(type: ClassicalType, name: Identifier, access: AccessControl | None = None)[source]

Bases: QASMNode

Classical argument for a gate or subroutine declaration

type: ClassicalType
name: Identifier
access: AccessControl | None = None
class braket.default_simulator.openqasm.parser.openqasm_ast.ClassicalAssignment(lvalue: Identifier | IndexedIdentifier, op: AssignmentOperator, rvalue: Expression)[source]

Bases: Statement

Classical assignment

Example:

a[0] = 1;
lvalue: Identifier | IndexedIdentifier
op: AssignmentOperator
rvalue: Expression
class braket.default_simulator.openqasm.parser.openqasm_ast.ClassicalDeclaration(type: ClassicalType, identifier: Identifier, init_expression: Expression | QuantumMeasurement | None = None)[source]

Bases: Statement

Classical variable declaration

Example:

bit c;
type: ClassicalType
identifier: Identifier
init_expression: Expression | QuantumMeasurement | None = None
class braket.default_simulator.openqasm.parser.openqasm_ast.ClassicalType[source]

Bases: QASMNode

Base class for classical type

class braket.default_simulator.openqasm.parser.openqasm_ast.ComplexType(base_type: FloatType | None)[source]

Bases: ClassicalType

Complex ClassicalType. Its real and imaginary parts are based on other classical types.

Example:

complex[float]
complex[float[32]]
base_type: FloatType | None
class braket.default_simulator.openqasm.parser.openqasm_ast.Concatenation(lhs: Expression, rhs: Expression)[source]

Bases: Expression

Concatenation of two registers, for example:

a ++ b
a[2:3] ++ a[0:1]
lhs: Expression
rhs: Expression
class braket.default_simulator.openqasm.parser.openqasm_ast.ConstantDeclaration(type: ClassicalType, identifier: Identifier, init_expression: Expression)[source]

Bases: Statement

Constant declaration

Example:

const int[16] n = 10;
type: ClassicalType
identifier: Identifier
init_expression: Expression
class braket.default_simulator.openqasm.parser.openqasm_ast.ContinueStatement[source]

Bases: Statement

Continue statement

Example:

continue;
class braket.default_simulator.openqasm.parser.openqasm_ast.DelayInstruction(duration: Expression, qubits: List[IndexedIdentifier | Identifier])[source]

Bases: QuantumStatement

Delay instruction

Example:

delay[start_stretch] $0;
duration: Expression
qubits: List[IndexedIdentifier | Identifier]
class braket.default_simulator.openqasm.parser.openqasm_ast.DiscreteSet(values: List[Expression])[source]

Bases: QASMNode

A set of discrete values. This can be used for the values in a for loop, or to index certain values out of a register:

for i in {1, 2, 3} {}
let alias = qubits[{2, 3, 4}];
values: List[Expression]
class braket.default_simulator.openqasm.parser.openqasm_ast.DurationLiteral(value: float, unit: TimeUnit)[source]

Bases: Expression

A duration literal

Example:

1.0ns
value: float
unit: TimeUnit
class braket.default_simulator.openqasm.parser.openqasm_ast.DurationOf(target: List[Statement])[source]

Bases: Expression

Duration Of

Example:

durationof({x $0;})
target: List[Statement]
class braket.default_simulator.openqasm.parser.openqasm_ast.DurationType[source]

Bases: ClassicalType

Leaf node representing the duration type.

class braket.default_simulator.openqasm.parser.openqasm_ast.EndStatement[source]

Bases: Statement

End statement

Example:

end;
class braket.default_simulator.openqasm.parser.openqasm_ast.Expression[source]

Bases: QASMNode

An expression: anything that returns a value

class braket.default_simulator.openqasm.parser.openqasm_ast.ExpressionStatement(expression: Expression)[source]

Bases: Statement

A statement that contains a single expression

expression: Expression
class braket.default_simulator.openqasm.parser.openqasm_ast.ExternDeclaration(name: Identifier, arguments: List[ExternArgument], return_type: ExternArgument | None = None)[source]

Bases: Statement

A extern declaration

Example:

extern get_pauli(int[prec]) -> bit[2 * n];

get_pauli  // <- name
int[prec]  // <- classical type
bit[2 * n] // <- return type
name: Identifier
arguments: List[ExternArgument]
return_type: ExternArgument | None = None
class braket.default_simulator.openqasm.parser.openqasm_ast.FloatLiteral(value: float)[source]

Bases: Expression

An real number literal

Example:

1.1
value: float
class braket.default_simulator.openqasm.parser.openqasm_ast.FloatType(size: Expression | None = None)[source]

Bases: ClassicalType

Node representing the classical float type, with the particular IEEE-754 floating-point size optionally specified.

Example

float[16] float[64]

size: Expression | None = None
class braket.default_simulator.openqasm.parser.openqasm_ast.ForInLoop(type: ClassicalType, identifier: Identifier, set_declaration: RangeDefinition | DiscreteSet | Identifier, block: List[Statement])[source]

Bases: Statement

For in loop

Example:

for i in [0: 2] {
    majority a[i], b[i + 1], a[i + 1];
}
type: ClassicalType
identifier: Identifier
set_declaration: RangeDefinition | DiscreteSet | Identifier
block: List[Statement]
class braket.default_simulator.openqasm.parser.openqasm_ast.FunctionCall(name: Identifier, arguments: List[Expression])[source]

Bases: Expression

A function call expression

Example:

foo(1)

foo // <- name
name: Identifier
arguments: List[Expression]
braket.default_simulator.openqasm.parser.openqasm_ast.GateModifierName

alias of GateModifier

class braket.default_simulator.openqasm.parser.openqasm_ast.IODeclaration(io_identifier: IOKeyword, type: ClassicalType, identifier: Identifier)[source]

Bases: Statement

Input/output variable declaration

Exampe:

input angle[16] theta;
output bit select;
io_identifier: IOKeyword
type: ClassicalType
identifier: Identifier
class braket.default_simulator.openqasm.parser.openqasm_ast.IOKeyword(value)

Bases: Enum

An enumeration.

input = 1
output = 2
class braket.default_simulator.openqasm.parser.openqasm_ast.Identifier(name: str)[source]

Bases: Expression

An identifier

Example:

q1
name: str
class braket.default_simulator.openqasm.parser.openqasm_ast.Include(filename: str)[source]

Bases: Statement

An include statement

filename: str
class braket.default_simulator.openqasm.parser.openqasm_ast.IndexExpression(collection: Expression, index: DiscreteSet | List[Expression | RangeDefinition])[source]

Bases: Expression

An index expression.

Example:

q[1]
collection: Expression
index: DiscreteSet | List[Expression | RangeDefinition]
class braket.default_simulator.openqasm.parser.openqasm_ast.IndexedIdentifier(name: Identifier, indices: List[DiscreteSet | List[Expression | RangeDefinition]])[source]

Bases: QASMNode

An indentifier with index operators, such that it can be used as an lvalue. The list of indices is subsequent index brackets, so in:

a[{1, 2, 3}][0:1, 0:1]

the list of indices will have two elements. The first will be a DiscreteSet, and the second will be a list of two RangeDefinitions.

name: Identifier
indices: List[DiscreteSet | List[Expression | RangeDefinition]]
class braket.default_simulator.openqasm.parser.openqasm_ast.IntType(size: Expression | None = None)[source]

Bases: ClassicalType

Node representing a classical int (signed integer) type, with an optional precision.

Example

int[8] int[16]

size: Expression | None = None
class braket.default_simulator.openqasm.parser.openqasm_ast.IntegerLiteral(value: int)[source]

Bases: Expression

An integer literal

Example:

1
value: int
class braket.default_simulator.openqasm.parser.openqasm_ast.Pragma(command: str)[source]

Bases: QASMNode

Example:

#pragma val1 val2 val3
command: str
class braket.default_simulator.openqasm.parser.openqasm_ast.Program(statements: List[Statement], version: str | None = None)[source]

Bases: QASMNode

An entire OpenQASM 3 program represented by a list of top level statements

statements: List[Statement]
version: str | None = None
class braket.default_simulator.openqasm.parser.openqasm_ast.QASMNode[source]

Bases: object

Base class for all OpenQASM 3 nodes

span: Span | None = None

The span(location) of the node in the source code. Because not all the nodes are generated from source, the span is optional. To make it easier to write unit test, we exclude span from the generated __eq__().

class braket.default_simulator.openqasm.parser.openqasm_ast.QuantumArgument(name: Identifier, size: Expression | None = None)[source]

Bases: QASMNode

Quantum argument for a subroutine declaration

name: Identifier
size: Expression | None = None
class braket.default_simulator.openqasm.parser.openqasm_ast.QuantumBarrier(qubits: List[Expression])[source]

Bases: QuantumStatement

A quantum barrier instruction

Example:

barrier q;
qubits: List[Expression]
class braket.default_simulator.openqasm.parser.openqasm_ast.QuantumGate(modifiers: List[QuantumGateModifier], name: Identifier, arguments: List[Expression], qubits: List[IndexedIdentifier | Identifier], duration: Expression | None = None)[source]

Bases: QuantumStatement

Invoking a quantum gate

Example::

cx[dur] 0, 1;

or

ctrl @ p(λ) a, b;

ctrl @ // <- quantumGateModifier p // <- quantumGateName λ // <- argument a, b // <- qubit

modifiers: List[QuantumGateModifier]
name: Identifier
arguments: List[Expression]
qubits: List[IndexedIdentifier | Identifier]
duration: Expression | None = None
class braket.default_simulator.openqasm.parser.openqasm_ast.QuantumGateDefinition(name: Identifier, arguments: List[Identifier], qubits: List[Identifier], body: List[QuantumStatement])[source]

Bases: Statement

Define a new quantum gate

Example:

gate cx c, t {
    ctrl @ unitary(pi, 0, pi) c, t;
}
name: Identifier
arguments: List[Identifier]
qubits: List[Identifier]
body: List[QuantumStatement]
class braket.default_simulator.openqasm.parser.openqasm_ast.QuantumGateModifier(modifier: GateModifier, argument: Expression | None = None)[source]

Bases: QASMNode

A quantum gate modifier

modifier

‘inv’, ‘pow’, or ‘ctrl’

Type:

braket.default_simulator.openqasm.parser.openqasm_ast.GateModifier

expression

only pow modifier has expression.

Example:

inv @
pow(1/2)
ctrl
modifier: GateModifier
argument: Expression | None = None
class braket.default_simulator.openqasm.parser.openqasm_ast.QuantumMeasurement(qubit: IndexedIdentifier | Identifier)[source]

Bases: QASMNode

A quantum measurement instruction

Example:

measure q;
qubit: IndexedIdentifier | Identifier
class braket.default_simulator.openqasm.parser.openqasm_ast.QuantumMeasurementStatement(measure: QuantumMeasurement, target: IndexedIdentifier | Identifier | None)[source]

Bases: Statement

Stand-alone statement of a quantum measurement, potentially assigning the result to a classical variable. This is not the only statement that measure can appear in (it can also be in classical declaration statements and returns).

measure: QuantumMeasurement
target: IndexedIdentifier | Identifier | None
class braket.default_simulator.openqasm.parser.openqasm_ast.QuantumPhase(modifiers: List[QuantumGateModifier], argument: Expression, qubits: List[IndexedIdentifier | Identifier])[source]

Bases: QuantumStatement

A quantum phase instruction

Example:

ctrl @ gphase(λ) a;

ctrl @ // <- quantumGateModifier
λ // <- argument
a // <- qubit
modifiers: List[QuantumGateModifier]
argument: Expression
qubits: List[IndexedIdentifier | Identifier]
class braket.default_simulator.openqasm.parser.openqasm_ast.QuantumReset(qubits: IndexedIdentifier | Identifier)[source]

Bases: QuantumStatement

A reset instruction.

Example:

reset q;
qubits: IndexedIdentifier | Identifier
class braket.default_simulator.openqasm.parser.openqasm_ast.QuantumStatement[source]

Bases: Statement

Statements that may appear inside a gate declaration

class braket.default_simulator.openqasm.parser.openqasm_ast.QubitDeclaration(qubit: Identifier, size: Expression | None = None)[source]

Bases: Statement

Global qubit declaration

Example:

qubit q;
qubit[4] q;

q // <- qubit
4 // <- size
qubit: Identifier
size: Expression | None = None
class braket.default_simulator.openqasm.parser.openqasm_ast.RangeDefinition(start: Expression | None, end: Expression | None, step: Expression | None)[source]

Bases: QASMNode

Range definition.

Example:

1:2
1:1:10
:
start: Expression | None
end: Expression | None
step: Expression | None
class braket.default_simulator.openqasm.parser.openqasm_ast.ReturnStatement(expression: Expression | QuantumMeasurement | None = None)[source]

Bases: Statement

Classical or quantum return statement

Example:

return measure q;

return a + b
expression: Expression | QuantumMeasurement | None = None
class braket.default_simulator.openqasm.parser.openqasm_ast.SizeOf(target: Expression, index: Expression | None = None)[source]

Bases: Expression

sizeof an array’s dimensions.

target: Expression
index: Expression | None = None
class braket.default_simulator.openqasm.parser.openqasm_ast.Span(start_line: int, start_column: int, end_line: int, end_column: int)[source]

Bases: object

Start and end line/column in the source file We use the Antlr convention. The starting line number is 1 and starting column number is 0.

start_line: int
start_column: int
end_line: int
end_column: int
class braket.default_simulator.openqasm.parser.openqasm_ast.Statement[source]

Bases: QASMNode

A statement: anything that can appear on its own line

annotations: List[Annotation]
class braket.default_simulator.openqasm.parser.openqasm_ast.StretchType[source]

Bases: ClassicalType

Leaf node representing the stretch type.

class braket.default_simulator.openqasm.parser.openqasm_ast.SubroutineDefinition(name: Identifier, arguments: List[ClassicalArgument | QuantumArgument], body: List[Statement], return_type: ClassicalType | None = None)[source]

Bases: Statement

Subroutine definition

Example:

def measure(qubit q) -> bit {
    s q;
    h q;
    return measure q;
}
name: Identifier
arguments: List[ClassicalArgument | QuantumArgument]
body: List[Statement]
return_type: ClassicalType | None = None
class braket.default_simulator.openqasm.parser.openqasm_ast.TimeUnit(value)

Bases: Enum

An enumeration.

dt = 1
ns = 2
us = 3
ms = 4
s = 5
class braket.default_simulator.openqasm.parser.openqasm_ast.UintType(size: Expression | None = None)[source]

Bases: ClassicalType

Node representing a classical uint (unsigned integer) type, with an optional precision.

Example

uint[8] uint[16]

size: Expression | None = None
class braket.default_simulator.openqasm.parser.openqasm_ast.UnaryExpression(op: UnaryOperator, expression: Expression)[source]

Bases: Expression

A unary expression

Example:

~b
!bool
-i
op: UnaryOperator
expression: Expression
class braket.default_simulator.openqasm.parser.openqasm_ast.UnaryOperator(value)

Bases: Enum

An enumeration.

class braket.default_simulator.openqasm.parser.openqasm_ast.WhileLoop(while_condition: Expression, block: List[Statement])[source]

Bases: Statement

While loop

Example:

while(~success) {
    reset magic;
    ry(pi / 4) magic;
    success = distill(magic, scratch);
}
while_condition: Expression
block: List[Statement]