Skip to content

Commit

Permalink
notebook for deutsch jozsa and simon, fix internal compiler for certa…
Browse files Browse the repository at this point in the history
…in cases
  • Loading branch information
dakk committed Dec 3, 2023
1 parent 0607b85 commit bcee074
Show file tree
Hide file tree
Showing 6 changed files with 266 additions and 15 deletions.
123 changes: 123 additions & 0 deletions docs/source/example_deutsch_jozsa.ipynb

Large diffs are not rendered by default.

124 changes: 124 additions & 0 deletions docs/source/example_simon.ipynb

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Python and translate them into unitary operators (gates) for use in quantum circ
example_grover_hash.ipynb
example_grover_sudoku.ipynb
example_unitary_of_f.ipynb
example_simon.ipynb
example_deutsch_jozsa.ipynb


Indices and tables
Expand Down
6 changes: 5 additions & 1 deletion qlasskit/compiler/internalcompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ def compile(
a_true = qc.add_qubit("TRUE")
qc.x(a_true)
iret = a_true
elif isinstance(symp_exp, Symbol):
iret = qc.add_qubit(sym.name)
qc.cx(qc[symp_exp.name], iret)
else:
iret = self.compile_expr(qc, symp_exp)

Expand All @@ -67,7 +70,8 @@ def compile(

qc.remove_identities()
if uncompute:
qc.uncompute_all(keep=[qc[r] for r in returns.bitvec])
keep = [qc[r] for r in filter(lambda r: r in qc, returns.bitvec)]
qc.uncompute_all(keep=keep)

return qc

Expand Down
15 changes: 7 additions & 8 deletions test/test_algo_deutschjozsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,23 @@

import unittest

from parameterized import parameterized_class

from qlasskit import Qint2, Qint4, qlassf
from qlasskit.algorithms import DeutschJozsa
from qlasskit.compiler import SupportedCompilers

from .utils import qiskit_measure_and_count
from .utils import ENABLED_COMPILERS, qiskit_measure_and_count


@parameterized_class(("compiler"), ENABLED_COMPILERS)
class TestAlgoDeutschJozsa(unittest.TestCase):
def test_deutschjozsa_balanced(self):
f = """
def hash(k: Qint2) -> bool:
return k[0] ^ k[1]
"""
qf = qlassf(f)
qf = qlassf(f, compiler=self.compiler)
algo = DeutschJozsa(qf)

qc_algo = algo.circuit().export("circuit", "qiskit")
Expand All @@ -36,15 +39,11 @@ def hash(k: Qint2) -> bool:
self.assertEqual(counts_readable["Balanced"], 1024)

def test_deutschjozsa_balanced2(self):
# TODO: fix this in internalcompiler
if "tweedledum" not in SupportedCompilers:
return

f = """
def hash(k: bool) -> bool:
return k
"""
qf = qlassf(f, compiler="tweedledum")
qf = qlassf(f, compiler=self.compiler)
algo = DeutschJozsa(qf)

qc_algo = algo.circuit().export("circuit", "qiskit")
Expand All @@ -57,7 +56,7 @@ def test_deutschjozsa_constant(self):
def hash(k: Qint2) -> bool:
return False
"""
qf = qlassf(f)
qf = qlassf(f, compiler=self.compiler)
algo = DeutschJozsa(qf)

qc_algo = algo.circuit().export("circuit", "qiskit")
Expand Down
11 changes: 5 additions & 6 deletions test/test_algo_simon.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,23 @@

import unittest

from parameterized import parameterized_class

from qlasskit import Qint2, Qint4, qlassf
from qlasskit.algorithms import Simon
from qlasskit.compiler import SupportedCompilers

from .utils import qiskit_measure_and_count
from .utils import ENABLED_COMPILERS, qiskit_measure_and_count


@parameterized_class(("compiler"), ENABLED_COMPILERS)
class TestAlgoSimon(unittest.TestCase):
def test_simon(self):
# TODO: fix this in internalcompiler
if "tweedledum" not in SupportedCompilers:
return

f = """
def hash(k: Qint4) -> Qint4:
return k >> 3
"""
qf = qlassf(f, compiler="tweedledum")
qf = qlassf(f, compiler=self.compiler)
algo = Simon(qf)

qc = algo.circuit().export("circuit", "qiskit")
Expand Down

0 comments on commit bcee074

Please sign in to comment.