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.

test_qi.py 4.7KB

8 years ago
7 years ago
5 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
5 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import numpy as np
  2. from abp import qi, GraphState
  3. import mock
  4. import pytest
  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_qubit(0)
  86. g.add_qubit(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. raise pytest.skip("CHP is not installed")
  105. def get_chp_state():
  106. """ Helper to convert CHP to CircuitModel """
  107. output = qi.CircuitModel(n)
  108. ket = chp.get_ket()
  109. nonzero = np.sqrt(len(ket))
  110. output.state[0, 0] = 0
  111. for key, phase in list(ket.items()):
  112. output.state[key] = np.exp(1j * phase * np.pi / 2) / nonzero
  113. return output
  114. # Run a simple circuit
  115. chp.init(n)
  116. chp.act_hadamard(0)
  117. chp.act_cnot(0, 1)
  118. psi = qi.CircuitModel(n)
  119. psi.act_hadamard(0)
  120. psi.act_cnot(0, 1)
  121. assert psi == get_chp_state()
  122. # Run a random circuit
  123. chp.init(n)
  124. psi = qi.CircuitModel(n)
  125. for i in list(range(DEPTH)):
  126. if np.random.rand() > .5:
  127. a = np.random.randint(0, n - 1)
  128. chp.act_hadamard(a)
  129. psi.act_hadamard(a)
  130. else:
  131. a, b = np.random.randint(0, n - 1, 2)
  132. if a != b:
  133. chp.act_cnot(a, b)
  134. psi.act_cnot(a, b)
  135. assert psi == get_chp_state()
  136. def test_sqrt_notation(n=2):
  137. """ Test that SQRT notation looks nice """
  138. c = mock.random_stabilizer_circuit(n)
  139. g = GraphState(list(range(n)))
  140. g.act_circuit(c)
  141. def test_indexint():
  142. """ Test that we can index into state vectors """
  143. psi = qi.CircuitModel(0)
  144. assert psi[0] == 1+0j
  145. psi[0] = 42
  146. assert psi[0] == 42