Skip to content

Commit

Permalink
make tweedledum optional
Browse files Browse the repository at this point in the history
  • Loading branch information
dakk committed Oct 30, 2023
1 parent a69fbd0 commit 81ac9e0
Show file tree
Hide file tree
Showing 15 changed files with 124 additions and 123 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,28 @@ jobs:
- name: Install dependencies
run: pip install tox

- name: Run tests
run: tox -e unit-tests --override testenv.deps+=tweedledum --override testenv.extras=tweedledum

unit-tests-notw:
runs-on: ubuntu-latest

strategy:
matrix:
python-version: ["3.11", "3.12"]

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: pip install tox

- name: Run tests
run: tox -e unit-tests

Expand Down
4 changes: 2 additions & 2 deletions qlasskit/ast2logic/t_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ def to_name(a):
ttypes: List[TType] = []

if sys.version_info < (3, 9):
_elts = ann.slice.value.elts # type: ignore
_elts = ann.slice.value.elts # type: ignore
else:
_elts = ann.slice.elts # type: ignore
_elts = ann.slice.elts # type: ignore

for i in _elts: # type: ignore
if isinstance(i, ast.Name) and to_name(i) == "bool":
Expand Down
8 changes: 4 additions & 4 deletions qlasskit/ast2logic/t_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ def translate_expression(expr, env: Env) -> TExp: # noqa: C901

def unroll_subscripts(sub, st):
if sys.version_info < (3, 9):
_sval = sub.slice.value # type: ignore
_sval = sub.slice.value # type: ignore
else:
_sval = sub.slice # type: ignore
_sval = sub.slice # type: ignore

if isinstance(sub.value, ast.Subscript):
st = f"{_sval.value}{'.' if st else ''}{st}"
Expand All @@ -67,9 +67,9 @@ def unroll_subscripts(sub, st):
raise exceptions.ExpressionNotHandledException(expr)

if sys.version_info < (3, 9):
_sval = expr.slice.value # type: ignore
_sval = expr.slice.value # type: ignore
else:
_sval = expr.slice # type: ignore
_sval = expr.slice # type: ignore

if not isinstance(_sval, ast.Constant):
raise exceptions.ExpressionNotHandledException(expr)
Expand Down
20 changes: 16 additions & 4 deletions qlasskit/compiler/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,26 @@
from .expqmap import ExpQMap # noqa: F401
from .compiler import Compiler, CompilerException, optimizer # noqa: F401

from .internalcompiler import InternalCompiler
from .poccompiler3 import POCCompiler3
from .tweedledumcompiler import TweedledumCompiler
try:
import tweedledum # noqa: F401

TWEEDLEDUM_ENABLED = True
except:
TWEEDLEDUM_ENABLED = False

from .internalcompiler import InternalCompiler # noqa: E402
from .poccompiler3 import POCCompiler3 # noqa: E402

if TWEEDLEDUM_ENABLED:
from .tweedledumcompiler import TweedledumCompiler


SupportedCompiler = Literal["internal", "poc3", "tweedledum"]
SupportedCompilers = list(get_args(SupportedCompiler))

if not TWEEDLEDUM_ENABLED:
SupportedCompilers.remove("tweedledum")


def to_quantum(name, args, returns, exprs, compiler: SupportedCompiler = "internal"):
sel_compiler: Compiler
Expand All @@ -34,7 +46,7 @@ def to_quantum(name, args, returns, exprs, compiler: SupportedCompiler = "intern
sel_compiler = InternalCompiler()
elif compiler == "poc3":
sel_compiler = POCCompiler3()
elif compiler == "tweedledum":
elif compiler == "tweedledum" and TWEEDLEDUM_ENABLED:
sel_compiler = TweedledumCompiler()
else:
raise Exception(
Expand Down
6 changes: 5 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@
"qlasskit.algorithms",
],
zip_safe=False,
install_requires=["sympy==1.12", "tweedledum==1.1.1"],
install_requires=["sympy==1.12"],
extras_require={
"tweedledum": ["tweedledum==1.1.1"],
},
classifiers=[
"Programming Language :: Python",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
],
)
17 changes: 10 additions & 7 deletions test/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,41 @@

import unittest

from parameterized import parameterized_class

from qlasskit import compiler, qlassf

from .utils import compute_and_compare_results
from .utils import ENABLED_COMPILERS, compute_and_compare_results


@parameterized_class(("compiler"), ENABLED_COMPILERS)
class TestCompiler(unittest.TestCase):
def test_not_arg(self):
f = "def test(a: bool) -> bool:\n\treturn not a"
qf = qlassf(f, to_compile=True)
qf = qlassf(f, to_compile=True, compiler=self.compiler)
compute_and_compare_results(self, qf)

def test_and(self):
f = "def test(a: bool, b: bool) -> bool:\n\treturn a and b"
qf = qlassf(f, to_compile=True)
qf = qlassf(f, to_compile=True, compiler=self.compiler)
compute_and_compare_results(self, qf)

def test_and_long(self):
f = "def test(a: bool, b: bool, c: bool, d: bool) -> bool:\n\treturn a and b and c and d"
qf = qlassf(f, to_compile=True)
qf = qlassf(f, to_compile=True, compiler=self.compiler)
compute_and_compare_results(self, qf)

