From 2604e8e7ed5e69648c68d7e3a39f78ab9c7d0641 Mon Sep 17 00:00:00 2001 From: Asa-Kosto-QTM <108833721+Asa-Kosto-QTM@users.noreply.github.com> Date: Wed, 21 Feb 2024 07:37:59 -0700 Subject: [PATCH] issue 133 bugfix for nested bitwise ops (#135) Update logic for nested classical ops (fixes issue #133) --------- Co-authored-by: Kartik Singhal --- pytket/phir/phirgen.py | 4 ++++ tests/test_phirgen.py | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/pytket/phir/phirgen.py b/pytket/phir/phirgen.py index ebf006d..676cd3f 100644 --- a/pytket/phir/phirgen.py +++ b/pytket/phir/phirgen.py @@ -144,6 +144,8 @@ def classical_op(exp: LogicExp, *, bitwise: bool = False) -> JsonDict: args: list[JsonDict | Var | Constant | Bit] = [] for arg in exp.args: match arg: + case BitLogicExp(): + args.append(classical_op(arg, bitwise=True)) case LogicExp(): args.append(classical_op(arg)) case BitRegister(): @@ -159,6 +161,8 @@ def classical_op(exp: LogicExp, *, bitwise: bool = False) -> JsonDict: args.append(arg_to_bit(arg)) else: args.append(arg.reg_name) + case _: + assert_never(arg) return { "cop": cop, "args": args, diff --git a/tests/test_phirgen.py b/tests/test_phirgen.py index 799ca4d..fa5e7d1 100644 --- a/tests/test_phirgen.py +++ b/tests/test_phirgen.py @@ -79,6 +79,32 @@ def test_bitwise_ops() -> None: } +def test_nested_bitwise_op() -> None: + """From https://github.com/CQCL/pytket-phir/issues/133 .""" + circ = Circuit(4) + a = circ.add_c_register("a", 4) + b = circ.add_c_register("b", 1) + circ.add_classicalexpbox_bit(a[0] ^ a[1] ^ a[2] ^ a[3], [b[0]]) + + phir = json.loads(pytket_to_phir(circ)) + assert phir["ops"][3] == { + "cop": "=", + "returns": [["b", 0]], + "args": [ + { + "cop": "^", + "args": [ + { + "cop": "^", + "args": [{"cop": "^", "args": [["a", 0], ["a", 1]]}, ["a", 2]], + }, + ["a", 3], + ], + } + ], + } + + def test_conditional_barrier() -> None: """From https://github.com/CQCL/pytket-phir/issues/119 .""" circ = get_qasm_as_circuit(QasmFile.cond_barrier)