sympy
sympy copied to clipboard
sympy can't handle symbolic Sum of JzKets
I tried to create a linear combination of sympy.physics.quantum.spin.JzKets using
state = sm.Sum((2**m)*qt.spin.JzKet(J, m), (m, -J, J))
and tried to find it's norm using
(sympy.physics.quantum.qapply(qt.Dagger(state)*state).doit())
The output is
which is clearly incorrect. Cleary qapply() doesn't understand a Sum object.
Can you show a complete code example to reproduce this including imports? What should the correct answer be?
It might be a bug in Sum rather than qapply.
The quantum module is currently unmaintained. Feel free to submit a PR to fix this.
Following are the imports I used to reproduce this issue:
import sympy as sp
from sympy.abc import k,m,J
import sympy.physics.quantum as qt
import sympy.concrete.summations as sm
The OP's statement state = sm.Sum((2**m)*qt.spin.JzKet(J, m), (m, -J, J))
produces the output to be:
which, while "looks" correct to our expectation, is not, as is indicated by:
This shows that the object is not behaving as a quantum state, but instead the $\sum_m 2^m$ chunk of the expression is being interpreted by sympy as a separate, geometric summation. The kets $|J,m\rangle$ ought to be part of the summation as well.
Hence, I do believe that it's an inadequacy of Sum, as suggested—an incompatibility of Sum and Quantum objects' implementations.
Above,state
is a <class 'sympy.concrete.summations.Sum'>
object, while one would expect a correct implementation to return an object of perhaps some subclass of <class 'sympy.physics.quantum.state'>
.
These seem to be the reasons (or more) for the OP's issue, hereby seen reproduced with a slight deviation (suspected to be from some update ever since):
Attempting to calculate OP's desired norm expression by alternatively using the innerproduct
method, hints that we should perhaps somehow try to implement returning a sympy.physics.quantum.state.KetBase
object.
I am wondering if this issue can be solved, because I also confront the same problem when I want to define a thermal state in Fock basis.
The problem here is that the summation function cannot recognize the dummy index inside the braket notation like sum over k for |k><k|.