xir.Transformer

class Transformer(*args, **kwargs)[source]

Bases: Transformer

Transformer 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 Decimal and DecimalComplex objects. Defaults to True.

eval_pi

Reports whether pi is evaluated and stored as a float.

use_floats

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.

ARBITRARY_NUM_WIRES(_)

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 ObservableFactor from name, params and wires.

obs_group(factors)

Observable Factors

obs_stmt(pref, factors)

Create an ObservableStmt from 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

ARBITRARY_NUM_WIRES(_)[source]

Arbitrary number of wires.

FALSE_(_)[source]

Returns False

PI(_)[source]

Mathematical constant pi.

Returns

value of pi or string ‘PI’

Return type

Union[Decimal, str]

TRUE_(_)[source]

Returns True

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

Statement

bool_(b)[source]

Boolean expressions

circuit(args)[source]

Any statement that is not an include. Appends gate and output statements to the program.

constants(args)[source]

Script-level constants. Adds any constant 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]

expr(args)[source]

Catch-all for expressions

float_(d)[source]

Floating point numbers

func_decl(name, params)[source]

Function declaration. Adds function declaration to program.

gate_decl(name, params, wires)[source]

Gate declaration. Adds declaration to program.

gate_def(name, params_list, wires, *stmts)[source]

Gate definition. Starts with keyword ‘gate’. Adds gate to program.

imag_(c)[source]

Imaginary numbers

include(args)[source]

Includes an external XIR script.

int_(n)[source]

Signed integers

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_decl(name, params, wires)[source]

Observable declaration. Adds declaration to program.

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

obs_factor(name, params, wires)[source]

Create ObservableFactor from name, params and wires.

obs_group(factors)[source]

Observable Factors

obs_stmt(pref, factors)[source]

Create an ObservableStmt from prefactor and factors.

obs_stmt_list(stmts)[source]

Observable statement list

out_decl(name, params, wires)[source]

Output declaration. Adds declaration to program.

params_dict(p)[source]

Tuple with dictionary of params and identifier

params_list(p)[source]

Tuple with list of params and identifier

path(args)[source]

Path to an included XIR script.

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

Program

range_(args)[source]

Range between two signed integers

script_options(args)[source]

Script-level options. Adds any options to the program.

sub(args)[source]

Subtraction operation.

Returns

resulting value after applied operation or string representation of operation if expression contains string variables

Return type

Union[number, str]

transform(tree)[source]

Transform the given tree, and return the final result

uint(n)[source]

Unsigned integers

var(v)[source]

String expressions that can be substituted by values at a later stage.

wire_list(args)[source]

Tuple of wires.

wires(w)[source]

Tuple with wires and identifier