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.

148 lines
3.4KB

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