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 2.9KB

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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import numpy as np
  2. from abp import qi
  3. from abp import GraphState
  4. def test_init():
  5. """ Can you initialize some qubits """
  6. psi = qi.CircuitModel(5)
  7. assert psi.d == 32
  8. def test_single_qubit_stuff():
  9. """ Try some sensible single-qubit things """
  10. psi = qi.CircuitModel(2)
  11. psi.act_local_rotation(0, qi.px)
  12. assert np.allclose(psi.state[1], 1)
  13. psi.act_local_rotation(0, qi.px)
  14. assert np.allclose(psi.state[0], 1)
  15. psi.act_local_rotation(0, qi.px)
  16. psi.act_local_rotation(0, qi.pz)
  17. psi.act_local_rotation(0, qi.px)
  18. assert np.allclose(psi.state[0], -1)
  19. def test_further_single_qubit_stuff():
  20. """ Try some sensible single-qubit things """
  21. psi = qi.CircuitModel(2)
  22. psi.act_local_rotation(0, qi.py)
  23. psi.act_local_rotation(1, qi.py)
  24. psi.act_local_rotation(0, qi.pz)
  25. psi.act_local_rotation(1, qi.py)
  26. psi.act_local_rotation(0, qi.hadamard)
  27. psi.act_local_rotation(0, qi.pz)
  28. psi.act_local_rotation(0, qi.px)
  29. def test_more_single_qubit_stuff():
  30. """ Try some sensible single-qubit things """
  31. psi = qi.CircuitModel(2)
  32. psi.act_local_rotation(0, qi.px)
  33. psi.act_local_rotation(1, qi.px)
  34. psi.act_cz(0, 1)
  35. def test_equality():
  36. """ Test that equality succeeds / fails as desired """
  37. a = qi.CircuitModel(2)
  38. b = qi.CircuitModel(2)
  39. assert a == b
  40. a.act_local_rotation(0, qi.px)
  41. assert a != b
  42. def test_hadamard():
  43. """ What does CZ do ? """
  44. psi = qi.CircuitModel(3)
  45. psi.act_hadamard(0)
  46. psi.act_hadamard(1)
  47. assert np.allclose(psi.state, np.array([[1, 1, 1, 1, 0, 0, 0, 0]]).T / 2.)
  48. psi.act_hadamard(1)
  49. psi.act_hadamard(0)
  50. psi.act_hadamard(2)
  51. assert np.allclose(
  52. psi.state, qi.ir2 * np.array([[1, 0, 0, 0, 1, 0, 0, 0]]).T)
  53. def test_cz():
  54. """ What does CZ do ? """
  55. psi = qi.CircuitModel(2)
  56. psi.act_hadamard(0)
  57. psi.act_hadamard(1)
  58. psi.act_cz(0, 1)
  59. assert np.allclose(psi.state, qi.bond)
  60. def test_local_rotation():
  61. """ Do local rotations work okay? ? """
  62. psi = qi.CircuitModel(2)
  63. psi.act_local_rotation(0, qi.ha)
  64. psi.act_local_rotation(0, qi.ha)
  65. assert np.allclose(psi.state[0], 1)
  66. psi.act_local_rotation(0, qi.ha)
  67. psi.act_local_rotation(1, qi.ha)
  68. psi.act_local_rotation(0, qi.ha)
  69. psi.act_local_rotation(1, qi.ha)
  70. assert np.allclose(psi.state[0], 1)
  71. def test_dumbness():
  72. """ Check that I haven't done something really dumb """
  73. a = qi.CircuitModel(1)
  74. b = qi.CircuitModel(1)
  75. assert a == b
  76. a.act_local_rotation(0, qi.px)
  77. assert not (a == b)
  78. a.act_local_rotation(0, qi.px)
  79. assert (a == b)
  80. def test_to_state_vector_single_qubit():
  81. """ Test some single-qubit stuff """
  82. g = GraphState()
  83. g.add_node(0)
  84. g.add_node(1)
  85. g.act_local_rotation(0, "hadamard")
  86. g.act_local_rotation(1, "hadamard")
  87. g.act_cz(0, 1)
  88. assert np.allclose(g.to_state_vector().state, qi.bond)