-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_funcs.py
More file actions
442 lines (364 loc) · 13.7 KB
/
_funcs.py
File metadata and controls
442 lines (364 loc) · 13.7 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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
import numpy as np
from qutip import *
from tqdm import tqdm
def get_first_hit(array, x):
# create a function that returs first hitting time of array >= x
try:
return np.min(np.where(array >= x))
except ValueError:
return None
class ProjectiveEvolutionPnt:
"""
This class is used to compute the projective evolution of the N resolved density operator
We use vectorised density operators and the projective evolution of the jump operator
"""
def __init__(self, H, c_ops, t, N):
"""
Parameters
----------
H : qutip.Qobj
The system Hamiltonian
c_ops : list of qutip.Qobj
The list of collapse operators
t : list of float
The list of times
N : list of float
The list of N values
"""
self.H = H
self.c_ops = c_ops
self.t = t
self.N = N
self.N_len = len(N)
self.dt = t[1] - t[0] # assuming uniform grid
self.dN = N[1] - N[0] # assuming unifrom grid
self.dim = H.shape[0] ** 2 # dimension of the Liouvillian space
def measurement_superoperator(self):
"""
Return the Kraus operators for the qubit jump operator.
"""
M0 = to_super(1 - 1j * self.H * self.dt).full()
Mi = [to_super(np.sqrt(self.dt) * c_op).full() for c_op in self.c_ops]
return [M0] + Mi
def evolution_matrix(self, nu_k: list):
"""
Implements an absorbing boundary condition on the n-resolved density matrix
Parameters
----------
nu_k : list of ints
List of what n state is coupled to
M[0] reserved for no jump evolution
M[i] reserved for jump evolution
Returns
-------
M_update : np.array
The evolution matrix
"""
M = self.measurement_superoperator()
# check that the length of nu_k is the same as the number of collapse operators
assert len(nu_k) == len(M), f"length of nu_k =! len(c_ops)"
assert nu_k[0] == 0, f"nu_k[0] must be 0"
# Compute M_update superoperats
M_update_ops = [
np.kron(np.diag(np.ones(self.N_len - np.abs(nu_k[i])), k=nu_k[i]), M[i])
for i in range(len(nu_k))
]
M_update = sum(M_update_ops)
return M_update
def solve(self, rho0, nu_k):
"""
Solve the projective evolution of the density matrix, starts with default index at ix=argmin(abs(N)
Parameters
----------
rho0 : Qobj
The initial density matrix
nu_k : list of ints
List of what n state is coupled to
Returns
-------
Pnt : np.array
Solution to the n-resolved density matrix
"""
# Compute the evolution matrix
M_update = self.evolution_matrix(nu_k)
ix = np.argmin(np.abs(self.N))
# Convert the initial state to a vector
if rho0.type == "oper":
print("Converting initial state to vector form")
rho0 = operator_to_vector(rho0).full()
elif rho0.type == "ket":
print("Converting initial state to vector form")
rho0 = operator_to_vector(ket2dm(rho0)).full()
else:
print("Initial state is already in vector form")
rho0 = rho0.full()
# Initialise the density matrix vector
rho_n_vec = np.zeros((self.dim * self.N_len, len(self.t)), dtype=complex)
rho_n_vec[self.dim * ix : self.dim * (ix + 1), 0] = rho0.flatten()
# Get Ivec
Ivec = np.eye(int(np.sqrt(self.dim))).reshape(
self.dim,
)
# Initialise Pn
Pn_vec = np.zeros((self.N_len, len(self.t)))
Pn_vec[:, 0] = [
np.real(np.dot(Ivec, rho_n_vec[self.dim * n : self.dim * (n + 1), 0]))
for n in range(self.N_len)
]
# evolve rho
for i in tqdm(range(1, len(self.t)), desc="Evolution Superoperator"):
rho_n_vec[:, i] = M_update @ rho_n_vec[:, i - 1]
# calculate Pn
for j in range(self.N_len):
Pn_vec[j, i] = np.real(
np.dot(Ivec, rho_n_vec[self.dim * j : self.dim * (j + 1), i])
)
return Pn_vec
class ProjectiveEvolutionPntAbsorb(ProjectiveEvolutionPnt):
"""
This class is used to compute the projective evolution of the N resolved density operator with absorbing boundary conditions
We use vectorised density operators and the projective evolution of the jump operator
"""
def __init__(self, H, c_ops, t, N, N_cutoff, kind="single"):
"""
Parameters
----------
H : qutip.Qobj
The system Hamiltonian
c_ops : list of qutip.Qobj
The list of collapse operators
t : list of float
The list of times
N : list of float
The list of N values
N_cutoff : list of float
The list of N values that are coupled to the absorbing boundary
kind : str
'single' or 'double' absorbing boundary condition with Nc for single and Nc and -Nc for double
"""
super().__init__(H, c_ops, t, N)
self.N_cutoff = N_cutoff
self.kind = kind
def evolution_matrix(self, nu_k: list):
"""
Implements an absorbing boundary condition on the n-resolved density matrix
Parameters
----------
nu_k : list of ints
List of what n state is coupled to
N_cutoff : list of ints
List of the N values that are coupled to the absorbing boundary
kind : str
'single' or 'double' absorbing boundary condition with Nc for single and Nc and -Nc for double
M[0] reserved for no jump evolution
M[i] reserved for jump evolution
Returns
-------
M_update : np.array
The evolution matrix
"""
M = self.measurement_superoperator()
# check that the length of nu_k is the same as the number of collapse operators
assert len(nu_k) == len(M), f"length of nu_k =! len(c_ops)"
assert nu_k[0] == 0, f"nu_k[0] must be 0"
# Replace N with N_cutoff
if self.kind == "single":
N = self.N[self.N <= self.N_cutoff]
elif self.kind == "double":
N = self.N[np.abs(self.N) <= self.N_cutoff]
# update ProjectiveEvolutionPnt.N and ProjectiveEvolutionPnt.N_len
self.N = N
self.N_len = len(N)
# Compute M_update superoperats
M_update_ops = [
np.kron(np.diag(np.ones(self.N_len - np.abs(nu_k[i])), k=nu_k[i]), M[i])
for i in range(len(nu_k))
]
M_update = sum(M_update_ops)
# Add the absorbing boundary condition
if self.kind == "single":
# Set first dim row to zero
M_update[: self.dim, :] = 0
elif self.kind == " double":
# Set first dim row to zero
M_update[: self.dim, :] = 0
# Set last dim row to zero
M_update[-self.dim :, :] = 0
return M_update
class DiffusiveEvolutionPnt:
"""
This class is used to compute the diffusive evoltuion of the N resolved density operator
We use vectorised density operators and the projective evolution of the jump operator
System evolves according to
d rho_n / dt = L rho_n - H (d rho_n / d N) + (K_d /2) (d^2 rho_n / d N^2)
where L is the Liouvillian, H is measurement superoperator, K_d = 1 is the dynamical activity (for this simple case)
NOTE: For time being, this class is only for a single collapse operator
"""
def __init__(self, H: Qobj, c_ops: list, K: float, t: np.ndarray, N: np.ndarray):
"""
Parameters
----------
H : qutip.Qobj
The system Hamiltonian
c_ops : list of qutip.Qobj
The list of collapse operators
t : list of float
The list of times
N : list of float
The list of N values
K : float
The dynamical activity
"""
self.H = H
self.c_ops = c_ops
self.t = t
self.N = N
self.N_len = len(N)
self.dt = t[1] - t[0] # assuming uniform grid
self.dN = N[1] - N[0] # assuming unifrom grid
self.dim = H.shape[0] ** 2 # dimension of the Liouvillian space
self.K = K
# assert len(c_ops) == 1, f"Only one collapse operator is supported for time being!"
def measurement_superoperators(self):
"""
Return the Kraus operators for the qubit diffusion operator
"""
L = (
1
+ self.dt * liouvillian(self.H, self.c_ops)
- self.dt * self.K / self.dN**2
).full()
H_op1 = (
(self.dt / (2 * self.dN))
* (spre(self.c_ops[0]) + spost(self.c_ops[0].dag()) + self.K / self.dN)
).full()
H_op2 = (
(self.dt / (2 * self.dN))
* (-spre(self.c_ops[0]) - spost(self.c_ops[0].dag()) + self.K / self.dN)
).full()
return [L, H_op1, H_op2]
def evolution_matrix(self):
"""
Implements an absorbing boundary condition on the n-resolved density matrix
M[0] reserved for normal evolution
M[1] reserved for diffusion operator
Returns
-------
M_update : np.array
The evolution matrix
"""
M = self.measurement_superoperators()
nu_k = [0, 1, -1]
# Compute M_update superoperats
M_update_ops = [
np.kron(np.diag(np.ones(self.N_len - np.abs(nu_k[i])), k=nu_k[i]), M[i])
for i in range(len(nu_k))
]
M_update = sum(M_update_ops)
return M_update
def solve(self, rho0):
"""
Solve the projective evolution of the density matrix
Parameters
----------
rho0 : Qobj
The initial density matrix
ix : int
The index of the initial state in the n-resolved basis
Returns
-------
Pnt : np.array
Solution to the n-resolved density matrix
"""
# Compute the evolution matrix
M_update = self.evolution_matrix()
ix = np.argmin(np.abs(self.N))
# Convert the initial state to a vector
if rho0.type == "oper":
print("Converting initial state to vector form")
rho0 = operator_to_vector(rho0).full()
elif rho0.type == "ket":
print("Converting initial state to vector form")
rho0 = operator_to_vector(ket2dm(rho0)).full()
else:
print("Initial state is already in vector form")
rho0 = rho0.full()
# Initialise the density matrix vector
rho_n_vec = np.zeros((self.dim * self.N_len, len(self.t)), dtype=complex)
rho_n_vec[self.dim * ix : self.dim * (ix + 1), 0] = rho0.flatten()
# Get Ivec
Ivec = np.eye(int(np.sqrt(self.dim))).reshape(
self.dim,
)
# Initialise Pn
Pn_vec = np.zeros((self.N_len, len(self.t)))
Pn_vec[:, 0] = [
np.real(np.dot(Ivec, rho_n_vec[self.dim * n : self.dim * (n + 1), 0]))
for n in range(self.N_len)
]
# evolve rho
for i in tqdm(range(1, len(self.t)), desc="Evolution Superoperator"):
rho_n_vec[:, i] = M_update @ rho_n_vec[:, i - 1]
# calculate Pn
for j in range(self.N_len):
Pn_vec[j, i] = np.real(
np.dot(Ivec, rho_n_vec[self.dim * j : self.dim * (j + 1), i])
)
return Pn_vec
class DiffusiveEvolutionPntAbsorb(DiffusiveEvolutionPnt):
def __init__(self, H, c_ops, K, t, N, N_cutoff, kind="single"):
"""
Parameters
----------
H : qutip.Qobj
The system Hamiltonian
c_ops : list of qutip.Qobj
The list of collapse operators
t : list of float
The list of times
N : list of float
The list of N values
N_cutoff : list of float
The list of N values that are coupled to the absorbing boundary
kind : str
'single' or 'double' absorbing boundary condition with Nc for single and Nc and -Nc for double
"""
super().__init__(H, c_ops, K, t, N)
self.N_cutoff = N_cutoff
self.kind = kind
def evolution_matrix(self):
"""
Implements an absorbing boundary condition on the n-resolved density matrix
M[0] reserved for normal evolution
M[1] reserved for diffusion operator
Returns
-------
M_update : np.array
The evolution matrix
"""
M = self.measurement_superoperators()
nu_k = [0, -1, 1]
# Replace N with N_cutoff
if self.kind == "single":
N = self.N[self.N <= self.N_cutoff]
elif self.kind == "double":
N = self.N[np.abs(self.N) <= self.N_cutoff]
# update ProjectiveEvolutionPnt.N and ProjectiveEvolutionPnt.N_len
self.N = N
self.N_len = len(N)
# Compute M_update superoperats
M_update_ops = [
np.kron(np.diag(np.ones(self.N_len - np.abs(nu_k[i])), k=nu_k[i]), M[i])
for i in range(len(nu_k))
]
M_update = sum(M_update_ops)
# Add the absorbing boundary condition
if self.kind == "single":
# Set first dim row to zero
M_update[: self.dim, :] = 0
elif self.kind == " double":
# Set first dim row to zero
M_update[: self.dim, :] = 0
# Set last dim row to zero
M_update[-self.dim :, :] = 0
return M_update