Skip to content

Commit

Permalink
parametrize compiler selection
Browse files Browse the repository at this point in the history
  • Loading branch information
dakk committed Oct 29, 2023
1 parent af14461 commit 661ee94
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
27 changes: 19 additions & 8 deletions qlasskit/compiler/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
19 changes: 12 additions & 7 deletions qlasskit/qlassf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand All @@ -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


Expand All @@ -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)

0 comments on commit 661ee94

Please sign in to comment.