test_represent.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. from sympy.core.numbers import (Float, I, Integer)
  2. from sympy.matrices.dense import Matrix
  3. from sympy.external import import_module
  4. from sympy.testing.pytest import skip
  5. from sympy.physics.quantum.dagger import Dagger
  6. from sympy.physics.quantum.represent import (represent, rep_innerproduct,
  7. rep_expectation, enumerate_states)
  8. from sympy.physics.quantum.state import Bra, Ket
  9. from sympy.physics.quantum.operator import Operator, OuterProduct
  10. from sympy.physics.quantum.tensorproduct import TensorProduct
  11. from sympy.physics.quantum.tensorproduct import matrix_tensor_product
  12. from sympy.physics.quantum.commutator import Commutator
  13. from sympy.physics.quantum.anticommutator import AntiCommutator
  14. from sympy.physics.quantum.innerproduct import InnerProduct
  15. from sympy.physics.quantum.matrixutils import (numpy_ndarray,
  16. scipy_sparse_matrix, to_numpy,
  17. to_scipy_sparse, to_sympy)
  18. from sympy.physics.quantum.cartesian import XKet, XOp, XBra
  19. from sympy.physics.quantum.qapply import qapply
  20. from sympy.physics.quantum.operatorset import operators_to_state
  21. Amat = Matrix([[1, I], [-I, 1]])
  22. Bmat = Matrix([[1, 2], [3, 4]])
  23. Avec = Matrix([[1], [I]])
  24. class AKet(Ket):
  25. @classmethod
  26. def dual_class(self):
  27. return ABra
  28. def _represent_default_basis(self, **options):
  29. return self._represent_AOp(None, **options)
  30. def _represent_AOp(self, basis, **options):
  31. return Avec
  32. class ABra(Bra):
  33. @classmethod
  34. def dual_class(self):
  35. return AKet
  36. class AOp(Operator):
  37. def _represent_default_basis(self, **options):
  38. return self._represent_AOp(None, **options)
  39. def _represent_AOp(self, basis, **options):
  40. return Amat
  41. class BOp(Operator):
  42. def _represent_default_basis(self, **options):
  43. return self._represent_AOp(None, **options)
  44. def _represent_AOp(self, basis, **options):
  45. return Bmat
  46. k = AKet('a')
  47. b = ABra('a')
  48. A = AOp('A')
  49. B = BOp('B')
  50. _tests = [
  51. # Bra
  52. (b, Dagger(Avec)),
  53. (Dagger(b), Avec),
  54. # Ket
  55. (k, Avec),
  56. (Dagger(k), Dagger(Avec)),
  57. # Operator
  58. (A, Amat),
  59. (Dagger(A), Dagger(Amat)),
  60. # OuterProduct
  61. (OuterProduct(k, b), Avec*Avec.H),
  62. # TensorProduct
  63. (TensorProduct(A, B), matrix_tensor_product(Amat, Bmat)),
  64. # Pow
  65. (A**2, Amat**2),
  66. # Add/Mul
  67. (A*B + 2*A, Amat*Bmat + 2*Amat),
  68. # Commutator
  69. (Commutator(A, B), Amat*Bmat - Bmat*Amat),
  70. # AntiCommutator
  71. (AntiCommutator(A, B), Amat*Bmat + Bmat*Amat),
  72. # InnerProduct
  73. (InnerProduct(b, k), (Avec.H*Avec)[0])
  74. ]
  75. def test_format_sympy():
  76. for test in _tests:
  77. lhs = represent(test[0], basis=A, format='sympy')
  78. rhs = to_sympy(test[1])
  79. assert lhs == rhs
  80. def test_scalar_sympy():
  81. assert represent(Integer(1)) == Integer(1)
  82. assert represent(Float(1.0)) == Float(1.0)
  83. assert represent(1.0 + I) == 1.0 + I
  84. np = import_module('numpy')
  85. def test_format_numpy():
  86. if not np:
  87. skip("numpy not installed.")
  88. for test in _tests:
  89. lhs = represent(test[0], basis=A, format='numpy')
  90. rhs = to_numpy(test[1])
  91. if isinstance(lhs, numpy_ndarray):
  92. assert (lhs == rhs).all()
  93. else:
  94. assert lhs == rhs
  95. def test_scalar_numpy():
  96. if not np:
  97. skip("numpy not installed.")
  98. assert represent(Integer(1), format='numpy') == 1
  99. assert represent(Float(1.0), format='numpy') == 1.0
  100. assert represent(1.0 + I, format='numpy') == 1.0 + 1.0j
  101. scipy = import_module('scipy', import_kwargs={'fromlist': ['sparse']})
  102. def test_format_scipy_sparse():
  103. if not np:
  104. skip("numpy not installed.")
  105. if not scipy:
  106. skip("scipy not installed.")
  107. for test in _tests:
  108. lhs = represent(test[0], basis=A, format='scipy.sparse')
  109. rhs = to_scipy_sparse(test[1])
  110. if isinstance(lhs, scipy_sparse_matrix):
  111. assert np.linalg.norm((lhs - rhs).todense()) == 0.0
  112. else:
  113. assert lhs == rhs
  114. def test_scalar_scipy_sparse():
  115. if not np:
  116. skip("numpy not installed.")
  117. if not scipy:
  118. skip("scipy not installed.")
  119. assert represent(Integer(1), format='scipy.sparse') == 1
  120. assert represent(Float(1.0), format='scipy.sparse') == 1.0
  121. assert represent(1.0 + I, format='scipy.sparse') == 1.0 + 1.0j
  122. x_ket = XKet('x')
  123. x_bra = XBra('x')
  124. x_op = XOp('X')
  125. def test_innerprod_represent():
  126. assert rep_innerproduct(x_ket) == InnerProduct(XBra("x_1"), x_ket).doit()
  127. assert rep_innerproduct(x_bra) == InnerProduct(x_bra, XKet("x_1")).doit()
  128. try:
  129. rep_innerproduct(x_op)
  130. except TypeError:
  131. return True
  132. def test_operator_represent():
  133. basis_kets = enumerate_states(operators_to_state(x_op), 1, 2)
  134. assert rep_expectation(
  135. x_op) == qapply(basis_kets[1].dual*x_op*basis_kets[0])
  136. def test_enumerate_states():
  137. test = XKet("foo")
  138. assert enumerate_states(test, 1, 1) == [XKet("foo_1")]
  139. assert enumerate_states(
  140. test, [1, 2, 4]) == [XKet("foo_1"), XKet("foo_2"), XKet("foo_4")]