Skip to content

Conversation

@matulni
Copy link
Contributor

@matulni matulni commented Jan 5, 2026

This commit introduces tooling to extract an XZCorrections object from a Pattern. It drops all the remaining dependencies on graphix.gflow. Additionally it fixes two unreported bugs (see discussion below).

Description of changes

  • Introduced the following methods:

    • graphix.optimization.StandardizedPattern.extract_xzcorrections

      • Constructs the X- and Z-corrections mappings by reading out the pattern's measurements and corrections.
      • Calls the constructor XZCorrections.from_measured_nodes_mapping.
    • graphix.pattern.Pattern.extract_xzcorrections

      • Wrapper which standardizes the pattern and calls the XZ-corrections extraction routine.
  • Modified the constructor XZCorrections.from_measured_nodes_mapping so that it doesn't need to create a nx.DiGraph instance. Instead it exploits graphix.flow._partial_order.compute_topological_generations introduced in Refactor of flow tools - Flow from pattern #393. This simplies the code in the public API and fixes an unreported bug in the method (see Code snippets section).

  • Fixed an unreported bug in OpenGraph.is_equal_structurally which failed to compare open graphs differing on the output nodes only.

  • The modules graphix.gflow and graphix.find_pflow are removed. Note that graphix.visualization does not fully exploit the new flow API but this is out of the scope of this PR.

  • Method graphix.Pattern.get_layers is removed (see bug Pattern.get_layers assumes that inputs cannot be outputs #367) in favor of existing graphix.Pattern.extract_partial_order_layers.

Code snippets

The following correction maps and open graph are incorrectly parsed by the current implementation of XZCorrections.from_measured_nodes_mapping in master. This is fixed in this PR.

from graphix.flow.core import XZCorrections
from graphix.opengraph import OpenGraph
from graphix.measurements import Measurement
from graphix.fundamentals import Plane
import networkx as nx

x_corr = {1: {2}, 2: {3}, 0: {4}, 4: {5}, 5: {6}, 6: {7}, 3: {8}, 8: {9}, 7: {10}, 10: {11}, 9: {12}, 12: {13}, 11: {14}, 14: {15}, 15: {16}, 16: {17}, 13: {18}, 18: {19}}
z_corr = {1: {0, 3}, 0: {3, 5}, 5: {7}, 3: {9}, 7: {9, 11}, 9: {11, 13}, 11: {15}, 14: {16}, 15: {17}, 13: {19}}

og = OpenGraph(graph= nx.Graph([(3, 4), (3, 8), (3, 2), (4, 5), (4, 0), (12, 13), (12, 9), (12, 11), (13, 18), (0, 2), (2, 1), (8, 9), (9, 10), (11, 14), (11, 10), (14, 15), (7, 10), (7, 6), (18, 19), (5, 6), (15, 16), (16, 17)]),input_nodes=[0, 1], output_nodes=[17, 19], measurements={1: Measurement(angle=0.0, plane=Plane.XY), 2: Measurement(angle=0.0, plane=Plane.XY), 0: Measurement(angle=0.0, plane=Plane.XY), 4: Measurement(angle=0.0, plane=Plane.XY), 5: Measurement(angle=0.0, plane=Plane.XY), 6: Measurement(angle=-1, plane=Plane.XY), 3: Measurement(angle=-0.5, plane=Plane.XY), 8: Measurement(angle=0.0, plane=Plane.XY), 7: Measurement(angle=0.0, plane=Plane.XY), 10: Measurement(angle=0.0, plane=Plane.XY), 9: Measurement(angle=0.0, plane=Plane.XY), 12: Measurement(angle=0.0, plane=Plane.XY), 11: Measurement(angle=0.5, plane=Plane.XY), 14: Measurement(angle=1.0, plane=Plane.XY), 15: Measurement(angle=-0.5, plane=Plane.XY), 16: Measurement(angle=0.0, plane=Plane.XY), 13: Measurement(angle=0.0, plane=Plane.XY), 18: Measurement(angle=0.25, plane=Plane.XY)})

xz = XZCorrections.from_measured_nodes_mapping(og=og, x_corrections=x_corr, z_corrections=z_corr)

print(xz.partial_order_layers[:2]) # -> (frozenset({17, 19}), frozenset({16, 19})). Node 19 appears twice
xz.to_pattern() # Assertion error due to incorrect partial order

@codecov
Copy link

codecov bot commented Jan 5, 2026

Codecov Report

❌ Patch coverage is 98.03922% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 86.56%. Comparing base (611e4bf) to head (a34eeb4).

Files with missing lines Patch % Lines
graphix/flow/core.py 96.77% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #407      +/-   ##
==========================================
+ Coverage   85.60%   86.56%   +0.95%     
==========================================
  Files          46       44       -2     
  Lines        6677     6126     -551     
==========================================
- Hits         5716     5303     -413     
+ Misses        961      823     -138     

☔ 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.

@matulni matulni changed the base branch from rf_flow_from_p to master January 6, 2026 15:05
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.

Looks good! Two minor comments.

Comment on lines 1068 to 1069
assert dict(xzc.x_corrections) == {}
assert dict(xzc.z_corrections) == {}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think casting to dict is not necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Opss, you are right. I had used a defaultdict somewhere before but I changed everything to dict. Fixed in 06dad95

og, x_corrections, z_corrections
) # Raises an `XZCorrectionsError` if mappings are not well formed.

return XZCorrections(og, x_corrections, z_corrections, tuple(partial_order_layers))
Copy link
Collaborator

Choose a reason for hiding this comment

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

The casting to tuple is no longer necessary since _corrections_to_partial_order_layers returns a tuple.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 06dad95

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.

LGTM! Thanks.

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.

2 participants