Skip to content

Commit

Permalink
Modify (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
glorialeezero authored Dec 6, 2024
2 parents af2f37c + d22aabe commit a855678
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 31 deletions.
21 changes: 19 additions & 2 deletions tests/test_language.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
from qupsy.language import HoleCmd, Pgm, SeqCmd
from qupsy.language import Div, GateCmd, HoleCmd, Integer, Mul, Pgm, Ry, SeqCmd


def test_pgm_create_with_empty_body():
pgm = Pgm("n")
assert isinstance(pgm.body, HoleCmd)


def test_pgm_cost():
def test_pgm_cost0():
pgm = Pgm("n")
assert pgm.cost == pgm.body.cost


def test_pgm_cost1():
pgm = Pgm("n", GateCmd(Ry(Integer(0), Integer(1), Integer(3))))
assert pgm.cost == 2


def test_pgm_depth():
pgm = Pgm("n")
assert pgm.depth == pgm.body.depth
Expand All @@ -20,3 +25,15 @@ def test_seq_cmd_wo_pre_and_post():
seq = SeqCmd()
assert isinstance(seq.pre, HoleCmd)
assert isinstance(seq.post, HoleCmd)


def test_pgm_call():
pgm = Mul(Integer(2), Integer(3))
res = pgm.__call__({"n": 5})
assert res == 6


def test_pgm_children():
pgm = Div(Integer(2), Integer(3))
children = pgm.children
assert children == [Integer(2), Integer(3)]
6 changes: 5 additions & 1 deletion tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
"3": {"input": None, "output": "0.70710677,0,0,0,0,0,0,0.70710677"},
},
}
spec = make_spec(raw_spec)


def test_search_ghz_one_step():
spec = make_spec(raw_spec)
init_pgm = Pgm(
"n",
SeqCmd(
Expand All @@ -42,6 +42,7 @@ def test_search_ghz_one_step():


def test_search_ghz_two_step():
spec = make_spec(raw_spec)
init_pgm = Pgm(
"n",
SeqCmd(
Expand All @@ -62,6 +63,7 @@ def test_search_ghz_two_step():


def test_search_ghz_three_step():
spec = make_spec(raw_spec)
init_pgm = Pgm(
"n",
SeqCmd(GateCmd(H(Integer(0))), ForCmd("i0", Integer(1), Var("n"), GateCmd())),
Expand All @@ -79,6 +81,7 @@ def test_search_ghz_three_step():


def test_search_ghz_four_step():
spec = make_spec(raw_spec)
init_pgm = Pgm(
"n", SeqCmd(GateCmd(H(Integer(0))), ForCmd("i0", Integer(1), Var("n")))
)
Expand All @@ -95,6 +98,7 @@ def test_search_ghz_four_step():


def test_search_ghz_five_step():
spec = make_spec(raw_spec)
init_pgm = Pgm("n", SeqCmd(GateCmd(H(Integer(0))), ForCmd("i0", Integer(1))))
final_pgm = Pgm(
"n",
Expand Down
43 changes: 15 additions & 28 deletions tests/test_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@
from qupsy.language import CX, H
from qupsy.spec import Spec, 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"},
},
}


def test_parse_spec_from_raw_data():
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)
assert spec.gates == [H, CX]
assert len(spec.testcases) == 3
Expand All @@ -39,20 +40,6 @@ def test_parse_spec_from_raw_data():


def test_parse_spec_from_json_data():
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)
assert spec.gates == [H, CX]
assert len(spec.testcases) == 3
Expand Down

0 comments on commit a855678

Please sign in to comment.