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.

143 lines
3.2KB

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