r/Python • • Jul 27 '19

Simulating Quantum Mechanics Using Matplotlib and Tkinter

https://gfycat.com/wellgroomedunsteadyballoonfish
1.4k Upvotes

60 comments sorted by

49

u/--CreativeUsername Jul 27 '19 edited Jul 28 '19

And also with numpy, sympy, and optionally numba. Github Repository:
https://github.com/marl0ny/1D-Quantum-Mechanics-Applet.

The code itself is a little messy, and I certainly haven't ironed out all bugs and quirks. Note that this uses numba, if you have it. In my experience numba either slows things down or speeds things up, depending on the computer and environment used. The notebook Performance_Test.ipynb checks to see if Numba improves things.

6

u/Fairbanksbus142 Jul 28 '19

This is wonderful! Thanks for sharing!

1

u/Science-Compliance Jul 28 '19

Okay, I'm a little confused. When you drew the first barrier, I was expecting to see some of the probability distribution on the other side vis-à-vis quantum tunneling, but it acted almost as an impenetrable barrier from which the wave function was reflected. Is my intuition for quantum mechanics out of calibration, or is this something you need to address in the software? Impressive work by the way.

1

u/--CreativeUsername Jul 28 '19

The tunneling of the wavefunction across a potential barrier depends on its energy (where this energy does not need to be greater than the barrier in order for tunneling to occur). Now the energy of the wavefunction is related to the second spatial derivative d2 /dx2 , which is an indicator of how much curvature the function has. So therefore in general, the more curvature the wavefunction has, the greater its energy. On the wavefunction's firsts collision with the barrier, it has relatively little curvature and energy. In this case tunneling still happens, but the amount of the wavefunction that gets transmitted through the barrier is so vanishingly tiny that it is effectively invisible. Later on, when the wavefunction is more jittery and so therefore has greater curvature, more of the wavefunction is transmitted through the barrier, and so the tunneling effect is visible.

1

u/Science-Compliance Jul 28 '19

Thanks, I did notice the tunneling occurring toward the end. Impressive program.

3

u/[deleted] Jul 28 '19

That was insane. I hope to learn much from you.

1

u/ovigo Jul 28 '19

Very cool stuff! I wonder how much effort it would be to rewrite this in Jupyter/IPython-widgets and make a web-launchable my binder.org? :)

179

u/towinu Jul 28 '19

Can you please explain what the fuck I’m looking at? I know a very small amount about the subject.

97

u/[deleted] Jul 28 '19

it is a really good simulation of the time dependent schroedinger's equation.

ignore Re(\psi) and Im(\psi), those aren't physically meaningful anyway and could (should) be omitted from the simulation since all it does is confuse.

the quantity psi is the wavefunction that represents where a particle "is" in space and time, though "is" is kinda tricky since its probablistic. its somewhere in there.

that also includes the potential barrier. the simulation accurately models quantum tunneling through a potential barrier.

2

u/[deleted] Jul 28 '19

Can you explain like I’m 5?

3

u/deadwisdom greenlet revolution Jul 28 '19

The graph is telling you where the particle might be, but no one knows exactly where. Modelling the might-be's is basically quantum mechanics.

1

u/aquic Jul 31 '19

The tunneling through the potential barrier is great! I think is a neat tool to experiment with while teaching QM!

Good job!

0

u/[deleted] Jul 28 '19

[deleted]

25

u/[deleted] Jul 28 '19

possibly.

if you integrate |\psi|2 over x=-\infty to x=\infty, you get "1".

this means that the particle exists somewhere but only once. you can guess where it is most probable by the maximum of the function but that's no guarantee.

0

u/[deleted] Jul 28 '19

[deleted]

24

u/Serialk Jul 28 '19

I don't think that's a correct way to put it, it's not everywhere, it's just fundamentally described by a probability distribution.

5

u/[deleted] Jul 28 '19

yeah this is a better way of phrasing it.

but it could be anywhere |\psi|2 is nonzero.

2

u/[deleted] Jul 28 '19

yeah.

measurement has its own time/energy position/momenta uncertainty. its a conjugate variable thing.

1

u/Kopachris Jul 28 '19

That's used as a common analogy but it's not accurate.

5

u/Globbi Jul 28 '19

That's not something which can clearly be answered. There are a few approaches on what it means and no agreement among physicists.

All we have are those equations that work well and that's what is being simulated.

5

u/rajbabu0663 Jul 28 '19

This is a good start if you are curious: https://en.m.wikipedia.org/wiki/Finite_potential_well.

3

u/WikiTextBot Jul 28 '19

Finite potential well

The finite potential well (also known as the finite square well) is a concept from quantum mechanics. It is an extension of the infinite potential well, in which a particle is confined to a box, but one which has finite potential walls. Unlike the infinite potential well, there is a probability associated with the particle being found outside the box. The quantum mechanical interpretation is unlike the classical interpretation, where if the total energy of the particle is less than the potential energy barrier of the walls it cannot be found outside the box.


[ ^PM | Exclude ^me | Exclude from ^subreddit | FAQ / ^Information | ^Source ] Downvote to remove | v0.28

23

u/Weihua99 Jul 28 '19

This is absolutely incredible -- I've seen QM simulations like this before in Python but the interactivity you've added really takes it to another level. I love it!

10

u/[deleted] Jul 28 '19

Yeah kind of surprised tkinter looks this smooth for a simulation

21

u/Rouge_Dragon Jul 28 '19

