Anders and Briegel in Python
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

test_graphstate.py 4.2KB

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