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 12 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 @@ -1644,6 +1644,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.

49 changes: 47 additions & 2 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 Down Expand Up @@ -643,7 +644,7 @@ class OrthogonalKet(OrthogonalState, KetBase):
>>> (OrthogonalBra(n)*OrthogonalKet(n+1)).doit()
0
>>> (OrthogonalBra(n)*OrthogonalKet(m)).doit()
<n|m>
KroneckerDelta(m, n)
"""

@classmethod
Expand All @@ -665,7 +666,7 @@ def _eval_innerproduct(self, bra, **hints):
return S.Zero # i.e. Integer(0)

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

Choose a reason for hiding this comment

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

I'm not sure about this.

Copy link
Author

Choose a reason for hiding this comment

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

The point of this is that $\langle i | j \rangle$ should be simplified to the Kronecker Delta for the case where they are orthonormal. In fact, maybe this entire method should be just a wrapper to the Kronecker Delta function. Leaving it as simply $\langle i | j \rangle$ is very annoying to perform summation over because you would have to replace it with $\delta_{ij}$ anyway to further simplify.


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

Expand Down Expand Up @@ -909,6 +910,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
3 changes: 2 additions & 1 deletion 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 Down Expand Up @@ -245,4 +246,4 @@ def test_orthogonal_states():
assert braket.doit() == 0

braket = OrthogonalBra(x) * OrthogonalKet(y)
assert braket.doit() == braket
assert braket.doit() == KroneckerDelta(x, y)
3 changes: 3 additions & 0 deletions sympy/testing/quality_unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@

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

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

unicode_strict_whitelist = [
Expand Down