Anders and Briegel in Python
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

8 лет назад
8 лет назад
8 лет назад
8 лет назад
8 лет назад
8 лет назад
8 лет назад
8 лет назад
8 лет назад
8 лет назад
8 лет назад
8 лет назад
8 лет назад
8 лет назад
8 лет назад
8 лет назад
8 лет назад
8 лет назад
8 лет назад
8 лет назад
8 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import numpy as np
  2. from abp import qi, GraphState
  3. from tqdm import tqdm
  4. import mock
  5. DEPTH = 1000
  6. def test_init():
  7. """ Can you initialize some qubits """
  8. psi = qi.CircuitModel(5)
  9. assert psi.d == 32
  10. def test_single_qubit_stuff():
  11. """ Try some sensible single-qubit things """
  12. psi = qi.CircuitModel(2)
  13. psi.act_local_rotation(0, qi.px)
  14. assert np.allclose(psi.state[1], 1)
  15. psi.act_local_rotation(0, qi.px)
  16. assert np.allclose(psi.state[0], 1)
  17. psi.act_local_rotation(0, qi.px)
  18. psi.act_local_rotation(0, qi.pz)
  19. psi.act_local_rotation(0, qi.px)
  20. assert np.allclose(psi.state[0], -1)
  21. def test_further_single_qubit_stuff():
  22. """ Try some sensible single-qubit things """
  23. psi = qi.CircuitModel(2)
  24. psi.act_local_rotation(0, qi.py)
  25. psi.act_local_rotation(1, qi.py)
  26. psi.act_local_rotation(0, qi.pz)
  27. psi.act_local_rotation(1, qi.py)
  28. psi.act_local_rotation(0, qi.hadamard)
  29. psi.act_local_rotation(0, qi.pz)
  30. psi.act_local_rotation(0, qi.px)
  31. def test_more_single_qubit_stuff():
  32. """ Try some sensible single-qubit things """
  33. psi = qi.CircuitModel(2)
  34. psi.act_local_rotation(0, qi.px)
  35. psi.act_local_rotation(1, qi.px)
  36. psi.act_cz(0, 1)
  37. def test_equality():
  38. """ Test that equality succeeds / fails as desired """
  39. a = qi.CircuitModel(2)
  40. b = qi.CircuitModel(2)
  41. assert a == b
  42. a.act_local_rotation(0, qi.px)
  43. assert a != b
  44. def test_hadamard():
  45. """ What does CZ do ? """
  46. psi = qi.CircuitModel(3)
  47. psi.act_hadamard(0)
  48. psi.act_hadamard(1)
  49. assert np.allclose(psi.state, np.array([[1, 1, 1, 1, 0, 0, 0, 0]]).T / 2.)
  50. psi.act_hadamard(1)
  51. psi.act_hadamard(0)
  52. psi.act_hadamard(2)
  53. assert np.allclose(
  54. psi.state, qi.ir2 * np.array([[1, 0, 0, 0, 1, 0, 0, 0]]).T)
  55. def test_cz():
  56. """ What does CZ do ? """
  57. psi = qi.CircuitModel(2)
  58. psi.act_hadamard(0)
  59. psi.act_hadamard(1)
  60. psi.act_cz(0, 1)
  61. assert np.allclose(psi.state, qi.bond)
  62. def test_local_rotation():
  63. """ Do local rotations work okay? ? """
  64. psi = qi.CircuitModel(2)
  65. psi.act_local_rotation(0, qi.ha)
  66. psi.act_local_rotation(0, qi.ha)
  67. assert np.allclose(psi.state[0], 1)
  68. psi.act_local_rotation(0, qi.ha)
  69. psi.act_local_rotation(1, qi.ha)
  70. psi.act_local_rotation(0, qi.ha)
  71. psi.act_local_rotation(1, qi.ha)
  72. assert np.allclose(psi.state[0], 1)
  73. def test_dumbness():
  74. """ Check that I haven't done something really dumb """
  75. a = qi.CircuitModel(1)
  76. b = qi.CircuitModel(1)
  77. assert a == b
  78. a.act_local_rotation(0, qi.px)
  79. assert not (a == b)
  80. a.act_local_rotation(0, qi.px)
  81. assert (a == b)
  82. def test_to_state_vector_single_qubit():
  83. """ Test some single-qubit stuff """
  84. g = GraphState()
  85. g.add_node(0)
  86. g.add_node(1)
  87. g.act_local_rotation(0, "hadamard")
  88. g.act_local_rotation(1, "hadamard")
  89. g.act_cz(0, 1)
  90. assert np.allclose(g.to_state_vector().state, qi.bond)
  91. def test_normalize_global_phase():
  92. """ We should be able to see that two states are equivalent up to a global phase """
  93. for i in range(10):
  94. u = qi.pz
  95. phase = np.random.uniform(0, 2 * np.pi)
  96. m = np.exp(1j * phase) * u
  97. normalized = qi.normalize_global_phase(m)
  98. assert np.allclose(normalized, u)
  99. def test_against_chp(n=5):
  100. """ Test against CHP if it is installed """
  101. try:
  102. import chp
  103. except ImportError:
  104. print "Not testing against CHP -- not installed"
  105. return
  106. def get_chp_state():
  107. """ Helper to convert CHP to CircuitModel """
  108. output = qi.CircuitModel(n)
  109. ket = chp.get_ket()
  110. nonzero = np.sqrt(len(ket))
  111. output.state[0, 0] = 0
  112. for key, phase in ket.items():
  113. output.state[key] = np.exp(1j * phase * np.pi / 2) / nonzero
  114. return output
  115. # Run a simple circuit
  116. chp.init(n)
  117. chp.act_hadamard(0)
  118. chp.act_cnot(0, 1)
  119. psi = qi.CircuitModel(n)
  120. psi.act_hadamard(0)
  121. psi.act_cnot(0, 1)
  122. assert psi == get_chp_state()
  123. # Run a random circuit
  124. chp.init(n)
  125. psi = qi.CircuitModel(n)
  126. for i in tqdm(range(DEPTH), "Testing CircuitModel against CHP"):
  127. if np.random.rand() > .5:
  128. a = np.random.randint(0, n - 1)
  129. chp.act_hadamard(a)
  130. psi.act_hadamard(a)
  131. else:
  132. a, b = np.random.randint(0, n - 1, 2)
  133. if a != b:
  134. chp.act_cnot(a, b)
  135. psi.act_cnot(a, b)
  136. assert psi == get_chp_state()
  137. def test_sqrt_notation(n=2):
  138. """ Test that SQRT notation looks nice """
  139. c = mock.random_stabilizer_circuit(n)
  140. g = GraphState(range(n))
  141. g.act_circuit(c)
  142. print g.to_state_vector()