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

fix: Emit nested binary expressions for classical ops #224

9 changes: 6 additions & 3 deletions pytket/phir/phirgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,13 @@ def convert_classicalevalop(op: tk.ClassicalEvalOp, cmd: tk.Command) -> JsonDict

def multi_bit_condition(args: "list[UnitID]", value: int) -> JsonDict:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a feeling this will error out if len(args) is 0 or 1. While these may be rare edge cases it would be good to handle them or at least raise an informative exception.

Copy link

@PabloAndresCQ PabloAndresCQ Sep 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would make sense, but PECOS does not seem to be able to deal with "cop": "&" with fewer than two arguments.

ValueError: not enough values to unpack (expected 2, got 1)

Is there a way to add a "True" constant to an expression in PHIR? The AND with a single element args == [x] could be implemented as (x == val) & (x == val), but in the case of args == [] you'd need to be able to output True somehow.

Not sure if we'd want to do this, though. Is it OK for pytket-phir to create valid PHIR when the conditions the user inputted would not be accepted by PECOS if done directly? It's true that in this case, the action of AND on 1 and 0 arguments is not ambiguous, but it still makes me uneasy. I'd just throw an exception with informative message.

Copy link
Member Author

@qartik qartik Sep 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function is only called at one place when len(args) is not 1, but for generality (and reusability), it's certainly worth doing an edge case check. Implemented and pushed.

"""Construct bitwise condition."""
val_bits = deque(map(int, f"{value:0{len(args)}b}"))
min_args = 2
if len(args) < min_args:
msg = "multi_bit_condition requires at least two arguments"
qartik marked this conversation as resolved.
Show resolved Hide resolved
raise TypeError(msg)

def nested_cop(cop: str, args: "deque[UnitID]", val_bits: deque[int]) -> JsonDict:
if len(args) == 2: # noqa: PLR2004
if len(args) == min_args:
return {
"cop": cop,
"args": [
Expand All @@ -376,7 +379,7 @@ def nested_cop(cop: str, args: "deque[UnitID]", val_bits: deque[int]) -> JsonDic
],
}

return nested_cop("&", deque(args), val_bits)
return nested_cop("&", deque(args), deque(map(int, f"{value:0{len(args)}b}")))


def convert_subcmd(op: tk.Op, cmd: tk.Command) -> JsonDict | None: # noqa: PLR0912
Expand Down