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.

181 lignes
5.0KB

  1. from abp import GraphState, CircuitModel, clifford
  2. from abp import clifford
  3. from mock import simple_graph
  4. import random
  5. import numpy as np
  6. from tqdm import tqdm
  7. REPEATS = 100
  8. DEPTH = 100
  9. def test_graph_basic():
  10. """ Test that we can construct graphs, delete edges, whatever """
  11. g = simple_graph()
  12. assert set(g.adj[0].keys()) == set([1, 2, 3])
  13. g._del_edge(0, 1)
  14. assert set(g.adj[0].keys()) == set([2, 3])
  15. assert g.has_edge(1, 2)
  16. assert not g.has_edge(0, 1)
  17. def test_local_complementation():
  18. """ Test that local complementation works as expected """
  19. g = simple_graph()
  20. g.local_complementation(0)
  21. assert g.has_edge(0, 1)
  22. assert g.has_edge(0, 2)
  23. assert not g.has_edge(1, 2)
  24. assert g.has_edge(3, 2)
  25. assert g.has_edge(3, 1)
  26. # TODO: test VOP conditions
  27. def test_remove_vop():
  28. """ Test that removing VOPs really works """
  29. g = simple_graph()
  30. g.remove_vop(0, 1)
  31. assert g.node[0]["vop"] == clifford.by_name["identity"]
  32. g.remove_vop(1, 1)
  33. assert g.node[1]["vop"] == clifford.by_name["identity"]
  34. g.remove_vop(2, 1)
  35. assert g.node[2]["vop"] == clifford.by_name["identity"]
  36. g.remove_vop(0, 1)
  37. assert g.node[0]["vop"] == clifford.by_name["identity"]
  38. def test_edgelist():
  39. """ Test making edgelists """
  40. g = simple_graph()
  41. el = g.edgelist()
  42. assert (0, 3) in el
  43. assert (0, 2) in el
  44. assert (100, 200) in el
  45. def test_stress(n = int(1e5)):
  46. """ Testing that making a graph of ten thousand qubits takes less than half a second"""
  47. import time
  48. g = GraphState(range(n+1))
  49. t = time.clock()
  50. for i in xrange(n):
  51. g._add_edge(i, i + 1)
  52. assert time.clock() - t < .5
  53. def test_cz():
  54. """ Test CZ gate """
  55. g = GraphState([0, 1])
  56. g.act_local_rotation(0, clifford.by_name["hadamard"])
  57. g.act_local_rotation(1, clifford.by_name["hadamard"])
  58. g.act_local_rotation(1, clifford.by_name["py"])
  59. assert not g.has_edge(0, 1)
  60. g.act_cz(0, 1)
  61. assert g.has_edge(0, 1)
  62. def test_stabilizer():
  63. """ Test that we can generate stabilizers okay """
  64. g = simple_graph()
  65. stab = g.to_stabilizer()
  66. #TODO
  67. def test_local_complementation():
  68. """ Test that local complementation works okay """
  69. psi = GraphState()
  70. psi.add_node(0)
  71. psi.add_node(1)
  72. psi.add_node(2)
  73. psi.add_node(3)
  74. for n in psi.node:
  75. psi.act_hadamard(n)
  76. psi.act_cz(0, 1)
  77. psi.act_cz(0, 3)
  78. psi.act_cz(1, 3)
  79. psi.act_cz(1, 2)
  80. old_edges = psi.edgelist()
  81. old_state = psi.to_state_vector()
  82. psi.local_complementation(1)
  83. assert old_edges != psi.edgelist()
  84. assert old_state == psi.to_state_vector()
  85. def test_single_qubit():
  86. """ A multi qubit test with Hadamards only"""
  87. for repeat in tqdm(range(REPEATS), desc="Testing against circuit model"):
  88. g = GraphState([0])
  89. c = CircuitModel(1)
  90. for i in range(100):
  91. op = np.random.choice(range(24))
  92. g.act_local_rotation(0, op)
  93. c.act_local_rotation(0, clifford.unitaries[op])
  94. assert g.to_state_vector() == c
  95. def test_hadamard_only_multiqubit(n=6):
  96. """ A multi qubit test with Hadamards only"""
  97. for repeat in tqdm(range(REPEATS), desc="Testing against circuit model"):
  98. g = GraphState(range(n))
  99. c = CircuitModel(n)
  100. for i in range(n):
  101. g.act_hadamard(i)
  102. c.act_hadamard(i)
  103. assert g.to_state_vector() == c
  104. for i in range(100):
  105. a, b = np.random.choice(range(n), 2, False)
  106. g.act_cz(a, b)
  107. c.act_cz(a, b)
  108. assert g.to_state_vector() == c
  109. def test_all_multiqubit(n=4):
  110. """ A multi qubit test with arbitrary local rotations """
  111. g = GraphState(range(n))
  112. c = CircuitModel(n)
  113. for i in range(10):
  114. qubit = np.random.randint(0, n - 1)
  115. rotation = np.random.randint(0, 24 - 1)
  116. g.act_local_rotation(qubit, rotation)
  117. c.act_local_rotation(qubit, clifford.unitaries[rotation])
  118. assert g.to_state_vector() == c
  119. for repeat in tqdm(range(REPEATS), desc="Testing against circuit model"):
  120. a, b = np.random.choice(range(n), 2, False)
  121. g.act_cz(a, b)
  122. c.act_cz(a, b)
  123. assert np.allclose(np.sum(np.abs(c.state) ** 2), 1)
  124. assert np.allclose(
  125. np.sum(np.abs(g.to_state_vector().state) ** 2), 1)
  126. assert g.to_state_vector() == c
  127. assert g.to_state_vector() == c
  128. def test_all(n=8):
  129. """ A multi qubit test with arbitrary local rotations """
  130. g = GraphState(range(n))
  131. c = CircuitModel(n)
  132. for repeat in tqdm(xrange(REPEATS), "Testing against circuit model"):
  133. for step in xrange(DEPTH):
  134. if random.random()>0.5:
  135. qubit = np.random.randint(0, n - 1)
  136. rotation = np.random.randint(0, 24 - 1)
  137. g.act_local_rotation(qubit, rotation)
  138. c.act_local_rotation(qubit, clifford.unitaries[rotation])
  139. else:
  140. a, b = np.random.choice(range(n), 2, False)
  141. g.act_cz(a, b)
  142. c.act_cz(a, b)
  143. assert g.to_state_vector() == c