Преглед изворни кода

Can now sketchily delete nodes. Needs work.

master
Pete Shadbolt пре 8 година
родитељ
комит
37dd9265ce
4 измењених фајлова са 32 додато и 17 уклоњено
  1. +10
    -0
      abp/graphstate.py
  2. +2
    -2
      abp/static/scripts/editor.js
  3. +13
    -15
      bin/abpserver
  4. +7
    -0
      tests/test_graphstate.py

+ 10
- 0
abp/graphstate.py Прегледај датотеку

@@ -47,6 +47,16 @@ class GraphState(object):
self._add_node(self, *args, **kwargs)


def _del_node(self, node):
""" Remove a node. TODO: this is a hack right now! """
if not node in self.node:
return
del self.node[node]
for k in self.adj[node]:
del self.adj[k][node]
del self.adj[node]


def _add_node(self, node, **kwargs):
""" Add a node. By default, nodes are initialized with ``vop=``:math:`I`, i.e. they are in the :math:`|+\\rangle` state.



+ 2
- 2
abp/static/scripts/editor.js Прегледај датотеку

@@ -77,7 +77,7 @@ editor.onCtrlClick = function() {
if (found === undefined){ return; }
if (editor.selection === undefined){ return; }
editor.focus(found);
abj.act_hadamard(found);
websocket.edit({action:"hadamard", node:found});
gui.serverMessage("Acted H on node " + found + ".");
graph.update();
};
@@ -134,7 +134,7 @@ editor.findNodeOnRay = function(ray) {

editor.deleteNode = function() {
if (editor.selection === undefined){ return; }
abj.del_node(editor.selection);
websocket.edit({action:"delete", node:editor.selection});
graph.update();
gui.serverMessage("Deleted node " + editor.selection + ".");
editor.selection = undefined;


+ 13
- 15
bin/abpserver Прегледај датотеку

@@ -18,32 +18,28 @@ from pkg_resources import resource_filename
clients = []
local_state = abp.GraphState()

def process_edit(edit, server):
def process_edit(edit, client, server):
action = edit["action"]

print edit

print local_state.node.keys()

try:
if action == "create":
print type(edit["name"])
local_state.add_qubit(edit["name"], position=edit["position"], vop=0)
if action == "cz":
local_state.act_cz(edit["start"], edit["end"])
except Exception as e:
print e

server.send_message_to_all(json.dumps(local_state.to_json()))

if action == "create":
local_state.add_qubit(edit["name"], position=edit["position"], vop=0)
if action == "cz":
local_state.act_cz(edit["start"], edit["end"])
if action == "hadamard":
local_state.act_cz(edit["start"], edit["end"])
if action == "delete":
local_state._del_node(edit["node"])

server.send_message(client, json.dumps(local_state.to_json()))

def new_message(client, server, message):
if message.startswith("edit:"):
print message[5:]
edit = json.loads(message[5:])
print "Received update from javascript."
process_edit(edit, server)
process_edit(edit, client, server)
else:
print "Received update from python {}.".format(client["id"])
print message
@@ -54,6 +50,8 @@ def new_message(client, server, message):
def new_client(client, server):
print "Client {} connected.".format(client["id"])
clients.append(client)
print "Sent state of {} nodes to client {}".format(local_state.order(), client["id"])
server.send_message(client, json.dumps(local_state.to_json()))

def client_left(client, server):
print "Client {} disconnected.".format(client["id"])


+ 7
- 0
tests/test_graphstate.py Прегледај датотеку

@@ -131,3 +131,10 @@ def test_from_nx():

psi = GraphState(nx.Graph(((0, 1),)))

def test_del_node():
""" Test deleting nodes """
g = GraphState(10)
g.act_circuit(mock.random_stabilizer_circuit())
g._del_node(0)
assert g.order() == 9


Loading…
Откажи
Сачувај