[docs]defhugr_to_qir(# noqa: PLR0913hugr:Package|bytes,*,validate_qir:bool=True,validate_hugr:bool=False,target:str=compile_target_default(),opt_level:str=opt_level_default(),output_format:OutputFormat=OutputFormat.BASE64,wasm_file:Path|None=None,)->str|bytes:"""A function for converting hugr to qir (llvm bitcode) :param hugr: HUGR in binary format :param validate_qir: Whether to validate the created QIR :param validate_hugr: Whether to validate the input hugr before and after each internal pass :param target: LLVM compilation target, same options as cli, run hugr-qir --help to see available options and default :param opt_level: LLVM optimization level, same options as cli, run hugr-qir --help to see available options and default :param output_format: Output format, see OutputFormat enum for available options :param wasm_file: Optional path to WASM binary file :returns: QIR corresponding to the HUGR input in format given by `output_format` """withtempfile.TemporaryDirectory(ignore_cleanup_errors=True)astmp_dir:hugr_bytes:bytestmp_infile_path=Path(f"{tmp_dir}/tmp.hugr")# noqa: S108tmp_outfile_path=Path(f"{tmp_dir}/tmp_output")# noqa: S108iftype(hugr)isbytes:hugr_bytes=hugrelse:asserttype(hugr)isPackage# noqa: S101hugr_bytes=hugr.to_bytes()withPath.open(tmp_infile_path,"wb")ascli_input:cli_input.write(hugr_bytes)hugr_qir_impl(validate_qir,validate_hugr,target,opt_level,output_format,tmp_infile_path,tmp_outfile_path,wasm_file,)read_mode="rb"ifoutput_format==OutputFormat.BITCODEelse"r"withPath.open(tmp_outfile_path,mode=read_mode)ascli_output:returncli_output.read()
[docs]defto_qir_str(hugr:Package|bytes,*,validate_qir:bool=True)->str:""" Converts hugr package to qir str Uses hugr_to_qir internally with default settings and llvm_ir output format. :param hugr: hugr package :type hugr: Package :param validate_qir: Whether to validate the created QIR :type validate_qir: bool :return: QIR corresponding to the HUGR input as str :rtype: str """qir_str=hugr_to_qir(hugr,output_format=OutputFormat.LLVM_IR,validate_qir=validate_qir)assertisinstance(qir_str,str)# noqa: S101returnqir_str
[docs]defto_qir_bytes(hugr:Package|bytes,*,validate_qir:bool=True)->bytes:""" Converts hugr package to qir bytes. Uses hugr_to_qir internally with default settings and bitcode output format. :param hugr: hugr package :type hugr: Package :param validate_qir: Whether to validate the created QIR :type validate_qir: bool :return: QIR corresponding to the HUGR input as bytes :rtype: bytes """qir_bytes=hugr_to_qir(hugr,output_format=OutputFormat.BITCODE,validate_qir=validate_qir)assertisinstance(qir_bytes,bytes)# noqa: S101returnqir_bytes