-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimulation.py
More file actions
184 lines (159 loc) · 6.55 KB
/
simulation.py
File metadata and controls
184 lines (159 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"""
simulation.py
Class that performs the evolution of the system.
created on: 24-04-2017.
@author: eduardo
"""
import numpy as np
import scipy.sparse as sp
import scipy.sparse.linalg as sla
import matrix
class Simulation:
"""
This class handles the whole simulation of the quantum system.
It takes a function for the potential and generates the discretised
Hamiltonian. The evoluion of the system is made using Crank-Nicholson.
"""
def __init__(self, dim, potentialFunc, dirichletBC, numberPoints,
startPoint, domainLength, dt):
"""Intilializes the object."""
self.dim = dim
self.numberPoints = numberPoints
self.startPoint = startPoint
self.domainLength = domainLength
self.time = 0
self.dt = dt
self.sign = -1
if dirichletBC:
self.sign = 1
self.allPoints = self.numberPoints + self.sign
H = self._getHamiltonian(np.vectorize(potentialFunc))
Id = sp.identity((self.numberPoints + self.sign)**self.dim)
# Define the matrices used in CN evolution
self.A = (Id + 1j*H*self.dt/2)
self.B = (Id - 1j*H*self.dt/2)
# Initialize wavefunction
self.psi = np.zeros(self.allPoints**self.dim, dtype=np.complex128)
self.pulse = np.zeros(self.allPoints**self.dim, dtype=np.complex128)
def _getHamiltonian(self, potentialFunc):
"""
Generate the Hamiltonian matrix using the functions
defined in "matrix.py".
"""
if self.dim == 1:
if self.sign == -1:
return matrix.A1D(self.numberPoints, potentialFunc,
self.startPoint, self.domainLength)
else:
return matrix.A1Dfull(self.numberPoints, potentialFunc,
self.startPoint, self.domainLength)
if self.dim == 2:
if self.sign == -1:
return matrix.A2D(self.numberPoints, potentialFunc,
self.startPoint, self.domainLength)
else:
return matrix.A2Dfull(self.numberPoints, potentialFunc,
self.startPoint, self.domainLength)
def setPsiPulse(self, pulse, energy, center, vel=1, width=.2):
"""
Generate the initial wavefunction as a Gaussian wavepacket. By default
it moves to the right.
Inputs:
pulse: (string) plane wave or circular pulse
energy: (int/float) The waves energy/size.
center: (float or tuple) Either x or [x, y], for a guassian line
profile or 2D gaussian, respectively.
vel: (float or tuple) Velocity v_x or [v_x, v_y], the Velocity
of the pulse
width: Standard deviation of the Gaussian wave pulse.
Output:
Sets psi of the object to have the desired wave form.
"""
if self.dim == 1:
if pulse == "plane":
x = self.domain()
self.pulse = np.exp(1j * vel * np.sqrt(energy) * x) * \
np.exp(-0.5 * (x-center)**2 / width**2)
norm_Const = np.linalg.norm(self.pulse)
else:
self.pulse = np.zeros(self.allPoints)
# Otherwise would divide by zero
norm_Const = 1
elif self.dim == 2:
[x, y] = self.domain()
if pulse == "plane":
psix = np.exp(1j * vel * np.sqrt(energy) * x) * \
np.exp(-0.5 * (x-center)**2 / width**2)
y_const = np.ones(self.allPoints)
self.pulse = np.kron(psix, y_const.flatten())
norm_Const = np.linalg.norm(self.pulse)
elif pulse == "circular":
psix = np.exp(1j * vel[0] * np.sqrt(energy) * x) * \
np.exp(-0.5 * (x-center[0])**2 / width**2)
psiy = np.exp(1j * vel[1] * np.sqrt(energy) * y) * \
np.exp(-0.5 * (y-center[1])**2 / width**2)
self.pulse = np.kron(psix, psiy.flatten())
norm_Const = np.linalg.norm(self.pulse)
else:
self.pulse = np.zeros(self.allPoints**2)
# Otherwise would divide by zero
norm_Const = 1
self.psi += self.pulse/norm_Const
def normPsi(self):
"""Return the norm of the wave function."""
return np.absolute(self.psi)**2
def realPsi(self):
"""Return the real part of the wavefunction."""
return np.real(self.psi)
# Time evolutions
def evolve(self):
"""Evolve the system using Crank-Nicholson."""
self.time += self.dt
self.psi = sp.linalg.spsolve(self.A, self.B.dot(self.psi),
permc_spec='NATURAL')
def evolvePulsed(self, freq):
"""Evolve the system using Crank-Nicholson."""
self.time += 1
if self.time % (freq) == 0:
self.psi += self.pulse
self.psi = self.psi/np.linalg.norm(self.psi)
self.psi = sp.linalg.spsolve(self.A, self.B.dot(self.psi),
permc_spec='NATURAL')
def consistencyCheck(self):
"""Check if system is consistent by summing probabilities"""
P = np.sum(self.normPsi())
if abs(P-1) < .001:
return True
else:
return False
def probability(self, time):
'''
Evolves the system, measures the probability at each instant and plots
it.
Input:
time: (int) iterations to run
Output:
P: (vector, length=time) Probability at each time
'''
P = np.zeros(time)
for i in range(time):
self.evolve()
P[i] = np.sum(self.normPsi())
return P
def domain(self):
'''Generates evenly spaced vectors spanning the x and y domains'''
if self.dim == 1:
x = np.linspace(self.startPoint,
self.startPoint + self.domainLength,
self.allPoints)
return x
elif self.dim == 2:
x = np.linspace(self.startPoint[0],
self.startPoint[0] + self.domainLength,
self.allPoints)
y = np.linspace(self.startPoint[1],
self.startPoint[1] + self.domainLength,
self.allPoints).reshape(-1, 1)
return [x, y]
else:
return 0