Skip to content

Fix #449: rewrite causal-flow algorithm iteratively.#455

Merged
matulni merged 9 commits intoTeamGraphix:masterfrom
matulni:cf-iter
Mar 5, 2026
Merged

Fix #449: rewrite causal-flow algorithm iteratively.#455
matulni merged 9 commits intoTeamGraphix:masterfrom
matulni:cf-iter

Conversation

@matulni
Copy link
Contributor

@matulni matulni commented Mar 2, 2026

This commit replaces the recursion call in the causal flow algorithm by a while loop. This rewriting prevents the RecursionError reported in #449 for linear graphs with more than 1000 nodes.

Example

import timeit

import networkx as nx
from graphix.fundamentals import Plane
from graphix.opengraph import OpenGraph


def create_line_og(n_nodes: int) -> OpenGraph[Plane]:
    if n_nodes < 2:
        raise ValueError("n_nodes must be larger than 1.")

    graph: nx.Graph[int] = nx.Graph([(i, i + 1) for i in range(n_nodes - 1)])
    input_nodes = [0]
    output_nodes = [n_nodes - 1]
    measurements = dict.fromkeys(range(n_nodes - 1), Plane.XY)
    return OpenGraph(graph, input_nodes, output_nodes, measurements)

def compute_flow(n_nodes: int):
    og = create_line_og(n_nodes)
    def _compute_flow():
        og.extract_causal_flow()
    return _compute_flow

n_nodes = 5000
t = timeit.Timer(compute_flow(n_nodes))
print(t.timeit(5))  # 6.928544000002148

TODO

Update CHANGELOG

@codecov
Copy link

codecov bot commented Mar 2, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.29%. Comparing base (daf62cb) to head (6810248).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #455      +/-   ##
==========================================
- Coverage   88.29%   88.29%   -0.01%     
==========================================
  Files          44       44              
  Lines        6535     6533       -2     
==========================================
- Hits         5770     5768       -2     
  Misses        765      765              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Collaborator

@thierry-martinez thierry-martinez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this nice change! I just have a few minor comments.

Copy link
Contributor

@emlynsg emlynsg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with Thierry's comments. Nice fix and clean tests!

matulni and others added 2 commits March 4, 2026 14:12
Co-authored-by: thierry-martinez <thierry.martinez@inria.fr>
@matulni matulni requested a review from thierry-martinez March 4, 2026 13:21
layers: list[frozenset[int]] = [
frozenset(corrected_nodes)
frozenset(og.output_nodes)
] # A copy is necessary because `corrected_nodes` is mutable and changes during the algorithm.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is no longer relevant. The whole expression can now fit on the same line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh sorry, I misunderstood which comment you referred to. Done in 115d800

@matulni matulni merged commit b70ffeb into TeamGraphix:master Mar 5, 2026
24 checks passed
emlynsg pushed a commit that referenced this pull request Mar 6, 2026
This commit replaces the recursion call in the causal flow algorithm by a while loop. This rewriting prevents the RecursionError reported in #449 for linear graphs with more than 1000 nodes.

---------

Co-authored-by: thierry-martinez <thierry.martinez@inria.fr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants