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)