Anders and Briegel in Python
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

116 lignes
2.3KB

  1. from abp import GraphState
  2. from anders_briegel import graphsim
  3. from abp import clifford
  4. import random
  5. import difflib
  6. import re
  7. def compare(a, b):
  8. """ TODO: Sketchy as you like. Remove this abomination """
  9. aa = a.get_adj_list()
  10. bb = b.adj_list()
  11. try:
  12. assert re.sub("\\s", "", aa) == re.sub("\\s", "", bb)
  13. except AssertionError:
  14. print aa
  15. print bb
  16. raise
  17. def test_hadamard():
  18. """ Test hadamards """
  19. a = graphsim.GraphRegister(1)
  20. b = GraphState()
  21. b.add_node(0)
  22. compare(a, b)
  23. a.hadamard(0)
  24. b.act_hadamard(0)
  25. compare(a, b)
  26. a.hadamard(0)
  27. b.act_hadamard(0)
  28. compare(a, b)
  29. def test_local_rotations():
  30. """ Test local rotations """
  31. a = graphsim.GraphRegister(1)
  32. b = GraphState()
  33. b.add_node(0)
  34. compare(a, b)
  35. for i in range(1000):
  36. j = random.randint(0, 23)
  37. a.local_op(0, graphsim.LocCliffOp(j))
  38. b.act_local_rotation(0, j)
  39. compare(a, b)
  40. def test_cz_table(N=10):
  41. """ Test the CZ table """
  42. clifford.use_old_cz()
  43. for j in range(24):
  44. a = graphsim.GraphRegister(2)
  45. b = GraphState()
  46. b.add_node(0)
  47. b.add_node(1)
  48. compare(a, b)
  49. a.local_op(0, graphsim.LocCliffOp(j))
  50. b.act_local_rotation(0, j)
  51. a.local_op(1, graphsim.LocCliffOp(j))
  52. b.act_local_rotation(1, j)
  53. a.cphase(0, 1)
  54. b.act_cz(0, 1)
  55. compare(a, b)
  56. def test_with_cphase_gates_hadamard_only(N=10):
  57. """ Hadamrds and CPHASEs, deterministic """
  58. a = graphsim.GraphRegister(N)
  59. b = GraphState()
  60. for i in range(N):
  61. a.hadamard(i)
  62. b.add_node(i)
  63. b.act_hadamard(i)
  64. for i in range(N-1):
  65. a.cphase(i, i+1)
  66. b.act_cz(i, i+1)
  67. compare(a, b)
  68. def test_all(N=10):
  69. """ Test all gates at random """
  70. clifford.use_old_cz()
  71. a = graphsim.GraphRegister(N)
  72. b = GraphState()
  73. for i in range(N):
  74. b.add_node(i)
  75. for i in range(100):
  76. if random.random()>0.5:
  77. j = random.randint(0, N-1)
  78. a.hadamard(j)
  79. b.act_hadamard(j)
  80. else:
  81. q = random.randint(0, N-2)
  82. a.cphase(q, q+1)
  83. b.act_cz(q, q+1)
  84. aa = a.get_adj_list()
  85. bb = b.adj_list()
  86. compare(a, b)