Browse Source

Implement full editing interface

master
Pete Shadbolt 6 years ago
parent
commit
d253418d83
7 changed files with 91 additions and 38 deletions
  1. +36
    -0
      app.py
  2. +41
    -0
      static/scripts/api.js
  3. +10
    -10
      static/scripts/editor.js
  4. +0
    -1
      static/scripts/graph.js
  5. +0
    -23
      static/scripts/poller.js
  6. +1
    -1
      templates/index.html
  7. +3
    -3
      test.py

+ 36
- 0
app.py View File

@@ -1,6 +1,7 @@
from flask import Flask, request, redirect, url_for, make_response, render_template, Markup, send_from_directory, send_file from flask import Flask, request, redirect, url_for, make_response, render_template, Markup, send_from_directory, send_file
from flask_redis import FlaskRedis from flask_redis import FlaskRedis
import json, abp, markdown import json, abp, markdown
from pprint import pprint


app = Flask(__name__) app = Flask(__name__)
redis = FlaskRedis(app) redis = FlaskRedis(app)
@@ -28,6 +29,41 @@ def graph():
else: else:
# Get from the database # Get from the database
return redis.get("graph") return redis.get("graph")


@app.route("/edit", methods=["POST"])
def edit():
# Load the graph from the database
g = abp.GraphState()
g.from_json(json.loads(redis.get("graph")))

# Apply the edit to the graph
edit = json.loads(request.data)
action = edit["action"]
pprint(edit, indent=2)

if action == "create":
g.add_qubit(edit["name"], position=edit["position"], vop=0)
elif action == "cz":
g.act_cz(edit["start"], edit["end"])
elif action == "hadamard":
g.act_hadamard(edit["node"])
elif action == "phase":
g.act_local_rotation(edit["node"], "phase")
elif action == "delete":
g._del_node(edit["node"])
elif action == "localcomplementation":
g.local_complementation(edit["node"])
elif action == "measure":
g.measure(edit["node"], "p"+edit["basis"])
else:
pass

# New state into JSON and database
data = json.dumps(g.to_json(stringify=True))
redis.execute_command("SET", "graph", data)
return data
@app.route("/doc") @app.route("/doc")
def doc(): def doc():


+ 41
- 0
static/scripts/api.js View File

@@ -0,0 +1,41 @@
var api = {};

api.poll = function() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE) {
if (xmlhttp.status == 200) {
api.update(xmlhttp.responseText);
}
}
};

// Send the request
xmlhttp.open("GET", "/graph", true);
xmlhttp.send();
};


api.update = function(s) {
json = JSON.parse(s);
graph.update(json);
}

api.edit = function(edit) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE) {
if (xmlhttp.status == 200) {
api.update(xmlhttp.responseText);
}
}
};

// Send the request
xmlhttp.open("POST", "/edit", true);
xmlhttp.setRequestHeader("Content-Type", "application/json");
xmlhttp.send(JSON.stringify(edit));
}

// Launch the polling loop
setInterval(api.poll, 1000);

+ 10
- 10
static/scripts/editor.js View File

@@ -48,7 +48,7 @@ editor.addQubitAtPoint = function(point) {
gui.serverMessage("Node " + new_node +" already exists."); gui.serverMessage("Node " + new_node +" already exists.");
return; return;
} }
websocket.edit({action:"create", name:new_node, position: point});
api.edit({action:"create", name:new_node, position: point});
editor.focus(new_node); editor.focus(new_node);
gui.serverMessage("Created node " + new_node +"."); gui.serverMessage("Created node " + new_node +".");
}; };
@@ -77,7 +77,7 @@ editor.onShiftClick = function() {
if (editor.selection === undefined){ return; } if (editor.selection === undefined){ return; }
if (found === editor.selection){ return; } if (found === editor.selection){ return; }
//abj.act_cz(found, editor.selection); //abj.act_cz(found, editor.selection);
websocket.edit({action:"cz", start:found, end:editor.selection});
api.edit({action:"cz", start:found, end:editor.selection});
gui.serverMessage("Acted CZ between " + found + " & " + editor.selection + "."); gui.serverMessage("Acted CZ between " + found + " & " + editor.selection + ".");
editor.focus(found); editor.focus(found);
}; };
@@ -87,7 +87,7 @@ editor.onCtrlClick = function() {
if (found === undefined){ return; } if (found === undefined){ return; }
if (editor.selection === undefined){ return; } if (editor.selection === undefined){ return; }
editor.focus(found); editor.focus(found);
websocket.edit({action:"hadamard", node:found});
api.edit({action:"hadamard", node:found});
gui.serverMessage("Acted H on node " + found + "."); gui.serverMessage("Acted H on node " + found + ".");
}; };


