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 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 defaultoutput_format (
OutputFormat) – Output format, see OutputFormat enum for available optionswasm_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 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 leveloutput_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, *, 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_IROutputFormat.BITCODEOutputFormat.BASE64