QXel.provider.braket.openqasm.parser.openqasm_ast module

Abstract Syntax Tree (openqasm3.ast)

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

class QXel.provider.braket.openqasm.parser.openqasm_ast.AccessControl(*values)

Bases: Enum

const = 1
mutable = 2
class QXel.provider.braket.openqasm.parser.openqasm_ast.AliasStatement(target, value)

Bases: Statement

Alias statement

Example:

let a = qubits[0];
target
value
class QXel.provider.braket.openqasm.parser.openqasm_ast.AngleType(size=None)

Bases: ClassicalType

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

Example:

angle[8]
angle[16]
size = None
class QXel.provider.braket.openqasm.parser.openqasm_ast.Annotation(keyword, command=None)

Bases: QASMNode

An annotation applied to a statment.

keyword
command = None
class QXel.provider.braket.openqasm.parser.openqasm_ast.ArrayLiteral(values)

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
class QXel.provider.braket.openqasm.parser.openqasm_ast.ArrayReferenceType(base_type, dimensions)

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
dimensions
class QXel.provider.braket.openqasm.parser.openqasm_ast.ArrayType(base_type, dimensions)

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
dimensions
class QXel.provider.braket.openqasm.parser.openqasm_ast.AssignmentOperator(*values)

Bases: Enum

class QXel.provider.braket.openqasm.parser.openqasm_ast.BinaryExpression(op, lhs, rhs)

Bases: Expression

A binary expression

Example:

q1 || q2
op
lhs
rhs
class QXel.provider.braket.openqasm.parser.openqasm_ast.BinaryOperator(*values)

Bases: Enum

class QXel.provider.braket.openqasm.parser.openqasm_ast.BitType(size=None)

Bases: ClassicalType

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

Example:

bit[8]
creg[8]
size = None
class QXel.provider.braket.openqasm.parser.openqasm_ast.BitstringLiteral(value, width)

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
width
class QXel.provider.braket.openqasm.parser.openqasm_ast.BoolType

Bases: ClassicalType

Leaf node representing the Boolean classical type.

class QXel.provider.braket.openqasm.parser.openqasm_ast.BooleanLiteral(value)

Bases: Expression

A boolean expression

Example:

true
false
value
class QXel.provider.braket.openqasm.parser.openqasm_ast.Box(duration, body)

Bases: QuantumStatement

Timing box

Example:

box [maxdur] {
    delay[start_stretch] $0;
    x $0;
}
duration
body
class QXel.provider.braket.openqasm.parser.openqasm_ast.BranchingStatement(condition, if_block, else_block)

Bases: Statement

Branch (if) statement

Example:

if (temp == 1) {
    ry(-pi / 2) scratch[0];
} else continue;
condition
if_block
else_block
class QXel.provider.braket.openqasm.parser.openqasm_ast.BreakStatement

Bases: Statement

Break statement

Example:

break;
class QXel.provider.braket.openqasm.parser.openqasm_ast.CalibrationDefinition(name, arguments, qubits, return_type, body)

Bases: Statement

Calibration definition

Example:

defcal rz(angle[20] theta) q {
    shift_phase drive(q), -theta;
}
name
arguments
qubits
return_type
body
class QXel.provider.braket.openqasm.parser.openqasm_ast.CalibrationGrammarDeclaration(name)

Bases: Statement

Calibration grammar declaration

Example:

defcalgrammar "openpulse";
name
class QXel.provider.braket.openqasm.parser.openqasm_ast.Cast(type, argument)

Bases: Expression

A cast call expression

Example:

counts += int[1](b);
type
argument
class QXel.provider.braket.openqasm.parser.openqasm_ast.ClassicalArgument(type, name, access=None)

Bases: QASMNode

Classical argument for a gate or subroutine declaration

type
name
access = None
class QXel.provider.braket.openqasm.parser.openqasm_ast.ClassicalAssignment(lvalue, op, rvalue)

Bases: Statement

Classical assignment

Example:

a[0] = 1;
lvalue
op
rvalue
class QXel.provider.braket.openqasm.parser.openqasm_ast.ClassicalDeclaration(type, identifier, init_expression=None)

Bases: Statement

Classical variable declaration

Example:

bit c;
type
identifier
init_expression = None
class QXel.provider.braket.openqasm.parser.openqasm_ast.ClassicalType

Bases: QASMNode

Base class for classical type

class QXel.provider.braket.openqasm.parser.openqasm_ast.ComplexType(base_type)

Bases: ClassicalType

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

Example:

complex[float]
complex[float[32]]
base_type
class QXel.provider.braket.openqasm.parser.openqasm_ast.Concatenation(lhs, rhs)