@@ -144,7 +144,7 @@ editor.findNodeOnRay = function(ray) {


editor.deleteNode = function() { editor.deleteNode = function() {
if (editor.selection === undefined){ return; } if (editor.selection === undefined){ return; }
websocket.edit({action:"delete", node:editor.selection});
api.edit({action:"delete", node:editor.selection});
gui.serverMessage("Deleted node " + editor.selection + "."); gui.serverMessage("Deleted node " + editor.selection + ".");
editor.selection = undefined; editor.selection = undefined;
node_data.className = "hidden"; node_data.className = "hidden";
@@ -154,37 +154,37 @@ editor.deleteNode = function() {


editor.hadamard = function() { editor.hadamard = function() {
if (editor.selection === undefined){ return; } if (editor.selection === undefined){ return; }
websocket.edit({action:"hadamard", node:editor.selection});
api.edit({action:"hadamard", node:editor.selection});
gui.serverMessage("Acted Hadamard on node " + editor.selection + "."); gui.serverMessage("Acted Hadamard on node " + editor.selection + ".");
}; };


editor.phase = function() { editor.phase = function() {
if (editor.selection === undefined){ return; } if (editor.selection === undefined){ return; }
websocket.edit({action:"phase", node:editor.selection});
api.edit({action:"phase", node:editor.selection});
gui.serverMessage("Acted phase on node " + editor.selection + "."); gui.serverMessage("Acted phase on node " + editor.selection + ".");
}; };


editor.measureX = function() { editor.measureX = function() {
if (editor.selection === undefined){ return; } if (editor.selection === undefined){ return; }
websocket.edit({action:"measure", node:editor.selection, basis:"x"});
api.edit({action:"measure", node:editor.selection, basis:"x"});
gui.serverMessage("Measured node " + editor.selection + " in X."); gui.serverMessage("Measured node " + editor.selection + " in X.");
}; };


editor.measureY = function() { editor.measureY = function() {
if (editor.selection === undefined){ return; } if (editor.selection === undefined){ return; }
websocket.edit({action:"measure", node:editor.selection, basis:"y"});
api.edit({action:"measure", node:editor.selection, basis:"y"});
gui.serverMessage("Measured node " + editor.selection + " in Y."); gui.serverMessage("Measured node " + editor.selection + " in Y.");
}; };


editor.measureZ = function() { editor.measureZ = function() {
if (editor.selection === undefined){ return; } if (editor.selection === undefined){ return; }
websocket.edit({action:"measure", node:editor.selection, basis:"z"});
api.edit({action:"measure", node:editor.selection, basis:"z"});
gui.serverMessage("Measured node " + editor.selection + " in z."); gui.serverMessage("Measured node " + editor.selection + " in z.");
}; };


editor.localComplementation = function() { editor.localComplementation = function() {
if (editor.selection === undefined){ return; } if (editor.selection === undefined){ return; }
websocket.edit({action:"localcomplementation", node:editor.selection});
api.edit({action:"localcomplementation", node:editor.selection});
abj.local_complementation(editor.selection); abj.local_complementation(editor.selection);
gui.serverMessage("Inverted neighbourhood of " + editor.selection + "."); gui.serverMessage("Inverted neighbourhood of " + editor.selection + ".");
}; };

+ 0
- 1
static/scripts/graph.js View File

@@ -48,7 +48,6 @@ graph.update = function(newState) {
} }


if (editor.selection) { if (editor.selection) {
console.log(editor.selection);
var node = editor.selection; var node = editor.selection;
if (Object.prototype.hasOwnProperty.call(abj.node, node)) { if (Object.prototype.hasOwnProperty.call(abj.node, node)) {
editor.grid.position.copy(abj.node[node].position); editor.grid.position.copy(abj.node[node].position);


+ 0
- 23
static/scripts/poller.js View File

@@ -1,23 +0,0 @@
function poll() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE) {
if (xmlhttp.status == 200) {
update(xmlhttp.responseText);
}
}
};

// Send the request
xmlhttp.open("GET", "/graph", true);
xmlhttp.send();
};


function update(s) {
json = JSON.parse(s);
graph.update(json);
}

// Launch the polling loop
setInterval(poll, 1000);

+ 1
- 1
templates/index.html View File

@@ -14,7 +14,7 @@
<script src="{{ url_for("static", filename="scripts/graph.js") }}"></script> <script src="{{ url_for("static", filename="scripts/graph.js") }}"></script>
<script src="{{ url_for("static", filename="scripts/gui.js") }}"></script> <script src="{{ url_for("static", filename="scripts/gui.js") }}"></script>
<script src="{{ url_for("static", filename="scripts/editor.js") }}"></script> <script src="{{ url_for("static", filename="scripts/editor.js") }}"></script>
<script src="{{ url_for("static", filename="scripts/poller.js") }}"></script>
<script src="{{ url_for("static", filename="scripts/api.js") }}"></script>
<script src="{{ url_for("static", filename="scripts/main.js") }}"></script> <script src="{{ url_for("static", filename="scripts/main.js") }}"></script>
</head> </head>




+ 3
- 3
test.py View File

@@ -6,11 +6,11 @@ import json
import abp import abp
import random import random


#URL = "http://localhost:5000"
URL = "https://abv.peteshadbolt.co.uk/"
URL = "http://localhost:5000"
#URL = "https://abv.peteshadbolt.co.uk/"


def test_graph(): def test_graph():
N = 50
N = 10
g = abp.NXGraphState(range(N)) g = abp.NXGraphState(range(N))


for i in range(N): for i in range(N):


Loading…
Cancel
Save