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.6KB

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
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import numpy as np
  2. from abp import qi, GraphState
  3. from tqdm import tqdm
  4. import mock
  5. import nose
  6. DEPTH = 1000
  7. def test_init():
  8. """ Can you initialize some qubits """
  9. psi = qi.CircuitModel(5)
  10. assert psi.d == 32
  11. def test_single_qubit_stuff():
  12. """ Try some sensible single-qubit things """
  13. psi = qi.CircuitModel(2)
  14. psi.act_local_rotation(0, qi.px)
  15. assert np.allclose(psi.state[1], 1)
  16. psi.act_local_rotation(0, qi.px)
  17. assert np.allclose(psi.state[0], 1)
  18. psi.act_local_rotation(0, qi.px)
  19. psi.act_local_rotation(0, qi.pz)
  20. psi.act_local_rotation(0, qi.px)
  21. assert np.allclose(psi.state[0], -1)
  22. def test_further_single_qubit_stuff():
  23. """ Try some sensible single-qubit things """
  24. psi = qi.CircuitModel(2)
  25. psi.act_local_rotation(0, qi.py)
  26. psi.act_local_rotation(1, qi.py)
  27. psi.act_local_rotation(0, qi.pz)
  28. psi.act_local_rotation(1, qi.py)
  29. psi.act_local_rotation(0, qi.hadamard)
  30. psi.act_local_rotation(0, qi.pz)
  31. psi.act_local_rotation(0, qi.px)
  32. def test_more_single_qubit_stuff():
  33. """ Try some sensible single-qubit things """
  34. psi = qi.CircuitModel(2)
  35. psi.act_local_rotation(0, qi.px)
  36. psi.act_local_rotation(1, qi.px)
  37. psi.act_cz(0, 1)
  38. def test_equality():
  39. """ Test that equality succeeds / fails as desired """
  40. a = qi.CircuitModel(2)
  41. b = qi.CircuitModel(2)
  42. assert a == b
  43. a.act_local_rotation(0, qi.px)
  44. assert a != b
  45. def test_hadamard():
  46. """ What does CZ do ? """
  47. psi = qi.CircuitModel(3)
  48. psi.act_hadamard(0)
  49. psi.act_hadamard(1)
  50. assert np.allclose(psi.state, np.array([[1, 1, 1, 1, 0, 0, 0, 0]]).T / 2.)
  51. psi.act_hadamard(1)
  52. psi.act_hadamard(0)
  53. psi.act_hadamard(2)
  54. assert np.allclose(
  55. psi.state, qi.ir2 * np.array([[1, 0, 0, 0, 1, 0, 0, 0]]).T)
  56. def test_cz():
  57. """ What does CZ do ? """
  58. psi = qi.CircuitModel(2)
  59. psi.act_hadamard(0)
  60. psi.act_hadamard(1)
  61. psi.act_cz(0, 1)
  62. assert np.allclose(psi.state, qi.bond)
  63. def test_local_rotation():
  64. """ Do local rotations work okay? ? """
  65. psi = qi.CircuitModel(2)
  66. psi.act_local_rotation(0, qi.ha)
  67. psi.act_local_rotation(0, qi.ha)
  68. assert np.allclose(psi.state[0], 1)
  69. psi.act_local_rotation(0, qi.ha)
  70. psi.act_local_rotation(1, qi.ha)
  71. psi.act_local_rotation(0, qi.ha)
  72. psi.act_local_rotation(1, qi.ha)
  73. assert np.allclose(psi.state[0], 1)
  74. def test_dumbness():
  75. """ Check that I haven't done something really dumb """
  76. a = qi.CircuitModel(1)
  77. b = qi.CircuitModel(1)
  78. assert a == b
  79. a.act_local_rotation(0, qi.px)
  80. assert not (a == b)
  81. a.act_local_rotation(0, qi.px)
  82. assert (a == b)
  83. def test_to_state_vector_single_qubit():
  84. """ Test some single-qubit stuff """
  85. g = GraphState()
  86. g.add_qubit(0)
  87. g.add_qubit(1)
  88. g.act_local_rotation(0, "hadamard")
  89. g.act_local_rotation(1, "hadamard")
  90. g.act_cz(0, 1)
  91. assert np.allclose(g.to_state_vector().state, qi.bond)
  92. def test_normalize_global_phase():
  93. """ We should be able to see that two states are equivalent up to a global phase """
  94. for i in range(10):
  95. u = qi.pz
  96. phase = np.random.uniform(0, 2 * np.pi)
  97. m = np.exp(1j * phase) * u
  98. normalized = qi.normalize_global_phase(m)
  99. assert np.allclose(normalized, u)
  100. def test_against_chp(n=5):
  101. """ Test against CHP if it is installed """
  102. try:
  103. import chp
  104. except ImportError:
  105. raise nose.SkipTest("CHP is not installed")
  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)