From 87c4c4f0a8b7d1d3aa74969019a1fa007d5d7cb3 Mon Sep 17 00:00:00 2001 From: Pete Shadbolt Date: Sat, 7 May 2016 17:22:31 +0100 Subject: [PATCH] Working on JSON --- abp/graph.py | 6 ++++++ abp/server.py | 17 +++++++++++------ static/index.html | 2 +- static/main.js | 43 ++++++++++++++++++++++++++++++++++++++----- static/poll.js | 6 +----- tests/test_graph.py | 12 ++++-------- 6 files changed, 61 insertions(+), 25 deletions(-) diff --git a/abp/graph.py b/abp/graph.py index 4f70d08..13ef3a3 100644 --- a/abp/graph.py +++ b/abp/graph.py @@ -5,6 +5,7 @@ Provides an extremely basic graph structure, based on neighbour lists from collections import defaultdict import itertools as it import clifford +import json class GraphState(object): @@ -93,6 +94,11 @@ class GraphState(object): return "graph:\n vops: {}\n ngbh: {}\n"\ .format(str(dict(self.vops)), str(dict(self.ngbh))) + def to_json(self): + """ Convert the graph to JSON form """ + ngbh = {key: tuple(value) for key, value in self.ngbh.items()} + return json.dumps({"vops": self.vops, "ngbh": ngbh}) + class DiffedGraphState(GraphState): """ Just like a graph state, but tracks changes for rendering purposes """ diff --git a/abp/server.py b/abp/server.py index 70ac192..1b8f493 100644 --- a/abp/server.py +++ b/abp/server.py @@ -8,8 +8,11 @@ import threading import time import os + class VizHandler(SimpleHTTPRequestHandler): + """ Handles requests to the server """ + def __init__(self, *args, **kwargs): SimpleHTTPRequestHandler.__init__(self, *args, **kwargs) @@ -19,8 +22,8 @@ class VizHandler(SimpleHTTPRequestHandler): self.send_header('Content-Type', 'application/json') self.end_headers() state = self.server.state - self.wfile.write(json.dumps({"state":"{}".format(state)})) - + self.wfile.write(json.dumps({"state": "{}".format(state)})) + def do_GET(self, *args, **kwargs): """ Someone belled the server """ parsed_path = urlparse.urlparse(self.path) @@ -29,14 +32,17 @@ class VizHandler(SimpleHTTPRequestHandler): else: return SimpleHTTPRequestHandler.do_GET(self, *args, **kwargs) + class Server(SocketServer.TCPServer): + """ Serves the good stuff """ allow_reuse_address = True - def __init__(self, port = 8000): + def __init__(self, port=8000): self.port = port self.state = None - SocketServer.TCPServer.__init__(self, ("127.0.0.1", self.port), VizHandler) + SocketServer.TCPServer.__init__( + self, ("127.0.0.1", self.port), VizHandler) def update(self, state): """ Update the in-memory state """ @@ -62,11 +68,10 @@ if __name__ == '__main__': server = Server() server.start() - i=0 + i = 0 while True: server.update(i) i += 1 time.sleep(1) server.shutdown() - diff --git a/static/index.html b/static/index.html index 68ac6a2..9190527 100644 --- a/static/index.html +++ b/static/index.html @@ -11,7 +11,7 @@ - + diff --git a/static/main.js b/static/main.js index efa28ac..8b17aaa 100644 --- a/static/main.js +++ b/static/main.js @@ -16,6 +16,7 @@ var curveProperties = { curvature: 10 }; var camera; +var qubits; // Run on startup window.onload = init; @@ -38,6 +39,15 @@ function makeEdge(e) { return line; } +function makeQubits() { + qubitGeometry = new THREE.Geometry(); + qubitGeometry.labels = []; + var vertex = new THREE.Vector3(0, 0, 0); + qubitGeometry.vertices.push(vertex); + particles = new THREE.Points(qubitGeometry, materials.qubit); + return particles; +} + // Clear the whole scene function makeScene() { // Scene, controls, camera and so on @@ -50,27 +60,38 @@ function makeScene() { linewidth: 1 }; materials.edge = new THREE.LineBasicMaterial(lineStyle); + var pointStyle = { - size: 0.2, + color: "0xcccccc", + size: 0.1, map: materials.sprite, alphaTest: 0.5, transparent: true, - vertexColors: THREE.VertexColors }; materials.point = new THREE.PointsMaterial(pointStyle); + var qubitStyle = { + size: 0.6, + map: materials.sprite, + alphaTest: 0.5, + transparent: true, + color: "red" + }; + materials.qubit = new THREE.PointsMaterial(qubitStyle); + // Build all the edges //var edgeGroup = new THREE.Object3D(); + qubits = makeQubits(); + myScene.add(qubits); + // Build all the nodes nodeGeometry = new THREE.Geometry(); nodeGeometry.labels = []; - nodeGeometry.colors = []; for (var i = 0; i < 10; ++i) { for (var j = 0; j < 10; ++j) { var vertex = new THREE.Vector3(i - 5, j - 5, 0); nodeGeometry.vertices.push(vertex); - nodeGeometry.colors.push(new THREE.Color(0.5, 0.5, 0.5)); nodeGeometry.labels.push("Click to add a qubit at (" + i + ", " + j + ")"); } } @@ -145,6 +166,14 @@ function onMouseMove(event) { checkIntersections(); } +function onClick(event){ + if (!selection){return;} + console.log(nodeGeometry.vertices[selection]); + qubits.geometry.dynamic = true; + qubits.geometry.vertices.push(nodeGeometry.vertices[selection].clone()); + qubits.geometry.verticesNeedUpdate = true; +} + // Render the current frame to the screen function render() { renderer.render(scene, camera); @@ -160,7 +189,8 @@ function loopForever() { // This just organises kickoff function startMainLoop() { scene = makeScene(); - document.addEventListener("mousemove", onMouseMove, false); + renderer.domElement.addEventListener("mousemove", onMouseMove, false); + renderer.domElement.addEventListener("click", onClick, false); controls.addEventListener("change", render); loopForever(); } @@ -194,6 +224,9 @@ function init() { controls.rotateSpeed = 0.2; camera.position.set(0, 0, 20); + // Start polling + setInterval(poll, 1000); + // Run startMainLoop(); } diff --git a/static/poll.js b/static/poll.js index a63a79f..def1d41 100644 --- a/static/poll.js +++ b/static/poll.js @@ -4,7 +4,7 @@ var state; function poll() { var xhr = new XMLHttpRequest(); - xhr.onload=function() { + xhr.onload = function() { state = JSON.parse(xhr.response); soft_console.innerHTML = "\n" + xhr.responseText; }; @@ -17,7 +17,3 @@ function poll() { xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8'); xhr.send(); } - -window.onload = function () { - setInterval(poll, 1000); -} diff --git a/tests/test_graph.py b/tests/test_graph.py index 86d19fb..1890f2b 100644 --- a/tests/test_graph.py +++ b/tests/test_graph.py @@ -67,6 +67,7 @@ def test_stress(): g.add_edge(i, i + 1) assert time.clock() - t < .5 + def test_cz(): """ Test CZ gate """ g = GraphState() @@ -75,9 +76,9 @@ def test_cz(): g.act_local_rotation(0, clifford.by_name["hadamard"]) g.act_local_rotation(1, clifford.by_name["hadamard"]) g.act_local_rotation(1, clifford.by_name["py"]) - print g + assert not g.has_edge(0, 1) g.act_cz(0, 1) - print g + assert g.has_edge(0, 1) def test_diff(): @@ -88,10 +89,5 @@ def test_diff(): g.act_local_rotation(0, clifford.by_name["hadamard"]) g.act_local_rotation(1, clifford.by_name["hadamard"]) g.act_local_rotation(1, clifford.by_name["py"]) - print g g.act_cz(0, 1) - print g.diff - - - - + assert len(g.diff) == 3