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.

120 lignes
2.4KB

  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. # Don't test if we are using Pete's CZ table - doesn't make sense
  43. if not clifford.legacy_cz:
  44. return
  45. for j in range(24):
  46. a = graphsim.GraphRegister(2)
  47. b = GraphState()
  48. b.add_node(0)
  49. b.add_node(1)
  50. compare(a, b)
  51. a.local_op(0, graphsim.LocCliffOp(j))
  52. b.act_local_rotation(0, j)
  53. a.local_op(1, graphsim.LocCliffOp(j))
  54. b.act_local_rotation(1, j)
  55. a.cphase(0, 1)
  56. b.act_cz(0, 1)
  57. compare(a, b)
  58. def test_with_cphase_gates_hadamard_only(N=10):
  59. """ Hadamrds and CPHASEs, deterministic """
  60. a = graphsim.GraphRegister(N)
  61. b = GraphState()
  62. for i in range(N):
  63. a.hadamard(i)
  64. b.add_node(i)
  65. b.act_hadamard(i)
  66. for i in range(N-1):
  67. a.cphase(i, i+1)
  68. b.act_cz(i, i+1)
  69. compare(a, b)
  70. def test_all(N=10):
  71. """ Test all gates at random """
  72. # Don't test if we are using Pete's CZ table - doesn't make sense
  73. if not clifford.legacy_cz:
  74. return
  75. a = graphsim.GraphRegister(N)
  76. b = GraphState()
  77. for i in range(N):
  78. b.add_node(i)
  79. for i in range(100):
  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. aa = a.get_adj_list()
  89. bb = b.adj_list()
  90. compare(a, b)