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.

147 lines
3.3KB

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