File size: 9,494 Bytes
9c6594c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
"""Logic for applying operators to states.
Todo:
* Sometimes the final result needs to be expanded, we should do this by hand.
"""
from sympy.concrete import Sum
from sympy.core.add import Add
from sympy.core.kind import NumberKind
from sympy.core.mul import Mul
from sympy.core.power import Pow
from sympy.core.singleton import S
from sympy.core.sympify import sympify, _sympify
from sympy.physics.quantum.anticommutator import AntiCommutator
from sympy.physics.quantum.commutator import Commutator
from sympy.physics.quantum.dagger import Dagger
from sympy.physics.quantum.innerproduct import InnerProduct
from sympy.physics.quantum.operator import OuterProduct, Operator
from sympy.physics.quantum.state import State, KetBase, BraBase, Wavefunction
from sympy.physics.quantum.tensorproduct import TensorProduct
__all__ = [
'qapply'
]
#-----------------------------------------------------------------------------
# Main code
#-----------------------------------------------------------------------------
def ip_doit_func(e):
"""Transform the inner products in an expression by calling ``.doit()``."""
return e.replace(InnerProduct, lambda *args: InnerProduct(*args).doit())
def sum_doit_func(e):
"""Transform the sums in an expression by calling ``.doit()``."""
return e.replace(Sum, lambda *args: Sum(*args).doit())
def qapply(e, **options):
"""Apply operators to states in a quantum expression.
Parameters
==========
e : Expr
The expression containing operators and states. This expression tree
will be walked to find operators acting on states symbolically.
options : dict
A dict of key/value pairs that determine how the operator actions
are carried out.
The following options are valid:
* ``dagger``: try to apply Dagger operators to the left
(default: False).
* ``ip_doit``: call ``.doit()`` in inner products when they are
encountered (default: True).
* ``sum_doit``: call ``.doit()`` on sums when they are encountered
(default: False). This is helpful for collapsing sums over Kronecker
delta's that are created when calling ``qapply``.
Returns
=======
e : Expr
The original expression, but with the operators applied to states.
Examples
========
>>> from sympy.physics.quantum import qapply, Ket, Bra
>>> b = Bra('b')
>>> k = Ket('k')
>>> A = k * b
>>> A
|k><b|
>>> qapply(A * b.dual / (b * b.dual))
|k>
>>> qapply(k.dual * A / (k.dual * k))
<b|
"""
from sympy.physics.quantum.density import Density
dagger = options.get('dagger', False)
sum_doit = options.get('sum_doit', False)
ip_doit = options.get('ip_doit', True)
e = _sympify(e)
# Using the kind API here helps us to narrow what types of expressions
# we call ``ip_doit_func`` on.
if e.kind == NumberKind:
return ip_doit_func(e) if ip_doit else e
# This may be a bit aggressive but ensures that everything gets expanded
# to its simplest form before trying to apply operators. This includes
# things like (A+B+C)*|a> and A*(|a>+|b>) and all Commutators and
# TensorProducts. The only problem with this is that if we can't apply
# all the Operators, we have just expanded everything.
# TODO: don't expand the scalars in front of each Mul.
e = e.expand(commutator=True, tensorproduct=True)
# If we just have a raw ket, return it.
if isinstance(e, KetBase):
return e
# We have an Add(a, b, c, ...) and compute
# Add(qapply(a), qapply(b), ...)
elif isinstance(e, Add):
result = 0
for arg in e.args:
result += qapply(arg, **options)
return result.expand()
# For a Density operator call qapply on its state
elif isinstance(e, Density):
new_args = [(qapply(state, **options), prob) for (state,
prob) in e.args]
return Density(*new_args)
# For a raw TensorProduct, call qapply on its args.
elif isinstance(e, TensorProduct):
return TensorProduct(*[qapply(t, **options) for t in e.args])
# For a Sum, call qapply on its function.
elif isinstance(e, Sum):
result = Sum(qapply(e.function, **options), *e.limits)
result = sum_doit_func(result) if sum_doit else result
return result
# For a Pow, call qapply on its base.
elif isinstance(e, Pow):
return qapply(e.base, **options)**e.exp
# We have a Mul where there might be actual operators to apply to kets.
elif isinstance(e, Mul):
c_part, nc_part = e.args_cnc()
c_mul = Mul(*c_part)
nc_mul = Mul(*nc_part)
if not nc_part: # If we only have a commuting part, just return it.
result = c_mul
elif isinstance(nc_mul, Mul):
result = c_mul*qapply_Mul(nc_mul, **options)
else:
result = c_mul*qapply(nc_mul, **options)
if result == e and dagger:
result = Dagger(qapply_Mul(Dagger(e), **options))
result = ip_doit_func(result) if ip_doit else result
result = sum_doit_func(result) if sum_doit else result
return result
# In all other cases (State, Operator, Pow, Commutator, InnerProduct,
# OuterProduct) we won't ever have operators to apply to kets.
else:
return e
def qapply_Mul(e, **options):
args = list(e.args)
extra = S.One
result = None
# If we only have 0 or 1 args, we have nothing to do and return.
if len(args) <= 1 or not isinstance(e, Mul):
return e
rhs = args.pop()
lhs = args.pop()
# Make sure we have two non-commutative objects before proceeding.
if (not isinstance(rhs, Wavefunction) and sympify(rhs).is_commutative) or \
(not isinstance(lhs, Wavefunction) and sympify(lhs).is_commutative):
return e
# For a Pow with an integer exponent, apply one of them and reduce the
# exponent by one.
if isinstance(lhs, Pow) and lhs.exp.is_Integer:
args.append(lhs.base**(lhs.exp - 1))
lhs = lhs.base
# Pull OuterProduct apart
if isinstance(lhs, OuterProduct):
args.append(lhs.ket)
lhs = lhs.bra
if isinstance(rhs, OuterProduct):
extra = rhs.bra # Append to the right of the result
rhs = rhs.ket
# Call .doit() on Commutator/AntiCommutator.
if isinstance(lhs, (Commutator, AntiCommutator)):
comm = lhs.doit()
if isinstance(comm, Add):
return qapply(
e.func(*(args + [comm.args[0], rhs])) +
e.func(*(args + [comm.args[1], rhs])),
**options
)*extra
else:
return qapply(e.func(*args)*comm*rhs, **options)*extra
# Apply tensor products of operators to states
if isinstance(lhs, TensorProduct) and all(isinstance(arg, (Operator, State, Mul, Pow)) or arg == 1 for arg in lhs.args) and \
isinstance(rhs, TensorProduct) and all(isinstance(arg, (Operator, State, Mul, Pow)) or arg == 1 for arg in rhs.args) and \
len(lhs.args) == len(rhs.args):
result = TensorProduct(*[qapply(lhs.args[n]*rhs.args[n], **options) for n in range(len(lhs.args))]).expand(tensorproduct=True)
return qapply_Mul(e.func(*args), **options)*result*extra
# For Sums, move the Sum to the right.
if isinstance(rhs, Sum):
if isinstance(lhs, Sum):
if set(lhs.variables).intersection(set(rhs.variables)):
raise ValueError('Duplicated dummy indices in separate sums in qapply.')
limits = lhs.limits + rhs.limits
result = Sum(qapply(lhs.function*rhs.function, **options), *limits)
return qapply_Mul(e.func(*args)*result, **options)
else:
result = Sum(qapply(lhs*rhs.function, **options), *rhs.limits)
return qapply_Mul(e.func(*args)*result, **options)
if isinstance(lhs, Sum):
result = Sum(qapply(lhs.function*rhs, **options), *lhs.limits)
return qapply_Mul(e.func(*args)*result, **options)
# Now try to actually apply the operator and build an inner product.
_apply = getattr(lhs, '_apply_operator', None)
if _apply is not None:
try:
result = _apply(rhs, **options)
except NotImplementedError:
result = None
else:
result = None
if result is None:
_apply_right = getattr(rhs, '_apply_from_right_to', None)
if _apply_right is not None:
try:
result = _apply_right(lhs, **options)
except NotImplementedError:
result = None
if result is None:
if isinstance(lhs, BraBase) and isinstance(rhs, KetBase):
result = InnerProduct(lhs, rhs)
# TODO: I may need to expand before returning the final result.
if isinstance(result, (int, complex, float)):
return _sympify(result)
elif result is None:
if len(args) == 0:
# We had two args to begin with so args=[].
return e
else:
return qapply_Mul(e.func(*(args + [lhs])), **options)*rhs*extra
elif isinstance(result, InnerProduct):
return result*qapply_Mul(e.func(*args), **options)*extra
else: # result is a scalar times a Mul, Add or TensorProduct
return qapply(e.func(*args)*result, **options)*extra
|