qat.frontend.qasm module

class BaseQasmFrontend(model=None, pipeline=None)

Bases: BaseFrontend, ABC

A base frontend for QASM programs.

Handles the parsing of QASM into QAT’s intermediate representation (IR). Optionally, it can also run pipelines before to optimize and validate QASM files. The QASM2 and QASM3 frontends are identical up to the language and its respective parser. This class implements the base functionality.

Parameters:
  • model (Union[QuantumHardwareModel, PhysicalHardwareModel, None]) – The hardware model can be required for the pipeline and is used in parsing.

  • pipeline (Optional[PassManager]) – A pipeline for validation and optimising QASM, defaults to a predefined pipeline that optimizes the QASM file.

static build_pass_pipeline(model)

Creates a pipeline to optimize QASM files.

Parameters:

model (QuantumHardwareModel) – The hardware model is needed for optimizations.

Return type:

PassManager

Returns:

The pipeline as a pass manager.

check_and_return_source(src)

Checks that the source program (or file path) can be interpreted as a QASM file by checking against the language.

Parameters:

src (str) – The QASM program, or the file path to the program.

Return type:

bool | str

Returns:

If the program is determined to not be valid, False is returned. Otherwise, the program is returned (and loaded if required).

emit(src, res_mgr=None, met_mgr=None, compiler_config=None)

Compiles the source QASM program into QAT’s intermediate representation.

Parameters:
  • src (str) – The source program, or path to the source program.

  • res_mgr (Optional[ResultManager]) – The result manager to store results from passes within the pipeline, defaults to an empty ResultManager.

  • met_mgr (Optional[MetricsManager]) – The metrics manager to store metrics, such as the optimized QASM circuit, defaults to an empty MetricsManager.

  • compiler_config (Optional[CompilerConfig]) – The compiler config is used in both the pipeline and for parsing.

Raises:

ValueError – An error is thrown if the the program is not detected to be a QASM program.

Return type:

InstructionBuilder

Returns:

The program as an InstructionBuilder.

abstract property version: int
class Qasm2Frontend(model, pipeline=None)

Bases: BaseQasmFrontend

A frontend for QASM2 programs.

Handles the parsing of QASM2 into QATs intermediate representation. Optionally, it can also run pipelines before to optimize and validate QASM2 files.

Parameters:
  • model (QuantumHardwareModel) – The hardware model can be required for the pipeline and is used in parsing.

  • pipeline (Optional[PassManager]) – A pipeline for validation and optimising QASM, defaults to a predefined pipeline that optimizes the QASM file.

version = 2
class Qasm3Frontend(model, pipeline=None)

Bases: BaseQasmFrontend

A frontend for QASM3 programs.

Handles the parsing of QASM3 into QATs intermediate representation. Optionally, it can also run pipelines before to optimize and validate QASM3 files.

Parameters:
  • model (QuantumHardwareModel) – The hardware model can be required for the pipeline and is used in parsing.

  • pipeline (Optional[PassManager]) – A pipeline for validation and optimising QASM, defaults to a predefined pipeline that optimizes the QASM file.

version = 3
get_qasm_version(src)

Extracts the QASM version from the source file.

Param:

The source program as a string.

Return type:

int

Returns:

The major version number.

is_qasm_path(src)

Determines if the string corresponds to a path, and has a .qasm extension.

Parameters:

src (str) – The path to the source program.

Return type:

bool

is_qasm_str(src)

Determines if the string contains OPENQASM headers.

Parameters:

src (str) – The source program as a string.

Return type:

bool

load_qasm_file(path)

Loads a Qasm file from a given file path.

Parameters:

path (str) – The file path.

Raises:

ValueError – Raises an error if the the file has an invalid extension.

Return type:

str

Returns:

The Qasm program as a string or bytes.