Anders and Briegel in Python
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
3.5KB

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