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

73 řádky
2.1KB

  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)