Bases: Expression

Concatenation of two registers, for example:

a ++ b
a[2:3] ++ a[0:1]
lhs
rhs
class QXel.provider.braket.openqasm.parser.openqasm_ast.ConstantDeclaration(type, identifier, init_expression)

Bases: Statement

Constant declaration

Example:

const int[16] n = 10;
type
identifier
init_expression
class QXel.provider.braket.openqasm.parser.openqasm_ast.ContinueStatement

Bases: Statement

Continue statement

Example:

continue;
class QXel.provider.braket.openqasm.parser.openqasm_ast.DelayInstruction(duration, qubits)

Bases: QuantumStatement

Delay instruction

Example:

delay[start_stretch] $0;
duration
qubits
class QXel.provider.braket.openqasm.parser.openqasm_ast.DiscreteSet(values)

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
class QXel.provider.braket.openqasm.parser.openqasm_ast.DurationLiteral(value, unit)

Bases: Expression

A duration literal

Example:

1.0ns
value
unit
class QXel.provider.braket.openqasm.parser.openqasm_ast.DurationOf(target)

Bases: Expression

Duration Of

Example:

durationof({x $0;})
target
class QXel.provider.braket.openqasm.parser.openqasm_ast.DurationType

Bases: ClassicalType

Leaf node representing the duration type.

class QXel.provider.braket.openqasm.parser.openqasm_ast.EndStatement

Bases: Statement

End statement

Example:

end;
class QXel.provider.braket.openqasm.parser.openqasm_ast.Expression

Bases: QASMNode

An expression: anything that returns a value

class QXel.provider.braket.openqasm.parser.openqasm_ast.ExpressionStatement(expression)

Bases: Statement

A statement that contains a single expression

expression
class QXel.provider.braket.openqasm.parser.openqasm_ast.ExternDeclaration(name, arguments, return_type=None)

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
arguments
return_type = None
class QXel.provider.braket.openqasm.parser.openqasm_ast.FloatLiteral(value)

Bases: Expression

An real number literal

Example:

1.1
value
class QXel.provider.braket.openqasm.parser.openqasm_ast.FloatType(size=None)

Bases: ClassicalType

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

Example

float[16] float[64]

size = None
class QXel.provider.braket.openqasm.parser.openqasm_ast.ForInLoop(type, identifier, set_declaration, block)

Bases: Statement

For in loop

Example:

for i in [0: 2] {
    majority a[i], b[i + 1], a[i + 1];
}
type
identifier
set_declaration
block
class QXel.provider.braket.openqasm.parser.openqasm_ast.FunctionCall(name, arguments)

Bases: Expression

A function call expression

Example:

foo(1)

foo // <- name
name
arguments
QXel.provider.braket.openqasm.parser.openqasm_ast.GateModifierName

alias of GateModifier

class QXel.provider.braket.openqasm.parser.openqasm_ast.IODeclaration(io_identifier, type, identifier)

Bases: Statement

Input/output variable declaration

Exampe:

input angle[16] theta;
output bit select;
io_identifier
type
identifier
class QXel.provider.braket.openqasm.parser.openqasm_ast.IOKeyword(*values)

Bases: Enum

input = 1
output = 2
class QXel.provider.braket.openqasm.parser.openqasm_ast.Identifier(name)

Bases: Expression

An identifier

Example:

q1
name
class QXel.provider.braket.openqasm.parser.openqasm_ast.Include(filename)

Bases: Statement

An include statement

filename
class QXel.provider.braket.openqasm.parser.openqasm_ast.IndexExpression(collection, index)

Bases: Expression

An index expression.

Example:

q[1]
collection
index
class QXel.provider.braket.openqasm.parser.openqasm_ast.IndexedIdentifier(name, indices)

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
indices
class QXel.provider.braket.openqasm.parser.openqasm_ast.IntType(size=None)

Bases: ClassicalType

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

Example

int[8] int[16]

size = None
class QXel.provider.braket.openqasm.parser.openqasm_ast.IntegerLiteral(value)

Bases: Expression

An integer literal

Example:

1
value
class QXel.provider.braket.openqasm.parser.openqasm_ast.Pragma

Bases: QASMNode

Example:

#pragma val1 val2 val3
command
class QXel.provider.braket.openqasm.parser.openqasm_ast.Program(statements, version=None)

Bases: QASMNode

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

statements
version = None
class QXel.provider.braket.openqasm.parser.openqasm_ast.QASMNode

Bases: object

Base class for all OpenQASM 3 nodes

