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

test_against_anders_and_briegel.py 3.0KB

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