diff --git a/qlasskit/compiler/__init__.py b/qlasskit/compiler/__init__.py index eb71412a..247a4da3 100644 --- a/qlasskit/compiler/__init__.py +++ b/qlasskit/compiler/__init__.py @@ -13,6 +13,7 @@ # limitations under the License. # isort:skip_file +from typing import Literal, get_args from .expqmap import ExpQMap # noqa: F401 from .compiler import Compiler, CompilerException, optimizer # noqa: F401 @@ -22,13 +23,23 @@ from .tweedledumcompiler import TweedledumCompiler -def to_quantum(name, args, returns, exprs, compiler="int"): - if compiler == "int": - s = InternalCompiler() - elif compiler == "poc3": - s = POCCompiler3() - elif compiler == "tw": - s = TweedledumCompiler() +SupportedCompiler = Literal["internal", "poc3", "tweedledum"] +SupportedCompilers = list(get_args(SupportedCompiler)) + - circ = s.compile(name, args, returns, exprs) +def to_quantum(name, args, returns, exprs, compiler: SupportedCompiler = "internal"): + sel_compiler: Compiler + + if compiler == "internal": + sel_compiler = InternalCompiler() + elif compiler == "poc3": + sel_compiler = POCCompiler3() + elif compiler == "tweedledum": + sel_compiler = TweedledumCompiler() + else: + raise Exception( + f"Compiler {compiler} not supported. Choose one between {SupportedCompilers}" + ) + + circ = sel_compiler.compile(name, args, returns, exprs) return circ diff --git a/qlasskit/qlassf.py b/qlasskit/qlassf.py index 33e4a187..f038a6e1 100644 --- a/qlasskit/qlassf.py +++ b/qlasskit/qlassf.py @@ -21,9 +21,9 @@ from sympy import Symbol from sympy.logic.boolalg import Boolean -from . import compiler from .ast2ast import ast2ast from .ast2logic import Arg, Args, BoolExpList, LogicFun, flatten, translate_ast +from .compiler import SupportedCompiler, to_quantum from .types import * # noqa: F403, F401 from .types import Qtype @@ -196,12 +196,13 @@ def truth_table(self, max=None) -> List[List[bool]]: return truth - def compile(self): - self._compiled_gate = compiler.to_quantum( + def compile(self, compiler: SupportedCompiler = "internal"): + self._compiled_gate = to_quantum( name=self.name, args=self.args, returns=self.returns, exprs=self.expressions, + compiler=compiler, ) def circuit(self): @@ -258,14 +259,16 @@ def from_function( types: List[Qtype] = [], defs: List[LogicFun] = [], to_compile: bool = True, + compiler: SupportedCompiler = "internal", ) -> "QlassF": """Create a QlassF from a function or a string containing a function Args: f (Union[str, Callable]): the function to be parsed, as a str code or callable types (List[Qtype]): list of qtypes to inject - to_compile (boolean, optional): if True, compile to quantum circuit (default: True) defs (List[LogicFun]): list of LogicFun to inject + to_compile (boolean, optional): if True, compile to quantum circuit (default: True) + compiler (SupportedCompiler, optional): override default compiler (default: internal) """ if isinstance(f, str): exec(f) @@ -286,7 +289,7 @@ def from_function( qf = QlassF(fun_name, original_f, args, fun_ret, exps) if to_compile: - qf.compile() + qf.compile(compiler) return qf @@ -295,15 +298,17 @@ def qlassf( types: List[Qtype] = [], defs: List[QlassF] = [], to_compile: bool = True, + compiler: SupportedCompiler = "internal", ) -> QlassF: """Decorator / function creating a QlassF object Args: f (Union[str, Callable]): the function to be parsed, as a str code or callable types (List[Qtype]): list of qtypes to inject - to_compile (boolean, optional): if True, compile to quantum circuit (default: True) defs (List[Qlassf]): list of qlassf to inject + to_compile (boolean, optional): if True, compile to quantum circuit (default: True) + compiler (SupportedCompiler, optional): override default compiler (default: internal) """ defs_fun = list(map(lambda q: q.to_logicfun(), defs)) - return QlassF.from_function(f, types, defs_fun, to_compile) + return QlassF.from_function(f, types, defs_fun, to_compile, compiler)