Anders and Briegel in Python
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

test_qi_circuit_model.py 2.3KB

il y a 8 ans
il y a 8 ans
il y a 8 ans
il y a 8 ans
il y a 8 ans
il y a 8 ans
il y a 8 ans
il y a 8 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import numpy as np
  2. from abp import qi
  3. def test_init():
  4. """ Can you initialize some qubits """
  5. psi = qi.CircuitModel(5)
  6. assert psi.d == 32
  7. def test_single_qubit_stuff():
  8. """ Try some sensible single-qubit things """
  9. psi = qi.CircuitModel(2)
  10. psi.act_local_rotation(0, qi.px)
  11. assert np.allclose(psi.state[1], 1)
  12. psi.act_local_rotation(0, qi.px)
  13. assert np.allclose(psi.state[0], 1)
  14. psi.act_local_rotation(0, qi.px)
  15. psi.act_local_rotation(0, qi.pz)
  16. psi.act_local_rotation(0, qi.px)
  17. assert np.allclose(psi.state[0], -1)
  18. def test_further_single_qubit_stuff():
  19. """ Try some sensible single-qubit things """
  20. psi = qi.CircuitModel(2)
  21. psi.act_local_rotation(0, qi.py)
  22. psi.act_local_rotation(1, qi.py)
  23. psi.act_local_rotation(0, qi.pz)
  24. psi.act_local_rotation(1, qi.py)
  25. psi.act_local_rotation(0, qi.hadamard)
  26. psi.act_local_rotation(0, qi.pz)
  27. psi.act_local_rotation(0, qi.px)
  28. def test_more_single_qubit_stuff():
  29. """ Try some sensible single-qubit things """
  30. psi = qi.CircuitModel(2)
  31. psi.act_local_rotation(0, qi.px)
  32. psi.act_local_rotation(1, qi.px)
  33. psi.act_cz(0, 1)
  34. def test_equality():
  35. """ Test that equality succeeds / fails as desired """
  36. a = qi.CircuitModel(2)
  37. b = qi.CircuitModel(2)
  38. assert a == b
  39. a.act_local_rotation(0, qi.px)
  40. assert a != b
  41. def test_hadamard():
  42. """ What does CZ do ? """
  43. psi = qi.CircuitModel(3)
  44. psi.act_hadamard(0)
  45. psi.act_hadamard(1)
  46. assert np.allclose(psi.state, np.array([[1,1,1,1,0,0,0,0]]).T/2.)
  47. psi.act_hadamard(1)
  48. psi.act_hadamard(0)
  49. psi.act_hadamard(2)
  50. assert np.allclose(psi.state, qi.ir2*np.array([[1,0,0,0,1,0,0,0]]).T)
  51. def test_cz():
  52. """ What does CZ do ? """
  53. psi = qi.CircuitModel(2)
  54. psi.act_hadamard(0)
  55. psi.act_hadamard(1)
  56. psi.act_cz(0, 1)
  57. assert np.allclose(psi.state, qi.bond)
  58. def test_local_rotation():
  59. """ Do local rotations work okay? ? """
  60. psi = qi.CircuitModel(2)
  61. psi.act_local_rotation(0, qi.ha)
  62. psi.act_local_rotation(0, qi.ha)
  63. assert np.allclose(psi.state[0], 1)
  64. psi.act_local_rotation(0, qi.ha)
  65. psi.act_local_rotation(1, qi.ha)
  66. psi.act_local_rotation(0, qi.ha)
  67. psi.act_local_rotation(1, qi.ha)
  68. assert np.allclose(psi.state[0], 1)