def test_and_long_with_not(self):
f = "def test(a: bool, b: bool, c: bool, d: bool) -> bool:\n\treturn a and b and not c and d"
qf = qlassf(f, to_compile=True)
qf = qlassf(f, to_compile=True, compiler=self.compiler)
compute_and_compare_results(self, qf)

def test_or(self):
f = "def test(a: bool, b: bool) -> bool:\n\treturn a or b"
qf = qlassf(f, to_compile=True)
qf = qlassf(f, to_compile=True, compiler=self.compiler)
compute_and_compare_results(self, qf)

def test_or_not(self):
f = "def test(a: bool, b: bool) -> bool:\n\treturn not a or b"
qf = qlassf(f, to_compile=True)
qf = qlassf(f, to_compile=True, compiler=self.compiler)
compute_and_compare_results(self, qf)
10 changes: 2 additions & 8 deletions test/test_qlassf.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from qlasskit import Qint, Qint4, Qint12, QlassF, exceptions, qlassf

from . import utils
from .utils import COMPILATION_ENABLED, compute_and_compare_results
from .utils import COMPILATION_ENABLED, ENABLED_COMPILERS, compute_and_compare_results


class TestQlassfDecorator(unittest.TestCase):
Expand All @@ -28,13 +28,7 @@ def test_decorator(self):
self.assertTrue(isinstance(c, QlassF))


@parameterized_class(
("compiler"),
[
("internal",),
("tweedledum",),
],
)
@parameterized_class(("compiler"), ENABLED_COMPILERS)
class TestQlassfCustomTypes(unittest.TestCase):
def test_custom_qint3(self):
qf = qlassf(
Expand Down
18 changes: 3 additions & 15 deletions test/test_qlassf_bool.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,13 @@

from qlasskit import QlassF, exceptions, qlassf

from .utils import COMPILATION_ENABLED, compute_and_compare_results
from .utils import COMPILATION_ENABLED, ENABLED_COMPILERS, compute_and_compare_results

a, b, c, d, e, g, h = symbols("a,b,c,d,e,g,h")
_ret = Symbol("_ret")


@parameterized_class(
("compiler"),
[
("internal",),
("tweedledum",),
],
)
@parameterized_class(("compiler"), ENABLED_COMPILERS)
class TestQlassfBoolean(unittest.TestCase):
def test_unbound(self):
f = "def test() -> bool:\n\treturn a"
Expand Down Expand Up @@ -205,13 +199,7 @@ def test_assign3(self):
# self.assertEqual(qf.expressions[-1][1], ITE(d & e, g, h))


@parameterized_class(
("compiler"),
[
("internal",),
("tweedledum",),
],
)
@parameterized_class(("compiler"), ENABLED_COMPILERS)
class TestQlassfBoolBitwise(unittest.TestCase):
def test_bitwise_and(self):
f = f"def test(a: bool, b: bool) -> bool:\n\treturn a & b"
Expand Down
10 changes: 2 additions & 8 deletions test/test_qlassf_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,10 @@

from qlasskit import QlassF, exceptions, qlassf

from .utils import COMPILATION_ENABLED, compute_and_compare_results
from .utils import COMPILATION_ENABLED, ENABLED_COMPILERS, compute_and_compare_results


@parameterized_class(
("compiler"),
[
("internal",),
("tweedledum",),
],
)
@parameterized_class(("compiler"), ENABLED_COMPILERS)
class TestQlassfBuiltinFunctions(unittest.TestCase):
def test_print_call(self):
f = "def test(a: bool) -> bool:\n\tprint(a)\n\treturn a"
Expand Down
10 changes: 2 additions & 8 deletions test/test_qlassf_for_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,13 @@

from qlasskit import Qint2, Qint4, Qint8, QlassF, exceptions, qlassf

from .utils import COMPILATION_ENABLED, compute_and_compare_results
from .utils import COMPILATION_ENABLED, ENABLED_COMPILERS, compute_and_compare_results

a, b, c, d = symbols("a,b,c,d")
_ret = Symbol("_ret")


@parameterized_class(
("compiler"),
[
("internal",),
("tweedledum",),
],
)
@parameterized_class(("compiler"), ENABLED_COMPILERS)
class TestForLoop(unittest.TestCase):
def test_for_1it(self):
f = "def test(a: Qint2) -> Qint2:\n\tfor x in range(1):\n\t\ta += 1\n\treturn a"
Expand Down
10 changes: 2 additions & 8 deletions test/test_qlassf_functiondef.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,10 @@

from qlasskit import QlassF, exceptions, qlassf

from .utils import COMPILATION_ENABLED, compute_and_compare_results
from .utils import COMPILATION_ENABLED, ENABLED_COMPILERS, compute_and_compare_results


@parameterized_class(
("compiler"),
[
("internal",),
("tweedledum",),
],
)
@parameterized_class(("compiler"), ENABLED_COMPILERS)
class TestQlassfFunctionDef(unittest.TestCase):
def test_pass_another_function(self):
f = "def neg(b: bool) -> bool:\n\treturn not b"
Expand Down
Loading

0 comments on commit 81ac9e0

Please sign in to comment.