Simulate graph states in the browser
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

app.py 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. from flask import Flask, request, redirect, url_for, make_response, render_template, Markup, send_from_directory, send_file
  2. from flask_redis import FlaskRedis
  3. import json, abp, markdown
  4. from pprint import pprint
  5. app = Flask(__name__)
  6. redis = FlaskRedis(app)
  7. @app.route("/")
  8. def index():
  9. return render_template("index.html")
  10. @app.route("/graph", methods=["GET", "POST"])
  11. def graph():
  12. if request.method == 'POST':
  13. # Convert the data to a graph state
  14. g = abp.GraphState()
  15. g.from_json(json.loads(request.data))
  16. # Convert it back to JSON just for fun
  17. data = json.dumps(g.to_json(stringify=True))
  18. # Insert into the database
  19. redis.execute_command("SET", "graph", data)
  20. # Return success
  21. return "Posted {} bytes OK".format(len(data))
  22. else:
  23. # Get from the database
  24. return redis.get("graph")
  25. @app.route("/edit", methods=["POST"])
  26. def edit():
  27. # Load the graph from the database
  28. g = abp.GraphState()
  29. g.from_json(json.loads(redis.get("graph")))
  30. # Apply the edit to the graph
  31. edit = json.loads(request.data)
  32. action = edit["action"]
  33. pprint(edit, indent=2)
  34. if action == "create":
  35. g.add_qubit(edit["name"], position=edit["position"], vop=0)
  36. elif action == "cz":
  37. g.act_cz(edit["start"], edit["end"])
  38. elif action == "hadamard":
  39. g.act_hadamard(edit["node"])
  40. elif action == "phase":
  41. g.act_local_rotation(edit["node"], "phase")
  42. elif action == "delete":
  43. g._del_node(edit["node"])
  44. elif action == "localcomplementation":
  45. g.local_complementation(edit["node"])
  46. elif action == "measure":
  47. g.measure(edit["node"], "p"+edit["basis"])
  48. else:
  49. pass
  50. # New state into JSON and database
  51. data = json.dumps(g.to_json(stringify=True))
  52. redis.execute_command("SET", "graph", data)
  53. return data
  54. @app.route("/doc")
  55. def doc():
  56. body = Markup(markdown.markdown(render_template("doc.md"), extensions=["markdown.extensions.codehilite"]))
  57. return render_template("boilerplate.html", body=body)