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ů.

78 řádky
2.2KB

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