Browse Source

Local complementation

master
Pete Shadbolt 8 years ago
parent
commit
ec55bd450d
4 changed files with 61 additions and 15 deletions
  1. +4
    -0
      TODO
  2. +29
    -0
      cz.py
  3. +23
    -13
      graph.py
  4. +5
    -2
      tests/test_graph.py

+ 4
- 0
TODO View File

@@ -0,0 +1,4 @@
Next things to do:
- Read A&B code which expresses C(1) as products of sqrt(iX), sqrt(iY)
- Read A&B code which exposes LC API
- Implement the above

+ 29
- 0
cz.py View File

@@ -0,0 +1,29 @@
from graph import Graph
from matplotlib import pyplot as plt

class GraphState(Graph):

def __init__(self, n):
Graph.__init__(self, n)

def local_complementation(self, a):
for i in self.neighbours[a]:
for j in self.neighbours[a]:
if i<j:
self.toggle_edge(i, j)

if __name__ == '__main__':
g = GraphState(10)
g.add_edge(0, 1)
g.add_edge(1, 3)
g.add_edge(3, 2)
g.add_edge(3, 0)
g.add_edge(2, 0)
g.add_edge(0, 5)
g.vops[0]=1
g.draw()

plt.clf()
g.local_complementation(0)
g.draw("out2.pdf")


+ 23
- 13
graph.py View File

@@ -1,36 +1,46 @@
import networkx as nx
from matplotlib import pyplot as plt

vop_colors = ["red", "green", "blue"]

class Graph(object):

def __init__(self, n):
self.vertices = [set() for i in xrange(n)]
self.vops = ["hadamard" for i in xrange(n)]
self.neighbours = [set() for i in xrange(n)]
self.vops = [0 for i in xrange(n)]

def add_edge(self, v1, v2):
self.vertices[v1].add(v2)
self.vertices[v2].add(v1)
self.neighbours[v1].add(v2)
self.neighbours[v2].add(v1)

def del_edge(self, v1, v2):
self.vertices[v1].remove(v2)
self.vertices[v2].remove(v1)
self.neighbours[v1].remove(v2)
self.neighbours[v2].remove(v1)

def has_edge(self, v1, v2):
return v2 in self.neighbours[v1]

def toggle_edge(self, v1, v2):
if self.has_edge(v1, v2):
self.del_edge(v1, v2)
else:
self.add_edge(v1, v2)


def edgelist(self):
edges = frozenset(frozenset((i, n))
for i, v in enumerate(self.vertices)
for i, v in enumerate(self.neighbours)
for n in v)
return [tuple(e) for e in edges]

def draw(self, filename="out.pdf"):
def draw(self, filename="out.pdf", ns=500):
g = nx.from_edgelist(self.edgelist())
pos = nx.spring_layout(g)
nx.draw_networkx_nodes(g, pos, node_color="white", node_size=1000)
colors = [vop_colors[vop] for vop in self.vops]
nx.draw_networkx_nodes(g, pos, node_color="white", node_size=ns)
nx.draw_networkx_nodes(g, pos, node_color=colors, node_size=ns, alpha=.4)
nx.draw_networkx_labels(g, pos)
nx.draw_networkx_edges(g, pos)
for i, vop in enumerate(self.vops):
if not i in pos: continue
x, y = pos[i]
plt.text(x, y+0.1, "hadamard", ha="center")

plt.axis('off')
plt.savefig(filename)


+ 5
- 2
tests/test_graph.py View File

@@ -5,11 +5,14 @@ def test_graph():
g.add_edge(0,1)
g.add_edge(1,2)
g.add_edge(2,0)
assert g.vertices[0]==set([1,2])
assert g.neighbours[0]==set([1,2])

g.del_edge(0,1)
assert g.vertices[0]==set([2])
assert g.neighbours[0]==set([2])
el = g.edgelist()
assert (1,2) in el
assert not (0,1) in el
assert len(el)==2

assert g.has_edge(1,2)
assert not g.has_edge(0,1)

Loading…
Cancel
Save