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.

164 lines
4.4KB

  1. import numpy as np
  2. from abp import qi, GraphState
  3. from tqdm import tqdm
  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_node(0)
  85. g.add_node(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. print "Not testing against CHP -- not installed"
  104. return
  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 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(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()