span = 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 QXel.provider.braket.openqasm.parser.openqasm_ast.QuantumArgument(name, size=None)

Bases: QASMNode

Quantum argument for a subroutine declaration

name
size = None
class QXel.provider.braket.openqasm.parser.openqasm_ast.QuantumBarrier(qubits)

Bases: QuantumStatement

A quantum barrier instruction

Example:

barrier q;
qubits
class QXel.provider.braket.openqasm.parser.openqasm_ast.QuantumGate(modifiers, name, arguments, qubits, duration=None)

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
name
arguments
qubits
duration = None
class QXel.provider.braket.openqasm.parser.openqasm_ast.QuantumGateDefinition(name, arguments, qubits, body)

Bases: Statement

Define a new quantum gate

Example:

gate cx c, t {
    ctrl @ unitary(pi, 0, pi) c, t;
}
name
arguments
qubits
body
class QXel.provider.braket.openqasm.parser.openqasm_ast.QuantumGateModifier(modifier, argument=None)

Bases: QASMNode

A quantum gate modifier.

Example:

inv @
pow(1/2)
ctrl
modifier
argument = None
class QXel.provider.braket.openqasm.parser.openqasm_ast.QuantumMeasurement(qubit)

Bases: QASMNode

A quantum measurement instruction

Example:

measure q;
qubit
class QXel.provider.braket.openqasm.parser.openqasm_ast.QuantumMeasurementStatement(measure, target)

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
target
class QXel.provider.braket.openqasm.parser.openqasm_ast.QuantumPhase(modifiers, argument, qubits)

Bases: QuantumStatement

A quantum phase instruction

Example:

ctrl @ gphase(λ) a;

ctrl @ // <- quantumGateModifier
λ // <- argument
a // <- qubit
modifiers
argument
qubits
class QXel.provider.braket.openqasm.parser.openqasm_ast.QuantumReset(qubits)

Bases: QuantumStatement

A reset instruction.

Example:

reset q;
qubits
class QXel.provider.braket.openqasm.parser.openqasm_ast.QuantumStatement

Bases: Statement

Statements that may appear inside a gate declaration

class QXel.provider.braket.openqasm.parser.openqasm_ast.QubitDeclaration(qubit, size=None)

Bases: Statement

Global qubit declaration

Example:

qubit q;
qubit[4] q;

q // <- qubit
4 // <- size
qubit
size = None
class QXel.provider.braket.openqasm.parser.openqasm_ast.RangeDefinition(start, end, step)

Bases: QASMNode

Range definition.

Example:

1:2
1:1:10
:
start
end
step
class QXel.provider.braket.openqasm.parser.openqasm_ast.ReturnStatement(expression=None)

Bases: Statement

Classical or quantum return statement

Example:

return measure q;

return a + b
expression = None
class QXel.provider.braket.openqasm.parser.openqasm_ast.SizeOf(target, index=None)

Bases: Expression

sizeof an array’s dimensions.

target
index = None
class QXel.provider.braket.openqasm.parser.openqasm_ast.Span(start_line, start_column, end_line, end_column)

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
start_column
end_line
end_column
class QXel.provider.braket.openqasm.parser.openqasm_ast.Statement

Bases: QASMNode

A statement: anything that can appear on its own line

annotations
class QXel.provider.braket.openqasm.parser.openqasm_ast.StretchType

Bases: ClassicalType

Leaf node representing the stretch type.

class QXel.provider.braket.openqasm.parser.openqasm_ast.SubroutineDefinition(name, arguments, body, return_type=None)

Bases: Statement

Subroutine definition

Example:

def measure(qubit q) -> bit {
    s q;
    h q;
    return measure q;
}
name
arguments
body
return_type = None
class QXel.provider.braket.openqasm.parser.openqasm_ast.TimeUnit(*values)

Bases: Enum

dt = 1
ns = 2
us = 3
ms = 4
s = 5
class QXel.provider.braket.openqasm.parser.openqasm_ast.UintType(size=None)

Bases: ClassicalType

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

Example

uint[8] uint[16]

size = None
class QXel.provider.braket.openqasm.parser.openqasm_ast.UnaryExpression(op, expression)

Bases: Expression

A unary expression

Example:

~b
!bool
-i
op
expression
class QXel.provider.braket.openqasm.parser.openqasm_ast.UnaryOperator(*values)

Bases: Enum

class QXel.provider.braket.openqasm.parser.openqasm_ast.WhileLoop(while_condition, block)

Bases: Statement

While loop

Example:

while(~success) {
    reset magic;
    ry(pi / 4) magic;
    success = distill(magic, scratch);
}
while_condition
block