Anders and Briegel in Python
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.

mock.py 3.2KB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. """
  2. Mock graphs used for testing
  3. """
  4. import numpy as np
  5. from abp import GraphState, clifford
  6. from anders_briegel import graphsim
  7. from numpy import random
  8. # We always run with A&B's CZ table when we are testing
  9. clifford.use_old_cz()
  10. class AndersWrapper(graphsim.GraphRegister):
  11. """ A wrapper for A&B to make the interface identical and enable equality testing """
  12. def __init__(self, nodes):
  13. assert list(nodes) == range(len(nodes))
  14. super(AndersWrapper, self).__init__(len(nodes))
  15. def act_local_rotation(self, qubit, operation):
  16. operation = clifford.by_name[str(operation)]
  17. op = graphsim.LocCliffOp(operation)
  18. super(AndersWrapper, self).local_op(qubit, op)
  19. def act_cz(self, a, b):
  20. super(AndersWrapper, self).cphase(a, b)
  21. def measure(self, qubit, basis, force):
  22. basis = clifford.by_name[basis]
  23. basis = {1: graphsim.lco_X,
  24. 2: graphsim.lco_Y,
  25. 3: graphsim.lco_Z}[clifford.by_name[basis]]
  26. super(AndersWrapper, self).measure(qubit, basis, None, force)
  27. def __eq__(self, other):
  28. return self.to_json() == other.to_json()
  29. def act_circuit(self, circuit):
  30. for node, operation in circuit:
  31. if operation == "cz":
  32. self.act_cz(*node)
  33. else:
  34. self.act_local_rotation(node, operation)
  35. class ABPWrapper(GraphState):
  36. """ A wrapper for abp, just to ensure determinism """
  37. def __init__(self, nodes=[]):
  38. super(ABPWrapper, self).__init__(nodes, deterministic=True)
  39. def random_pair(n):
  40. """ Helper function to get random pairs"""
  41. return tuple(random.choice(range(n), 2, replace=False))
  42. def random_graph_state(n=10):
  43. """ A random Graph state. """
  44. czs = [(random_pair(n), "cz") for i in range(n * 2)]
  45. for Base in AndersWrapper, ABPWrapper:
  46. g = Base(range(n))
  47. g.act_circuit((i, "hadamard") for i in range(n))
  48. g.act_circuit(czs)
  49. yield g
  50. def random_stabilizer_state(n=10):
  51. """ Generate a random stabilizer state, without any VOPs """
  52. rotations = [(i, random.choice(range(24))) for i in range(n)]
  53. for g in random_graph_state():
  54. g.act_circuit(rotations)
  55. yield g
  56. def bell_pair():
  57. for Base in AndersWrapper, ABPWrapper:
  58. g = Base((0, 1))
  59. g.act_circuit(((0, "hadamard"), (1, "hadamard"), ((0, 1), "cz")))
  60. yield g
  61. def onequbit():
  62. for Base in AndersWrapper, ABPWrapper:
  63. g = Base((0,))
  64. yield g
  65. def named_node_graph():
  66. """ A graph with named nodes"""
  67. edges = (0, 1), (1, 2), (2, 0), (0, 3), (100, 200), (200, "named")
  68. g = ABPWrapper([0, 1, 2, 3, 100, 200, "named"])
  69. g.act_circuit((i, "hadamard") for i in g.node)
  70. g.act_circuit((edge, "cz") for edge in edges)
  71. return g
  72. def simple_graph():
  73. """ A simple graph to test with"""
  74. edges = (0, 1), (1, 2), (2, 0), (0, 3), (100, 200)
  75. g = ABPWrapper([0, 1, 2, 3, 100, 200])
  76. g.act_circuit((i, "hadamard") for i in g.node)
  77. g.act_circuit((edge, "cz") for edge in edges)
  78. return g
  79. if __name__ == '__main__':
  80. a, b = random_graph_state()
  81. assert a == b
  82. a, b = random_stabilizer_state()
  83. assert a == b
  84. print named_node_graph()