microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/pip/qsharp/openqasm/_import.py
72lines · modecode
| 1 | # Copyright (c) Microsoft Corporation. |
| 2 | # Licensed under the MIT License. |
| 3 | |
| 4 | from time import monotonic |
| 5 | from typing import Any |
| 6 | |
| 7 | from ._ipython import display_or_print |
| 8 | from .._fs import read_file, list_directory, resolve |
| 9 | from .._http import fetch_github |
| 10 | from .._qsharp import ( |
| 11 | get_interpreter, |
| 12 | ipython_helper, |
| 13 | ) |
| 14 | from .. import telemetry_events |
| 15 | |
| 16 | |
| 17 | def import_openqasm( |
| 18 | source: str, |
| 19 | **kwargs: Any, |
| 20 | ) -> Any: |
| 21 | """ |
| 22 | Imports OpenQASM source code into the active QDK interpreter. By default, import uses ``ProgramType.Operation`` |
| 23 | such that the source becomes a Q# operation in the global namespace with parameters for any declared classical |
| 24 | inputs and parameters for each of the declared qubits, while any explicit or implicit output declarations become |
| 25 | the return type of the operation. |
| 26 | Alternatively, specifying ``ProgramType.File`` will treat the input source as a stand-alone program and create |
| 27 | an operation in the ``qasm_import`` namespace that only takes classical parameters, allocates the required qubits |
| 28 | internally and releases them at the end of the operation. |
| 29 | Finally, using ``ProgramType.Fragments`` executes the provided source in the current interactive interpreter, |
| 30 | defining any declared variables or operations in the current scope and returning the value of the last statement |
| 31 | in the source. |
| 32 | |
| 33 | :param source: An OpenQASM program or fragment. |
| 34 | :type source: str |
| 35 | :param **kwargs: Additional keyword arguments. Common options: |
| 36 | |
| 37 | - ``name`` (str): The name of the program. This is used as the entry point for the program. |
| 38 | - ``search_path`` (str): The optional search path for resolving file references. |
| 39 | - ``output_semantics`` (OutputSemantics): The output semantics for the compilation. |
| 40 | - ``program_type`` (ProgramType): The type of program compilation to perform. |
| 41 | Defaults to ``ProgramType.Operation``. |
| 42 | :return: The value returned by the last statement in the source code. |
| 43 | :rtype: Any |
| 44 | :raises QasmError: If there is an error generating, parsing, or analyzing the OpenQASM source. |
| 45 | :raises QSharpError: If there is an error compiling the program. |
| 46 | """ |
| 47 | |
| 48 | ipython_helper() |
| 49 | |
| 50 | telemetry_events.on_import_qasm() |
| 51 | start_time = monotonic() |
| 52 | |
| 53 | # remove any entries from kwargs with a None key or None value |
| 54 | kwargs = {k: v for k, v in kwargs.items() if k is not None and v is not None} |
| 55 | |
| 56 | if "search_path" not in kwargs: |
| 57 | kwargs["search_path"] = "." |
| 58 | |
| 59 | res = get_interpreter().import_qasm( |
| 60 | source, |
| 61 | display_or_print, |
| 62 | read_file, |
| 63 | list_directory, |
| 64 | resolve, |
| 65 | fetch_github, |
| 66 | **kwargs, |
| 67 | ) |
| 68 | |
| 69 | durationMs = (monotonic() - start_time) * 1000 |
| 70 | telemetry_events.on_import_qasm_end(durationMs) |
| 71 | |
| 72 | return res |
| 73 | |