From ec941d4c06cd11e9fbd507d346c78234d02e61f1 Mon Sep 17 00:00:00 2001 From: John van de Wetering Date: Wed, 23 Oct 2024 21:50:16 +0200 Subject: [PATCH 1/4] Made it so that all files are loaded as multigraphs, and set_auto_simplify is set to False, as is the behaviour when you make a new graph in ZXLive. --- zxlive/dialogs.py | 25 ++++++++++++++++++------- zxlive/editor_base_panel.py | 1 + 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/zxlive/dialogs.py b/zxlive/dialogs.py index 323fdb3..0a9725e 100644 --- a/zxlive/dialogs.py +++ b/zxlive/dialogs.py @@ -139,25 +139,36 @@ def import_diagram_from_file(file_path: str, selected_filter: str = FileFormat.A elif selected_format == FileFormat.ZXRule: return ImportRuleOutput(selected_format, file_path, CustomRule.from_json(data)) elif selected_format in (FileFormat.QGraph, FileFormat.Json): - return ImportGraphOutput(selected_format, file_path, GraphT.from_json(data)) # type: ignore # This is something that needs to be better annotated in PyZX + g = GraphT.from_json(data) + g.set_auto_simplify(False) + return ImportGraphOutput(selected_format, file_path, g) # type: ignore # This is something that needs to be better annotated in PyZX elif selected_format == FileFormat.QASM: - return ImportGraphOutput(selected_format, file_path, Circuit.from_qasm(data).to_graph()) # type: ignore + g = Circuit.from_qasm(data).to_graph(zh=True,backend='multigraph') + g.set_auto_simplify(False) + return ImportGraphOutput(selected_format, file_path, ) # type: ignore elif selected_format == FileFormat.TikZ: try: - return ImportGraphOutput(selected_format, file_path, GraphT.from_tikz(data)) # type: ignore + g = GraphT.from_tikz(data) + g.set_auto_simplify(False) + return ImportGraphOutput(selected_format, file_path, g) # type: ignore except ValueError: raise ValueError("Probable reason: attempted to import a proof from TikZ, which is not supported.") else: assert selected_format == FileFormat.All try: - circ = Circuit.load(file_path) - return ImportGraphOutput(FileFormat.QASM, file_path, circ.to_graph()) # type: ignore + g = Circuit.load(file_path).to_graph(zx=True,backend='multigraph') + g.set_auto_simplify(False) + return ImportGraphOutput(FileFormat.QASM, file_path, g) # type: ignore except TypeError: try: - return ImportGraphOutput(FileFormat.QGraph, file_path, GraphT.from_json(data)) # type: ignore + g = GraphT.from_json(data) + g.set_auto_simplify(False) + return ImportGraphOutput(FileFormat.QGraph, file_path, g) # type: ignore except Exception: try: - return ImportGraphOutput(FileFormat.TikZ, file_path, GraphT.from_tikz(data)) # type: ignore + g = GraphT.from_tikz(data) + g.set_auto_simplify(False) + return ImportGraphOutput(FileFormat.TikZ, file_path, g) # type: ignore except: show_error_msg(f"Failed to import {selected_format.name} file", f"Couldn't determine filetype: {file_path}.", parent=parent) diff --git a/zxlive/editor_base_panel.py b/zxlive/editor_base_panel.py index d04bc60..401114f 100644 --- a/zxlive/editor_base_panel.py +++ b/zxlive/editor_base_panel.py @@ -182,6 +182,7 @@ def add_vert(self, x: float, y: float, edges: list[EItem]) -> None: def add_edge(self, u: VT, v: VT, verts: list[VItem]) -> None: """Add an edge between vertices u and v. `verts` is a list of VItems that collide with the edge. + If self.snap_vertex_edge is true, then we try to connect `u` through all the `vertices` in `verts`, and then to `v`. """ cmd: BaseCommand graph = self.graph_view.graph_scene.g From 612b29e8011602a87f0d494f48fb58b5455e4579 Mon Sep 17 00:00:00 2001 From: John van de Wetering Date: Wed, 23 Oct 2024 22:05:32 +0200 Subject: [PATCH 2/4] mypy complaints --- zxlive/dialogs.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/zxlive/dialogs.py b/zxlive/dialogs.py index 0a9725e..ac0186c 100644 --- a/zxlive/dialogs.py +++ b/zxlive/dialogs.py @@ -140,15 +140,18 @@ def import_diagram_from_file(file_path: str, selected_filter: str = FileFormat.A return ImportRuleOutput(selected_format, file_path, CustomRule.from_json(data)) elif selected_format in (FileFormat.QGraph, FileFormat.Json): g = GraphT.from_json(data) + if TYPE_CHECKING: assert isinstance(g, GraphT) g.set_auto_simplify(False) return ImportGraphOutput(selected_format, file_path, g) # type: ignore # This is something that needs to be better annotated in PyZX elif selected_format == FileFormat.QASM: g = Circuit.from_qasm(data).to_graph(zh=True,backend='multigraph') + if TYPE_CHECKING: assert isinstance(g, GraphT) g.set_auto_simplify(False) return ImportGraphOutput(selected_format, file_path, ) # type: ignore elif selected_format == FileFormat.TikZ: try: g = GraphT.from_tikz(data) + if TYPE_CHECKING: assert isinstance(g, GraphT) g.set_auto_simplify(False) return ImportGraphOutput(selected_format, file_path, g) # type: ignore except ValueError: @@ -157,16 +160,19 @@ def import_diagram_from_file(file_path: str, selected_filter: str = FileFormat.A assert selected_format == FileFormat.All try: g = Circuit.load(file_path).to_graph(zx=True,backend='multigraph') + if TYPE_CHECKING: assert isinstance(g, GraphT) g.set_auto_simplify(False) return ImportGraphOutput(FileFormat.QASM, file_path, g) # type: ignore except TypeError: try: g = GraphT.from_json(data) + if TYPE_CHECKING: assert isinstance(g, GraphT) g.set_auto_simplify(False) return ImportGraphOutput(FileFormat.QGraph, file_path, g) # type: ignore except Exception: try: g = GraphT.from_tikz(data) + if TYPE_CHECKING: assert isinstance(g, GraphT) g.set_auto_simplify(False) return ImportGraphOutput(FileFormat.TikZ, file_path, g) # type: ignore except: From 1f7750eeba4b6130bd1d842006a4b6788bb442fa Mon Sep 17 00:00:00 2001 From: John van de Wetering Date: Wed, 23 Oct 2024 22:10:15 +0200 Subject: [PATCH 3/4] More places where we make sure that set_auto_simplify is correctly set --- zxlive/custom_rule.py | 2 ++ zxlive/proof.py | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/zxlive/custom_rule.py b/zxlive/custom_rule.py index 368ace9..0673245 100644 --- a/zxlive/custom_rule.py +++ b/zxlive/custom_rule.py @@ -184,6 +184,8 @@ def from_json(cls, json_str: Union[str,Dict[str,Any]]) -> "CustomRule": rhs_graph = GraphT.from_json(d['rhs_graph']) # Mypy issue: https://github.com/python/mypy/issues/11673 assert (isinstance(lhs_graph, GraphT) and isinstance(rhs_graph, GraphT)) # type: ignore + lhs_graph.set_auto_simplify(False) + rhs_graph.set_auto_simplify(False) return cls(lhs_graph, rhs_graph, d['name'], d['description']) def to_rewrite_data(self) -> "RewriteData": diff --git a/zxlive/proof.py b/zxlive/proof.py index 4bd79c8..97839ca 100644 --- a/zxlive/proof.py +++ b/zxlive/proof.py @@ -47,6 +47,7 @@ def from_json(json_str: Union[str,Dict[str,Any]]) -> "Rewrite": grouped_rewrites = d.get("grouped_rewrites") graph = GraphT.from_json(d["graph"]) assert isinstance(graph, GraphT) + graph.set_auto_simplify(False) return Rewrite( display_name=d.get("display_name", d["rule"]), # Old proofs may not have display names @@ -213,7 +214,8 @@ def from_json(json_str: Union[str,Dict[str,Any]]) -> "ProofModel": d = json_str initial_graph = GraphT.from_json(d["initial_graph"]) # Mypy issue: https://github.com/python/mypy/issues/11673 - assert isinstance(initial_graph, GraphT) # type: ignore + if TYPE_CHECKING: assert isinstance(initial_graph, GraphT) + initial_graph.set_auto_simplify(False) model = ProofModel(initial_graph) for step in d["proof_steps"]: rewrite = Rewrite.from_json(step) From faade970aaa51db702b5ba08c8dee7334892d2ec Mon Sep 17 00:00:00 2001 From: John van de Wetering Date: Wed, 23 Oct 2024 22:12:59 +0200 Subject: [PATCH 4/4] typo --- zxlive/dialogs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zxlive/dialogs.py b/zxlive/dialogs.py index ac0186c..a598fa5 100644 --- a/zxlive/dialogs.py +++ b/zxlive/dialogs.py @@ -159,7 +159,7 @@ def import_diagram_from_file(file_path: str, selected_filter: str = FileFormat.A else: assert selected_format == FileFormat.All try: - g = Circuit.load(file_path).to_graph(zx=True,backend='multigraph') + g = Circuit.load(file_path).to_graph(zh=True,backend='multigraph') if TYPE_CHECKING: assert isinstance(g, GraphT) g.set_auto_simplify(False) return ImportGraphOutput(FileFormat.QASM, file_path, g) # type: ignore