Factory Tools
Tools to process an AST
as used by the compiler factory in qadence2-ir.factory
.
This module defines a collection of functions that manipulate an AST
. Using these AST manipulation
tools the factory function in qadence2-ir.factory
can build the instruction list from a given AST.
build_instructions(ast)
Converts an AST into a list of Model
instructions.
PARAMETER | DESCRIPTION |
---|---|
ast
|
A parsed tree containing the sequence of instructions to be added to the
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
list[QuInstruct | Assign]
|
A list of quantum operations and temporary static single-assigned variables. Temporary |
list[QuInstruct | Assign]
|
variables store the outcomes of classical operations and are used as arguments for |
list[QuInstruct | Assign]
|
parametric quantum operations. |
Source code in qadence2_ir/factory_tools.py
extract_inputs_variables(ast)
Convert all the input variables in the AST into allocation instructions.
PARAMETER | DESCRIPTION |
---|---|
ast
|
A parsed tree containing the sequence of instructions to be added to the
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
dict[str, Alloc]
|
A dictionary with the variables names as keys and their respective allocation instructions |
dict[str, Alloc]
|
as values. |
Source code in qadence2_ir/factory_tools.py
filter_ast(predicate, ast)
Filters the elements of the AST according to the predicate
function.
PARAMETER | DESCRIPTION |
---|---|
predicate
|
A function that checks if a specific property is present in the
TYPE:
|
ast
|
A parsed AST containing the sequence of instructions to be added to the
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Iterable[AST]
|
An iterable and flattened version of the AST that contains the selected elements. |
Example:
>>> ast = AST.div(AST.numeric(2), AST.callable("fn", AST.numeric(3)))
>>> list(filter_ast(lambda x: x.is_numeric, ast))
[AST.numeric(2), AST.numeric(3)]
Source code in qadence2_ir/factory_tools.py
flatten_ast(ast)
Returns an iterable and flattened version of the AST.
PARAMETER | DESCRIPTION |
---|---|
ast
|
A parsed tree containing the sequence of instructions to be added to the
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Iterable[AST]
|
An iterable and flattened version of the AST. The arguments of operations/functions will |
Iterable[AST]
|
appear before the operation/function. |
Example:
>>> ast = AST.div(AST.numeric(2), AST.callable("fn", AST.numeric(3)))
>>> list(flatten_ast(ast))
[
AST.numeric(2),
AST.numeric(3),
AST.callable("fn", AST.numeric(3)),
AST.binar_op("/", AST.numeric(2), AST.callable("fn", AST.numeric(3))),
]
Source code in qadence2_ir/factory_tools.py
to_alloc(inputs, ast)
If the ast
is an input variable, add it to the inputs to be allocated if not present yet.
PARAMETER | DESCRIPTION |
---|---|
inputs
|
A dictionary containing pairs of variables and their allocation instructions, which are already allocated.
TYPE:
|
ast
|
A parsed tree containing the sequence of instructions to be added to the
TYPE:
|
Return
An updated dictionary containing pairs of variables and their allocation instructions. If
the ast
is an input variable, it is added to the dictionary. Otherwise, the dictionary is
returned unchanged.
Source code in qadence2_ir/factory_tools.py
to_instruct(ast, instructions_list, memoise, single_assign_index)
Adds the ast
to the instructions_list
if it is a Call
or QuInstruct
.
When the ast
is a classical function, it uses the single_assign_index
to assign the call to
a temporary variable using memoisation to avoid duplicated assignments. If the ast
is a
quantum instruction, the instruction will be added to the instruction list.
PARAMETER | DESCRIPTION |
---|---|
ast
|
A parsed tree containing the sequence of instructions to be added to the
TYPE:
|
instructions_list
|
A list of quantum operations and temporary static single-assigned variables.
TYPE:
|
memoise
|
A dictionary containing pairs of AST objects and the respective temporary variables they were assigned to. |
single_assign_index
|
The index to be used by the next temporary variable assignement. Tempmorary variables are labled from "%0" to "%n".
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
list[QuInstruct | Assign]
|
A tuple consisting of an updated list of instructions and assignments, a dictionary of pairs |
dict[AST, Load]
|
of AST objects and temporary variables, and the updated index for the next assignment. |