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