xir.Transformer¶
- class Transformer(*args, **kwargs)[source]¶
Bases:
TransformerTransformer for processing the Lark parse tree.
Transformers visit each node of the tree, and run the appropriate method on it according to the node’s data. All method names mirror the corresponding symbols from the grammar.
- Keyword Arguments
eval_pi (bool) – Whether pi should be evaluated and stored as a float instead of symbolically as a string. Defaults to
False.use_floats (bool) – Whether floats and complex types are returned instead of
DecimalandDecimalComplexobjects. Defaults toTrue.
Attributes
Reports whether pi is evaluated and stored as a float.
Reports whether floats and complex types are used.
- eval_pi¶
Reports whether pi is evaluated and stored as a float.
- use_floats¶
Reports whether floats and complex types are used.
Methods
Arbitrary number of wires.
FALSE_(_)Returns False
PI(_)Mathematical constant pi.
TRUE_(_)Returns True
add(args)Addition operation.
application_stmt(args)Application statement.
bool_(b)Boolean expressions
circuit(args)Any statement that is not an include.
constants(args)Script-level constants.
div(args)Division operation.
expr(args)Catch-all for expressions
float_(d)Floating point numbers
func_decl(name, params)Function declaration.
gate_decl(name, params, wires)Gate declaration.
gate_def(name, params_list, wires, *stmts)Gate definition.
imag_(c)Imaginary numbers
include(args)Includes an external XIR script.
int_(n)Signed integers
math_op(args)Mathemetical operation.
name(n)Name of variable, gate, observable, measurement type, option, external file, observable, wire, mathematical operation, etc.
neg(args)Negation operation.
obs_decl(name, params, wires)Observable declaration.
obs_def(name, params, wires, statements)Create an observable definition.
obs_factor(name, params, wires)Create
ObservableFactorfrom name, params and wires.obs_group(factors)Observable Factors
obs_stmt(pref, factors)Create an
ObservableStmtfrom prefactor and factors.obs_stmt_list(stmts)Observable statement list
out_decl(name, params, wires)Output declaration.
params_dict(p)Tuple with dictionary of params and identifier
params_list(p)Tuple with list of params and identifier
path(args)Path to an included XIR script.
prod(args)Product operation.
program(args)Root of AST containing include statements and the main circuit.
range_(args)Range between two signed integers
script_options(args)Script-level options.
sub(args)Subtraction operation.
transform(tree)Transform the given tree, and return the final result
uint(n)Unsigned integers
var(v)String expressions that can be substituted by values at a later stage.
wire_list(args)Tuple of wires.
wires(w)Tuple with wires and identifier
- PI(_)[source]¶
Mathematical constant pi.
- Returns
value of pi or string ‘PI’
- Return type
Union[Decimal, str]
- add(args)[source]¶
Addition operation.
- Returns
resulting value after applied operation or string representation of operation if expression contains string variables
- Return type
Union[number, str]
- application_stmt(args)[source]¶
Application statement. Can be either a gate statement or an output statement and is defined either directly in the circuit or inside a gate definition.
- Returns
statement with the given data
- Return type
- circuit(args)[source]¶
Any statement that is not an include. Appends gate and output statements to the program.
- div(args)[source]¶
Division operation.
- Returns
resulting value after applied operation or string representation of operation if expression contains string variables
- Return type
Union[number, str]
- gate_def(name, params_list, wires, *stmts)[source]¶
Gate definition. Starts with keyword ‘gate’. Adds gate to program.
- math_op(args)[source]¶
Mathemetical operation. Adds operation to the program.
- Returns
string representation of operation
- Return type
str
- name(n)[source]¶
Name of variable, gate, observable, measurement type, option, external file, observable, wire, mathematical operation, etc.
- neg(args)[source]¶
Negation operation.
- Returns
resulting value after applied operation or string representation of operation if expression contains string variables
- Return type
Union[number, str]
- obs_def(name, params, wires, statements)[source]¶
Create an observable definition.
Creates an observable definition from text of the form:
obs my_obs(params)[0, 1]: 1, obs_1[0]; 0.5, obs_2[1]; end;- Parameters
name – observable name
params – observable params
wires – observable wires
statements – list of statements
- prod(args)[source]¶
Product operation.
- Returns
resulting value after applied operation or string representation of operation if expression contains string variables
- Return type
Union[number, str]
- program(args)[source]¶
Root of AST containing include statements and the main circuit.
- Returns
program containing all parsed data
- Return type