# Quantum Mechanics with the Python

## 32 thoughts on “Quantum Mechanics with the Python”

1. Thanks, I’m glad that you liked it!

1. Wow, this sounds pretty cool! Thanks

1. this post is now in my resume at the section “reading” 🙂

2. Anandaram Mandyam says:

I just executed your 1DSE for finite Sq Well and it gave two nice plots.
The 2nd plot titled “wave function” should be renamed “probability density, \Psi(x)^2”.
Si I added a 3rd plot for just the “wave function,\Psi(x)” by omitting the squaring:
The two plot codes are listed below:
# Plot the wavefunctions for first 4 eigenstates
figure(2)
for E in E_zeroes[0:4]:
Wave_function(E)
plot(x, psi[:,0], label=”E = %.3f”%E)
legend(loc=”upper right”, frameon=False)
title(‘Wave function, $\Psi(x)$’)
xlabel(r’x, $x/L$’)
ylabel(r’$\Psi(x)$’, rotation=’horizontal’, fontsize = 15)
grid()

# Plot the probability densities for first 4 eigenstates
figure(3)
for E in E_zeroes[0:4]:
Wave_function(E)
plot(x, psi[:,0]**2, label=”E = %.3f”%E)
#xlim(())
ylim((-1000,35000))
legend(loc=”upper right”, frameon=False)
title(‘Probabability density, $\Psi(x)^2$’)
xlabel(r’x, $x/L$’)
ylabel(r’$\Psi(x)^2$’, rotation=’horizontal’, fontsize = 15)
grid()
———————————————————————————————–
Overall a nice effoet. Keep going.

1. Thanks for the feedback! You’re right, I forgot that I changed the code before updating post!

1. Thanks, I’m glad you like it. Keep on rockin’!

3. Anandaram Mandyam says:

Hi: The energy level of 19.9726 given by the analytical model is not detected
by the brentq() procedure. I failed to get it even with setting b = 3 and Nsteps to 3000.
What could be the reason?

1. Thanks for the reply, Anandaram, I am happy to see that my post interests you so much! Regarding your question, I tested the program and this is my conclusion: brentq() fails to find last energy level because odeint() doesn’t give solution which drops so fast at b! This is because of relatively small potential V0 – the smaller V0, the wave function lives longer outside the well, and program can not find its exact zero-value. Solution to this problem? Increase V0 or b! Than the numerical values of energies will give you exact energies from analytic model!

Cheers!

4. Anandaram Mandyam says:

Thanks for your advice. The problem rarely recurs when Vo is increased keeping b = 3.
I found the plotted curves at their best when I set psi0 = [1.0e-15, 1.0e-5]. I applied your
program to the case of Vo = Inf. It didn’t work. Then I set b = L = 1 itself with Vo = 50(say).
Expected results came along nicely. The wavefunction plot is quite like your plot above but
truncated between -1 and 1. This I think mimics Inf potential result. Hope this interests
Anandaram

5. taquan soho says:

How to extend this to a 2D particle-in-a-box ?

1. Hi,
I guess nice try would be making a matrix in x,y coordinates and solve the schroedinger equation in odeint() with two variables. I am only not sure wheather the Python (odeint) supports such an extravaganze

6. arnaldo armida says:

