@@ -301,6 +301,7 @@ def ecatch(self, f: Callable[_S, _T], *args: _S.args, **kwargs: _S.kwargs) -> _T
301
301
302
302
303
303
def _infer_layer_impl (gd : nx .DiGraph [_V ]) -> Mapping [_V , int ]:
304
+ """Fix flow layers one by one depending on order constraints."""
304
305
pred = {u : set (gd .predecessors (u )) for u in gd .nodes }
305
306
work = {u for u , pu in pred .items () if not pu }
306
307
ret : dict [_V , int ] = {}
@@ -322,6 +323,20 @@ def _infer_layer_impl(gd: nx.DiGraph[_V]) -> Mapping[_V, int]:
322
323
return ret
323
324
324
325
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
+
325
340
def _special_edges (
326
341
g : nx .Graph [_V ],
327
342
anyflow : Mapping [_V , _V | AbstractSet [_V ]],
@@ -337,17 +352,8 @@ def _special_edges(
337
352
for v in itertools .chain (fu , fu_odd ):
338
353
if u == v :
339
354
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 ):
349
356
ret .add ((u , v ))
350
- continue
351
357
return ret
352
358
353
359
@@ -356,7 +362,7 @@ def infer_layer(
356
362
anyflow : Mapping [_V , _V | AbstractSet [_V ]],
357
363
pplane : Mapping [_V , PPlane ] | None = None ,
358
364
) -> Mapping [_V , int ]:
359
- """Infer layer from flow/gflow.
365
+ """Infer layer from flow/gflow using greedy algorithm .
360
366
361
367
Parameters
362
368
----------
@@ -365,11 +371,11 @@ def infer_layer(
365
371
anyflow : `tuple` of flow-like/layer
366
372
Flow to verify. Compatible with both flow and generalized flow.
367
373
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.
369
375
370
376
Notes
371
377
-----
372
- This function is based on greedy algorithm .
378
+ This function operates in Pauli flow mode only when :py:obj`pplane` is explicitly given .
373
379
"""
374
380
gd : nx .DiGraph [_V ] = nx .DiGraph ()
375
381
gd .add_nodes_from (g .nodes )
0 commit comments