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.

148 line
4.3KB

  1. from abp import GraphState, CircuitModel, clifford
  2. import random
  3. import numpy as np
  4. import networkx as nx
  5. import pytest
  6. mock = pytest.importorskip("mock")
  7. REPEATS = 100
  8. DEPTH = 100
  9. def test_initialization():
  10. g = GraphState("abc")
  11. assert g.node["a"]["vop"] == clifford.identity
  12. g = GraphState("abc", vop="hadamard")
  13. assert g.node["c"]["vop"] == clifford.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.identity
  39. g.remove_vop(1, 1)
  40. assert g.node[1]["vop"] == clifford.identity
  41. g.remove_vop(2, 1)
  42. assert g.node[2]["vop"] == clifford.identity
  43. g.remove_vop(0, 1)
  44. assert g.node[0]["vop"] == clifford.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(list(range(n + 1)), vop="hadamard")
  56. t = time.clock()
  57. for i in range(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.hadamard)
  64. g.act_local_rotation(1, clifford.hadamard)
  65. g.act_local_rotation(1, clifford.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_czs():
  70. """ Test multiple CZ shorthand """
  71. g = GraphState([0, 1, 2])
  72. g.act_czs((0, 1), (1, 2))
  73. assert len(g.edgelist()) == 2
  74. def test_local_complementation():
  75. """ Test that local complementation works okay """
  76. pairs = (0, 1), (0, 3), (1, 3), (1, 2),
  77. psi = GraphState(list(range(4)), vop="hadamard")
  78. psi.act_circuit([(i, "hadamard") for i in psi.node])
  79. psi.act_circuit([(pair, "cz") for pair in pairs])
  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 list(range(REPEATS)):
  88. circuit = [(0, random.choice(list(range(24)))) for i in range(DEPTH)]
  89. a = mock.circuit_to_state(mock.ABPWrapper, 1, circuit)
  90. b = mock.circuit_to_state(mock.CircuitModelWrapper, 1, circuit)
  91. assert a.to_state_vector() == b
  92. def test_graph_state_multiqubit(n=6):
  93. """ A multi qubit test with Hadamards only"""
  94. for repeat in list(range(REPEATS)):
  95. circuit = mock.random_graph_circuit(n)
  96. a = mock.circuit_to_state(mock.ABPWrapper, n, circuit)
  97. b = mock.circuit_to_state(mock.CircuitModelWrapper, n, circuit)
  98. assert a.to_state_vector() == b
  99. def test_stabilizer_state_multiqubit(n=6):
  100. """ A multi qubit test with arbitrary local rotations """
  101. for repeat in list(range(REPEATS)):
  102. circuit = mock.random_stabilizer_circuit(n)
  103. a = mock.circuit_to_state(mock.ABPWrapper, n, circuit)
  104. b = mock.circuit_to_state(mock.CircuitModelWrapper, n, circuit)
  105. assert a.to_state_vector() == b
  106. def test_from_nx():
  107. """ Creating from a networkx graph """
  108. g = nx.random_geometric_graph(100, 2)
  109. psi = GraphState(g)
  110. assert len(psi.node) == 100
  111. psi = GraphState(nx.Graph(((0, 1),)))
  112. def test_del_node():
  113. """ Test deleting nodes """
  114. g = GraphState(10)
  115. g.act_circuit(mock.random_stabilizer_circuit())
  116. g._del_node(0)
  117. assert g.order() == 9