xir.Validator

class Validator(program: Program, ignore_includes: bool = False)[source]

Bases: object

Validator used to validate an XIR program.

Checks if an XIR program is correctly defined. While the XIR parser and transformer catch syntactic errors, the validator ensures that all statements are logically valid and consistent.

Parameters
  • program (Program) – the program which is to be validated

  • ignore_includes (bool) – whether to ignore any include statements and always check that all statements have their corresponding declarations in the same file

Example:

The Validator class is used to create a validator object which accepts a program to be validated. The validator can then be run by calling the run() method. Optionally, the user can set the raise_exception flag, which determines whether or not to raise a ValidationError or return None, or to simply return the error message log without raising an exception.

valid_script = """
    gate rx(a)[0];
    out amplitude(state)[0..2];

    rx(0.7) | [0];
    rx(1.2) | [1];

    amplitude(state: [1, 0]) | [0..2];
"""

prog = parse_script(valid_script)
xir.Validator(prog).run()

Optionally, the validator can be set to ignore any include statements, meaning that all gates, outputs, functions and observables must be declared in the same file.

validator = xir.Validator(prog, ignore_includes=True)
log = validator(raise_exception=False).run()

run([raise_exception])

Runs the validation checks.

run(raise_exception: bool = True) Optional[Sequence[str]][source]

Runs the validation checks.

Parameters

raise_exception (bool) – whether to raise a ValidationError if any issues are found

Returns

list of potential issues iff raise_exception is False and at least one validation error was detected

Return type

Sequence[str], None

Raises

ValidationError – if any issues are found and raise_exception is True