Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue 133 bugfix for nested bitwise ops #135

Merged
merged 4 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
Asa-Kosto-QTM marked this conversation as resolved.
Show resolved Hide resolved
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