Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search test #23

Merged
merged 4 commits into from
Dec 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 73 additions & 33 deletions tests/test_search.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
import time

from qupsy.language import CX, ForCmd, GateCmd, H, Integer, Pgm, SeqCmd, Var
from qupsy.search import search
from qupsy.spec import make_spec, SpecData
from qupsy.spec import SpecData, make_spec

raw_spec: SpecData = {
"gates": ["H", "CX"],
"testcases": {
"1": {
"input": None,
"output": "0.70710677,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.70710677",
},
"2": {
"input": None,
"output": "0.70710677, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.70710677",
},
"3": {"input": None, "output": "0.70710677,0,0,0,0,0,0,0.70710677"},
},
}
spec = make_spec(raw_spec)


def test_search_ghz_last_step():
def test_search_ghz_one_step():
init_pgm = Pgm(
"n",
SeqCmd(
Expand All @@ -18,26 +36,12 @@ def test_search_ghz_last_step():
ForCmd("i0", Integer(1), Var("n"), GateCmd(CX(Integer(0), Var("i0")))),
),
)
raw_spec: SpecData = {
"gates": ["H", "CX"],
"testcases": {
"1": {
"input": None,
"output": "0.70710677,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.70710677",
},
"2": {
"input": None,
"output": "0.70710677, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.70710677",
},
"3": {"input": None, "output": "0.70710677,0,0,0,0,0,0,0.70710677"},
},
}
spec = make_spec(raw_spec)

synthesized_pgm = search(spec, initial_pgm=init_pgm)
assert synthesized_pgm == final_pgm


def test_search_ghz_second_last_step():
def test_search_ghz_two_step():
init_pgm = Pgm(
"n",
SeqCmd(
Expand All @@ -52,20 +56,56 @@ def test_search_ghz_second_last_step():
ForCmd("i0", Integer(1), Var("n"), GateCmd(CX(Integer(0), Var("i0")))),
),
)
raw_spec: SpecData = {
"gates": ["H", "CX"],
"testcases": {
"1": {
"input": None,
"output": "0.70710677,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.70710677",
},
"2": {
"input": None,
"output": "0.70710677, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.70710677",
},
"3": {"input": None, "output": "0.70710677,0,0,0,0,0,0,0.70710677"},
},
}
spec = make_spec(raw_spec)

synthesized_pgm = search(spec, initial_pgm=init_pgm)
assert synthesized_pgm == final_pgm


def test_search_ghz_three_step():
init_pgm = Pgm(
"n",
SeqCmd(GateCmd(H(Integer(0))), ForCmd("i0", Integer(1), Var("n"), GateCmd())),
)
final_pgm = Pgm(
"n",
SeqCmd(
GateCmd(H(Integer(0))),
ForCmd("i0", Integer(1), Var("n"), GateCmd(CX(Integer(0), Var("i0")))),
),
)

synthesized_pgm = search(spec, initial_pgm=init_pgm)
assert synthesized_pgm == final_pgm


def test_search_ghz_four_step():
init_pgm = Pgm(
"n", SeqCmd(GateCmd(H(Integer(0))), ForCmd("i0", Integer(1), Var("n")))
)
final_pgm = Pgm(
"n",
SeqCmd(
GateCmd(H(Integer(0))),
ForCmd("i0", Integer(1), Var("n"), GateCmd(CX(Integer(0), Var("i0")))),
),
)

synthesized_pgm = search(spec, initial_pgm=init_pgm)
assert synthesized_pgm == final_pgm


def test_search_ghz_five_step():
init_pgm = Pgm("n", SeqCmd(GateCmd(H(Integer(0))), ForCmd("i0", Integer(1))))
final_pgm = Pgm(
"n",
SeqCmd(
GateCmd(H(Integer(0))),
ForCmd("i0", Integer(1), Var("n"), GateCmd(CX(Integer(0), Var("i0")))),
),
)

start = time.time()
synthesized_pgm = search(spec, initial_pgm=init_pgm)
end = time.time()
assert synthesized_pgm == final_pgm
assert end - start < 1
Loading