Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
1798137
Draft extended causal flow
thierry-martinez Nov 12, 2026
f9741c5
Remove refs to circuit extraction
matulni Nov 16, 2026
636a618
Adapt finding flow algorithm
matulni Nov 16, 2026
08aa7bf
Merge branch 'efc-mods' into ecf
matulni Nov 16, 2026
ea9e19b
Tests passing, simple extraction algorithm
matulni Nov 16, 2026
64ac80e
Replace causal flow by extended causal flow in minimization heuristics
matulni Nov 16, 2026
58035b1
Refactor exceptions
matulni Nov 16, 2026
6dd9906
Update extended causal flow docstrings and test ExtendedCausalFlow.ch…
claude Nov 16, 2026
e24ed84
Address review and test extended causal flow extraction and space min…
claude Nov 16, 2026
519f554
Merge branch 'ecf-docs' into 'ecf'
Nov 16, 2026
571ab66
Add methods pattern.to_extendedcausalflow
matulni Nov 16, 2026
efc68eb
Fix docstrings
matulni Nov 17, 2026
6d4a24b
Up README
matulni Nov 17, 2026
bb2fb2c
Refactor XZCorrections.to_gflow and XZCorrections.to_extendedcausalflow
matulni Nov 17, 2026
6f8d4f8
Fix visualization error
matulni Nov 17, 2026
726caf8
Introduce YZPlane flow exception
matulni Nov 17, 2026
89595b6
Add tests Pattern.to_extendedcausalflow
matulni Nov 19, 2026
c85af6b
Change prettyprinting of extended causal flow from 'c' to 'e'
matulni Nov 19, 2026
1efec45
Move flow definitions to class docstrings
claude Nov 19, 2026
200dc3e
Fix mistakes in Claude's docstrings
matulni Nov 19, 2026
8922c0c
Fix errors flow pretty printing and add tests
matulni Nov 19, 2026
242467b
Merge branch 'master' into ecf
matulni Nov 19, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- #474, #564: Added `FocusedPauliFlow` and `FocusedGFlow` classes to encapsulate operations specific to focused flows. Flow-finding algorithms return focused flows by construction. Having dedicated classes removes the need to dynamically recheck that a flow is focused during circuit extraction.

- #651:
- Added new class `ExtendedCausalFlow`.
- Added methods `OpenGraph.to_extendedcausalflow`, `XZCorrections.to_extendedcausalflow`, and `Pattern.to_extendedcausalflow`.

### Fixed

- Fixed incorrect Clifford signs during circuit extraction for input vertices measured in negative Pauli X or Y bases.
Expand Down Expand Up @@ -46,6 +50,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- #626, #635: The `domain` argument of the `X` and `Z` commands is now mandatory. Previously, it defaulted to the empty set, so that `X(1)` silently had no effect; write `X(1, set())` to state this explicitly.

- #651:
- The default space minimization heuristic `SpaceMinimizationHeuristics.extended_causal_flow`
replaces `causal_flow`, and is optimal for patterns with extended causal flow.
- The specific implementation of `CausalFlow.to_xzcorrections` is removed since it was equivalent to that in its parent class, `GFlow`.
- `OpenGraph.to_pattern` and `Pattern.draw` attempt to extract an extended causal flow rather than a causal flow.

## [0.4.1] - 2026-10-13

### Added
Expand Down
121 changes: 102 additions & 19 deletions graphix/flow/_find_cflow.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
"""Causal flow finding algorithm.
"""Causal flow and extended causal flow finding algorithms.

This module implements Algorithm 1 from Ref. [1]. For a given labelled open graph (G, I, O, meas_plane), this algorithm finds a causal flow [2] in polynomial time with the number of nodes, :math:`O(N^2)`.
This module implements Algorithm 1 from Ref. [1] and an adaptation of it to extended causal flows [3]. For a given labelled open graph (G, I, O, meas_plane), these algorithms find a causal flow [2] or an extended causal flow [3] in polynomial time with the number of nodes, :math:`O(N^2)`.

