From 7ab21172779f4baadbfe070d8348e1debebc9fbe Mon Sep 17 00:00:00 2001 From: Kartik Singhal Date: Tue, 10 Sep 2024 15:58:09 -0500 Subject: [PATCH] fix: generate nested multi-bit conditions for pecos support --- pytket/phir/phirgen.py | 30 +++++++++++++++++++++--------- requirements.txt | 2 +- tests/test_phirgen.py | 4 ++-- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/pytket/phir/phirgen.py b/pytket/phir/phirgen.py index 5cfb4e0..1d6eca7 100644 --- a/pytket/phir/phirgen.py +++ b/pytket/phir/phirgen.py @@ -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 @@ -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 diff --git a/requirements.txt b/requirements.txt index 5d083f1..48fe235 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/tests/test_phirgen.py b/tests/test_phirgen.py index 75d9d93..c7a33c2 100644 --- a/tests/test_phirgen.py +++ b/tests/test_phirgen.py @@ -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": [ @@ -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]]}],