From 37dd9265ce95dff7b08402e7a4769b6471540224 Mon Sep 17 00:00:00 2001 From: Pete Shadbolt Date: Sat, 5 Nov 2016 16:34:36 +0000 Subject: [PATCH] Can now sketchily delete nodes. Needs work. --- abp/graphstate.py | 10 ++++++++++ abp/static/scripts/editor.js | 4 ++-- bin/abpserver | 28 +++++++++++++--------------- tests/test_graphstate.py | 7 +++++++ 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/abp/graphstate.py b/abp/graphstate.py index a62c888..b8f4fde 100644 --- a/abp/graphstate.py +++ b/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. diff --git a/abp/static/scripts/editor.js b/abp/static/scripts/editor.js index f5bc9e7..6d0ca91 100644 --- a/abp/static/scripts/editor.js +++ b/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; diff --git a/bin/abpserver b/bin/abpserver index 15ed0f7..25786d5 100755 --- a/bin/abpserver +++ b/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"]) diff --git a/tests/test_graphstate.py b/tests/test_graphstate.py index 7c09f05..5670507 100644 --- a/tests/test_graphstate.py +++ b/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 +