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.

151 lines
3.3KB

  1. from abp import GraphState
  2. from anders_briegel import graphsim
  3. from abp import CircuitModel
  4. from abp import clifford
  5. import random
  6. import difflib
  7. import re
  8. from copy import deepcopy
  9. def compare(a, b):
  10. """ TODO: Sketchy as you like. Remove this abomination """
  11. aa = a.get_adj_list()
  12. bb = b.adj_list()
  13. try:
  14. assert re.sub("\\s", "", aa) == re.sub("\\s", "", bb)
  15. except AssertionError:
  16. print aa
  17. print bb
  18. raise
  19. def isequal(a, b):
  20. """ TODO: Sketchy as you like. Remove this abomination """
  21. aa = a.get_adj_list()
  22. bb = b.adj_list()
  23. return re.sub("\\s", "", aa) == re.sub("\\s", "", bb)
  24. def test_hadamard():
  25. """ Test hadamards """
  26. a = graphsim.GraphRegister(1)
  27. b = GraphState()
  28. b.add_node(0)
  29. compare(a, b)
  30. a.hadamard(0)
  31. b.act_hadamard(0)
  32. compare(a, b)
  33. a.hadamard(0)
  34. b.act_hadamard(0)
  35. compare(a, b)
  36. def test_local_rotations():
  37. """ Test local rotations """
  38. a = graphsim.GraphRegister(1)
  39. b = GraphState()
  40. b.add_node(0)
  41. compare(a, b)
  42. for i in range(1000):
  43. j = random.randint(0, 23)
  44. a.local_op(0, graphsim.LocCliffOp(j))
  45. b.act_local_rotation(0, j)
  46. compare(a, b)
  47. def test_cz_table(N=10):
  48. """ Test the CZ table """
  49. clifford.use_old_cz()
  50. for i in range(24):
  51. for j in range(24):
  52. a = graphsim.GraphRegister(2)
  53. b = GraphState()
  54. b.add_nodes([0, 1])
  55. a.local_op(0, graphsim.LocCliffOp(i))
  56. b.act_local_rotation(0, i)
  57. a.local_op(1, graphsim.LocCliffOp(j))
  58. b.act_local_rotation(1, j)
  59. a.cphase(0, 1)
  60. b.act_cz(0, 1)
  61. compare(a, b)
  62. for i in range(24):
  63. for j in range(24):
  64. a = graphsim.GraphRegister(2)
  65. b = GraphState()
  66. b.add_nodes([0, 1])
  67. a.local_op(0, graphsim.LocCliffOp(10))
  68. b.act_local_rotation(0, 10)
  69. a.cphase(0, 1)
  70. b.act_cz(0, 1)
  71. a.local_op(0, graphsim.LocCliffOp(i))
  72. b.act_local_rotation(0, i)
  73. a.local_op(1, graphsim.LocCliffOp(j))
  74. b.act_local_rotation(1, j)
  75. a.cphase(0, 1)
  76. b.act_cz(0, 1)
  77. compare(a, b)
  78. def test_with_cphase_gates_hadamard_only(N=10):
  79. """ Hadamrds and CPHASEs, deterministic """
  80. a = graphsim.GraphRegister(N)
  81. b = GraphState()
  82. for i in range(N):
  83. a.hadamard(i)
  84. b.add_node(i)
  85. b.act_hadamard(i)
  86. for i in range(N-1):
  87. a.cphase(i, i+1)
  88. b.act_cz(i, i+1)
  89. compare(a, b)
  90. def test_all(N=3):
  91. """ Test all gates at random """
  92. #TODO: Currently fails. Why???
  93. clifford.use_old_cz()
  94. a = graphsim.GraphRegister(N)
  95. b = GraphState(range(N))
  96. previous_state, previous_cz = None, None
  97. while isequal(a, b):
  98. if random.random()>0.5:
  99. j = random.randint(0, N-1)
  100. a.hadamard(j)
  101. b.act_hadamard(j)
  102. else:
  103. q = random.randint(0, N-2)
  104. a.cphase(q, q+1)
  105. b.act_cz(q, q+1)
  106. previous_state = deepcopy(b)
  107. previous_cz = q, q+1
  108. print "Initial state:"
  109. print previous_state.node
  110. print previous_state.adj
  111. print "CZ:", previous_cz
  112. print "Pete state:\n", a.get_adj_list()
  113. print "Anders state:\n", b.adj_list()