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.

139 lines
3.3KB

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