Skip to content

Commit bd2d157

Browse files
committed
appease mypy
1 parent 6777102 commit bd2d157

File tree

6 files changed

+31
-22
lines changed

6 files changed

+31
-22
lines changed

pyzx/graph/base.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -823,15 +823,15 @@ def num_vertices(self) -> int:
823823
"""Returns the amount of vertices in the graph."""
824824
raise NotImplementedError("Not implemented on backend " + type(self).backend)
825825

826-
def num_edges(self) -> int:
826+
def num_edges(self, s: Optional[VT]=None, t: Optional[VT]=None) -> int:
827827
"""Returns the amount of edges in the graph"""
828-
raise NotImplementedError("Not implemented on backend " + type(self).backend)
828+
return len(list(self.edges(s, t)))
829829

830-
def vertices(self) -> Sequence[VT]:
830+
def vertices(self) -> Iterable[VT]:
831831
"""Iterator over all the vertices."""
832832
raise NotImplementedError("Not implemented on backend " + type(self).backend)
833833

834-
def edges(self, s: Optional[VT]=None, t: Optional[VT]=None) -> Sequence[ET]:
834+
def edges(self, s: Optional[VT]=None, t: Optional[VT]=None) -> Iterable[ET]:
835835
"""Iterator that returns all the edges in the graph, or all the edges connecting the pair of vertices.
836836
Output type depends on implementation in backend."""
837837
raise NotImplementedError("Not implemented on backend " + type(self).backend)

pyzx/graph/graph_s.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# limitations under the License.
1616

1717
from fractions import Fraction
18-
from typing import Tuple, Dict, Set, Any
18+
from typing import Optional, Tuple, Dict, Set, Any
1919

2020
from .base import BaseGraph
2121

@@ -202,9 +202,16 @@ def remove_edge(self, edge):
202202
def num_vertices(self):
203203
return len(self.graph)
204204

205-
def num_edges(self):
206-
#return self.nedges
207-
return len(self.edge_set())
205+
def num_edges(self, s=None, t=None):
206+
if s is not None and t is not None:
207+
if self.connected(s, t):
208+
return 1
209+
else:
210+
return 0
211+
elif s is not None:
212+
return self.vertex_degree(s)
213+
else:
214+
return len(list(self.edges()))
208215

209216
def vertices(self):
210217
return self.graph.keys()

pyzx/graph/multigraph.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,11 @@ def remove_edge(self, edge):
255255
def num_vertices(self):
256256
return len(self.graph)
257257

258-
def num_edges(self):
259-
return self.nedges
260-
#return len(self.edge_set())
258+
def num_edges(self, s=None, t=None):
259+
if s != None or t != None:
260+
return len(list(self.edges(s, t)))
261+
else:
262+
return self.nedges
261263

262264
def vertices(self):
263265
return self.graph.keys()

pyzx/pauliweb.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ def __init__(self, g: BaseGraph[VT,ET], c: Set[VT]):
4444
self.g = g
4545
self.c = c
4646

47-
def vertices(self):
47+
def vertices(self) -> Set[VT]:
4848
vs = self.c.copy()
4949
for v in self.c:
5050
vs |= set(self.g.neighbors(v))
5151
return vs
5252

53-
def half_edges(self):
53+
def half_edges(self) -> Dict[Tuple[VT,VT],str]:
5454
es: Dict[Tuple[VT,VT],str] = dict()
5555
for v in self.c:
5656
for e in self.g.incident_edges(v):
@@ -67,7 +67,7 @@ def half_edges(self):
6767
es[(v1,v)] = multiply_paulis(t2, ty)
6868
return es
6969

70-
def boundary(self):
70+
def boundary(self) -> Set[VT]:
7171
b: Dict[VT, int] = dict()
7272
for v in self.c:
7373
for n in self.g.neighbors(v):
@@ -136,8 +136,8 @@ def preprocess(g: BaseGraph[VT,ET]):
136136
return (in_circ, out_circ)
137137

138138

139-
def transpose_corrections(c) -> Dict[VT, Set[VT]]:
140-
ct = dict()
139+
def transpose_corrections(c: Dict[VT, Set[VT]]) -> Dict[VT, Set[VT]]:
140+
ct: Dict[VT, Set[VT]] = dict()
141141
for k,s in c.items():
142142
for v in s:
143143
if v not in ct: ct[v] = set()

pyzx/rules.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ def match_bialg_parallel(
122122
v1n = [n for n in g.neighbors(v1) if not n == v0]
123123
if (all([types[n] == v1t and phases[n] == 0 for n in v0n]) and # all neighbors of v0 are of the same type as v1
124124
all([types[n] == v0t and phases[n] == 0 for n in v1n]) and # all neighbors of v1 are of the same type as v0
125-
len(g.edges(v0, v1)) == 1 and # there is exactly one edge between v0 and v1
126-
len(g.edges(v0, v0)) == 0 and # there are no self-loops on v0
127-
len(g.edges(v1, v1)) == 0): # there are no self-loops on v1
125+
g.num_edges(v0, v1) == 1 and # there is exactly one edge between v0 and v1
126+
g.num_edges(v0, v0) == 0 and # there are no self-loops on v0
127+
g.num_edges(v1, v1) == 0): # there are no self-loops on v1
128128
i += 1
129129
for vn in [v0n, v1n]:
130130
for v in vn:

pyzx/simplify.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,8 @@ def max_cut(g: BaseGraph[VT,ET], vs0: Optional[Set[VT]]=None, vs1: Optional[Set[
318318
319319
This uses the quadratic-time SG3 heuristic explained by Wang et al in https://arxiv.org/abs/2312.10895 .
320320
"""
321-
if vs0 == None: vs0 = set()
322-
if vs1 == None: vs1 = set()
321+
if vs0 is None: vs0 = set()
322+
if vs1 is None: vs1 = set()
323323
# print(f'vs0={vs0} vs1={vs1}')
324324
remaining = set(g.vertices()) - vs0 - vs1
325325
while len(remaining) > 0:
@@ -337,7 +337,7 @@ def max_cut(g: BaseGraph[VT,ET], vs0: Optional[Set[VT]]=None, vs1: Optional[Set[
337337
in0 = wt0 >= wt1
338338
# print(f'choosing {v_max} for set {"vs0" if in0 else "vs1"}')
339339

340-
if v_max == None: raise RuntimeError("No max found")
340+
if v_max is None: raise RuntimeError("No max found")
341341
remaining.remove(v_max)
342342
if in0: vs0.add(v_max)
343343
else: vs1.add(v_max)

0 commit comments

Comments
 (0)