microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
source/pip/qsharp/openqasm/_circuit.py
84lines · 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 | from .._native import circuit_qasm_program # type: ignore |
| 9 | from .._qsharp import ( |
| 10 | get_interpreter, |
| 11 | ipython_helper, |
| 12 | Circuit, |
| 13 | CircuitConfig, |
| 14 | python_args_to_interpreter_args, |
| 15 | ) |
| 16 | from .. import telemetry_events |
| 17 | |
| 18 | |
| 19 | def circuit( |
| 20 | source: Optional[Union[str, Callable]] = None, |
| 21 | *args, |
| 22 | **kwargs: Any, |
| 23 | ) -> Circuit: |
| 24 | """ |
| 25 | Synthesizes a circuit for an OpenQASM program. Either a program string or |
| 26 | an operation must be provided. |
| 27 | |
| 28 | Args: |
| 29 | source (str): An OpenQASM program. Alternatively, a callable can be provided, |
| 30 | which must be an already imported global callable. |
| 31 | *args: The arguments to pass to the callable, if one is provided. |
| 32 | **kwargs: Additional keyword arguments to pass to the execution. |
| 33 | - name (str): The name of the program. This is used as the entry point for the program. |
| 34 | - search_path (Optional[str]): The optional search path for resolving file references. |
| 35 | Returns: |
| 36 | Circuit: The synthesized circuit. |
| 37 | |
| 38 | Raises: |
| 39 | QasmError: If there is an error generating, parsing, or analyzing the OpenQASM source. |
| 40 | QSharpError: If there is an error evaluating the program. |
| 41 | QSharpError: If there is an error synthesizing the circuit. |
| 42 | """ |
| 43 | |
| 44 | ipython_helper() |
| 45 | start = monotonic() |
| 46 | telemetry_events.on_circuit_qasm() |
| 47 | |
| 48 | max_operations = kwargs.pop("max_operations", None) |
| 49 | generation_method = kwargs.pop("generation_method", None) |
| 50 | source_locations = kwargs.pop("source_locations", None) |
| 51 | group_by_scope = kwargs.pop("group_by_scope", None) |
| 52 | config = CircuitConfig( |
| 53 | max_operations=max_operations, |
| 54 | generation_method=generation_method, |
| 55 | source_locations=source_locations, |
| 56 | group_by_scope=group_by_scope, |
| 57 | ) |
| 58 | |
| 59 | if isinstance(source, Callable) and hasattr(source, "__global_callable"): |
| 60 | args = python_args_to_interpreter_args(args) |
| 61 | res = get_interpreter().circuit( |
| 62 | config, callable=source.__global_callable, args=args |
| 63 | ) |
| 64 | else: |
| 65 | # remove any entries from kwargs with a None key or None value |
| 66 | kwargs = {k: v for k, v in kwargs.items() if k is not None and v is not None} |
| 67 | |
| 68 | if "search_path" not in kwargs: |
| 69 | kwargs["search_path"] = "." |
| 70 | |
| 71 | res = circuit_qasm_program( |
| 72 | source, |
| 73 | config, |
| 74 | read_file, |
| 75 | list_directory, |
| 76 | resolve, |
| 77 | fetch_github, |
| 78 | **kwargs, |
| 79 | ) |
| 80 | |
| 81 | durationMs = (monotonic() - start) * 1000 |
| 82 | telemetry_events.on_circuit_qasm_end(durationMs) |
| 83 | |
| 84 | return res |
| 85 | |