Anders and Briegel in Python
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

177 wiersze
4.7KB

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