Simulate graph states in the browser
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

95 řádky
2.7KB

  1. from flask import Flask, request, redirect, url_for, make_response, render_template, Markup, send_from_directory, send_file, jsonify, Response
  2. from flask_redis import FlaskRedis
  3. import json, abp, markdown
  4. from pprint import pprint
  5. import raussendorf
  6. import uuid
  7. DAY = 60*60*24
  8. app = Flask(__name__)
  9. redis = FlaskRedis(app)
  10. @app.route("/")
  11. def index():
  12. secret = str(uuid.uuid4())
  13. return redirect("/{}".format(secret))
  14. @app.route("/<uuid>")
  15. def main(uuid):
  16. return render_template("index.html", uuid=uuid)
  17. @app.route("/<uuid>/graph", methods=["GET", "POST"])
  18. def graph(uuid):
  19. if request.method == 'POST':
  20. # Convert the data to a graph state
  21. g = abp.GraphState()
  22. g.from_json(json.loads(request.data))
  23. # Convert it back to JSON just for fun
  24. data = json.dumps(g.to_json(stringify=True))
  25. # Insert into the database
  26. redis.setex(uuid, DAY, data)
  27. # Return success
  28. return "Posted {} bytes OK".format(len(data))
  29. elif redis.exists(uuid):
  30. return jsonify(json.loads(redis.get(uuid)))
  31. else:
  32. g = abp.GraphState()
  33. data = g.to_json(stringify=True)
  34. redis.setex(uuid, DAY, json.dumps(data))
  35. return jsonify(data)
  36. @app.route("/<uuid>/edit", methods=["POST"])
  37. def edit(uuid):
  38. # Load the graph from the database
  39. g = abp.GraphState()
  40. g.from_json(json.loads(redis.get(uuid)))
  41. # Apply the edit to the graph
  42. edit = json.loads(request.data)
  43. action = edit["action"]
  44. pprint(edit, indent=2)
  45. if action == "create":
  46. g.add_qubit(edit["name"], position=edit["position"], vop=0)
  47. elif action == "cz":
  48. g.act_cz(edit["start"], edit["end"])
  49. elif action == "hadamard":
  50. g.act_hadamard(edit["node"])
  51. elif action == "phase":
  52. g.act_local_rotation(edit["node"], "phase")
  53. elif action == "delete":
  54. g._del_node(edit["node"])
  55. elif action == "localcomplementation":
  56. g.local_complementation(edit["node"])
  57. elif action == "measure":
  58. g.measure(edit["node"], "p"+edit["basis"])
  59. elif action == "clear":
  60. g = abp.GraphState()
  61. elif action == "raussendorf":
  62. g = raussendorf.raussendorf()
  63. else:
  64. pass
  65. # New state into JSON and database
  66. data = json.dumps(g.to_json(stringify=True))
  67. redis.setex(uuid, DAY, data)
  68. return data
  69. @app.route("/doc")
  70. def doc():
  71. body = Markup(markdown.markdown(render_template("doc.md"), extensions=["markdown.extensions.codehilite"]))
  72. return render_template("boilerplate.html", body=body)
  73. @app.route("/<uuid>/download")
  74. def download(uuid):
  75. return Response(redis.get(uuid),
  76. mimetype='application/json',
  77. headers={'Content-Disposition':'attachment;filename=graph.json'})