diff --git a/graphix/flow/core.py b/graphix/flow/core.py index fa674c3fd..20c90207b 100644 --- a/graphix/flow/core.py +++ b/graphix/flow/core.py @@ -1378,13 +1378,18 @@ def _check_flow_general_properties(flow: PauliFlow[_AM_co]) -> None: - The nodes in the partial order are the nodes in the open graph. - The first layer of the partial order layers is :math:`O`, the output nodes of the open graph. This is guaranteed because open graphs without outputs do not have flow. """ + if len(flow.og.graph.nodes) == 0: + return + if not _check_correction_function_domain(flow.og, flow.correction_function): raise FlowGenericError(FlowGenericErrorReason.IncorrectCorrectionFunctionDomain) if not _check_correction_function_image(flow.og, flow.correction_function): raise FlowGenericError(FlowGenericErrorReason.IncorrectCorrectionFunctionImage) - if len(flow.partial_order_layers) == 0: + if len(flow.partial_order_layers) == 0 and len(flow.og.graph.nodes) > 0: + if not flow.og.graph: + return raise PartialOrderError(PartialOrderErrorReason.Empty) first_layer = flow.partial_order_layers[0] diff --git a/tests/test_empty_graph_fix.py b/tests/test_empty_graph_fix.py new file mode 100644 index 000000000..e56f43fc4 --- /dev/null +++ b/tests/test_empty_graph_fix.py @@ -0,0 +1,21 @@ +from __future__ import annotations + +from typing import TYPE_CHECKING + +import networkx as nx + +from graphix.opengraph import OpenGraph + +if TYPE_CHECKING: + from graphix.measurement import Measurement + + +def test_empty_graph_well_formed() -> None: + """Test that empty graphs pass the well-formedness check. + + This is a regression test for issue #531. + """ + og: OpenGraph[Measurement] = OpenGraph(graph=nx.Graph(), input_nodes=[], output_nodes=[], measurements={}) + pf = og.extract_causal_flow() + pf.check_well_formed() + assert True diff --git a/tests/test_issue_531.py b/tests/test_issue_531.py new file mode 100644 index 000000000..2755cca54 --- /dev/null +++ b/tests/test_issue_531.py @@ -0,0 +1,15 @@ +from __future__ import annotations + +import networkx as nx + +from graphix.opengraph import OpenGraph + + +def test_empty_graph(): + og = OpenGraph(graph=nx.Graph(), input_nodes=[], output_nodes=[], measurements={}) + pf = og.extract_causal_flow() + pf.check_well_formed() # This should NOT raise PartialOrderError anymore + print("✅ Test passed! Empty graph is now well-formed.") + +if __name__ == "__main__": + test_empty_graph()