@@ -1,4 +1,4 @@ | |||||
# Alias some stuff to make imports cleaner | # Alias some stuff to make imports cleaner | ||||
from abp.graphstate import GraphState | from abp.graphstate import GraphState | ||||
from abp.qi import CircuitModel | from abp.qi import CircuitModel | ||||
from abp.graphstate import VisibleGraphState | |||||
from abp.visiblegraphstate import VisibleGraphState |
@@ -6,27 +6,24 @@ import itertools as it | |||||
import clifford | import clifford | ||||
import json | import json | ||||
import qi | import qi | ||||
import networkx as nx | |||||
from websocket import create_connection | |||||
import atexit | |||||
try: | |||||
import networkx as nx | |||||
except ImportError: | |||||
print "Could not import networkx." | |||||
class GraphState(object): | |||||
class GraphState(): | |||||
def __init__(self, nodes=[]): | def __init__(self, nodes=[]): | ||||
self.ngbh = {} | |||||
self.vops = {} | |||||
self.meta = {} | |||||
try: | |||||
self.add_nodes(nodes) | |||||
except TypeError: | |||||
self.add_nodes(xrange(nodes)) | |||||
def add_node(self, v): | |||||
self.ngbh, self.vops, self.meta = {}, {}, {} | |||||
self.add_nodes(nodes) | |||||
def add_node(self, v, meta={}): | |||||
""" Add a node """ | """ Add a node """ | ||||
self.ngbh[v] = set() | self.ngbh[v] = set() | ||||
self.vops[v] = clifford.by_name["hadamard"] | self.vops[v] = clifford.by_name["hadamard"] | ||||
self.meta[v] = dict() | |||||
self.meta[v] = meta | |||||
def add_nodes(self, nodes): | def add_nodes(self, nodes): | ||||
""" Add a buncha nodes """ | """ Add a buncha nodes """ | ||||
@@ -62,10 +59,10 @@ class GraphState(object): | |||||
def edgelist(self): | def edgelist(self): | ||||
""" Describe a graph as an edgelist """ | """ Describe a graph as an edgelist """ | ||||
edges = frozenset(tuple(sorted((i, n))) | |||||
edges = set(tuple(sorted((i, n))) | |||||
for i, v in self.ngbh.items() | for i, v in self.ngbh.items() | ||||
for n in v) | for n in v) | ||||
return [tuple(e) for e in edges] | |||||
return tuple(edges) | |||||
def remove_vop(self, a, avoid): | def remove_vop(self, a, avoid): | ||||
""" Reduces VOP[a] to the identity """ | """ Reduces VOP[a] to the identity """ | ||||
@@ -230,30 +227,4 @@ class GraphState(object): | |||||
return self.ngbh == other.ngbh and self.vops == other.vops | return self.ngbh == other.ngbh and self.vops == other.vops | ||||
class VisibleGraphState(GraphState): | |||||
def __init__(self, *args, **kwargs): | |||||
GraphState.__init__(self, *args, **kwargs) | |||||
self.ws = create_connection("ws://localhost:5001") | |||||
atexit.register(self.ws.close) | |||||
self.send("clear") | |||||
def send(self, method, *args, **kwargs): | |||||
kwargs.update({"method": method}) | |||||
self.ws.send(json.dumps(kwargs)) | |||||
def add_node(self, node): | |||||
GraphState.add_node(self, node) | |||||
self.send("add_node", node=node) | |||||
def add_edge(self, start, end): | |||||
GraphState.add_edge(self, start, end) | |||||
self.send("add_edge", start=start, end=end) | |||||
def del_edge(self, start, end): | |||||
GraphState.del_edge(self, start, end) | |||||
self.send("del_edge", start=start, end=end) | |||||
def act_local_rotation(self, node, operation): | |||||
GraphState.del_edge(self, start, end) | |||||
self.send("update_vop", node=node, vop=self.vops[node]) |
@@ -0,0 +1,51 @@ | |||||
""" | |||||
Allows us to visualize the state in a browser | |||||
""" | |||||
import atexit | |||||
import threading | |||||
import time | |||||
from websocket import create_connection | |||||
from graphstate import GraphState | |||||
import json | |||||
class VisibleGraphState(GraphState): | |||||
def __init__(self, *args, **kwargs): | |||||
GraphState.__init__(self, *args, **kwargs) | |||||
self.ws = create_connection("ws://localhost:5001") | |||||
self.diff = [] | |||||
atexit.register(self.shutdown) | |||||
self.ws.send(json.dumps({"method":"clear"})) | |||||
def shutdown(self): | |||||
if len(self.diff)>0: | |||||
self.update() | |||||
self.ws.close() | |||||
def send(self, method, *args, **kwargs): | |||||
kwargs.update({"method": method}) | |||||
self.diff.append(kwargs) | |||||
def add_node(self, node, meta = {}): | |||||
GraphState.add_node(self, node, meta) | |||||
self.send("add_node", node=node, meta=meta) | |||||
def add_edge(self, start, end): | |||||
GraphState.add_edge(self, start, end) | |||||
self.send("add_edge", start=start, end=end) | |||||
def del_edge(self, start, end): | |||||
GraphState.del_edge(self, start, end) | |||||
self.send("del_edge", start=start, end=end) | |||||
def act_local_rotation(self, node, operation): | |||||
GraphState.act_local_rotation(self, node, operation) | |||||
self.send("update_vop", node = node, vop = self.vops[node]) | |||||
def update(self): | |||||
#data = json.dumps({"diff": self.diff, "state":self.to_json()}) | |||||
data = json.dumps({"diff": self.diff}) | |||||
self.ws.send(data) | |||||
self.diff = [] | |||||
@@ -0,0 +1,10 @@ | |||||
from abp import VisibleGraphState | |||||
s = VisibleGraphState() | |||||
for i in range(1000): | |||||
s.add_node(i) | |||||
s.act_local_rotation(i, "hadamard") | |||||
s.update() | |||||
for i in range(1000-1): | |||||
s.act_cz(i, i+1) | |||||
s.update() |
@@ -2,9 +2,10 @@ from websocket_server import WebsocketServer | |||||
import threading | import threading | ||||
clients = [] | clients = [] | ||||
state = "awd" | |||||
#state = "awd" | |||||
def new_message(client, server, message): | def new_message(client, server, message): | ||||
print "Sending message ..." | |||||
server.send_message_to_all(message) | server.send_message_to_all(message) | ||||
def new_client(client, server): | def new_client(client, server): | ||||
@@ -1,17 +0,0 @@ | |||||
import requests | |||||
import abp, json | |||||
import time | |||||
def test_client(): | |||||
client = abp.Client(clear=True) | |||||
client.clear() | |||||
for i in range(100): | |||||
client.add_node(i) | |||||
client.act_local_rotation(i, 10) | |||||
for i in range(100-1): | |||||
client.act_cz(i, i+1) | |||||
print client.get_state() | |||||