diff --git a/tests/test_language.py b/tests/test_language.py index 1ed526c..dd00168 100644 --- a/tests/test_language.py +++ b/tests/test_language.py @@ -1,4 +1,4 @@ -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(): @@ -6,11 +6,16 @@ def test_pgm_create_with_empty_body(): 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 @@ -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)] diff --git a/tests/test_search.py b/tests/test_search.py index 4944e02..af062d1 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -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( @@ -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( @@ -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())), @@ -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"))) ) @@ -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", diff --git a/tests/test_spec.py b/tests/test_spec.py index 5275e83..47e81cb 100644 --- a/tests/test_spec.py +++ b/tests/test_spec.py @@ -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 @@ -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