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

158 行
3.7KB

  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 numpy as np
  7. from tqdm import tqdm
  8. import itertools as it
  9. from config import *
  10. def assert_equal(a, b, debug=""):
  11. assert a.to_json() == b.to_json()
  12. def test_hadamard():
  13. """ Test hadamards """
  14. a = graphsim.GraphRegister(1)
  15. b = GraphState()
  16. b.add_node(0)
  17. assert_equal(a, b)
  18. a.hadamard(0)
  19. b.act_hadamard(0)
  20. assert_equal(a, b)
  21. a.hadamard(0)
  22. b.act_hadamard(0)
  23. assert_equal(a, b)
  24. def test_local_rotations():
  25. """ Test local rotations """
  26. a = graphsim.GraphRegister(1)
  27. b = GraphState()
  28. b.add_node(0)
  29. assert_equal(a, b)
  30. for i in range(1000):
  31. j = random.randint(0, 23)
  32. a.local_op(0, graphsim.LocCliffOp(j))
  33. b.act_local_rotation(0, j)
  34. assert_equal(a, b)
  35. def test_times_table():
  36. """ Test times table """
  37. for i, j in it.product(range(24), range(24)):
  38. a = graphsim.GraphRegister(1)
  39. b = GraphState([0])
  40. a.local_op(0, graphsim.LocCliffOp(i))
  41. a.local_op(0, graphsim.LocCliffOp(j))
  42. b.act_local_rotation(0, i)
  43. b.act_local_rotation(0, j)
  44. assert_equal(a, b)
  45. def test_cz_table(N=10):
  46. """ Test the CZ table """
  47. clifford.use_old_cz()
  48. for i in range(24):
  49. for j in range(24):
  50. a = graphsim.GraphRegister(2)
  51. b = GraphState()
  52. b.add_nodes([0, 1])
  53. a.local_op(0, graphsim.LocCliffOp(i))
  54. b.act_local_rotation(0, i)
  55. a.local_op(1, graphsim.LocCliffOp(j))
  56. b.act_local_rotation(1, j)
  57. a.cphase(0, 1)
  58. b.act_cz(0, 1)
  59. assert_equal(a, b)
  60. for i in range(24):
  61. for j in range(24):
  62. a = graphsim.GraphRegister(2)
  63. b = GraphState()
  64. b.add_nodes([0, 1])
  65. a.local_op(0, graphsim.LocCliffOp(10))
  66. b.act_local_rotation(0, 10)
  67. a.cphase(0, 1)
  68. b.act_cz(0, 1)
  69. a.local_op(0, graphsim.LocCliffOp(i))
  70. b.act_local_rotation(0, i)
  71. a.local_op(1, graphsim.LocCliffOp(j))
  72. b.act_local_rotation(1, j)
  73. a.cphase(0, 1)
  74. b.act_cz(0, 1)
  75. assert_equal(a, b)
  76. def test_with_cphase_gates_hadamard_only(N=10):
  77. """ Hadamrds and CPHASEs, deterministic """
  78. a = graphsim.GraphRegister(N)
  79. b = GraphState()
  80. for i in range(N):
  81. a.hadamard(i)
  82. b.add_node(i)
  83. b.act_hadamard(i)
  84. for i in range(N-1):
  85. a.cphase(i, i+1)
  86. b.act_cz(i, i+1)
  87. assert_equal(a, b)
  88. def test_cz_hadamard(N=10):
  89. """ Test CZs and Hadamards at random """
  90. clifford.use_old_cz()
  91. a = graphsim.GraphRegister(N)
  92. b = GraphState(range(N))
  93. for i in tqdm(range(REPEATS), desc="Testing CZ and Hadamard against A&B"):
  94. if random.random()>0.5:
  95. j = random.randint(0, N-1)
  96. a.hadamard(j)
  97. b.act_hadamard(j)
  98. else:
  99. q = random.randint(0, N-2)
  100. a.cphase(q, q+1)
  101. b.act_cz(q, q+1)
  102. assert_equal(a, b)
  103. def test_all(N=9):
  104. """ Test everything"""
  105. clifford.use_old_cz()
  106. a = graphsim.GraphRegister(N)
  107. b = GraphState(range(N))
  108. for i in tqdm(range(REPEATS), desc="Testing all gates against Anders and Briegel"):
  109. if random.random()>0.5:
  110. j = random.randint(0, N-1)
  111. u = random.randint(0, 23)
  112. a.local_op(j, graphsim.LocCliffOp(u))
  113. b.act_local_rotation(j, u)
  114. else:
  115. q = random.randint(0, N-2)
  116. a.cphase(q, q+1)
  117. b.act_cz(q, q+1)
  118. assert_equal(a, b, str(i))