test_density.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. from sympy.core.numbers import Rational
  2. from sympy.core.singleton import S
  3. from sympy.core.symbol import symbols
  4. from sympy.functions.elementary.exponential import log
  5. from sympy.external import import_module
  6. from sympy.physics.quantum.density import Density, entropy, fidelity
  7. from sympy.physics.quantum.state import Ket, TimeDepKet
  8. from sympy.physics.quantum.qubit import Qubit
  9. from sympy.physics.quantum.represent import represent
  10. from sympy.physics.quantum.dagger import Dagger
  11. from sympy.physics.quantum.cartesian import XKet, PxKet, PxOp, XOp
  12. from sympy.physics.quantum.spin import JzKet
  13. from sympy.physics.quantum.operator import OuterProduct
  14. from sympy.physics.quantum.trace import Tr
  15. from sympy.functions import sqrt
  16. from sympy.testing.pytest import raises
  17. from sympy.physics.quantum.matrixutils import scipy_sparse_matrix
  18. from sympy.physics.quantum.tensorproduct import TensorProduct
  19. def test_eval_args():
  20. # check instance created
  21. assert isinstance(Density([Ket(0), 0.5], [Ket(1), 0.5]), Density)
  22. assert isinstance(Density([Qubit('00'), 1/sqrt(2)],
  23. [Qubit('11'), 1/sqrt(2)]), Density)
  24. #test if Qubit object type preserved
  25. d = Density([Qubit('00'), 1/sqrt(2)], [Qubit('11'), 1/sqrt(2)])
  26. for (state, prob) in d.args:
  27. assert isinstance(state, Qubit)
  28. # check for value error, when prob is not provided
  29. raises(ValueError, lambda: Density([Ket(0)], [Ket(1)]))
  30. def test_doit():
  31. x, y = symbols('x y')
  32. A, B, C, D, E, F = symbols('A B C D E F', commutative=False)
  33. d = Density([XKet(), 0.5], [PxKet(), 0.5])
  34. assert (0.5*(PxKet()*Dagger(PxKet())) +
  35. 0.5*(XKet()*Dagger(XKet()))) == d.doit()
  36. # check for kets with expr in them
  37. d_with_sym = Density([XKet(x*y), 0.5], [PxKet(x*y), 0.5])
  38. assert (0.5*(PxKet(x*y)*Dagger(PxKet(x*y))) +
  39. 0.5*(XKet(x*y)*Dagger(XKet(x*y)))) == d_with_sym.doit()
  40. d = Density([(A + B)*C, 1.0])
  41. assert d.doit() == (1.0*A*C*Dagger(C)*Dagger(A) +
  42. 1.0*A*C*Dagger(C)*Dagger(B) +
  43. 1.0*B*C*Dagger(C)*Dagger(A) +
  44. 1.0*B*C*Dagger(C)*Dagger(B))
  45. # With TensorProducts as args
  46. # Density with simple tensor products as args
  47. t = TensorProduct(A, B, C)
  48. d = Density([t, 1.0])
  49. assert d.doit() == \
  50. 1.0 * TensorProduct(A*Dagger(A), B*Dagger(B), C*Dagger(C))
  51. # Density with multiple Tensorproducts as states
  52. t2 = TensorProduct(A, B)
  53. t3 = TensorProduct(C, D)
  54. d = Density([t2, 0.5], [t3, 0.5])
  55. assert d.doit() == (0.5 * TensorProduct(A*Dagger(A), B*Dagger(B)) +
  56. 0.5 * TensorProduct(C*Dagger(C), D*Dagger(D)))
  57. #Density with mixed states
  58. d = Density([t2 + t3, 1.0])
  59. assert d.doit() == (1.0 * TensorProduct(A*Dagger(A), B*Dagger(B)) +
  60. 1.0 * TensorProduct(A*Dagger(C), B*Dagger(D)) +
  61. 1.0 * TensorProduct(C*Dagger(A), D*Dagger(B)) +
  62. 1.0 * TensorProduct(C*Dagger(C), D*Dagger(D)))
  63. #Density operators with spin states
  64. tp1 = TensorProduct(JzKet(1, 1), JzKet(1, -1))
  65. d = Density([tp1, 1])
  66. # full trace
  67. t = Tr(d)
  68. assert t.doit() == 1
  69. #Partial trace on density operators with spin states
  70. t = Tr(d, [0])
  71. assert t.doit() == JzKet(1, -1) * Dagger(JzKet(1, -1))
  72. t = Tr(d, [1])
  73. assert t.doit() == JzKet(1, 1) * Dagger(JzKet(1, 1))
  74. # with another spin state
  75. tp2 = TensorProduct(JzKet(S.Half, S.Half), JzKet(S.Half, Rational(-1, 2)))
  76. d = Density([tp2, 1])
  77. #full trace
  78. t = Tr(d)
  79. assert t.doit() == 1
  80. #Partial trace on density operators with spin states
  81. t = Tr(d, [0])
  82. assert t.doit() == JzKet(S.Half, Rational(-1, 2)) * Dagger(JzKet(S.Half, Rational(-1, 2)))
  83. t = Tr(d, [1])
  84. assert t.doit() == JzKet(S.Half, S.Half) * Dagger(JzKet(S.Half, S.Half))
  85. def test_apply_op():
  86. d = Density([Ket(0), 0.5], [Ket(1), 0.5])
  87. assert d.apply_op(XOp()) == Density([XOp()*Ket(0), 0.5],
  88. [XOp()*Ket(1), 0.5])
  89. def test_represent():
  90. x, y = symbols('x y')
  91. d = Density([XKet(), 0.5], [PxKet(), 0.5])
  92. assert (represent(0.5*(PxKet()*Dagger(PxKet()))) +
  93. represent(0.5*(XKet()*Dagger(XKet())))) == represent(d)
  94. # check for kets with expr in them
  95. d_with_sym = Density([XKet(x*y), 0.5], [PxKet(x*y), 0.5])
  96. assert (represent(0.5*(PxKet(x*y)*Dagger(PxKet(x*y)))) +
  97. represent(0.5*(XKet(x*y)*Dagger(XKet(x*y))))) == \
  98. represent(d_with_sym)
  99. # check when given explicit basis
  100. assert (represent(0.5*(XKet()*Dagger(XKet())), basis=PxOp()) +
  101. represent(0.5*(PxKet()*Dagger(PxKet())), basis=PxOp())) == \
  102. represent(d, basis=PxOp())
  103. def test_states():
  104. d = Density([Ket(0), 0.5], [Ket(1), 0.5])
  105. states = d.states()
  106. assert states[0] == Ket(0) and states[1] == Ket(1)
  107. def test_probs():
  108. d = Density([Ket(0), .75], [Ket(1), 0.25])
  109. probs = d.probs()
  110. assert probs[0] == 0.75 and probs[1] == 0.25
  111. #probs can be symbols
  112. x, y = symbols('x y')
  113. d = Density([Ket(0), x], [Ket(1), y])
  114. probs = d.probs()
  115. assert probs[0] == x and probs[1] == y
  116. def test_get_state():
  117. x, y = symbols('x y')
  118. d = Density([Ket(0), x], [Ket(1), y])
  119. states = (d.get_state(0), d.get_state(1))
  120. assert states[0] == Ket(0) and states[1] == Ket(1)
  121. def test_get_prob():
  122. x, y = symbols('x y')
  123. d = Density([Ket(0), x], [Ket(1), y])
  124. probs = (d.get_prob(0), d.get_prob(1))
  125. assert probs[0] == x and probs[1] == y
  126. def test_entropy():
  127. up = JzKet(S.Half, S.Half)
  128. down = JzKet(S.Half, Rational(-1, 2))
  129. d = Density((up, S.Half), (down, S.Half))
  130. # test for density object
  131. ent = entropy(d)
  132. assert entropy(d) == log(2)/2
  133. assert d.entropy() == log(2)/2
  134. np = import_module('numpy', min_module_version='1.4.0')
  135. if np:
  136. #do this test only if 'numpy' is available on test machine
  137. np_mat = represent(d, format='numpy')
  138. ent = entropy(np_mat)
  139. assert isinstance(np_mat, np.ndarray)
  140. assert ent.real == 0.69314718055994529
  141. assert ent.imag == 0
  142. scipy = import_module('scipy', import_kwargs={'fromlist': ['sparse']})
  143. if scipy and np:
  144. #do this test only if numpy and scipy are available
  145. mat = represent(d, format="scipy.sparse")
  146. assert isinstance(mat, scipy_sparse_matrix)
  147. assert ent.real == 0.69314718055994529
  148. assert ent.imag == 0
  149. def test_eval_trace():
  150. up = JzKet(S.Half, S.Half)
  151. down = JzKet(S.Half, Rational(-1, 2))
  152. d = Density((up, 0.5), (down, 0.5))
  153. t = Tr(d)
  154. assert t.doit() == 1.0
  155. #test dummy time dependent states
  156. class TestTimeDepKet(TimeDepKet):
  157. def _eval_trace(self, bra, **options):
  158. return 1
  159. x, t = symbols('x t')
  160. k1 = TestTimeDepKet(0, 0.5)
  161. k2 = TestTimeDepKet(0, 1)
  162. d = Density([k1, 0.5], [k2, 0.5])
  163. assert d.doit() == (0.5 * OuterProduct(k1, k1.dual) +
  164. 0.5 * OuterProduct(k2, k2.dual))
  165. t = Tr(d)
  166. assert t.doit() == 1.0
  167. def test_fidelity():
  168. #test with kets
  169. up = JzKet(S.Half, S.Half)
  170. down = JzKet(S.Half, Rational(-1, 2))
  171. updown = (S.One/sqrt(2))*up + (S.One/sqrt(2))*down
  172. #check with matrices
  173. up_dm = represent(up * Dagger(up))
  174. down_dm = represent(down * Dagger(down))
  175. updown_dm = represent(updown * Dagger(updown))
  176. assert abs(fidelity(up_dm, up_dm) - 1) < 1e-3
  177. assert fidelity(up_dm, down_dm) < 1e-3
  178. assert abs(fidelity(up_dm, updown_dm) - (S.One/sqrt(2))) < 1e-3
  179. assert abs(fidelity(updown_dm, down_dm) - (S.One/sqrt(2))) < 1e-3
  180. #check with density
  181. up_dm = Density([up, 1.0])
  182. down_dm = Density([down, 1.0])
  183. updown_dm = Density([updown, 1.0])
  184. assert abs(fidelity(up_dm, up_dm) - 1) < 1e-3
  185. assert abs(fidelity(up_dm, down_dm)) < 1e-3
  186. assert abs(fidelity(up_dm, updown_dm) - (S.One/sqrt(2))) < 1e-3
  187. assert abs(fidelity(updown_dm, down_dm) - (S.One/sqrt(2))) < 1e-3
  188. #check mixed states with density
  189. updown2 = sqrt(3)/2*up + S.Half*down
  190. d1 = Density([updown, 0.25], [updown2, 0.75])
  191. d2 = Density([updown, 0.75], [updown2, 0.25])
  192. assert abs(fidelity(d1, d2) - 0.991) < 1e-3
  193. assert abs(fidelity(d2, d1) - fidelity(d1, d2)) < 1e-3
  194. #using qubits/density(pure states)
  195. state1 = Qubit('0')
  196. state2 = Qubit('1')
  197. state3 = S.One/sqrt(2)*state1 + S.One/sqrt(2)*state2
  198. state4 = sqrt(Rational(2, 3))*state1 + S.One/sqrt(3)*state2
  199. state1_dm = Density([state1, 1])
  200. state2_dm = Density([state2, 1])
  201. state3_dm = Density([state3, 1])
  202. assert fidelity(state1_dm, state1_dm) == 1
  203. assert fidelity(state1_dm, state2_dm) == 0
  204. assert abs(fidelity(state1_dm, state3_dm) - 1/sqrt(2)) < 1e-3
  205. assert abs(fidelity(state3_dm, state2_dm) - 1/sqrt(2)) < 1e-3
  206. #using qubits/density(mixed states)
  207. d1 = Density([state3, 0.70], [state4, 0.30])
  208. d2 = Density([state3, 0.20], [state4, 0.80])
  209. assert abs(fidelity(d1, d1) - 1) < 1e-3
  210. assert abs(fidelity(d1, d2) - 0.996) < 1e-3
  211. assert abs(fidelity(d1, d2) - fidelity(d2, d1)) < 1e-3
  212. #TODO: test for invalid arguments
  213. # non-square matrix
  214. mat1 = [[0, 0],
  215. [0, 0],
  216. [0, 0]]
  217. mat2 = [[0, 0],
  218. [0, 0]]
  219. raises(ValueError, lambda: fidelity(mat1, mat2))
  220. # unequal dimensions
  221. mat1 = [[0, 0],
  222. [0, 0]]
  223. mat2 = [[0, 0, 0],
  224. [0, 0, 0],
  225. [0, 0, 0]]
  226. raises(ValueError, lambda: fidelity(mat1, mat2))
  227. # unsupported data-type
  228. x, y = 1, 2 # random values that is not a matrix
  229. raises(ValueError, lambda: fidelity(x, y))