Skip to content

Commit

Permalink
groover testing, fix examples
Browse files Browse the repository at this point in the history
  • Loading branch information
dakk committed Nov 1, 2023
1 parent 57e0aa7 commit 6294dd3
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 18 deletions.
6 changes: 2 additions & 4 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,11 @@
- [x] Integrate qrack on test suite
- [x] Test / support on multiple py version >= 3.8
- [x] Fixed size list
- [ ] Groover algorithm tests
- [ ] Slideshow for UF midterm
- [x] Groover algorithm tests
- [x] Slideshow for UF midterm

### Week 3: (6 Nov 23)

- [ ] Slideshow for UF midterm

### Week 4: (13 Nov 23)

## Month 3:
Expand Down
8 changes: 4 additions & 4 deletions examples/ex1.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ def f_comp(n: Qint4) -> bool:
gate1 = f1.gate()
gate2 = f2.gate()
gate3 = f3.gate()
qc = QuantumCircuit(10)
qc = QuantumCircuit(12)
qc.barrier(label="=")
qc.append(gate1, [0, 1, 2, 3, 4, 5, 6])
qc.append(gate1, [0, 1, 2, 3, 4, 5])
qc.barrier(label=">")
qc.append(gate2, [0, 1, 2, 3, 7, 8])
qc.append(gate2, [0, 1, 2, 3, 6, 7, 8, 9])
qc.barrier(label="|")
qc.append(gate3, [5, 8, 9])
qc.append(gate3, [5, 7, 9, 10, 11])
print(qc.decompose().draw("text"))
print(qc.decompose().count_ops())
20 changes: 18 additions & 2 deletions examples/ex1_2.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from matplotlib import pyplot as plt
from qiskit import QuantumCircuit

from qlasskit import Qint2, qlassf
Expand All @@ -18,20 +19,35 @@ def f_comp(b: bool, n: Qint2) -> Qint2:


print(f_comp.expressions)
f_comp.compile("tweedledum")
gate = f_comp.gate()
qc = QuantumCircuit(gate.num_qubits)
qc.barrier(label="f_comp")
qc.append(gate, list(range(gate.num_qubits)))
print(qc.decompose().draw("text"))
print(qc.decompose().count_ops())

print(
qc.decompose().draw(
"mpl",
style={"textcolor": "#ffab40", "backgroundcolor": "black", "linecolor": "#444"},
)
)

f1.compile("tweedledum")
gate1 = f1.gate()
qc = QuantumCircuit(gate.num_qubits * 3)
qc = QuantumCircuit(gate.num_qubits * 2)

for i in range(3):
qc.barrier(label="=")
qc.barrier(label=f"it_{i}")
qc.append(gate1, [0] + list(range(1 + i * INTSIZ, 1 + i * INTSIZ + INTSIZ * 2)))

print(qc.decompose().draw("text"))
print(qc.decompose().count_ops())
print(
qc.decompose().draw(
"mpl",
style={"textcolor": "#ffab40", "backgroundcolor": "black", "linecolor": "#444"},
)
)
plt.show()
4 changes: 2 additions & 2 deletions qlasskit/algorithms/groover.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ class Groover(QAlgorithm):
def __init__(
self,
oracle: QlassF,
element_to_search: Qtype,
element_to_search: Optional[Qtype] = None,
n_iterations: Optional[int] = None,
):
"""
Args:
oracle (QlassF): our f(x) -> bool that returns True if x satisfies the function or
a generic function f(x) = y that we want to compare with element_to_search
element_to_search (Qtype): the element we want to search
element_to_search (Qtype, optional): the element we want to search
n_iterations (int, optional): force a number of iterations (otherwise, pi/4*sqrt(N))
"""
if len(oracle.args) != 1:
Expand Down
58 changes: 52 additions & 6 deletions test/test_algo_groover.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,58 @@

import unittest

from qlasskit import Qint2, Qint4, ast2logic, exceptions
from qlasskit import Qint2, Qint4, qlassf
from qlasskit.algorithms import Groover

from .utils import qiskit_measure_and_count


class TestAlgoGroover(unittest.TestCase):
# test interpret outcome
# test various combinations of input / output size
# test out_qubits
# test without element to search
pass
def test_groover(self):
f = """
def hash(k: Qint4) -> bool:
h = True
for i in range(4):
h = h and k[i]
return h
"""
qf = qlassf(f)
algo = Groover(qf, True)

qc = algo.circuit().export("circuit", "qiskit")
counts = qiskit_measure_and_count(qc, shots=1024)
counts_readable = algo.interpet_counts(counts)

self.assertEqual(15 in counts_readable, True)
self.assertEqual(algo.out_qubits(), [0, 1, 2, 3])
self.assertEqual(counts_readable[15] > 600, True)

def test_groover_without_element_to_search(self):
f = """
def hash(k: Qint4) -> bool:
h = True
for i in range(4):
h = h and k[i]
return h
"""
qf = qlassf(f)
algo = Groover(qf)

qc = algo.circuit().export("circuit", "qiskit")
counts = qiskit_measure_and_count(qc, shots=1024)
counts_readable = algo.interpet_counts(counts)

self.assertEqual(15 in counts_readable, True)
self.assertEqual(counts_readable[15] > 600, True)

def test_groover_too_many_args(self):
f = """
def hash(k: Qint4, q: Qint4) -> bool:
h = True
for i in range(4):
h = h and (k[i] or q[i])
return h
"""
qf = qlassf(f)

self.assertRaises(Exception, lambda x: Groover(x), qf)

0 comments on commit 6294dd3

Please sign in to comment.