References
----------
[1] Mhalla and Perdrix, (2008), Finding Optimal Flows Efficiently, doi.org/10.1007/978-3-540-70575-8_70
[2] Browne et al., 2007 New J. Phys. 9 250 (arXiv:quant-ph/0702212)
[3] Backens and Perdrix, (2026), Completeness for flow-preserving rewrite rules, arXiv:2608.13035
"""

from __future__ import annotations

from typing import TYPE_CHECKING

from graphix.flow.core import CausalFlow
from graphix.flow.core import CausalFlow, ExtendedCausalFlow
from graphix.fundamentals import Plane

if TYPE_CHECKING:
from graphix.opengraph import OpenGraph, _PM_co
from collections.abc import Set as AbstractSet
from typing import TypeAlias, TypeVar

from graphix.fundamentals import AbstractPlanarMeasurement
from graphix.opengraph import OpenGraph

FlowArguments: TypeAlias = tuple[dict[int, frozenset[int]], tuple[frozenset[int], ...]]
_PM_co = TypeVar("_PM_co", bound=AbstractPlanarMeasurement, covariant=True)


def find_cflow(og: OpenGraph[_PM_co]) -> CausalFlow[_PM_co] | None:
Expand All @@ -30,12 +38,12 @@ def find_cflow(og: OpenGraph[_PM_co]) -> CausalFlow[_PM_co] | None:
Returns
-------
CausalFlow[_PM_co] | None
A causal flow object if the open graph has causal flow, `None` otherwise.
A causal flow object if the open graph has causal flow, ``None`` otherwise.

Notes
-----
- See Definition 2, Theorem 1 and Algorithm 1 in Ref. [1].
- The open graph instance must be of parametric type `Measurement` or `Plane` since the causal flow is only defined on open graphs with :math:`XY` measurements.
- Causal flow is only defined on open graphs with :math:`XY` measurements: ``None`` is returned if some measurement is in the :math:`XZ` or :math:`YZ` plane. Measurements are labelled by :meth:`AbstractPlanarMeasurement.to_plane`.

References
----------
Expand All @@ -45,10 +53,81 @@ def find_cflow(og: OpenGraph[_PM_co]) -> CausalFlow[_PM_co] | None:
if measurement.to_plane() in {Plane.XZ, Plane.YZ}:
return None

flow_args = _find_flow(og)
if flow_args is None:
return None
return CausalFlow(og, *flow_args)


def find_ecflow(og: OpenGraph[_PM_co]) -> ExtendedCausalFlow[_PM_co] | None:
"""Return the extended causal flow of the input open graph if it exists.

Parameters
----------
og : OpenGraph[_PM_co]
Open graph whose extended causal flow is calculated.

Returns
-------
ExtendedCausalFlow[_PM_co] | None
An extended causal flow object if the open graph has extended causal flow, ``None`` otherwise.

Notes
-----
- See Definition 10 in Ref. [1]. The algorithm is an adaptation of Algorithm 1 in Ref. [2] where, at each step, a :math:`YZ`-measured node is self-corrected and added to the current layer as soon as all its neighbors have been corrected. :math:`YZ`-measured nodes never correct other nodes.
- Extended causal flow is only defined on open graphs with :math:`XY` and :math:`YZ` measurements: ``None`` is returned if some measurement is in the :math:`XZ` plane. Measurements are labelled by :meth:`AbstractPlanarMeasurement.to_plane`.

References
----------
[1] Backens and Perdrix, (2026), Completeness for flow-preserving rewrite rules, arXiv:2608.13035
[2] Mhalla and Perdrix, (2008), Finding Optimal Flows Efficiently, doi.org/10.1007/978-3-540-70575-8_70
"""
yz_candidates: set[int] = set()
for node, measurement in og.measurements.items():
plane = measurement.to_plane()
if plane == Plane.XZ:
return None
if plane == Plane.YZ:
yz_candidates.add(node)

flow_args = _find_flow(og, yz_candidates)
if flow_args is None:
return None
return ExtendedCausalFlow(og, *flow_args)


def _find_flow(og: OpenGraph[_PM_co], yz_candidates: AbstractSet[int] | None = None) -> FlowArguments | None:
"""Compute the correction function and the partial order layers of a causal or an extended causal flow.