The level of interactivity you have made is quite something. I'm quite surprised by matplotlibs ability to update the plot so quickly, I thought people usually used other opengl kind of things for continuously updating plots and stuff.

4

u/[deleted] Jul 28 '19

I wondered the same thing. I've avoided interactive animations with matplotlib for this reason and used bokeh instead, which was a semi-good idea :)

1

u/some_boi_569348 Jul 29 '19

Ipywidgets & the jupyter/matplotlib back-end in ipython notebooks makes animated plots much easier than it used to be!!

I'm still struggling to find a simple solution to dealing with plotting millions of points....

35

u/drunkpolishbear Jul 28 '19 edited Nov 20 '24

Comment has been redacted using PowerDeleteSuite.

10

u/st_malachy Jul 28 '19

I have no idea what’s happening in this graphic, but I hope you’re right and he works for me at least once.

3

u/drunkpolishbear Jul 28 '19 edited Nov 20 '24

Comment has been redacted using PowerDeleteSuite.

5

u/[deleted] Jul 28 '19

open the repo and start figuring it out.

you'll see a fair bit of numerical analysis since he's solving a time dependent differential equation. but the ways of solving them are well discussed in the relevant circles.

7

u/jdkwak Jul 28 '19 edited Jul 28 '19

Numerical solutions to differential equations have been around for quite some time.

The impressive thing here is that he learned something very complicated both mathematically and conceptually, and then also had the ability to learn the numerical simulations in order to create an interactive way (GUI!) of visualizing and exploring the mathematics and the physics of it!

The point is: He didn't invent anything new, but he managed to combine all the knowledge and skills of different complicated fields into something really nice, usable and user friendly.

He'll go far I'm sure!

7

u/m3wolf Jul 28 '19

I could watch this for days. Very nicely done.

Out of curiosity, why is there no tunneling when the wave packet reaches that first barrier and reflects? I see tunneling later on after you've really messed up the potential, but not at first.

2

u/[deleted] Jul 28 '19

Out of curiosity, why is there no tunneling when the wave packet reaches that first barrier and reflects?

tunneling at any nontrivial probability requires a big shove. its really low probability.

5

u/Suspicious_Writer Jul 28 '19

Dat tunneling at 00:30!

1

u/kidpixo Jul 28 '19

That's how a Field-effect_transistor (FET) works! Nice!

1

u/WikiTextBot Jul 28 '19

Field-effect transistor

The field-effect transistor (FET) is an electronic device which uses an electric field to control the flow of current. FETs are devices with three terminals: source, gate, and drain. FETs control the flow of current by the application of a voltage to the gate, which in turn alters the conductivity between the drain and source.

FETs are also known as unipolar transistors since they involve single-carrier-type operation.


[ ^PM | Exclude ^me | Exclude from ^subreddit | FAQ / ^Information | ^Source ] Downvote to remove | v0.28

4

u/eyeofpython Jul 28 '19

Two lines to represent one number? That looks... complex

1

u/mrasadnoman Jul 28 '19

Computer scientist here. 👌 Looked at some basic quantum mechanics stuff a year ago. But dont really know what this simulation is representing.

Could you just explain it in a sentence.

2

u/--CreativeUsername Jul 28 '19

The wavefunction of the particle gets reflected and transmitted through potential barriers and wells.

1

u/roseinabox28 Jul 28 '19

Is there a tutorial or git hub for this?

1

u/--CreativeUsername Jul 28 '19

In the readme file I linked to the exercises given in Computational Physics by Mark Newman, which you can download for free. Exercise 9.8 and 9.9 gives a detailed explanation on how to numerically solve the Schrodinger equation in a finite 1D region when there is no potential involved.

As for how to integrate a matplotlib animation with tkinter and add interactivity, I may put a gist out or something like that in the future.

1

u/JanDerion47 flair=None Jul 28 '19

Where can one learn the math to comprehend this?

2

u/--CreativeUsername Jul 28 '19

The usual prerequisites for QM are multivariable calculus, linear algebra and ODEs. You can find these courses in MIT OCW.

1

u/JanDerion47 flair=None Jul 28 '19

Thank you

6

u/[deleted] Jul 28 '19

holy shit. you even accurately simulated the tunneling aspect.

1

u/_requires_assistance Jul 28 '19

It doesn't require any special handling, as long as the pde is solved properly.

3

u/[deleted] Jul 28 '19

Neat.

I have no idea Wtf is going on.

3

u/faintingoat Jul 28 '19 edited Jul 28 '19

the possibility to create and change potential walls in real time is stunning. congrats! where s the script, please?

2

u/IdleRocket Jul 28 '19

You know that scene in Good Will Hunting where Matt Damon draws some lines and circles on a chalkboard and we’re supposed to understand that it’s math.

Yeah, this gives me that same feeling.

1

u/Cretank Jul 28 '19

Awesome. Would have loved to see that during my studies!

1

u/Dev_Hassam Jul 28 '19

I dont know physics n quantum but what u made is astonishing ♥️

1

u/R0B0_Ninja Jul 28 '19

That is one ugly wavefunction at the end! Good job.

1

u/jdkwak Jul 28 '19

I think your quantum is leaking!

1

u/ImmaWreckUM8 Jul 28 '19

This is awesome!

1

u/Albus70007 Jul 28 '19

Me: Reads Quantum Mechanics,

My mind: wow these weird lines are cool

1

u/mirandamickey Jul 28 '19

Your particle in a box is not behaving intself :)

1

u/[deleted] Jul 28 '19

Wow. I didn't know you could do stuff like this on python.

How long have you been coding?