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.

112 lignes
2.2KB

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