diff --git a/abp/clifford.py b/abp/clifford.py index 1027cb0..4c23c6b 100644 --- a/abp/clifford.py +++ b/abp/clifford.py @@ -9,7 +9,7 @@ from functools import reduce import itertools as it import numpy as np from tqdm import tqdm -import argparse +#import argparse import qi @@ -122,27 +122,28 @@ def get_cz_table(unitaries): # First try to load tables from cache. If that fails, build them from # scratch and store in /tmp/ -os.chdir(tempfile.gettempdir()) +tempdir = tempfile.gettempdir() +temp = lambda filename: os.path.join(tempdir, filename) try: if __name__ == "__main__": raise IOError # Parse command line args - parser = argparse.ArgumentParser() - parser.add_argument("-l", "--legacy", help="Use legacy CZ table", action="store_true", default=False) - args = parser.parse_args() - legacy_cz = args.legacy - - unitaries = np.load("unitaries.npy") - conjugation_table = np.load("conjugation_table.npy") - times_table = np.load("times_table.npy") + #parser = argparse.ArgumentParser() + #parser.add_argument("-l", "--legacy", help="Use legacy CZ table", action="store_true", default=False) + #args = parser.parse_args() + legacy_cz = False + + unitaries = np.load(temp("unitaries.npy")) + conjugation_table = np.load(temp("conjugation_table.npy")) + times_table = np.load(temp("times_table.npy")) if legacy_cz: import anders_cz cz_table = anders_cz.cz_table else: - cz_table = np.load("cz_table.npy") + cz_table = np.load(temp("cz_table.npy")) - with open("by_name.json") as f: + with open(temp("by_name.json")) as f: by_name = json.load(f) @@ -156,10 +157,11 @@ except IOError: cz_table = get_cz_table(unitaries) # Write it all to disk - np.save("unitaries.npy", unitaries) - np.save("conjugation_table.npy", conjugation_table) - np.save("times_table.npy", times_table) - np.save("cz_table.npy", cz_table) + np.save(temp("unitaries.npy"), unitaries) + np.save(temp("conjugation_table.npy"), conjugation_table) + np.save(temp("times_table.npy"), times_table) + np.save(temp("cz_table.npy"), cz_table) - with open("by_name.json", "wb") as f: + with open(temp("by_name.json"), "wb") as f: json.dump(by_name, f) + diff --git a/abp/graphstate.py b/abp/graphstate.py index d6c6fcc..790de1c 100644 --- a/abp/graphstate.py +++ b/abp/graphstate.py @@ -159,7 +159,15 @@ class GraphState(object): """ Convert the graph to JSON form """ meta = {key: value for key, value in self.meta.items()} edge = self.edgelist() - return json.dumps({"nodes": self.vops, "edges": edge, "meta": meta}) + return {"nodes": self.vops, "edges": edge, "meta": meta} + + def from_json(self, data): + """ Reconstruct from JSON """ + self.__init__([]) + self.vops = data["nodes"] + self.meta = data["meta"] + self.ngbh = {key: set() for key in self.vops} + self.add_edges(data["edges"]) def to_networkx(self): """ Convert the graph to a networkx graph """ @@ -222,3 +230,6 @@ class GraphState(object): rows.append(s) return " \n".join(rows) + " \n" + def __eq__(self, other): + """ Check equality between graphs """ + return self.ngbh == other.ngbh and self.vops == other.vops diff --git a/examples/client.py b/examples/client.py new file mode 100644 index 0000000..a821140 --- /dev/null +++ b/examples/client.py @@ -0,0 +1,8 @@ +import requests +import abp, json + +s = requests.Session() +output = json.loads(s.get("http://localhost:5000/state").content) +print output +state = json.loads(s.post("http://localhost:5000/state").content) + diff --git a/server/server.py b/server/server.py index 4e8ee7f..4564a57 100644 --- a/server/server.py +++ b/server/server.py @@ -1,48 +1,27 @@ -import urlparse -from BaseHTTPServer import BaseHTTPRequestHandler -from SimpleHTTPServer import SimpleHTTPRequestHandler -import SocketServer -import sys, json, time, os -from graphstate import GraphState -import requests +from flask import Flask, request, render_template, jsonify +import abp +graphstate = abp.GraphState() +app = Flask(__name__) -class VizHandler(SimpleHTTPRequestHandler): +@app.route("/") +def index(): + return render_template("index.html") - """ Handles requests to the server """ +@app.route("/state") +def state(): + if request.method == "GET": + return jsonify(graphstate.to_json()) + elif request.method == "POST": + graphstate.from_json(request.data) + return jsonify(graphstate.to_json()) - def get_state(self): - """ Get the current graphstate state """ - self.send_response(200) - self.send_header('Content-Type', 'application/json') - self.end_headers() - self.wfile.write(state.to_json()) +@app.route("/state") +def state(): + return jsonify(graphstate.to_json()) - def do_GET(self, *args, **kwargs): - """ Someone belled the server """ - parsed_path = urlparse.urlparse(self.path) - if parsed_path.path == "/state": - return self.get_state() - else: - return SimpleHTTPRequestHandler.do_GET(self, *args, **kwargs) +if __name__ == "__main__": + app.debug = True + app.run(host="0.0.0.0") -class Server(SocketServer.TCPServer): - - """ Serves the good stuff """ - allow_reuse_address = True - - def __init__(self, port=8000): - self.port = port - self.state = None - SocketServer.TCPServer.__init__(self, ("127.0.0.1", self.port), VizHandler) - print "Serving on port {} (Press CTRL-C to quit)".format(self.port) - self.serve_forever() - - -if __name__ == '__main__': - os.chdir(os.path.join(sys.path[0], "../static")) - server = Server() - server.shutdown() - - requests.get("http://localhost:8000/state") diff --git a/server/static/index.html b/server/templates/index.html similarity index 91% rename from server/static/index.html rename to server/templates/index.html index a847321..2e84832 100644 --- a/server/static/index.html +++ b/server/templates/index.html @@ -9,16 +9,17 @@ - - - - - - - + + + + + + + +
Hello World
diff --git a/setup.py b/setup.py old mode 100644 new mode 100755 diff --git a/tests/test_json.py b/tests/test_json.py index 1e6d174..ae121bc 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -10,6 +10,9 @@ def test_json_basic(): js = g.to_json() assert "edges" in js assert "nodes" in js - json.loads(js) + e = GraphState() + assert e != g + e.from_json(js) + assert e == g