Hi,
sorry for my question..I’m a beginner….so….forgive me:)
running the software I get the equation solution in the terminal but not the plots…I’m using qutip ..
Energies for the bound states are:
Shroedinger_1.py:90: RuntimeWarning: divide by zero encountered in divide
f_sym = lambda z: tan(z)-sqrt((z0/z)**2-1) # Formula 2.138, symmetrical case
Energies from the analyitical model are:
Symmetrical case)
0.9179
8.0922
19.9726
Shroedinger_1.py:91: RuntimeWarning: divide by zero encountered in divide
f_asym = lambda z: -1/tan(z)-sqrt((z0/z)**2-1) # Formula 2.138, antisymmetrical case
(Antisymmetrical case)
3.6462
14.0022
/usr/lib/pymodules/python2.7/matplotlib/axes.py:4747: UserWarning: No labeled objects found. Use label=’…’ kwarg on individual plots.
warnings.warn(“No labeled objects found. ”

——-
Where is my mistake?
Thanks a lot for your help
Arnaldo

1. what do you mean, you’re using qtip? If you run my code on the python supporting platform, there should be no error messages.

1. Arnaldo Armida says:

I mean that I have used the Qutip (http://qutip.org/)platform….but it doesn’t matter..
Using the platform you have used I get the numeric solutions but not the plots.
It’s surely my fault but I do not know where I made mistake!
Sorry to disturb you.
Regards.
Arnaldo

2. sorry, for some reason I can not answer your latest post, so I’ll write here. Are you sure that all your modules are installed in proper version? Maybe you’re using Python 3?

1. esperia says:

Hi,
I’m using python 2.7 as you can see from the following:Last login: Thu Dec 11 09:37:03 on ttys002
MacArnaldo:~ arnaldoarmida$python Python 2.7.9rc1 (v2.7.9rc1:40eada278702, Nov 25 2014, 17:25:50) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type “help”, “copyright”, “credits” or “license” for more information. >>> from pylab import * >>> from scipy.integrate import odeint >>> from scipy.optimize import brentq >>> The system is MAC..but the same I have in Linus Ubuntu.. AS you can see I have put in a terminal the calls to the libraries and I do not get errors….. I’m really sorry… You can be so kind to tell me what python have you used and all the libraries you have used?..it can solve the problem since I will follow step by step everything. Waiting for your kind reply. Arnaldo 2. Arnaldo Armida says: Hi, thanks for your reply and thanks for your patience! I’m using python 2.7 as you can see from the following:Last login: Thu Dec 11 09:37:03 on ttys002 MacArnaldo:~ arnaldoarmida$ python
Python 2.7.9rc1 (v2.7.9rc1:40eada278702, Nov 25 2014, 17:25:50)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
>>> from pylab import *
>>> from scipy.integrate import odeint
>>> from scipy.optimize import brentq
>>>
The system is MAC..but the same I have in Linus Ubuntu..
AS you can see I have put in a terminal the calls to the libraries and I do not get errors…..
I’m really sorry…
You can be so kind to tell me what python have you used and all the libraries you have used?..it can solve the problem since I will follow step by step everything.
Arnaldo

3. everything seems fine, judging from your latest post. I used also Python 2.7 in Spyder software tool on Win 7. Sorry, but I honestly have no idea for what may cause your problems. Try to run the program on some windows computer. If it works it must have been some Mac/Linux incopatibility issue.

7. Sachin says:

Hello all,
I am reading such posts of animation of quantum mechanics equations using python. I want to learn it. May I know the minimum requirement of tools. Mathematics, python relation files etc and their combination to use.
I have basic python only. and I know a bit of that only. Please suggest stepwise and I will try to learn.
Thanks
Sachin
sacheein@gmail.com

1. Hello Sachin. To learn Python I used many online resources and tutorials. There are multiple blogs and online courses that teach you, this one in.e. http://openedx.seas.gwu.edu/courses/GW/MAE6286/2014_fall/info I started with Python many years ago with simple book “Python for dummies”.
To learn QM on very very basic level I recommend “Introduction to quantum mechanics” by D. Griffiths. Under assumption, of course, that you are familiar with the first-year mathematics (calculus, linear algebra, vectors etc)

8. quantuman says:

hi,

I’m a new user of Python, and i’m wondering if I’m able to make a program for quantum dots without the “fenics”, and if there’s any references it will be helpful

9. Alexandre says:

I found a mistake in your code, the potential is inverted, you have a wall instead of a well.

1. Though I disagree, thank you for your opinion. Could you point out more precisely what error did you find?

1. Nishant says:

The error he found was:

def V(x):
“””
Potential function in the finite square well. Width is L and value is global variable Vo
“””
L = 1
if abs(x) > L:
return 0
else:
return Vo

This is what you have in the code above, but if you look at it carefully, it shows that its a wall. When I run the code as is, I do not get any bound solutions numerically. If I switch 0 and Vo, then I get all the same plots you have shown in the blog post.

1. I agree with Alexandre and Nishant. I did not get energy for bound state solutions with the current code. Alternative to what Nishant used, changing “abs(x)<L" to get 0 also works, which is the condition for finite well.

10. uni-fiz says:

How to calculate energy states and w.functions in multilayer quantum wells?