Skip to content

Commit 4849939

Browse files
committed
♻️ Refactor
1 parent 32dacda commit 4849939

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

python/swiflow/_common.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ def ecatch(self, f: Callable[_S, _T], *args: _S.args, **kwargs: _S.kwargs) -> _T
301301

302302

303303
def _infer_layer_impl(gd: nx.DiGraph[_V]) -> Mapping[_V, int]:
304+
"""Fix flow layers one by one depending on order constraints."""
304305
pred = {u: set(gd.predecessors(u)) for u in gd.nodes}
305306
work = {u for u, pu in pred.items() if not pu}
306307
ret: dict[_V, int] = {}
@@ -322,6 +323,20 @@ def _infer_layer_impl(gd: nx.DiGraph[_V]) -> Mapping[_V, int]:
322323
return ret
323324

324325

326+
def _is_special(
327+
pp: PPlane | None,
328+
in_fu: bool, # noqa: FBT001
329+
in_fu_odd: bool, # noqa: FBT001
330+
) -> bool:
331+
if pp == PPlane.X:
332+
return in_fu
333+
if pp == PPlane.Y:
334+
return in_fu and in_fu_odd
335+
if pp == PPlane.Z:
336+
return in_fu_odd
337+
return False
338+
339+
325340
def _special_edges(
326341
g: nx.Graph[_V],
327342
anyflow: Mapping[_V, _V | AbstractSet[_V]],
@@ -337,17 +352,8 @@ def _special_edges(
337352
for v in itertools.chain(fu, fu_odd):
338353
if u == v:
339354
continue
340-
if (pp := pplane.get(v)) is None:
341-
continue
342-
if pp == PPlane.X and v in fu:
343-
ret.add((u, v))
344-
continue
345-
if pp == PPlane.Y and v in fu and v in fu_odd:
346-
ret.add((u, v))
347-
continue
348-
if pp == PPlane.Z and v in fu_odd:
355+
if _is_special(pplane.get(v), v in fu, v in fu_odd):
349356
ret.add((u, v))
350-
continue
351357
return ret
352358

353359

@@ -356,7 +362,7 @@ def infer_layer(
356362
anyflow: Mapping[_V, _V | AbstractSet[_V]],
357363
pplane: Mapping[_V, PPlane] | None = None,
358364
) -> Mapping[_V, int]:
359-
"""Infer layer from flow/gflow.
365+
"""Infer layer from flow/gflow using greedy algorithm.
360366
361367
Parameters
362368
----------
@@ -365,11 +371,11 @@ def infer_layer(
365371
anyflow : `tuple` of flow-like/layer
366372
Flow to verify. Compatible with both flow and generalized flow.
367373
pplane : `collections.abc.Mapping`, optional
368-
Measurement plane or Pauli index. If provided, :py:obj:`anyflow` is treated as Pauli flow.
374+
Measurement plane or Pauli index.
369375
370376
Notes
371377
-----
372-
This function is based on greedy algorithm.
378+
This function operates in Pauli flow mode only when :py:obj`pplane` is explicitly given.
373379
"""
374380
gd: nx.DiGraph[_V] = nx.DiGraph()
375381
gd.add_nodes_from(g.nodes)

0 commit comments

Comments
 (0)