qapply.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. """Logic for applying operators to states.
  2. Todo:
  3. * Sometimes the final result needs to be expanded, we should do this by hand.
  4. """
  5. from sympy.core.add import Add
  6. from sympy.core.mul import Mul
  7. from sympy.core.power import Pow
  8. from sympy.core.singleton import S
  9. from sympy.core.sympify import sympify
  10. from sympy.physics.quantum.anticommutator import AntiCommutator
  11. from sympy.physics.quantum.commutator import Commutator
  12. from sympy.physics.quantum.dagger import Dagger
  13. from sympy.physics.quantum.innerproduct import InnerProduct
  14. from sympy.physics.quantum.operator import OuterProduct, Operator
  15. from sympy.physics.quantum.state import State, KetBase, BraBase, Wavefunction
  16. from sympy.physics.quantum.tensorproduct import TensorProduct
  17. __all__ = [
  18. 'qapply'
  19. ]
  20. #-----------------------------------------------------------------------------
  21. # Main code
  22. #-----------------------------------------------------------------------------
  23. def qapply(e, **options):
  24. """Apply operators to states in a quantum expression.
  25. Parameters
  26. ==========
  27. e : Expr
  28. The expression containing operators and states. This expression tree
  29. will be walked to find operators acting on states symbolically.
  30. options : dict
  31. A dict of key/value pairs that determine how the operator actions
  32. are carried out.
  33. The following options are valid:
  34. * ``dagger``: try to apply Dagger operators to the left
  35. (default: False).
  36. * ``ip_doit``: call ``.doit()`` in inner products when they are
  37. encountered (default: True).
  38. Returns
  39. =======
  40. e : Expr
  41. The original expression, but with the operators applied to states.
  42. Examples
  43. ========
  44. >>> from sympy.physics.quantum import qapply, Ket, Bra
  45. >>> b = Bra('b')
  46. >>> k = Ket('k')
  47. >>> A = k * b
  48. >>> A
  49. |k><b|
  50. >>> qapply(A * b.dual / (b * b.dual))
  51. |k>
  52. >>> qapply(k.dual * A / (k.dual * k), dagger=True)
  53. <b|
  54. >>> qapply(k.dual * A / (k.dual * k))
  55. <k|*|k><b|/<k|k>
  56. """
  57. from sympy.physics.quantum.density import Density
  58. dagger = options.get('dagger', False)
  59. if e == 0:
  60. return S.Zero
  61. # This may be a bit aggressive but ensures that everything gets expanded
  62. # to its simplest form before trying to apply operators. This includes
  63. # things like (A+B+C)*|a> and A*(|a>+|b>) and all Commutators and
  64. # TensorProducts. The only problem with this is that if we can't apply
  65. # all the Operators, we have just expanded everything.
  66. # TODO: don't expand the scalars in front of each Mul.
  67. e = e.expand(commutator=True, tensorproduct=True)
  68. # If we just have a raw ket, return it.
  69. if isinstance(e, KetBase):
  70. return e
  71. # We have an Add(a, b, c, ...) and compute
  72. # Add(qapply(a), qapply(b), ...)
  73. elif isinstance(e, Add):
  74. result = 0
  75. for arg in e.args:
  76. result += qapply(arg, **options)
  77. return result.expand()
  78. # For a Density operator call qapply on its state
  79. elif isinstance(e, Density):
  80. new_args = [(qapply(state, **options), prob) for (state,
  81. prob) in e.args]
  82. return Density(*new_args)
  83. # For a raw TensorProduct, call qapply on its args.
  84. elif isinstance(e, TensorProduct):
  85. return TensorProduct(*[qapply(t, **options) for t in e.args])
  86. # For a Pow, call qapply on its base.
  87. elif isinstance(e, Pow):
  88. return qapply(e.base, **options)**e.exp
  89. # We have a Mul where there might be actual operators to apply to kets.
  90. elif isinstance(e, Mul):
  91. c_part, nc_part = e.args_cnc()
  92. c_mul = Mul(*c_part)
  93. nc_mul = Mul(*nc_part)
  94. if isinstance(nc_mul, Mul):
  95. result = c_mul*qapply_Mul(nc_mul, **options)
  96. else:
  97. result = c_mul*qapply(nc_mul, **options)
  98. if result == e and dagger:
  99. return Dagger(qapply_Mul(Dagger(e), **options))
  100. else:
  101. return result
  102. # In all other cases (State, Operator, Pow, Commutator, InnerProduct,
  103. # OuterProduct) we won't ever have operators to apply to kets.
  104. else:
  105. return e
  106. def qapply_Mul(e, **options):
  107. ip_doit = options.get('ip_doit', True)
  108. args = list(e.args)
  109. # If we only have 0 or 1 args, we have nothing to do and return.
  110. if len(args) <= 1 or not isinstance(e, Mul):
  111. return e
  112. rhs = args.pop()
  113. lhs = args.pop()
  114. # Make sure we have two non-commutative objects before proceeding.
  115. if (sympify(rhs).is_commutative and not isinstance(rhs, Wavefunction)) or \
  116. (sympify(lhs).is_commutative and not isinstance(lhs, Wavefunction)):
  117. return e
  118. # For a Pow with an integer exponent, apply one of them and reduce the
  119. # exponent by one.
  120. if isinstance(lhs, Pow) and lhs.exp.is_Integer:
  121. args.append(lhs.base**(lhs.exp - 1))
  122. lhs = lhs.base
  123. # Pull OuterProduct apart
  124. if isinstance(lhs, OuterProduct):
  125. args.append(lhs.ket)
  126. lhs = lhs.bra
  127. # Call .doit() on Commutator/AntiCommutator.
  128. if isinstance(lhs, (Commutator, AntiCommutator)):
  129. comm = lhs.doit()
  130. if isinstance(comm, Add):
  131. return qapply(
  132. e.func(*(args + [comm.args[0], rhs])) +
  133. e.func(*(args + [comm.args[1], rhs])),
  134. **options
  135. )
  136. else:
  137. return qapply(e.func(*args)*comm*rhs, **options)
  138. # Apply tensor products of operators to states
  139. if isinstance(lhs, TensorProduct) and all(isinstance(arg, (Operator, State, Mul, Pow)) or arg == 1 for arg in lhs.args) and \
  140. isinstance(rhs, TensorProduct) and all(isinstance(arg, (Operator, State, Mul, Pow)) or arg == 1 for arg in rhs.args) and \
  141. len(lhs.args) == len(rhs.args):
  142. result = TensorProduct(*[qapply(lhs.args[n]*rhs.args[n], **options) for n in range(len(lhs.args))]).expand(tensorproduct=True)
  143. return qapply_Mul(e.func(*args), **options)*result
  144. # Now try to actually apply the operator and build an inner product.
  145. try:
  146. result = lhs._apply_operator(rhs, **options)
  147. except (NotImplementedError, AttributeError):
  148. try:
  149. result = rhs._apply_operator(lhs, **options)
  150. except (NotImplementedError, AttributeError):
  151. if isinstance(lhs, BraBase) and isinstance(rhs, KetBase):
  152. result = InnerProduct(lhs, rhs)
  153. if ip_doit:
  154. result = result.doit()
  155. else:
  156. result = None
  157. # TODO: I may need to expand before returning the final result.
  158. if result == 0:
  159. return S.Zero
  160. elif result is None:
  161. if len(args) == 0:
  162. # We had two args to begin with so args=[].
  163. return e
  164. else:
  165. return qapply_Mul(e.func(*(args + [lhs])), **options)*rhs
  166. elif isinstance(result, InnerProduct):
  167. return result*qapply_Mul(e.func(*args), **options)
  168. else: # result is a scalar times a Mul, Add or TensorProduct
  169. return qapply(e.func(*args)*result, **options)