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

test_graphstate.py 3.9KB

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