Anders and Briegel in Python
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

116 行
3.6KB

  1. from abp import GraphState, CircuitModel, clifford
  2. import mock
  3. import random
  4. import numpy as np
  5. from tqdm import tqdm
  6. REPEATS = 100
  7. DEPTH = 100
  8. def test_graph_basic():
  9. """ Test that we can construct graphs, delete edges, whatever """
  10. g = mock.simple_graph()
  11. assert set(g.adj[0].keys()) == set([1, 2, 3])
  12. g._del_edge(0, 1)
  13. assert set(g.adj[0].keys()) == set([2, 3])
  14. assert g.has_edge(1, 2)
  15. assert not g.has_edge(0, 1)
  16. def test_local_complementation():
  17. """ Test that local complementation works as expected """
  18. g = mock.simple_graph()
  19. g.local_complementation(0)
  20. assert g.has_edge(0, 1)
  21. assert g.has_edge(0, 2)
  22. assert not g.has_edge(1, 2)
  23. assert g.has_edge(3, 2)
  24. assert g.has_edge(3, 1)
  25. # TODO: test VOP conditions
  26. def test_remove_vop():
  27. """ Test that removing VOPs really works """
  28. g = mock.simple_graph()
  29. g.remove_vop(0, 1)
  30. assert g.node[0]["vop"] == clifford.by_name["identity"]
  31. g.remove_vop(1, 1)
  32. assert g.node[1]["vop"] == clifford.by_name["identity"]
  33. g.remove_vop(2, 1)
  34. assert g.node[2]["vop"] == clifford.by_name["identity"]
  35. g.remove_vop(0, 1)
  36. assert g.node[0]["vop"] == clifford.by_name["identity"]
  37. def test_edgelist():
  38. """ Test making edgelists """
  39. g = mock.simple_graph()
  40. el = g.edgelist()
  41. assert (0, 3) in el
  42. assert (0, 2) in el
  43. assert (100, 200) in el
  44. def test_stress(n=int(1e5)):
  45. """ Testing that making a graph of ten thousand qubits takes less than half a second"""
  46. import time
  47. g = GraphState(range(n + 1))
  48. t = time.clock()
  49. for i in xrange(n):
  50. g._add_edge(i, i + 1)
  51. assert time.clock() - t < .5
  52. def test_cz():
  53. """ Test CZ gate """
  54. g = GraphState([0, 1])
  55. g.act_local_rotation(0, clifford.by_name["hadamard"])
  56. g.act_local_rotation(1, clifford.by_name["hadamard"])
  57. g.act_local_rotation(1, clifford.by_name["py"])
  58. assert not g.has_edge(0, 1)
  59. g.act_cz(0, 1)
  60. assert g.has_edge(0, 1)
  61. def test_local_complementation():
  62. """ Test that local complementation works okay """
  63. pairs = (0, 1), (0, 3), (1, 3), (1, 2),
  64. psi = GraphState(range(4))
  65. psi.act_circuit([(i, "hadamard") for i in psi.node])
  66. psi.act_circuit([(pair, "cz") for pair in pairs])
  67. old_edges = psi.edgelist()
  68. old_state = psi.to_state_vector()
  69. psi.local_complementation(1)
  70. assert old_edges != psi.edgelist()
  71. assert old_state == psi.to_state_vector()
  72. def test_single_qubit():
  73. """ A multi qubit test with Hadamards only"""
  74. for repeat in tqdm(range(REPEATS), desc="Single qubit rotations against CircuitModel"):
  75. circuit = [(0, random.choice(range(24))) for i in range(DEPTH)]
  76. a = mock.circuit_to_state(mock.ABPWrapper, 1, circuit)
  77. b = mock.circuit_to_state(mock.CircuitModelWrapper, 1, circuit)
  78. assert a.to_state_vector() == b
  79. def test_graph_state_multiqubit(n=6):
  80. """ A multi qubit test with Hadamards only"""
  81. for repeat in tqdm(range(REPEATS), desc="Random graph states against the CircuitModel"):
  82. circuit = mock.random_graph_circuit(n)
  83. a = mock.circuit_to_state(mock.ABPWrapper, n, circuit)
  84. b = mock.circuit_to_state(mock.CircuitModelWrapper, n, circuit)
  85. assert a.to_state_vector() == b
  86. def test_stabilizer_state_multiqubit(n=6):
  87. """ A multi qubit test with arbitrary local rotations """
  88. for repeat in tqdm(range(REPEATS), desc="Random Clifford circuits against the CircuitModel"):
  89. circuit = mock.random_stabilizer_circuit(n)
  90. a = mock.circuit_to_state(mock.ABPWrapper, n, circuit)
  91. b = mock.circuit_to_state(mock.CircuitModelWrapper, n, circuit)
  92. assert a.to_state_vector() == b