From 922acd9a3a4956e6c7325934cac7f3eeee227b57 Mon Sep 17 00:00:00 2001 From: Boldi Date: Wed, 3 Jul 2024 10:15:55 +0100 Subject: [PATCH] Update the uses of EdgeType and VertexType Fix related typing errors --- zxlive/commands.py | 12 ++++++------ zxlive/common.py | 4 +++- zxlive/construct.py | 24 +++++++++++++----------- zxlive/edit_panel.py | 4 ++-- zxlive/editor_base_panel.py | 28 ++++++++++++++-------------- zxlive/eitem.py | 2 +- zxlive/graphscene.py | 5 ++--- zxlive/proof_panel.py | 2 +- zxlive/rule_panel.py | 4 ++-- zxlive/vitem.py | 4 ++-- 10 files changed, 46 insertions(+), 43 deletions(-) diff --git a/zxlive/commands.py b/zxlive/commands.py index 10c607e7..5b14009f 100644 --- a/zxlive/commands.py +++ b/zxlive/commands.py @@ -101,11 +101,11 @@ def redo(self) -> None: class ChangeNodeType(BaseCommand): """Changes the color of a set of spiders.""" vs: list[VT] | set[VT] - vty: VertexType.Type + vty: VertexType WInfo = namedtuple('WInfo', ['partner', 'partner_type', 'partner_pos', 'neighbors']) - _old_vtys: Optional[list[VertexType.Type]] = field(default=None, init=False) + _old_vtys: Optional[list[VertexType]] = field(default=None, init=False) _old_w_info: Optional[dict[VT, WInfo]] = field(default=None, init=False) _new_w_inputs: Optional[list[VT]] = field(default=None, init=False) @@ -170,7 +170,7 @@ def redo(self) -> None: class ChangeEdgeColor(BaseCommand): """Changes the color of a set of edges""" es: Iterable[ET] - ety: EdgeType.Type + ety: EdgeType _old_etys: Optional[list[EdgeType]] = field(default=None, init=False) @@ -192,7 +192,7 @@ class AddNode(BaseCommand): """Adds a new spider at a given position.""" x: float y: float - vty: VertexType.Type + vty: VertexType _added_vert: Optional[VT] = field(default=None, init=False) @@ -236,7 +236,7 @@ class AddEdge(BaseCommand): """Adds an edge between two spiders.""" u: VT v: VT - ety: EdgeType.Type + ety: EdgeType def undo(self) -> None: self.g.remove_edge((self.u, self.v, self.ety)) @@ -291,7 +291,7 @@ class AddIdentity(BaseCommand): """Adds an X or Z identity spider on an edge between two vertices.""" u: VT v: VT - vty: VertexType.Type + vty: VertexType _new_vert: Optional[VT] = field(default=None, init=False) diff --git a/zxlive/common.py b/zxlive/common.py index 8ab14c91..a09d65a8 100644 --- a/zxlive/common.py +++ b/zxlive/common.py @@ -1,6 +1,8 @@ import os from enum import IntEnum from typing import Final, Dict, Any + +from pyzx import EdgeType from typing_extensions import TypeAlias from PySide6.QtCore import QSettings @@ -21,7 +23,7 @@ def get_custom_rules_path() -> str: VT: TypeAlias = int -ET: TypeAlias = tuple[int, int, int] +ET: TypeAlias = tuple[int, int, EdgeType] GraphT: TypeAlias = pyzx.graph.multigraph.Multigraph def new_graph() -> GraphT: g = GraphT() diff --git a/zxlive/construct.py b/zxlive/construct.py index 6a695f81..93188c60 100644 --- a/zxlive/construct.py +++ b/zxlive/construct.py @@ -11,20 +11,22 @@ def construct_circuit() -> GraphT: # numbered from 0 to 3. # id, qubit number, vertex type (1 = Z, 2 = X). + z, x = VertexType.Z, VertexType.X vlist = [ - (0, 0, 1), (1, 1, 2), (2, 2, 1), (3, 3, 1), (4, 0, 1), (5, 1, 1), - (6, 2, 2), (7, 3, 1), (8, 0, 1), (9, 1, 2), (10, 2, 1), (11, 3, 1), - (12, 0, 2), (13, 1, 2), (14, 2, 1), (15, 3, 2)] + (0, 0, z), (1, 1, x), (2, 2, z), (3, 3, z), (4, 0, z), (5, 1, z), + (6, 2, x), (7, 3, z), (8, 0, z), (9, 1, x), (10, 2, z), (11, 3, z), + (12, 0, x), (13, 1, x), (14, 2, z), (15, 3, x)] # id1, id2, edge type (0 = SIMPLE, 1 = HADAMARD) + s, h = EdgeType.SIMPLE, EdgeType.HADAMARD elist = [ - (0, 1, 0), (0, 4, 0), (1, 5, 0), (1, 6, 0), (2, 6, 0), (3, 7, 0), - (4, 8, 0), (5, 9, 1), (6, 10, 0), (7, 11, 0), (8, 12, 0), (8, 13, 0), - (9, 13, 1), (9, 14, 1), (10, 13, 0), (10, 14, 0), (11, 14, 0), - (11, 15, 0)] + (0, 1, s), (0, 4, s), (1, 5, s), (1, 6, s), (2, 6, s), (3, 7, s), + (4, 8, s), (5, 9, h), (6, 10, s), (7, 11, s), (8, 12, s), (8, 13, s), + (9, 13, h), (9, 14, h), (10, 13, s), (10, 14, s), (11, 14, s), + (11, 15, s)] nvertices = len(vlist) + (2 * qubits) - nvlist: list[tuple[int, int, VertexType.Type]] = [] + nvlist: list[tuple[int, int, VertexType]] = [] # Adding inputs nodes to the nvlist. for i in range(qubits): nvlist.append((i, i, VertexType.BOUNDARY)) @@ -37,7 +39,7 @@ def construct_circuit() -> GraphT: for i in range(qubits): nvlist.append((nvertices - qubits + i, i, VertexType.BOUNDARY)) - nelist = [] + nelist: list[tuple[int, int, EdgeType]] = [] # Updating the user provided elist to include input indices for edge in elist: @@ -45,8 +47,8 @@ def construct_circuit() -> GraphT: # Adding the edges between inputs nodes and output nodes to internal nodes for i in range(qubits): - nelist.append((i, i+qubits, 0)) - nelist.append((nvertices - qubits + i, nvertices - (2*qubits) + i, 0)) + nelist.append((i, i+qubits, EdgeType.SIMPLE)) + nelist.append((nvertices - qubits + i, nvertices - (2*qubits) + i, EdgeType.SIMPLE)) cur_row = [1] * qubits diff --git a/zxlive/edit_panel.py b/zxlive/edit_panel.py index bd5a5ba8..d0e24807 100644 --- a/zxlive/edit_panel.py +++ b/zxlive/edit_panel.py @@ -25,8 +25,8 @@ class GraphEditPanel(EditorBasePanel): graph_scene: EditGraphScene start_derivation_signal = Signal(object) - _curr_ety: EdgeType.Type - _curr_vty: VertexType.Type + _curr_ety: EdgeType + _curr_vty: VertexType def __init__(self, graph: GraphT, *actions: QAction) -> None: super().__init__(*actions) diff --git a/zxlive/editor_base_panel.py b/zxlive/editor_base_panel.py index 92913ce1..ad06122f 100644 --- a/zxlive/editor_base_panel.py +++ b/zxlive/editor_base_panel.py @@ -38,7 +38,7 @@ class DrawPanelNodeType(TypedDict): icon: tuple[ShapeType, QColor] -def vertices_data() -> dict[VertexType.Type, DrawPanelNodeType]: +def vertices_data() -> dict[VertexType, DrawPanelNodeType]: return { VertexType.Z: {"text": "Z spider", "icon": (ShapeType.CIRCLE, colors.z_spider)}, VertexType.X: {"text": "X spider", "icon": (ShapeType.CIRCLE, colors.x_spider)}, @@ -48,7 +48,7 @@ def vertices_data() -> dict[VertexType.Type, DrawPanelNodeType]: VertexType.BOUNDARY: {"text": "boundary", "icon": (ShapeType.CIRCLE, colors.w_input)}, } -def edges_data() -> dict[EdgeType.Type, DrawPanelNodeType]: +def edges_data() -> dict[EdgeType, DrawPanelNodeType]: return { EdgeType.SIMPLE: {"text": "Simple", "icon": (ShapeType.LINE, QColor(BLACK))}, EdgeType.HADAMARD: {"text": "Hadamard", "icon": (ShapeType.DASHED_LINE, QColor(HAD_EDGE_BLUE))}, @@ -63,8 +63,8 @@ class EditorBasePanel(BasePanel): start_derivation_signal = Signal(object) sidebar: QSplitter - _curr_ety: EdgeType.Type - _curr_vty: VertexType.Type + _curr_ety: EdgeType + _curr_vty: VertexType def __init__(self, *actions: QAction) -> None: super().__init__(*actions) @@ -96,21 +96,21 @@ def update_colors(self) -> None: def _tool_clicked(self, tool: ToolType) -> None: self.graph_scene.curr_tool = tool - def _vty_clicked(self, vty: VertexType.Type) -> None: + def _vty_clicked(self, vty: VertexType) -> None: self._curr_vty = vty - def _vty_double_clicked(self, vty: VertexType.Type) -> None: + def _vty_double_clicked(self, vty: VertexType) -> None: self._curr_vty = vty selected = list(self.graph_scene.selected_vertices) if len(selected) > 0: cmd = ChangeNodeType(self.graph_view, selected, vty) self.undo_stack.push(cmd) - def _ety_clicked(self, ety: EdgeType.Type) -> None: + def _ety_clicked(self, ety: EdgeType) -> None: self._curr_ety = ety self.graph_scene.curr_ety = ety - def _ety_double_clicked(self, ety: EdgeType.Type) -> None: + def _ety_double_clicked(self, ety: EdgeType) -> None: self._curr_ety = ety self.graph_scene.curr_ety = ety selected = list(self.graph_scene.selected_edges) @@ -314,9 +314,9 @@ def toolbar_select_node_edge(parent: EditorBasePanel) -> ToolbarSection: def create_list_widget(parent: EditorBasePanel, - data: dict[VertexType.Type, DrawPanelNodeType] | dict[EdgeType.Type, DrawPanelNodeType], - onclick: Callable[[VertexType.Type], None] | Callable[[EdgeType.Type], None], - ondoubleclick: Callable[[VertexType.Type], None] | Callable[[EdgeType.Type], None]) \ + data: dict[VertexType, DrawPanelNodeType] | dict[EdgeType, DrawPanelNodeType], + onclick: Callable[[VertexType], None] | Callable[[EdgeType], None], + ondoubleclick: Callable[[VertexType], None] | Callable[[EdgeType], None]) \ -> QListWidget: list_widget = QListWidget(parent) list_widget.setResizeMode(QListView.ResizeMode.Adjust) @@ -331,9 +331,9 @@ def create_list_widget(parent: EditorBasePanel, def populate_list_widget(list_widget: QListWidget, - data: dict[VertexType.Type, DrawPanelNodeType] | dict[EdgeType.Type, DrawPanelNodeType], - onclick: Callable[[VertexType.Type], None] | Callable[[EdgeType.Type], None], - ondoubleclick: Callable[[VertexType.Type], None] | Callable[[EdgeType.Type], None]) \ + data: dict[VertexType, DrawPanelNodeType] | dict[EdgeType, DrawPanelNodeType], + onclick: Callable[[VertexType], None] | Callable[[EdgeType], None], + ondoubleclick: Callable[[VertexType], None] | Callable[[EdgeType], None]) \ -> None: row = list_widget.currentRow() list_widget.clear() diff --git a/zxlive/eitem.py b/zxlive/eitem.py index 8c083640..5a4d5226 100644 --- a/zxlive/eitem.py +++ b/zxlive/eitem.py @@ -152,7 +152,7 @@ def mouseReleaseEvent(self, e: QGraphicsSceneMouseEvent) -> None: class EDragItem(QGraphicsPathItem): """A QGraphicsItem representing an edge in construction during a drag""" - def __init__(self, g: GraphT, ety: EdgeType.Type, start: VItem, mouse_pos: QPointF) -> None: + def __init__(self, g: GraphT, ety: EdgeType, start: VItem, mouse_pos: QPointF) -> None: super().__init__() self.setZValue(EITEM_Z) self.g = g diff --git a/zxlive/graphscene.py b/zxlive/graphscene.py index e809c65a..4233a87c 100644 --- a/zxlive/graphscene.py +++ b/zxlive/graphscene.py @@ -141,8 +141,7 @@ def update_graph(self, new: GraphT, select_new: bool = False) -> None: if select_new: selected_vertices.add(v) - for e, typ in diff.new_edges: - s, t = self.g.edge_st(e) + for (s, t), typ in diff.new_edges: e = (s,t,typ) if e not in self.edge_map: self.edge_map[e] = {} @@ -235,7 +234,7 @@ class EditGraphScene(GraphScene): # Currently selected edge type for preview when dragging # to add a new edge - curr_ety: EdgeType.Type + curr_ety: EdgeType curr_tool: ToolType # The vertex a right mouse button drag was initiated on diff --git a/zxlive/proof_panel.py b/zxlive/proof_panel.py index 70202117..03b7891b 100644 --- a/zxlive/proof_panel.py +++ b/zxlive/proof_panel.py @@ -206,7 +206,7 @@ def _magic_identity(self, trace: WandTrace) -> bool: t = self.graph.edge_t(item.e) if self.identity_choice[0].isChecked(): - vty: VertexType.Type = VertexType.Z + vty: VertexType = VertexType.Z elif self.identity_choice[1].isChecked(): vty = VertexType.X else: diff --git a/zxlive/rule_panel.py b/zxlive/rule_panel.py index 04657781..dbc4cd66 100644 --- a/zxlive/rule_panel.py +++ b/zxlive/rule_panel.py @@ -24,8 +24,8 @@ class RulePanel(EditorBasePanel): graph_scene_right: EditGraphScene start_derivation_signal = Signal(object) - _curr_ety: EdgeType.Type - _curr_vty: VertexType.Type + _curr_ety: EdgeType + _curr_vty: VertexType def __init__(self, graph1: GraphT, graph2: GraphT, name: str, description: str, *actions: QAction) -> None: diff --git a/zxlive/vitem.py b/zxlive/vitem.py index 55263e99..68f71f81 100644 --- a/zxlive/vitem.py +++ b/zxlive/vitem.py @@ -105,8 +105,8 @@ def g(self) -> GraphT: return self.graph_scene.g @property - def ty(self) -> VertexType.Type: - _ty: VertexType.Type = self.g.type(self.v) + def ty(self) -> VertexType: + _ty: VertexType = self.g.type(self.v) return _ty @property