Skip to content

Commit

Permalink
Update the uses of EdgeType and VertexType
Browse files Browse the repository at this point in the history
Fix related typing errors
  • Loading branch information
boldar99 committed Jul 3, 2024
1 parent 7c588aa commit 922acd9
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 43 deletions.
12 changes: 6 additions & 6 deletions zxlive/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)

Expand Down
4 changes: 3 additions & 1 deletion zxlive/common.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
Expand Down
24 changes: 13 additions & 11 deletions zxlive/construct.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -37,16 +39,16 @@ 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:
nelist.append((edge[0]+qubits, edge[1]+qubits, edge[2]))

# 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

Expand Down
4 changes: 2 additions & 2 deletions zxlive/edit_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 14 additions & 14 deletions zxlive/editor_base_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)},
Expand All @@ -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))},
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion zxlive/eitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions zxlive/graphscene.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion zxlive/proof_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions zxlive/rule_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions zxlive/vitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 922acd9

Please sign in to comment.