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: Package | bytes, *, validate_qir: bool = True, validate_hugr: bool = False, target: str = 'quantinuum-hardware', opt_level: str = 'aggressive', max_loop_unroll: int = 800, output_format: OutputFormat = OutputFormat.BASE64, wasm_file: Path | None = None) str | bytes[source]¶
A function for converting hugr to qir (llvm bitcode)
- Parameters:
hugr (
Union[Package,bytes]) – HUGR in binary formatvalidate_qir (
bool) – Whether to validate the created QIRvalidate_hugr (
bool) – Whether to validate the input hugr before and after each internal passtarget (
str) – LLVM compilation target, same options as cli, run hugr-qir –help to see available options and defaultopt_level (
str) – LLVM optimization level, same options as cli, run hugr-qir –help to see available options and defaultmax_loop_unroll (
int) – Maximum statically-known loop trip count to fully unroll before QIR emissionoutput_format (
OutputFormat) – Output format, see OutputFormat enum for available optionswasm_file (
Optional[Path]) – Optional path to WASM binary file
- Return type:
Union[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 ahugr.package.Packageor serialized HUGR bytesvalidate_qir: run QIR validation after code generationvalidate_hugr: validate the input HUGR before and during compilationtarget: compilation target passed through to the Rust backendopt_level: LLVM optimization levelmax_loop_unroll: maximum static loop trip count to fully unroll (default:800)output_format:LLVM_IR,BITCODE, orBASE64wasm_file: optional path to a Wasm module used by the Wasm extension
Convenience helpers¶
- hugr_qir.hugr_to_qir.to_qir_str(hugr: Package | bytes, *, validate_qir: bool = True, max_loop_unroll: int = 800) str[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
max_loop_unroll (
int) – Maximum statically-known loop trip count to fully unroll
- Returns:
QIR corresponding to the HUGR input as str
- Return type:
str
- hugr_qir.hugr_to_qir.to_qir_bytes(hugr: Package | bytes, *, validate_qir: bool = True, max_loop_unroll: int = 800) bytes[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
max_loop_unroll (
int) – Maximum statically-known loop trip count to fully unroll
- Returns:
QIR corresponding to the HUGR input as bytes
- Return type:
bytes
Result conversion¶
- hugr_qir.h2_helpers.results.backendresult_to_qsysresult(backend_result: BackendResult) QsysResult[source]¶
Convert a pytket BackendResult into a QsysResult.
Register names produced by hugr-qir >= 0.3.0 contain the Guppy output type. Missing or malformed type information is interpreted as a signed 64-bit integer. Every register must contain exactly bits 0 through 63.
- Raises:
ResultConversionError – If a register does not contain exactly bits 0 through 63, distinct registers normalize to the same user tag, or an array result contains duplicate or incomplete indices.
- Parameters:
backend_result (
BackendResult)- Return type:
QsysResult
Output formats¶
The output format enum lives in python/hugr_qir/output.py.
OutputFormat.LLVM_IROutputFormat.BITCODEOutputFormat.BASE64