Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extract_expression_from_wavefunction to sympy.physics.quantum.state.Wavefunction #26520

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .mailmap
Original file line number Diff line number Diff line change
Expand Up @@ -1649,6 +1649,7 @@ znxftw <vishnu2101@gmail.com>
zsc347 <zsc347@gmail.com>
zzc <1378113190@qq.com> zzc <58017008+zzc0430@users.noreply.github.com>
zzj <29055749+zjzh@users.noreply.github.com>
Ícaro <icarosadero@gmail.com>
Óscar Nájera <najera.oscar@gmail.com>
Øyvind Jensen <jensen.oyvind@gmail.com>
Łukasz Pankowski <lukpank@o2.pl>
Expand Down
2 changes: 1 addition & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -1266,4 +1266,4 @@ Augusto Borges <borges.augustoar@gmail.com>
Han Wei Ang <ang.h.w@u.nus.edu>
Pablo <48098178+PabloRuizCuevas@users.noreply.github.com>
Congxu Yang <u7189828@anu.edu.au>
Saicharan <62512681+saicharan2804@users.noreply.github.com>
Saicharan <62512681+saicharan2804@users.noreply.github.com>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The AUTHORS file should not be edited.

86 changes: 61 additions & 25 deletions sympy/physics/quantum/state.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Dirac notation for states."""

from sympy import KroneckerDelta
from sympy.core.cache import cacheit
from sympy.core.containers import Tuple
from sympy.core.expr import Expr
Expand All @@ -22,9 +23,9 @@
'TimeDepState',
'TimeDepBra',
'TimeDepKet',
'OrthogonalKet',
'OrthogonalBra',
'OrthogonalState',
'OrthonormalKet',
'OrthonormalBra',
'OrthonormalState',
'Wavefunction'
]

Expand Down Expand Up @@ -626,57 +627,48 @@ def dual_class(self):
return TimeDepKet


class OrthogonalState(State, StateBase):
class OrthonormalState(State, StateBase):
"""General abstract quantum state used as a base class for Ket and Bra."""
pass

class OrthogonalKet(OrthogonalState, KetBase):
"""Orthogonal Ket in quantum mechanics.
class OrthonormalKet(OrthonormalState, KetBase):
"""Orthonormal Ket in quantum mechanics.

The inner product of two states with different labels will give zero,
states with the same label will give one.

>>> from sympy.physics.quantum import OrthogonalBra, OrthogonalKet
>>> from sympy.physics.quantum import OrthonormalBra, OrthonormalKet
>>> from sympy.abc import m, n
>>> (OrthogonalBra(n)*OrthogonalKet(n)).doit()
>>> (OrthonormalBra(n)*OrthonormalKet(n)).doit()
1
>>> (OrthogonalBra(n)*OrthogonalKet(n+1)).doit()
>>> (OrthonormalBra(n)*OrthonormalKet(n+1)).doit()
0
>>> (OrthogonalBra(n)*OrthogonalKet(m)).doit()
<n|m>
>>> (OrthonormalBra(n)*OrthonormalKet(m)).doit()
KroneckerDelta(m, n)
"""

@classmethod
def dual_class(self):
return OrthogonalBra
return OrthonormalBra

def _eval_innerproduct(self, bra, **hints):

if len(self.args) != len(bra.args):
raise ValueError('Cannot multiply a ket that has a different number of labels.')

for arg, bra_arg in zip(self.args, bra.args):
diff = arg - bra_arg
diff = diff.expand()

is_zero = diff.is_zero

if is_zero is False:
return S.Zero # i.e. Integer(0)

if is_zero is None:
return None
return KroneckerDelta(arg, bra_arg)

return S.One # i.e. Integer(1)


class OrthogonalBra(OrthogonalState, BraBase):
"""Orthogonal Bra in quantum mechanics.
class OrthonormalBra(OrthonormalState, BraBase):
"""Orthonormal Bra in quantum mechanics.
"""

@classmethod
def dual_class(self):
return OrthogonalKet
return OrthonormalKet


class Wavefunction(Function):
Expand Down Expand Up @@ -909,6 +901,50 @@ def expr(self):
"""
return self._args[0]

