Skip to content

Commit

Permalink
issue 133 bugfix for nested bitwise ops (#135)
Browse files Browse the repository at this point in the history
Update logic for nested classical ops (fixes issue #133) 

---------

Co-authored-by: Kartik Singhal <kartik.singhal@quantinuum.com>
  • Loading branch information
Asa-Kosto-QTM and qartik authored Feb 21, 2024
1 parent ee110bc commit 2604e8e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pytket/phir/phirgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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,
Expand Down
26 changes: 26 additions & 0 deletions tests/test_phirgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2604e8e

Please sign in to comment.