Skip to content

Commit

Permalink
fix: generate nested multi-bit conditions for pecos support
Browse files Browse the repository at this point in the history
  • Loading branch information
qartik committed Sep 10, 2024
1 parent f0ea2e9 commit 0d7815c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
30 changes: 21 additions & 9 deletions pytket/phir/phirgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import json
import logging
import sys
from collections import deque
from copy import deepcopy
from importlib.metadata import version
from typing import TYPE_CHECKING, Any, TypeAlias
Expand Down Expand Up @@ -356,15 +357,26 @@ def convert_classicalevalop(op: tk.ClassicalEvalOp, cmd: tk.Command) -> JsonDict

def multi_bit_condition(args: "list[UnitID]", value: int) -> JsonDict:
"""Construct bitwise condition."""
return {
"cop": "&",
"args": [
{"cop": "==", "args": [arg_to_bit(arg), bval]}
for (arg, bval) in zip(
args[::-1], map(int, f"{value:0{len(args)}b}"), strict=True
)
],
}
val_bits = deque(map(int, f"{value:0{len(args)}b}"))

def nested_cop(cop: str, args: "deque[UnitID]", val_bits: deque[int]) -> JsonDict:
if len(args) == 2: # noqa: PLR2004
return {
"cop": cop,
"args": [
{"cop": "==", "args": [arg_to_bit(args.popleft()), val_bits.pop()]},
{"cop": "==", "args": [arg_to_bit(args.popleft()), val_bits.pop()]},
],
}
return {
"cop": cop,
"args": [
{"cop": "==", "args": [arg_to_bit(args.popleft()), val_bits.pop()]},
nested_cop(cop, args, val_bits),
],
}

return nested_cop("&", deque(args), val_bits)


def convert_subcmd(op: tk.Op, cmd: tk.Command) -> JsonDict | None: # noqa: PLR0912
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pre-commit==3.8.0
pydata_sphinx_theme==0.15.4
pytest==8.3.3
pytket==1.32.0
quantum-pecos==v0.6.0.dev4
quantum-pecos[simulators]==v0.6.0.dev4
ruff==0.6.4
setuptools_scm==8.1.0
sphinx==8.0.2
Expand Down
4 changes: 2 additions & 2 deletions tests/test_phirgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ def test_pytket_classical_only() -> None:
"condition": {
"cop": "&",
"args": [
{"cop": "==", "args": [["b", 2], 1]},
{"cop": "==", "args": [["b", 1], 0]},
{"cop": "==", "args": [["b", 2], 1]},
],
},
"true_branch": [
Expand Down Expand Up @@ -179,8 +179,8 @@ def test_conditional_barrier() -> None:
"condition": {
"cop": "&",
"args": [
{"cop": "==", "args": [["m", 1], 0]},
{"cop": "==", "args": [["m", 0], 0]},
{"cop": "==", "args": [["m", 1], 0]},
],
},
"true_branch": [{"meta": "barrier", "args": [["q", 0], ["q", 1]]}],
Expand Down

0 comments on commit 0d7815c

Please sign in to comment.