Skip to content

Commit 1e31556

Browse files
berquistleios
authored andcommitted
Add QM energy in Python (#333)
* Implement QM energy in Python. * Add QM energy in Python to book.
1 parent 15e5a54 commit 1e31556

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import numpy as np
2+
3+
4+
def calculate_energy(wfc, H_k, H_r, dx):
5+
"""Calculate the energy <Psi|H|Psi>."""
6+
# Creating momentum conjugate wavefunctions
7+
wfc_k = np.fft.fft(wfc)
8+
wfc_c = np.conj(wfc)
9+
10+
# Finding the momentum and real-space energy terms
11+
energy_k = 0.5 * wfc_c * np.fft.ifft((H_k ** 2) * wfc_k)
12+
energy_r = wfc_c * H_r * wfc
13+
14+
# Integrating over all space
15+
energy_final = sum(energy_k + energy_r).real
16+
17+
return energy_final * dx

contents/quantum_systems/quantum_systems.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ This ultimately looks like this:
227227
{% method %}
228228
{% sample lang="jl" %}
229229
[import, lang:"julia"](code/julia/energy.jl)
230+
{% sample lang="py" %}
231+
[import:4-17, lang:"python"](code/python/energy.py)
230232
{% endmethod %}
231233

232234
This calculation will be used in many different simulations of quantum systems to check our results.

0 commit comments

Comments
 (0)