This is the routine shared by :func:`find_cflow` and :func:`find_ecflow`. Nodes not in ``yz_candidates`` are treated as :math:`XY`-measured nodes and are corrected by a single neighbor, as in a causal flow. Nodes in ``yz_candidates`` are treated as :math:`YZ`-measured nodes: they are corrected by themselves and never correct other nodes.

Parameters
----------
og : OpenGraph[_PM_co]
Open graph whose flow is calculated. The caller is responsible for discarding open graphs with measurements in the :math:`XZ` plane (and in the :math:`YZ` plane when looking for a causal flow).
yz_candidates : AbstractSet[int] | None, optional
Non-output nodes measured in the :math:`YZ` plane. The set is not modified. If ``None`` (default), it is considered to be empty.

Returns
-------
FlowArguments | None
A tuple ``(correction_function, partial_order_layers)`` used to instantiate a :class:`CausalFlow` or an :class:`ExtendedCausalFlow` if the flow exists, ``None`` otherwise.

Notes
-----
See Definition 2, Theorem 1 and Algorithm 1 in Ref. [1] for causal flow, and Definition 10 in Ref. [2] for extended causal flow.

References
----------
[1] Mhalla and Perdrix, (2008), Finding Optimal Flows Efficiently, doi.org/10.1007/978-3-540-70575-8_70
[2] Backens and Perdrix, (2026), Completeness for flow-preserving rewrite rules, arXiv:2608.13035
"""
yz_candidates = set(yz_candidates) if yz_candidates is not None else set()

graph_nodes = set(og.graph.nodes)
corrected_nodes = set(og.output_nodes)
corrector_candidates = corrected_nodes - set(og.input_nodes)
non_input_nodes = og.graph.nodes - set(og.input_nodes)
iset = set(og.input_nodes)
corrector_candidates = corrected_nodes - iset

cf: dict[int, frozenset[int]] = {}
# Output nodes are always in layer 0.
Expand All @@ -58,25 +137,29 @@ def find_cflow(og: OpenGraph[_PM_co]) -> CausalFlow[_PM_co] | None:
while corrected_nodes != graph_nodes:
corrected_nodes_new: set[int] = set()
corrector_nodes_new: set[int] = set()
curr_layer: set[int] = set()

non_corrected_nodes = og.graph.nodes - corrected_nodes

for p in corrector_candidates:
non_corrected_neighbors = og.neighbors({p}) & non_corrected_nodes
non_corrected_neighbors = og.neighbors({p}) - corrected_nodes
if len(non_corrected_neighbors) == 1:
(q,) = non_corrected_neighbors
cf[q] = frozenset({p})
curr_layer.add(q)
corrected_nodes_new |= {q}
corrector_nodes_new |= {p}
if og.measurements[q].to_plane() == Plane.XY:
cf[q] = frozenset({p})
corrected_nodes_new.add(q)
corrector_nodes_new.add(p)

layers.append(frozenset(curr_layer))
for q in yz_candidates - corrected_nodes_new:
if og.neighbors({q}) <= corrected_nodes:
cf[q] = frozenset({q})
corrected_nodes_new.add(q)

if len(corrected_nodes_new) == 0:
return None

layers.append(frozenset(corrected_nodes_new))
corrected_nodes |= corrected_nodes_new
corrector_candidates = (corrector_candidates - corrector_nodes_new) | (corrected_nodes_new & non_input_nodes)
yz_candidates -= corrected_nodes_new
corrector_candidates = (corrector_candidates - corrector_nodes_new) | {
q for q in corrected_nodes_new if q not in iset and q not in cf[q]
}

return CausalFlow(og, cf, tuple(layers))
return cf, tuple(layers)
Loading
Loading