Anders and Briegel in Python
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
4.2KB

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