diff --git a/zxlive/custom_rule.py b/zxlive/custom_rule.py index a19e37e..cff75f3 100644 --- a/zxlive/custom_rule.py +++ b/zxlive/custom_rule.py @@ -271,11 +271,8 @@ def filter_matchings_if_symbolic_compatible(matchings: list[Dict[VT, VT]], left: for matching in matchings: if len(matching) != len(left): continue - try: - match_symbolic_parameters(matching, left, right) - new_matchings.append(matching) - except ValueError: - pass + match_symbolic_parameters(matching, left, right) + new_matchings.append(matching) return new_matchings diff --git a/zxlive/dialogs.py b/zxlive/dialogs.py index a598fa5..6efb796 100644 --- a/zxlive/dialogs.py +++ b/zxlive/dialogs.py @@ -228,10 +228,7 @@ def save_diagram_dialog(graph: GraphT, parent: QWidget) -> Optional[tuple[str, F file_path, selected_format = file_path_and_format if selected_format in (FileFormat.QGraph, FileFormat.Json): - try: - graph.auto_detect_io() - except TypeError: - pass + graph.auto_detect_io() data = graph.to_json() elif selected_format == FileFormat.QASM: try: diff --git a/zxlive/rewrite_action.py b/zxlive/rewrite_action.py index 6b13517..762d5f9 100644 --- a/zxlive/rewrite_action.py +++ b/zxlive/rewrite_action.py @@ -79,6 +79,8 @@ def do_rewrite(self, panel: ProofPanel) -> None: try: g, rem_verts = self.apply_rewrite(g, matches) + except ValueError as ex: + raise ex except Exception as ex: show_error_msg('Error while applying rewrite rule', str(ex)) return diff --git a/zxlive/rewrite_data.py b/zxlive/rewrite_data.py index bb31864..3c0cc9b 100644 --- a/zxlive/rewrite_data.py +++ b/zxlive/rewrite_data.py @@ -50,6 +50,24 @@ def read_custom_rules() -> list[RewriteData]: custom_rules.append(rule) return custom_rules +const_true = lambda graph, matches: matches + + +def apply_simplification(simplification: Callable[[GraphT], Optional[int]]) -> Callable[ + [GraphT, list], pyzx.rules.RewriteOutputType[VT, ET]]: + def rule(g: GraphT, matches: list) -> pyzx.rules.RewriteOutputType[VT, ET]: + simplification(g) + return ({}, [], [], True) + + return rule + + +def _extract_circuit(graph: GraphT, matches: list) -> GraphT: + graph.auto_detect_io() + simplify.full_reduce(graph) + return cast(GraphT, extract_circuit(graph).to_graph()) + + # We want additional actions that are not part of the original PyZX editor # So we add them to operations @@ -67,7 +85,7 @@ def read_custom_rules() -> list[RewriteData]: "text": "pivot", "tooltip": "Deletes a pair of spiders with 0/pi phases by performing a pivot", "matcher": lambda g, matchf: pyzx.rules.match_pivot_parallel(g, matchf, check_edge_types=True), - "rule": pyzx.rules.pivot, + "rule": apply_simplification(simplify.pivot_simp), "type": MATCHES_EDGES, "copy_first": True, "picture": "pivot_regular.png" @@ -108,24 +126,6 @@ def read_custom_rules() -> list[RewriteData]: }, } -const_true = lambda graph, matches: matches - - -def apply_simplification(simplification: Callable[[GraphT], Optional[int]]) -> Callable[ - [GraphT, list], pyzx.rules.RewriteOutputType[VT, ET]]: - def rule(g: GraphT, matches: list) -> pyzx.rules.RewriteOutputType[VT, ET]: - simplification(g) - return ({}, [], [], True) - - return rule - - -def _extract_circuit(graph: GraphT, matches: list) -> GraphT: - graph.auto_detect_io() - simplify.full_reduce(graph) - return cast(GraphT, extract_circuit(graph).to_graph()) - - # The OCM action simply saves the current graph without modifying anything. # This can be used to make repositioning the vertices an explicit proof step. def ocm_rule(_graph: GraphT, _matches: list) -> pyzx.rules.RewriteOutputType[VT, ET]: diff --git a/zxlive/rule_panel.py b/zxlive/rule_panel.py index d78c223..f295b7e 100644 --- a/zxlive/rule_panel.py +++ b/zxlive/rule_panel.py @@ -97,12 +97,9 @@ def add_edge(self, u: VT, v: VT, verts: list[VItem]) -> None: self.update_io_labels(self.graph_scene) def update_io_labels(self, scene: EditGraphScene) -> None: - try: - scene.g.auto_detect_io() - for v in scene.g.vertices(): - if v in scene.g.inputs(): - scene.vertex_map[v].phase_item.setPlainText("in-" + str(scene.g.inputs().index(v))) - elif v in scene.g.outputs(): - scene.vertex_map[v].phase_item.setPlainText("out-" + str(scene.g.outputs().index(v))) - except TypeError: - return + scene.g.auto_detect_io() + for v in scene.g.vertices(): + if v in scene.g.inputs(): + scene.vertex_map[v].phase_item.setPlainText("in-" + str(scene.g.inputs().index(v))) + elif v in scene.g.outputs(): + scene.vertex_map[v].phase_item.setPlainText("out-" + str(scene.g.outputs().index(v))) diff --git a/zxlive/vitem.py b/zxlive/vitem.py index 7e2dff7..8ac11c2 100644 --- a/zxlive/vitem.py +++ b/zxlive/vitem.py @@ -107,6 +107,7 @@ def g(self) -> GraphT: @property def ty(self) -> VertexType: + # https://github.com/zxcalc/zxlive/pull/281 _ty: VertexType = self.g.type(self.v) return _ty