IRAST
Definition of the abstract syntax tree (AST) for Qadence 2 IR.
This module defines the abstract syntax tree (AST) in Qadence 2 IR. The AST is used in the front-
end to IR compilation. Implementations of IRBuilder
, defined in qadence2-ir.irbuilder
,
translate front-end code to the AST defined here. Then, the tools in qadence2-ir.factory_tools
can be used to manipulate an AST
instance and build valid IR instructions from the AST.
AST
Represents an instruction sequence as an abstract syntax tree (AST).
The initialization of this class must be done using the specific constructors.
Constructors:
- AST.numeric(value): For numerical values.
- AST.input_variable(name, size, trainable): For literal variables.
- AST.callable(fn_name, *args): For classical functions.
- AST.support(target, control): For qubit indices.
- AST.quantum_op(name, support, *args): For quantum operators with and without parameters.
- AST.sequence(*q_ops): For sequences of quantum operations.
- AST.add(lhs, rhs): For addition, lhs + rhs.
- AST.sub(lhs, rhs): For subtraction, lhs - rhs.
- AST.mul(lhs, rhs): For multiplication, lhs * rhs.
- AST.div(lhs, rhs): For division, lhs / rhs.
- AST.rem(lhs, rhs): For remainder, lhs % rhs.
- AST.pow(base, power): For power, base ** power.
__construct__(tag, head, *args, **attrs)
classmethod
Base constructor method.
To void arbitrary initialization, this class must be initialized with one of the standard
constructors provided. This method hides the initialization from the regular __new__
to
enforce that.
PARAMETER | DESCRIPTION |
---|---|
tag
|
A Tag indicating the AST type. Has one of the following values:
TYPE:
|
head
|
A string identifier of the AST object.
TYPE:
|
args
|
Optional arguments for specific constructors.
TYPE:
|
attrs
|
Optional attributes for specific constructors.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ast
|
A newly constructed AST object.
TYPE:
|
Source code in qadence2_ir/irast.py
add(lhs, rhs)
classmethod
callable(name, *args)
classmethod
div(lhs, rhs)
classmethod
input_variable(name, size, trainable, **attributes)
classmethod
Create an AST-input variable.
PARAMETER | DESCRIPTION |
---|---|
name
|
The variable's name.
TYPE:
|
size
|
Number of slots to be reserved for the variable, 1 for scalar values and n>1 for array variables.
TYPE:
|
trainable
|
A boolean flag to indicate if the variable is intend to be optimized or used as a constant during the run.
TYPE:
|
attributes
|
Extra flags, values or dictionaries that can provide more context to the backends.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ast
|
An input variable AST.
TYPE:
|
Source code in qadence2_ir/irast.py
mul(lhs, rhs)
classmethod
numeric(value)
classmethod
Create an AST-numeric object.
PARAMETER | DESCRIPTION |
---|---|
value
|
Numerical value to be converted in the Qadence2-IR AST.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ast
|
A numerical value AST.
TYPE:
|
Source code in qadence2_ir/irast.py
pow(base, power)
classmethod
quantum_op(name, target, control, *args, **attributes)
classmethod
Create an AST-quantum operator.
PARAMETER | DESCRIPTION |
---|---|
name
|
Operator's name.
TYPE:
|
target
|
A tuple of indices a quantum operator is acting on.
TYPE:
|
control
|
A tuple of indices a quantum operator uses as control qubits.
TYPE:
|
args
|
Arguments to be passed to parameteric quantum operators. Non-parametric operators like Puali gates are treated as a parametric operator with no arguments.
TYPE:
|
attributes
|
Extra flags, values or dictionaries that can provide more context to the backends.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ast
|
A quantum operator AST.
TYPE:
|
Source code in qadence2_ir/irast.py
rem(lhs, rhs)
classmethod
sequence(*quantum_operators)
classmethod
Create an AST-sequence of quantum operators objects.
PARAMETER | DESCRIPTION |
---|---|
quantum_operators
|
Sequence of quantum operators to be applied by the backend in the given order.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ast
|
An AST-sequence of quantum operators.
TYPE:
|
Source code in qadence2_ir/irast.py
sub(lhs, rhs)
classmethod
support(target, control)
classmethod
Create an AST-support object used to indicate to which qubits a quantum operation is applied.
PARAMETER | DESCRIPTION |
---|---|
target
|
A tuple of indices a quantum operator is acting on.
TYPE:
|
control
|
A tuple of indices a quantum operator uses as control qubits.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ast
|
A support AST.
TYPE:
|