Python API

Main functions

The Python layer centers on python/hugr_qir/hugr_to_qir.py.

hugr_to_qir

hugr_qir.hugr_to_qir.hugr_to_qir(hugr, *, validate_qir=True, validate_hugr=False, target='quantinuum-hardware', opt_level='aggressive', output_format=OutputFormat.BASE64, wasm_file=None)[source]

A function for converting hugr to qir (llvm bitcode)

Parameters:
  • hugr (Package | bytes) – HUGR in binary format

  • validate_qir (bool) – Whether to validate the created QIR

  • validate_hugr (bool) – Whether to validate the input hugr before and after each internal pass

  • target (str) – LLVM compilation target, same options as cli, run hugr-qir –help to see available options and default

  • opt_level (str) – LLVM optimization level, same options as cli, run hugr-qir –help to see available options and default

  • output_format (OutputFormat) – Output format, see OutputFormat enum for available options

  • wasm_file (Path | None) – Optional path to WASM binary file

Return type:

str | bytes

Returns:

QIR corresponding to the HUGR input in format given by output_format

Example:

from hugr_qir.hugr_to_qir import hugr_to_qir
from hugr_qir.output import OutputFormat

bitcode = hugr_to_qir(hugr_package, output_format=OutputFormat.BITCODE)

Key parameters:

  • hugr: either a hugr.package.Package or serialized HUGR bytes

  • validate_qir: run QIR validation after code generation

  • validate_hugr: validate the input HUGR before and during compilation

  • target: compilation target passed through to the Rust backend

  • opt_level: LLVM optimization level

  • output_format: LLVM_IR, BITCODE, or BASE64

  • wasm_file: optional path to a Wasm module used by the Wasm extension

Convenience helpers

hugr_qir.hugr_to_qir.to_qir_str(hugr, *, validate_qir=True)[source]

Converts hugr package to qir str

Uses hugr_to_qir internally with default settings and llvm_ir output format.

Parameters:
  • hugr (Package) – hugr package

  • validate_qir (bool) – Whether to validate the created QIR

Returns:

QIR corresponding to the HUGR input as str

Return type:

str

hugr_qir.hugr_to_qir.to_qir_bytes(hugr, *, validate_qir=True)[source]

Converts hugr package to qir bytes.

Uses hugr_to_qir internally with default settings and bitcode output format.

Parameters:
  • hugr (Package) – hugr package

  • validate_qir (bool) – Whether to validate the created QIR

Returns:

QIR corresponding to the HUGR input as bytes

Return type:

bytes

Output formats

The output format enum lives in python/hugr_qir/output.py.

  • OutputFormat.LLVM_IR

  • OutputFormat.BITCODE

  • OutputFormat.BASE64