Anders and Briegel in Python
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

159 lines
3.7KB

  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_times_table():
  35. """ Test times table """
  36. for i, j in it.product(range(24), range(24)):
  37. a = graphsim.GraphRegister(1)
  38. b = GraphState([0])
  39. a.local_op(0, graphsim.LocCliffOp(i))
  40. a.local_op(0, graphsim.LocCliffOp(j))
  41. b.act_local_rotation(0, i)
  42. b.act_local_rotation(0, j)
  43. assert_equal(a, b)
  44. def test_cz_table(N=10):
  45. """ Test the CZ table """
  46. clifford.use_old_cz()
  47. for i in range(24):
  48. for j in range(24):
  49. a = graphsim.GraphRegister(2)
  50. b = GraphState()
  51. b.add_nodes([0, 1])
  52. a.local_op(0, graphsim.LocCliffOp(i))
  53. b.act_local_rotation(0, i)
  54. a.local_op(1, graphsim.LocCliffOp(j))
  55. b.act_local_rotation(1, j)
  56. a.cphase(0, 1)
  57. b.act_cz(0, 1)
  58. assert_equal(a, b)
  59. for i in range(24):
  60. for j in range(24):
  61. a = graphsim.GraphRegister(2)
  62. b = GraphState()
  63. b.add_nodes([0, 1])
  64. a.local_op(0, graphsim.LocCliffOp(10))
  65. b.act_local_rotation(0, 10)
  66. a.cphase(0, 1)
  67. b.act_cz(0, 1)
  68. a.local_op(0, graphsim.LocCliffOp(i))
  69. b.act_local_rotation(0, i)
  70. a.local_op(1, graphsim.LocCliffOp(j))
  71. b.act_local_rotation(1, j)
  72. a.cphase(0, 1)
  73. b.act_cz(0, 1)
  74. assert_equal(a, b)
  75. def test_with_cphase_gates_hadamard_only(N=10):
  76. """ Hadamrds and CPHASEs, deterministic """
  77. a = graphsim.GraphRegister(N)
  78. b = GraphState()
  79. for i in range(N):
  80. a.hadamard(i)
  81. b.add_node(i)
  82. b.act_hadamard(i)
  83. for i in range(N-1):
  84. a.cphase(i, i+1)
  85. b.act_cz(i, i+1)
  86. assert_equal(a, b)
  87. def test_cz_hadamard(N=10):
  88. """ Test CZs and Hadamards at random """
  89. clifford.use_old_cz()
  90. a = graphsim.GraphRegister(N)
  91. b = GraphState(range(N))
  92. for i in tqdm(range(REPEATS), desc="Testing CZ and Hadamard against A&B"):
  93. if random.random()>0.5:
  94. j = random.randint(0, N-1)
  95. a.hadamard(j)
  96. b.act_hadamard(j)
  97. else:
  98. q = random.randint(0, N-2)
  99. a.cphase(q, q+1)
  100. b.act_cz(q, q+1)
  101. assert_equal(a, b)
  102. def test_all(N=9):
  103. """ Test everything"""
  104. clifford.use_old_cz()
  105. a = graphsim.GraphRegister(N)
  106. b = GraphState(range(N))
  107. print "woi"
  108. for i in tqdm(range(REPEATS), desc="Testing all gates against Anders and Briegel"):
  109. if random.random()>0.5:
  110. j = random.randint(0, N-1)
  111. u = random.randint(0, 23)
  112. a.local_op(j, graphsim.LocCliffOp(u))
  113. b.act_local_rotation(j, u)
  114. else:
  115. q = random.randint(0, N-2)
  116. a.cphase(q, q+1)
  117. b.act_cz(q, q+1)
  118. assert_equal(a, b, str(i))