-
Notifications
You must be signed in to change notification settings - Fork 0
/
quantum_state.py
302 lines (217 loc) · 7.62 KB
/
quantum_state.py
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# Author: Leonardo Flores Torres
###############################################
# Quantum State
###############################################
import numpy as np
from numba import njit
from const import U0, L, M, H, G
import mathieu_functions as mf
from scipy.integrate import odeint
from scipy.interpolate import UnivariateSpline
def energy(val: float):
return (H ** 2 / (8 * M * L ** 2)) * val + U0
@njit
def energy_numba(val: float):
return (H ** 2 / (8 * M * L ** 2)) * val + U0
def energy_crit(vals: list):
# for indx, val in enumerate(vals):
# if energy(val) > 2 * U0:
# n_crit = indx - 1
# break
# return n_crit, energy(vals[n_crit])
n_crit = np.argmax(energy(vals) > 2 * U0) - 1
return n_crit, energy(vals[n_crit])
# Time evolution operator
def time_opr(val: float, t: float):
return np.exp(1j * energy(val) * t / H)
# Gaussian coeficients that describe the eigen states distribution
# for creating the state of the quantum pendulum
def gauss_coeff(nbar: int, n: int, sigma: float):
# return ((-1) ** np.random.randint(2)) * np.exp(-((n - nbar) ** 2) / (2 * sigma))
return np.exp(-((n - nbar) ** 2) / (2 * sigma ** 2))
@njit
def gauss_coeff_numba(nbar: int, n: int, sigma: float):
return np.exp(-((n - nbar) ** 2) / (2 * sigma ** 2))
# Normalization factor for the quantum state
def norm(nbar: int, n_max: int, sigma: float):
# summation = np.zeros(n_max)
# for i in range(len(summation)):
# summation[i] = gauss_coeff(nbar, i, sigma)
# return 1 / np.sqrt(np.sum(summation ** 2))
summation = np.array([gauss_coeff(nbar, i, sigma) for i in range(n_max)])
return 1 / np.sqrt(np.sum(summation ** 2))
@njit
def norm_numba(nbar: int, n_max: int, sigma: float):
# sum = 0
# for i in range(n_max):
# sum += gauss_coeff_numba(nbar, i, sigma) ** 2
# return 1 / np.sqrt(sum)
summation = np.zeros(n_max)
for i in range(n_max):
summation[i] = gauss_coeff_numba(nbar, i, sigma)
return 1 / np.sqrt(np.sum(summation ** 2))
# Eigen states selection function.
def eigen_state(n: int, x: np.ndarray, vects: np.ndarray):
norm_factor = 1 / np.sqrt(np.pi)
# Select Ce or Se depending on n.
if n % 2 == 0:
# Select a_i vectors
return mf.ce_even(int(n / 2), x, vects[0::2]) * norm_factor
else:
# Select b_i vetors
return mf.se_even(int((n - 1) / 2), x, vects[1::2]) * norm_factor
@njit
def eigen_state_numba(n: int, x: list, vects: list):
norm_factor = 1 / np.sqrt(np.pi)
# Adjust n for ce & se, respectively
if n % 2 == 0:
# Select a_i vectors
return mf.ce_even_numba(int(n / 2), x, vects[0::2]) * norm_factor
else:
# Select b_i vetors
return mf.se_even_numba(int((n - 1) / 2), x, vects[1::2]) * norm_factor
# Quantum state
def state(nbar: int, sigma: float, x: np.ndarray, vects: np.ndarray):
# n_max = len(vects)
# summation = [gauss_coeff(nbar, i, sigma) * eigen_state(i, x, vects) for i in range(n_max)]
# return norm(nbar, n_max, sigma) * np.sum(summation, axis=0)
n_max = len(vects)
summation = np.zeros((n_max, len(x)))
for i in range(n_max):
summation[i] = gauss_coeff(nbar, i, sigma) * eigen_state(i, x, vects)
return norm(nbar, n_max, sigma) * np.sum(summation, axis=0)
# Esta version de la funcion de estado va RAPIDO!
# Esta version de la funcion de estado va RAPIDO!
# Esta version de la funcion de estado va RAPIDO!
@njit
def state_numba(nbar, sigma, x, vects):
n_max = len(vects)
# 1ST BLOCK
# sum = []
# for i in range(n_max):
# .append(gauss_coeff(nbar, i, sigma) * eigen_state_numba(i, x, vects))
# sum = np.transpose(np.array(sum))
# return norm(nbar, n_max, sigma) * np.sum(sum, axis=1)
# 2ND BLOCK
summation = np.zeros((n_max, len(x)))
for i in range(n_max):
summation[i] = gauss_coeff_numba(nbar, i, sigma) * eigen_state_numba(
i, x, vects
)
sum = np.transpose(summation)
return norm_numba(nbar, n_max, sigma) * np.sum(sum, axis=1)
# 3RD BLOCK
# n_max = len(vects)
# sum = [
# gauss_coeff(nbar, i, sigma) * eigen_state_numba(i, x, vects)
# for i in range(n_max)
# ]
# sum = np.transpose(sum)
# return norm(nbar, n_max, sigma) * np.sum(sum, axis=1)
# Time dependent quantum state.
def state_time(
nbar: int,
sigma: float,
x: np.ndarray,
time: float,
vals: np.ndarray,
vects: np.ndarray
):
n_max = len(vals)
summation = [time_opr(val, time) * gauss_coeff(nbar, indx, sigma) * \
eigen_state(indx, x, vects) for indx, val in enumerate(vals)]
return norm(nbar, n_max, sigma) * np.sum(summation, axis=0)
# Generic function for the expected angle to the nth power.
def generic_integrand(
nbar: int,
sigma: float,
x: np.ndarray,
time: float,
vals: np.ndarray,
vects: np.ndarray,
powr: int
):
return (x ** powr) * np.abs(state_time(nbar, sigma, x, time, vals, vects)) ** 2
# Generic integration for the generic expectation to the nth power.
def generic_expectation(
nbar: int,
sigma: float,
x: np.ndarray,
time: float,
vals: np.ndarray,
vects: np.ndarray,
powr: int
):
spl = UnivariateSpline(x, generic_integrand(nbar, sigma, x, time, vals, vects, powr), s=0)
return spl.integral(x[0], x[-1])
def uncertainty(
nbar: int,
sigma: float,
x: np.ndarray,
time: np.ndarray,
vals: np.ndarray,
vects: np.ndarray
):
exp_data = [[generic_expectation(nbar, sigma, x, t, vals, vects, 2),
generic_expectation(nbar, sigma, x, t, vals, vects, 1)]
for t in time]
exp_data = np.array(exp_data)
exp_powr_two = exp_data[:, 0]
exp_powr_one = exp_data[:, 1]
unc = np.sqrt(exp_powr_two - exp_powr_one ** 2)
return unc, exp_powr_one
def expected_energy(
nbar: int,
sigma: float,
vals: np.ndarray
):
summation = [energy(val) * np.abs(gauss_coeff(nbar, indx, sigma)) ** 2 for indx, val in enumerate(vals)]
return np.sum(summation) * (norm(nbar, len(vals), sigma) ** 2)
def classic_pend_energy(
init_en: float,
time: np.ndarray,
):
# The model function is the differential equation
# corresponding to this problem
def model(u, t):
return (u[1], - (G / L) * np.sin(u[0]))
# Initial angle in terms of the initial energy.
init_ang = + np.arccos(1 - init_en / (M * G * L))
# The initial velocity.
init_vel = 0.0
# Initial conditions.
init_cond = [init_ang, init_vel]
# Solution per time-step
solution = odeint(model, init_cond, time)
return solution[:, 0]
def classic_pend_angle(
init_ang: float,
time: np.ndarray,
):
# The model function is the differential equation
# corresponding to this problem
def model(u, t):
return (u[1], - (G / L) * np.sin(u[0]))
# The initial velocity.
init_vel = 0.0
# Initial conditions.
init_cond = [init_ang, init_vel]
# Solution per time-step
solution = odeint(model, init_cond, time)
return solution[:, 0]
def classic_pend_vel(
init_ang: float,
init_en: float,
time: np.ndarray,
):
# The model function is the differential equation
# corresponding to this problem
def model(u, t):
return (u[1], - (G / L) * np.sin(u[0]))
# The initial velocity.
init_vel = (1 / L) * np.sqrt(2 * ((init_en / M) - G * L * (1 - np.cos(init_ang))))
# Initial conditions.
init_cond = [init_ang, -init_vel]
# Solution per time-step
solution = odeint(model, init_cond, time)
return solution[:, 0]