microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/pip/qsharp/openqasm/_import.py
73lines · 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` such that |
| 23 | the source becomes a Q# operation in the global namespace with parameters for any declared classical inputs and |
| 24 | paramters for each of the declared qubits, while any explicit or implicit output declarations become the return |
| 25 | type of the operation. |
| 26 | Alternatively, specifying `ProgramType.File` will treat the input source as a stand-alone program and create an |
| 27 | 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 | Args: |
| 34 | source (str): An OpenQASM program or fragment. |
| 35 | **kwargs: Additional keyword arguments to pass to the execution. |
| 36 | - name (str): The name of the program. This is used as the entry point for the program. |
| 37 | - search_path (Optional[str]): The optional search path for resolving file references. |
| 38 | - output_semantics (OutputSemantics, optional): The output semantics for the compilation. |
| 39 | - program_type (ProgramType, optional): The type of program compilation to perform. Defaults to `ProgramType.Operation`. |
| 40 | |
| 41 | Returns: |
| 42 | value: The value returned by the last statement in the source code. |
| 43 | |
| 44 | Raises: |
| 45 | QasmError: If there is an error generating, parsing, or analyzing the OpenQASM source. |
| 46 | QSharpError: If there is an error compiling the program. |
| 47 | """ |
| 48 | |
| 49 | ipython_helper() |
| 50 | |
| 51 | telemetry_events.on_import_qasm() |
| 52 | start_time = monotonic() |
| 53 | |
| 54 | # remove any entries from kwargs with a None key or None value |
| 55 | kwargs = {k: v for k, v in kwargs.items() if k is not None and v is not None} |
| 56 | |
| 57 | if "search_path" not in kwargs: |
| 58 | kwargs["search_path"] = "." |
| 59 | |
| 60 | res = get_interpreter().import_qasm( |
| 61 | source, |
| 62 | display_or_print, |
| 63 | read_file, |
| 64 | list_directory, |
| 65 | resolve, |
| 66 | fetch_github, |
| 67 | **kwargs, |
| 68 | ) |
| 69 | |
| 70 | durationMs = (monotonic() - start_time) * 1000 |
| 71 | telemetry_events.on_import_qasm_end(durationMs) |
| 72 | |
| 73 | return res |
| 74 | |