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.

149 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. import numpy as np
  7. from tqdm import tqdm
  8. from abp.anders_cz import cz_table as abczt
  9. REPEATS = 100000
  10. def assert_equal(a, b, debug=""):
  11. assert a.to_json() == b.to_json(), "\n\n" + debug + "\n\n" + str(a.to_json()) + "\n\n" + str(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_cz_table(N=10):
  36. """ Test the CZ table """
  37. clifford.use_old_cz()
  38. for i in range(24):
  39. for j in range(24):
  40. a = graphsim.GraphRegister(2)
  41. b = GraphState()
  42. b.add_nodes([0, 1])
  43. a.local_op(0, graphsim.LocCliffOp(i))
  44. b.act_local_rotation(0, i)
  45. a.local_op(1, graphsim.LocCliffOp(j))
  46. b.act_local_rotation(1, j)
  47. a.cphase(0, 1)
  48. b.act_cz(0, 1)
  49. assert_equal(a, b)
  50. for i in range(24):
  51. for j in range(24):
  52. a = graphsim.GraphRegister(2)
  53. b = GraphState()
  54. b.add_nodes([0, 1])
  55. a.local_op(0, graphsim.LocCliffOp(10))
  56. b.act_local_rotation(0, 10)
  57. a.cphase(0, 1)
  58. b.act_cz(0, 1)
  59. a.local_op(0, graphsim.LocCliffOp(i))
  60. b.act_local_rotation(0, i)
  61. a.local_op(1, graphsim.LocCliffOp(j))
  62. b.act_local_rotation(1, j)
  63. a.cphase(0, 1)
  64. b.act_cz(0, 1)
  65. assert_equal(a, b)
  66. def test_with_cphase_gates_hadamard_only(N=10):
  67. """ Hadamrds and CPHASEs, deterministic """
  68. a = graphsim.GraphRegister(N)
  69. b = GraphState()
  70. for i in range(N):
  71. a.hadamard(i)
  72. b.add_node(i)
  73. b.act_hadamard(i)
  74. for i in range(N-1):
  75. a.cphase(i, i+1)
  76. b.act_cz(i, i+1)
  77. assert_equal(a, b)
  78. def _test_cz_hadamard(N=10):
  79. """ Test CZs and Hadamards at random """
  80. clifford.use_old_cz()
  81. assert np.allclose(clifford.cz_table, abczt)
  82. a = graphsim.GraphRegister(N)
  83. b = GraphState(range(N))
  84. for i in tqdm(range(REPEATS), desc="Testing CZ and Hadamard against A&B"):
  85. if random.random()>0.5:
  86. j = random.randint(0, N-1)
  87. a.hadamard(j)
  88. b.act_hadamard(j)
  89. else:
  90. q = random.randint(0, N-2)
  91. a.cphase(q, q+1)
  92. b.act_cz(q, q+1)
  93. assert_equal(a, b)
  94. def test_all(N=9):
  95. """ Test everything"""
  96. clifford.use_old_cz()
  97. a = graphsim.GraphRegister(N)
  98. b = GraphState(range(N))
  99. previous_state, previous_cz = None, None
  100. for i in tqdm(range(REPEATS), desc="Testing all gates against Anders and Briegel"):
  101. if random.random()>0.5:
  102. j = random.randint(0, N-1)
  103. u = random.randint(0, 23)
  104. a.local_op(j, graphsim.LocCliffOp(u))
  105. b.act_local_rotation(j, u)
  106. else:
  107. q = random.randint(0, N-2)
  108. a.cphase(q, q+1)
  109. b.act_cz(q, q+1)
  110. assert_equal(a, b, str(i))