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.

178 lines
4.8KB

  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_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 nose.SkipTest("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 tqdm(list(range(DEPTH)), "Testing CircuitModel against CHP"):
  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