소스 검색

Fix edgelist generation with infinite graphs

master
Pete Shadbolt 8 년 전
부모
커밋
96fd7e2a0c
3개의 변경된 파일41개의 추가작업 그리고 37개의 파일을 삭제
  1. +20
    -19
      graph.py
  2. +19
    -18
      tests/test_graph.py
  3. +2
    -0
      tests/test_viz.py

+ 20
- 19
graph.py 파일 보기

@@ -8,30 +8,29 @@ import tables as clifford


class GraphState(object):

def __init__(self):
self.ngbh = defaultdict(set)
self.vops = defaultdict(int)


def add_edge(self, v1, v2):
""" Add an edge between two vertices in the self """
if not v1 in self.ngbh: self.vops[v1] = clifford.by_name["hadamard"]
if not v2 in self.ngbh: self.vops[v2] = clifford.by_name["hadamard"]
if not v1 in self.ngbh:
self.vops[v1] = clifford.by_name["hadamard"]
if not v2 in self.ngbh:
self.vops[v2] = clifford.by_name["hadamard"]
self.ngbh[v1].add(v2)
self.ngbh[v2].add(v1)


def del_edge(self, v1, v2):
""" Delete an edge between two vertices in the self """
self.ngbh[v1].remove(v2)
self.ngbh[v2].remove(v1)


def has_edge(self, v1, v2):
""" Test existence of an edge between two vertices in the self """
return v2 in self.ngbh[v1]


def toggle_edge(self, v1, v2):
""" Toggle an edge between two vertices in the self """
if self.has_edge(v1, v2):
@@ -41,18 +40,30 @@ class GraphState(object):

def edgelist(self):
""" Describe a graph as an edgelist """
edges = frozenset(frozenset((i, n))
for i, v in enumerate(self.ngbh.values())
edges = frozenset(tuple(sorted((i, n)))
for i, v in self.ngbh.items()
for n in v)
return [tuple(e) for e in edges]

def remove_vop(self, a, avoid):
""" Reduces VOP[a] to the identity, avoiding (if possible) the use of vertex b as a swapping partner """
""" Reduces VOP[a] to the identity """
others = self.ngbh[a] - {avoid}
swap_qubit = others.pop() if others else avoid
for v in reversed(clifford.decompositions[self.vops[a]]):
self.local_complementation(a if v == "x" else swap_qubit)

def local_complementation(self, v):
""" As defined in LISTING 1 of Anders & Briegel """
for i, j in it.combinations(self.ngbh[v], 2):
self.toggle_edge(i, j)

# Update VOPs
self.vops[v] = clifford.times_table[
self.vops[v]][clifford.by_name["sqx"]]
for i in self.ngbh[v]:
self.vops[i] = clifford.times_table[
self.vops[i]][clifford.by_name["msqz"]]

def cphase(self, a, b):
""" Act a controlled-phase gate on two qubits """
if self.ngbh[a] - {b}:
@@ -66,13 +77,3 @@ class GraphState(object):
if new_edge != edge:
self.toggle_edge(a, b)

def local_complementation(self, v):
""" As defined in LISTING 1 of Anders & Briegel """
for i, j in it.combinations(self.ngbh[v], 2):
self.toggle_edge(i, j)

# Update VOPs
self.vops[v] = clifford.times_table[self.vops[v]][clifford.by_name["sqx"]]
for i in self.ngbh[v]:
self.vops[i] = clifford.times_table[self.vops[i]][clifford.by_name["msqz"]]


+ 19
- 18
tests/test_graph.py 파일 보기

@@ -1,30 +1,26 @@
from graph import GraphState
import tables as lc

def test_graph():
def demograph():
g = GraphState()
g.add_edge(0,1)
g.add_edge(1,2)
g.add_edge(2,0)
assert g.ngbh[0]==set([1,2])
g.add_edge(0,3)
g.add_edge(100,200)
return g

def test_graph():
g = demograph()
assert g.ngbh[0]==set([1,2,3])
g.del_edge(0,1)
assert g.ngbh[0]==set([2])
el = g.edgelist()
assert (1,2) in el
assert not (0,1) in el
assert len(el)==2

assert g.ngbh[0]==set([2, 3])
assert g.has_edge(1,2)
assert not g.has_edge(0,1)

def test_local_complementation():
""" Test that local complementation works as expected """
g = GraphState()
g.add_edge(0,1)
g.add_edge(1,2)
g.add_edge(2,0)
g.add_edge(0,3)
g = demograph()
g.local_complementation(0)
assert g.has_edge(0, 1)
assert g.has_edge(0, 2)
@@ -36,11 +32,7 @@ def test_local_complementation():

def test_remove_vop():
""" Test that removing VOPs really works """
g = GraphState()
g.add_edge(0,1)
g.add_edge(1,2)
g.add_edge(2,0)
g.add_edge(0,3)
g = demograph()
g.remove_vop(0, 1)
assert g.vops[0] == lc.by_name["identity"]
g.remove_vop(1, 1)
@@ -50,3 +42,12 @@ def test_remove_vop():
g.remove_vop(0, 1)
assert g.vops[0] == lc.by_name["identity"]

def test_edgelist():
""" Test making edgelists """
g = demograph()
el = g.edgelist()
assert (0,3) in el
assert (0,2) in el
assert (100,200) in el



+ 2
- 0
tests/test_viz.py 파일 보기

@@ -7,5 +7,7 @@ def test_viz():
g.add_edge(1,2)
g.add_edge(2,0)
g.add_edge(0,3)
g.add_edge(100,200)
print g.vops
#g.remove_vop(0, 1)
viz.draw(g)

불러오는 중...
취소
저장