test_represent.py 5.1 KB

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