microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.23.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

source/pip/qsharp/openqasm/_compile.py

98lines · modecode

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