microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
source/pip/qsharp/openqasm/_compile.py
95lines · modecode
| 1 | # Copyright (c) Microsoft Corporation. |
| 2 | # Licensed under the MIT License. |
| 3 | |
| 4 | from time import monotonic |
| 5 | from typing import Any, Callable, Dict, Optional, Union |
| 6 | from .._fs import read_file, list_directory, resolve |
| 7 | from .._http import fetch_github |
| 8 | |
| 9 | from .._native import ( # type: ignore |
| 10 | compile_qasm_program_to_qir, |
| 11 | ) |
| 12 | from .._qsharp import QirInputData, get_interpreter, ipython_helper, TargetProfile |
| 13 | from .. import telemetry_events |
| 14 | |
| 15 | |
| 16 | def compile( |
| 17 | source: Union[str, Callable], |
| 18 | *args, |
| 19 | **kwargs: Optional[Dict[str, Any]], |
| 20 | ) -> QirInputData: |
| 21 | """ |
| 22 | Compiles the OpenQASM source code into a program that can be submitted to a |
| 23 | target as QIR (Quantum Intermediate Representation). |
| 24 | Either a full program or a callable with arguments must be provided. |
| 25 | |
| 26 | Args: |
| 27 | source (str): An OpenQASM program. Alternatively, a callable can be provided, |
| 28 | which must be an already imported global callable. |
| 29 | *args: The arguments to pass to the callable, if one is provided. |
| 30 | **kwargs: Additional keyword arguments to pass to the compilation when source program is provided. |
| 31 | - name (str): The name of the circuit. This is used as the entry point for the program. |
| 32 | - target_profile (TargetProfile): The target profile to use for code generation. |
| 33 | - search_path (Optional[str]): The optional search path for resolving file references. |
| 34 | - output_semantics (OutputSemantics, optional): The output semantics for the compilation. |
| 35 | |
| 36 | Returns: |
| 37 | QirInputData: The compiled program. |
| 38 | |
| 39 | Raises: |
| 40 | QasmError: If there is an error generating, parsing, or analyzing the OpenQASM source. |
| 41 | QSharpError: If there is an error compiling the program. |
| 42 | |
| 43 | To get the QIR string from the compiled program, use `str()`. |
| 44 | |
| 45 | Example: |
| 46 | |
| 47 | .. code-block:: python |
| 48 | from qsharp.openqasm import compile |
| 49 | source = ... |
| 50 | program = compile(source) |
| 51 | with open('myfile.ll', 'w') as file: |
| 52 | file.write(str(program)) |
| 53 | """ |
| 54 | |
| 55 | ipython_helper() |
| 56 | start = monotonic() |
| 57 | |
| 58 | # This doesn't work the same way as the Q# compile function as it doesn't |
| 59 | # have access to the global configuration which has the target profile. |
| 60 | # Instead, we get the target profile from the kwargs and pass it to the telemetry event. |
| 61 | target_profile = kwargs.get("target_profile", "unspecified") |
| 62 | |
| 63 | telemetry_events.on_compile_qasm(target_profile) |
| 64 | |
| 65 | if isinstance(source, Callable) and hasattr(source, "__global_callable"): |
| 66 | if len(args) == 1: |
| 67 | args = args[0] |
| 68 | elif len(args) == 0: |
| 69 | args = None |
| 70 | ll_str = get_interpreter().qir( |
| 71 | entry_expr=None, callable=source.__global_callable, args=args |
| 72 | ) |
| 73 | else: |
| 74 | # remove any entries from kwargs with a None key or None value |
| 75 | kwargs = {k: v for k, v in kwargs.items() if k is not None and v is not None} |
| 76 | |
| 77 | if "search_path" not in kwargs: |
| 78 | kwargs["search_path"] = "." |
| 79 | if "target_profile" not in kwargs: |
| 80 | kwargs["target_profile"] = TargetProfile.Base |
| 81 | |
| 82 | ll_str = compile_qasm_program_to_qir( |
| 83 | source, |
| 84 | read_file, |
| 85 | list_directory, |
| 86 | resolve, |
| 87 | fetch_github, |
| 88 | **kwargs, |
| 89 | ) |
| 90 | res = QirInputData("main", ll_str) |
| 91 | |
| 92 | durationMs = (monotonic() - start) * 1000 |
| 93 | telemetry_events.on_compile_qasm_end(durationMs, target_profile) |
| 94 | |
| 95 | return res |
| 96 | |