Anders and Briegel in Python
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

test_against_anders_and_briegel.py 3.8KB

8年前
8年前
8年前
8年前
8年前
8年前
8年前
8年前
8年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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_cz_hadamard(N=3):
  92. """ Test CZs and Hadamards at random """
  93. clifford.use_old_cz()
  94. a = graphsim.GraphRegister(N)
  95. b = GraphState(range(N))
  96. previous_state, previous_cz = None, None
  97. for i in tqdm(range(100000), desc="Testing CZ and Hadamard against 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. if a!=b:
  105. a.cphase(q, q+1)
  106. b.act_cz(q, q+1)
  107. compare(a, b)
  108. def test_all(N=5):
  109. """ Test everything"""
  110. clifford.use_old_cz()
  111. a = graphsim.GraphRegister(N)
  112. b = GraphState(range(N))
  113. previous_state, previous_cz = None, None
  114. for i in tqdm(range(100000), desc="Testing all gates against Anders and Briegel"):
  115. if random.random()>0.5:
  116. j = random.randint(0, N-1)
  117. u = random.randint(0, 23)
  118. a.local_op(j, graphsim.LocCliffOp(u))
  119. b.act_local_rotation(j, u)
  120. else:
  121. q = random.randint(0, N-2)
  122. if a!=b:
  123. a.cphase(q, q+1)
  124. b.act_cz(q, q+1)
  125. compare(a, b)