def test_extract_macro(self): text = "macro foo a { g a }" parser = make_lark_parser() exp_result = { Identifier.parse("foo"): MacroRecord( [Identifier.parse("a")], make_lark_parser(start="sequential_gate_block").parse("{ g a }"), ) } act_result = extract_macro(parser.parse(text)) self.assertEqual(exp_result, act_result)
def test_multiple_registers(self): text = "register q[3]; register r[2]" exp_result = { Identifier.parse("q"): TreeManipulators.make_let_or_integer( TreeManipulators.make_integer(3)), Identifier.parse("r"): TreeManipulators.make_let_or_integer( TreeManipulators.make_integer(2)), } self.run_test(text, exp_result)
def test_track_macro(self): text = "g0; macro m0 a b c {g1}" exp_result = { Identifier.parse("g0"): None, Identifier.parse("g1"): Identifier.parse("m0"), } parser = make_lark_parser() tree = parser.parse(text) visitor = MacroTaggerVisitor() visitor.visit(tree) act_result = visitor.context_by_gate self.assertEqual(exp_result, act_result)
def test_replace_map_statement(self): let_dict = { Identifier.parse("a"): 1, Identifier.parse("b"): 10, Identifier.parse("c"): 2, } parser = make_lark_parser(start="map_statement") text = "map arr q[a:b:c]" exp_text = "map arr q[1:10:2]" exp_tree = parser.parse(exp_text) act_tree = resolve_let(parser.parse(text), let_dict) self.assertEqual(exp_tree, act_tree)
def test_usepulses(self): """Test getting information from the usepulses statement.""" text = "from foo.bar usepulses *" exp_result = {Identifier.parse("foo.bar"): all} iface = Interface(text, allow_no_usepulses=True) act_result = iface.usepulses self.assertEqual(exp_result, act_result)
def test_dont_replace_macro_argument(self): let_dict = {Identifier.parse("a"): 3.14} parser = make_lark_parser(start="macro_definition") text = "macro foo a { g a }" exp_tree = parser.parse(text) act_tree = resolve_let(exp_tree, let_dict) self.assertEqual(exp_tree, act_tree)
def test_replace_register_statement(self): let_dict = {Identifier.parse("a"): 1} parser = make_lark_parser(start="register_statement") text = "register q[a]" exp_text = "register q[1]" exp_tree = parser.parse(exp_text) act_tree = resolve_let(parser.parse(text), let_dict) self.assertEqual(exp_tree, act_tree)
def test_replace_loop_count(self): let_dict = {Identifier.parse("a"): 1} parser = make_lark_parser(start="loop_statement") text = "loop a { g }" exp_text = "loop 1 { g }" exp_tree = parser.parse(exp_text) act_tree = resolve_let(parser.parse(text), let_dict) self.assertEqual(exp_tree, act_tree)
def test_replace_array_index(self): let_dict = {Identifier.parse("a"): 1} parser = make_lark_parser(start="gate_statement") text = "g arr[a]" exp_text = "g arr[1]" exp_tree = parser.parse(exp_text) act_tree = resolve_let(parser.parse(text), let_dict) self.assertEqual(exp_tree, act_tree)
def test_single_register(self): text = "register q[3]" exp_result = { Identifier.parse("q"): TreeManipulators.make_let_or_integer( TreeManipulators.make_integer(3)) } self.run_test(text, exp_result)
def test_replace_gate_arg(self): """Test replacing a single gate argument.""" let_dict = {Identifier.parse("a"): 1} parser = make_lark_parser(start="gate_statement") text = "g a" exp_text = "g 1" exp_tree = parser.parse(exp_text) act_tree = resolve_let(parser.parse(text), let_dict) self.assertEqual(exp_tree, act_tree)
def test_let_constant_as_size(self): text = "let a 5; register q[a]" exp_result = { Identifier.parse("q"): TreeManipulators.make_let_or_integer( TreeManipulators.make_let_identifier( TreeManipulators.make_qualified_identifier("a"))) } self.run_test(text, exp_result)
def test_return_usepulses(self): text = "from MyPulses.MyClass usepulses *" exp_value = { "usepulses": { Identifier.parse("MyPulses.MyClass"): all, } } _, act_value = parse_jaqal_string(text, return_usepulses=True, autoload_pulses=False) self.assertEqual(act_value, exp_value)
def test_reject_non_integer_register_statement(self): let_dict = {Identifier.parse("a"): 3.14} parser = make_lark_parser(start="register_statement") text = "register q[a]" with self.assertRaises(Exception): resolve_let(parser.parse(text), let_dict)
def test_reject_non_integer_loop_count(self): let_dict = {Identifier.parse("a"): 12.34} parser = make_lark_parser(start="loop_statement") text = "loop a { g }" with self.assertRaises(Exception): resolve_let(parser.parse(text), let_dict)
def test_combine_dicts_extra_keys(self): """Test for failure when combining a dictionary that is not fully represented in the let values.""" main_dict = {Identifier.parse("foo"): 1, Identifier.parse("bar"): 2} addl_dict = {Identifier.parse("foo"): 42, Identifier.parse("extra"): 666} with self.assertRaises(Exception): combine_let_dicts(main_dict, addl_dict)
def test_combine_dicts(self): main_dict = {Identifier.parse("foo"): 1, Identifier.parse("bar"): 2} addl_dict = {Identifier.parse("foo"): 42} comb_dict = combine_let_dicts(main_dict, addl_dict) self.assertEqual(comb_dict[Identifier.parse("foo")], 42) self.assertEqual(comb_dict[Identifier.parse("bar")], 2)
def test_simple_usepulses_all(self): """Test importing all symbols from a namespace.""" text = "from foo usepulses *" exp_value = {Identifier.parse("foo"): all} self.run_test(text, exp_value)
def test_qualified_usepulses_all(self): """Test importing all symbols from a qualified namespace.""" text = "from foo.bar usepulses *" exp_value = {Identifier.parse("foo.bar"): all} self.run_test(text, exp_value)