@staticmethod
def extract_expression_from_wavefunction(input_expression):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function does not seem to be used anywhere.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this as a handicap to extract the expression from the wavefunction objects. In the current state, simply doing .expr on a linear combination of wavefunction objects yields an error.

"""
This function recursively traverses the given expression, replacing any instances of Wavefunction with their
internal expressions.

Parameters
==========

input_expression : sympy.Expr
The expression that needs to be simplified.

Example
=======

import sympy as sp
from sympy.physics.quantum.state import Wavefunction

R = sp.Function('R')
Z = sp.Function('Z')
x,y = sp.symbols('x y', real=True)

ϕ = Wavefunction(R(x)*Z(y), x, y)

extract_expression_from_wavefunction(ϕ + ϕ)

Returns
=======

sympy.Expr
The simplified expression with Wavefunction instances replaced by their internal expressions.
"""
def replace_wavefunctions(expression):
simplified_expression = expression
for argument in expression.args:
if argument.func == Wavefunction:
simplified_expression = simplified_expression.subs(argument, argument.expr)
else:
new_term = replace_wavefunctions(argument)
simplified_expression = simplified_expression.subs(argument, new_term)
return simplified_expression

return replace_wavefunctions(input_expression)

@property
def is_normalized(self):
"""
Expand Down
19 changes: 14 additions & 5 deletions sympy/physics/quantum/tests/test_state.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from sympy import KroneckerDelta
from sympy.core.add import Add
from sympy.core.function import diff
from sympy.core.mul import Mul
Expand All @@ -6,6 +7,7 @@
from sympy.core.singleton import S
from sympy.core.symbol import (Symbol, symbols)
from sympy.core.sympify import sympify
from sympy.core.function import Function
from sympy.functions.elementary.complexes import conjugate
from sympy.functions.elementary.miscellaneous import sqrt
from sympy.functions.elementary.trigonometric import sin
Expand All @@ -16,7 +18,7 @@
from sympy.physics.quantum.state import (
Ket, Bra, TimeDepKet, TimeDepBra,
KetBase, BraBase, StateBase, Wavefunction,
OrthogonalKet, OrthogonalBra
OrthonormalKet, OrthonormalBra
)
from sympy.physics.quantum.hilbert import HilbertSpace

Expand Down Expand Up @@ -238,11 +240,18 @@ def test_wavefunction():
assert type(k.variables[0]) == Symbol

def test_orthogonal_states():
braket = OrthogonalBra(x) * OrthogonalKet(x)
braket = OrthonormalBra(x) * OrthonormalKet(x)
assert braket.doit() == 1

braket = OrthogonalBra(x) * OrthogonalKet(x+1)
braket = OrthonormalBra(x) * OrthonormalKet(x+1)
assert braket.doit() == 0

braket = OrthogonalBra(x) * OrthogonalKet(y)
assert braket.doit() == braket
braket = OrthonormalBra(x) * OrthonormalKet(y)
assert braket.doit() == KroneckerDelta(x, y)

def test_wavefunction_expr():
x = symbols('x', real = True)
a, b = symbols('a b', complex = True)
ψ1 = Wavefunction(Function('ψ1')(x), x)
ψ2 = Wavefunction(Function('ψ2')(x), x)
assert Wavefunction.extract_expression_from_wavefunction(a*ψ1 + b*ψ2) == a*Function('ψ1')(x) + b*Function('ψ2')(x)
4 changes: 4 additions & 0 deletions sympy/testing/quality_unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@

# Explanation of symbols uses greek letters
r'*/sympy/core/symbol.py',

# Greek letter used as variable
r'*/sympy/physics/quantum/state.py',
r'*/sympy/physics/quantum/tests/test_state.py',
]

unicode_strict_whitelist = [
Expand Down