xir.Program¶
- class Program(version: str = '0.1.0', use_floats: bool = True)[source]¶
Bases:
object
Structured representation of an XIR program.
- Parameters
version (str) – Version number of the program. Must follow SemVer style (MAJOR.MINOR.PATCH).
use_floats (bool) – Whether floats and complex types are returned instead of
Decimal
andDecimalComplex
objects. Defaults toTrue
.
Attributes
Returns the functions that are called in the XIR program.
Returns the script-level constants declared in the XIR program.
Returns the declarations in the XIR program.
Returns the gates in the XIR program.
Returns the included XIR modules used by the XIR program.
Returns the observables in the XIR program.
Returns the script-level options declared in the XIR program.
Returns the statements in the XIR program.
Returns whether floats and complex types are used by the XIR program.
Returns the free parameter variables used when defining gates and observables in the XIR program.
Returns the version number of the XIR program.
Returns the wires of the XIR program.
- called_functions¶
Returns the functions that are called in the XIR program.
- Returns
collection of function names
- Return type
Collection[str]
- constants¶
Returns the script-level constants declared in the XIR program.
- Returns
declared script-level constants
- Return type
Mapping[str, Any]
- declarations¶
Returns the declarations in the XIR program.
- Returns
dictionary of declarations with the following keys: ‘gate’, ‘func’, ‘out’, and ‘obs’
- Return type
Mapping[str, Sequence[Declaration]]
- gates¶
Returns the gates in the XIR program.
- Returns
dictionary of gates, each one consisting of a name and a dictionary with the following keys: ‘parameters’, ‘wires’, and ‘statements’
- Return type
Mapping[str, Mapping[str, Sequence]]
- includes¶
Returns the included XIR modules used by the XIR program.
- Returns
sequence of included XIR modules
- Return type
Sequence[str]
- observables¶
Returns the observables in the XIR program.
- Returns
dictionary of observables, each one consisting of a name and a dictionary with the following keys: ‘parameters’, ‘wires’, and ‘statements’
- Return type
Mapping[str, Mapping[str, Sequence]]
- options¶
Returns the script-level options declared in the XIR program.
- Returns
declared script-level options
- Return type
Mapping[str, Any]
- statements¶
Returns the statements in the XIR program.
- Returns
sequence of statements
- Return type
Sequence[Statement]
- use_floats¶
Returns whether floats and complex types are used by the XIR program.
- Returns
whether floats and complex types are used
- Return type
bool
- variables¶
Returns the free parameter variables used when defining gates and observables in the XIR program.
- Returns
collection of free parameter variable names
- Return type
Collection[str]
- version¶
Returns the version number of the XIR program.
- Returns
version number
- Return type
str
- wires¶
Returns the wires of the XIR program.
- Returns
collection of wires
- Return type
Collection[Wire]
Methods
add_called_function
(name)Adds the name of a called function to the XIR program.
add_constant
(name, value)Adds a constant to the XIR program.
add_declaration
(decl)Adds a declaration to the XIR program.
add_gate
(name, params, wires, statements)Adds a gate to the XIR program.
add_include
(include)Adds an included XIR module to the XIR program.
add_observable
(name, params, wires, statements)Adds an observable to the XIR program.
add_option
(name, value)Adds an option to the XIR program.
add_statement
(statement)Adds a statement to the XIR program.
add_variable
(name)Adds the name of a free parameter variable to the XIR program.
Clears the includes of an XIR program.
merge
(*programs)Merges one or more XIR programs into a new XIR program.
resolve
(library, name)Resolves the includes of an XIR program using an XIR library.
search
(decl_type, attr_type, name)Searches an XIR program for the wires or parameters of a declaration.
serialize
([minimize])Serializes an XIR program to an XIR script.
- add_called_function(name: str) None [source]¶
Adds the name of a called function to the XIR program.
- Parameters
name (str) – name of the function
- add_constant(name: str, value: Any) None [source]¶
Adds a constant to the XIR program.
- Parameters
name (str) – name of the constant
value (Any) – value of the constant
- add_declaration(decl: Declaration) None [source]¶
Adds a declaration to the XIR program.
- Parameters
decl (Declaration) – the declaration
- add_gate(name: str, params: Sequence[str], wires: Sequence[Union[int, str]], statements: Sequence[Statement]) None [source]¶
Adds a gate to the XIR program.
- Parameters
name (str) – name of the gate
params (Sequence[str]) – parameters used in the gate
wires (Sequence[Wire]) – wires that the gate is applied to
statements (Sequence[Statement]) – statements that the gate applies
- add_include(include: str) None [source]¶
Adds an included XIR module to the XIR program.
- Parameters
include (str) – name of the XIR module
- add_observable(name: str, params: Sequence[str], wires: Sequence[Union[int, str]], statements: Sequence[ObservableStmt]) None [source]¶
Adds an observable to the XIR program.
- Parameters
name (str) – name of the observable
params (Sequence[str]) – parameters used in the observable
wires (Sequence[Wire]) – wires that the observable is applied to
statements (Sequence[ObservableStmt]) – statements that the observable applies
- add_option(name: str, value: Any) None [source]¶
Adds an option to the XIR program.
- Parameters
name (str) – name of the option
value (Any) – value of the option
- add_statement(statement: Statement) None [source]¶
Adds a statement to the XIR program.
- Parameters
statement (Statement) – the statement
- add_variable(name: str) None [source]¶
Adds the name of a free parameter variable to the XIR program.
- Parameters
name (str) – name of the variable
- static merge(*programs: Program) Program [source]¶
Merges one or more XIR programs into a new XIR program.
The merged XIR program is formed by concatenating the given XIR programs in the order they are passed to the function. Warnings may be issued for duplicate declarations, gates, includes, observables, and options.
- static resolve(library: Mapping[str, Program], name: str) Program [source]¶
Resolves the includes of an XIR program using an XIR library.
- Parameters
library (Mapping[str, Program]) – mapping from names to XIR programs
name (str) – name of the root XIR program to resolve
- Returns
XIR program obtained by recursively merging each XIR program starting from the root XIR program
- Return type
Example
Consider an XIR program
main
which includes another XIR programutil
:import xir # Create two simple XIR programs. main_program = xir.parse_script("use util; H2 | [0, 1];") util_program = xir.parse_script("gate H2, 0, 2; gate H2: H | [0]; H | [1]; end;")
The
main
XIR program can be resolved using# Define a library containing the XIR programs in the resolution scope. library = {"main": main_program, "util": util_program} # Resolve the imports from the main XIR program. resolved_main_program = xir.Program.resolve(library, "main") # Display the result. print(resolved_main_program.serialize())
The output is
gate H2: H | [0]; H | [1]; end; H2 | [0, 1];
- search(decl_type: str, attr_type: str, name: str) Sequence [source]¶
Searches an XIR program for the wires or parameters of a declaration.
- Parameters
decl_type (str) – type of the declaration (“gate”, “obs”, “out”, or “func”)
attr_type (str) – type of the attribute (“wires” or “params”)
name (str) – name of the decalaration
- Returns
wires or parameters of the declaration with the given name and type
- Return type
Sequence
- Raises
ValueError – if an invalid declaration or attribute type was provided or no declaration with the given name and type was found