-
Notifications
You must be signed in to change notification settings - Fork 5
/
rhs_generate.py
522 lines (442 loc) · 16.8 KB
/
rhs_generate.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
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
# This file is part of QuTiP: Quantum Toolbox in Python.
#
# Copyright (c) 2011 and later, Paul D. Nation and Robert J. Johansson.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the QuTiP: Quantum Toolbox in Python nor the names
# of its contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
###############################################################################
__all__ = ['rhs_generate', 'rhs_clear']
import os
import numpy as np
from types import FunctionType, BuiltinFunctionType
from functools import partial
from qutip.cy.codegen import Codegen
from qutip.solver import Options, config
from qutip.qobj import Qobj
from qutip.superoperator import spre, spost
from qutip.interpolate import Cubic_Spline
def rhs_clear():
"""
Resets the string-format time-dependent Hamiltonian parameters.
Parameters
----------
Returns
-------
Nothing, just clears data from internal config module.
"""
# time-dependent (TD) function stuff
config.tdfunc = None # Placeholder for TD RHS function.
config.colspmv = None # Placeholder for TD col-spmv function.
config.colexpect = None # Placeholder for TD col_expect function.
config.string = None # Holds string of variables to be passed to solver
config.tdname = None # Name of td .pyx file (used in parallel mc code)
def rhs_generate(H, c_ops, args={}, options=Options(), name=None,
cleanup=True):
"""
--------------------
TDB documentation
--------------------
This is the standard qutip.rhs_generate function split into two steps:
rhs_prepare: prepares the .pyx file ready for compilation,
rhs_compile: complies the .pyx file into a Cython function.
This is done to allow further customised editing of the .pyx file prior
to compilation for specific use-cases (such as c-defined pulse shapes).
--------------------
qutip documentation
--------------------
Generates the Cython functions needed for solving the dynamics of a
given system using the mesolve function inside a parfor loop.
Parameters
----------
H : qobj
System Hamiltonian.
c_ops : list
``list`` of collapse operators.
args : dict
Arguments for time-dependent Hamiltonian and collapse operator terms.
options : Options
Instance of ODE solver options.
name: str
Name of generated RHS
cleanup: bool
Whether the generated cython file should be automatically removed or
not.
Notes
-----
Using this function with any solver other than the mesolve function
will result in an error.
"""
rhs_prepare(H, c_ops, args, options, name)
rhs_compile(cleanup)
def rhs_prepare(H, c_ops, args={}, options=Options(), name=None):
"""
--------------------
TDB documentation
--------------------
This is simply everything in the standard qutip.rhs_generate function upto
the point where the pyx file is compiled. This includes the patched
behaviour to prepend the constant part of the Lagrangian in the correct way.
--------------------
qutip documentation
--------------------
Generates the Cython functions needed for solving the dynamics of a
given system using the mesolve function inside a parfor loop.
Parameters
----------
H : qobj
System Hamiltonian.
c_ops : list
``list`` of collapse operators.
args : dict
Arguments for time-dependent Hamiltonian and collapse operator terms.
options : Options
Instance of ODE solver options.
name: str
Name of generated RHS
Notes
-----
Using this function with any solver other than the mesolve function
will result in an error.
"""
config.reset()
config.options = options
if name:
config.tdname = name
else:
config.tdname = "rhs" + str(os.getpid()) + str(config.cgen_num)
Lconst = 0
Ldata = []
Linds = []
Lptrs = []
Lcoeff = []
Lobj = []
# loop over all hamiltonian terms, convert to superoperator form and
# add the data of sparse matrix represenation to
msg = "Incorrect specification of time-dependence: "
for h_spec in H:
if isinstance(h_spec, Qobj):
h = h_spec
if not isinstance(h, Qobj):
raise TypeError(msg + "expected Qobj")
if h.isoper:
Lconst += -1j * (spre(h) - spost(h))
elif h.issuper:
Lconst += h
else:
raise TypeError(msg + "expected operator or superoperator")
elif isinstance(h_spec, list):
h = h_spec[0]
h_coeff = h_spec[1]
if not isinstance(h, Qobj):
raise TypeError(msg + "expected Qobj")
if h.isoper:
L = -1j * (spre(h) - spost(h))
elif h.issuper:
L = h
else:
raise TypeError(msg + "expected operator or superoperator")
Ldata.append(L.data.data)
Linds.append(L.data.indices)
Lptrs.append(L.data.indptr)
if isinstance(h_coeff, Cubic_Spline):
Lobj.append(h_coeff.coeffs)
Lcoeff.append(h_coeff)
else:
raise TypeError(msg + "expected string format")
# loop over all collapse operators
for c_spec in c_ops:
if isinstance(c_spec, Qobj):
c = c_spec
if not isinstance(c, Qobj):
raise TypeError(msg + "expected Qobj")
if c.isoper:
cdc = c.dag() * c
Lconst += spre(c) * spost(c.dag()) - 0.5 * spre(cdc) \
- 0.5 * spost(cdc)
elif c.issuper:
Lconst += c
else:
raise TypeError(msg + "expected operator or superoperator")
elif isinstance(c_spec, list):
c = c_spec[0]
c_coeff = c_spec[1]
if not isinstance(c, Qobj):
raise TypeError(msg + "expected Qobj")
if c.isoper:
cdc = c.dag() * c
L = spre(c) * spost(c.dag()) - 0.5 * spre(cdc) \
- 0.5 * spost(cdc)
c_coeff = "(" + c_coeff + ")**2"
elif c.issuper:
L = c
else:
raise TypeError(msg + "expected operator or superoperator")
Ldata.append(L.data.data)
Linds.append(L.data.indices)
Lptrs.append(L.data.indptr)
Lcoeff.append(c_coeff)
else:
raise TypeError(msg + "expected string format")
# add the constant part of the lagrangian
if Lconst != 0:
# Ldata.append(Lconst.data.data)
# Linds.append(Lconst.data.indices)
# Lptrs.append(Lconst.data.indptr)
# Lcoeff.append("1.0")
Ldata = [Lconst.data.data]+Ldata #TDB 30/08/18
Linds = [Lconst.data.indices]+Linds
Lptrs = [Lconst.data.indptr]+Lptrs
Lcoeff = ["1.0"]+Lcoeff
# the total number of liouvillian terms (hamiltonian terms + collapse
# operators)
n_L_terms = len(Ldata)
cgen = Codegen(h_terms=n_L_terms, h_tdterms=Lcoeff, args=args,
config=config)
cgen.generate(config.tdname + ".pyx")
def rhs_compile(cleanup):
"""
--------------------
TDB documentation
--------------------
This is simply everything in the standard qutip.rhs_generate function that
compiles an already prepared .pyx file into a Cython function.
--------------------
qutip documentation
--------------------
Generates the Cython functions needed for solving the dynamics of a
given system using the mesolve function inside a parfor loop.
Parameters
----------
cleanup: bool
Whether the generated cython file should be automatically removed or
not.
Notes
-----
Using this function with any solver other than the mesolve function
will result in an error.
"""
code = compile('from ' + config.tdname +
' import cy_td_ode_rhs', '<string>', 'exec')
exec(code, globals())
config.tdfunc = cy_td_ode_rhs
if cleanup:
try:
os.remove(config.tdname + ".pyx")
except:
pass
def _td_format_check(H, c_ops, solver='me'):
"""
Checks on time-dependent format.
"""
h_const = []
h_func = []
h_str = []
h_obj = []
# check H for incorrect format
if isinstance(H, Qobj):
pass
elif isinstance(H, (FunctionType, BuiltinFunctionType, partial)):
pass # n_func += 1
elif isinstance(H, list):
for k, H_k in enumerate(H):
if isinstance(H_k, Qobj):
h_const.append(k)
elif isinstance(H_k, list):
if len(H_k) != 2 or not isinstance(H_k[0], Qobj):
raise TypeError("Incorrect hamiltonian specification")
else:
if isinstance(H_k[1], (FunctionType,
BuiltinFunctionType, partial)):
h_func.append(k)
elif isinstance(H_k[1], str):
h_str.append(k)
elif isinstance(H_k[1], Cubic_Spline):
h_obj.append(k)
elif isinstance(H_k[1], np.ndarray):
h_str.append(k)
else:
raise TypeError("Incorrect hamiltonian specification")
else:
raise TypeError("Incorrect hamiltonian specification")
# the the whole thing again for c_ops
c_const = []
c_func = []
c_str = []
c_obj = []
if isinstance(c_ops, list):
for k in range(len(c_ops)):
if isinstance(c_ops[k], Qobj):
c_const.append(k)
elif isinstance(c_ops[k], list):
if len(c_ops[k]) != 2:
raise TypeError(
"Incorrect collapse operator specification.")
else:
if isinstance(c_ops[k][1], (FunctionType,
BuiltinFunctionType, partial)):
c_func.append(k)
elif isinstance(c_ops[k][1], str):
c_str.append(k)
elif isinstance(c_ops[k][1], Cubic_Spline):
c_obj.append(k)
elif isinstance(c_ops[k][1], np.ndarray):
c_str.append(k)
elif isinstance(c_ops[k][1], tuple):
c_str.append(k)
else:
raise TypeError(
"Incorrect collapse operator specification")
else:
raise TypeError("Incorrect collapse operator specification")
#
# if n_str == 0 and n_func == 0:
# # no time-dependence at all
#
if ((len(h_str) > 0 and len(h_func) > 0) or
(len(c_str) > 0 and len(c_func) > 0)):
raise TypeError(
"Cannot mix string and function type time-dependence formats")
# check to see if Cython is installed and version is high enough.
if len(h_str) > 0 or len(c_str) > 0:
try:
import Cython
except:
raise Exception(
"Unable to load Cython. Use Python function format.")
else:
if Cython.__version__ < '0.21':
raise Exception("Cython version (%s) is too old. Upgrade to " +
"version 0.21+" % Cython.__version__)
# If only time-dependence is in Objects, then prefer string based format
if (len(h_func) + len(c_func) + len(h_str) + len(c_str)) == 0:
h_str += h_obj #Does nothing if not objects
c_str += c_obj
else:
# Combine Hamiltonian objects
if len(h_func) > 0:
h_func += h_obj
elif len(h_str) > 0:
h_str += h_obj
#Combine collapse objects
if len(c_func) > 0:
c_func += c_obj
elif len(c_str) > 0:
c_str += c_obj
if solver == 'me':
return (len(h_const + c_const),
len(h_func) + len(c_func),
len(h_str) + len(c_str))
elif solver == 'mc':
# H C_ops #
# -- ----- --
# NO NO 00
# NO STR 01
# NO FUNC 02
#
# STR NO 10
# STR STR 11
#
# FUNC NO 20
#
# FUNC FUNC 22
if isinstance(H, FunctionType):
time_type = 3
# Time-indepdent problems
elif ((len(h_func) == len(h_str) == 0) and
(len(c_func) == len(c_str) == 0)):
time_type = 0
# constant Hamiltonian, time-dependent collapse operators
elif len(h_func) == len(h_str) == 0:
if len(c_str) > 0:
time_type = 1
elif len(c_func) > 0:
time_type = 2
else:
raise Exception("Error determining time-dependence.")
# list style Hamiltonian
elif len(h_str) > 0:
if len(c_func) == len(c_str) == 0:
time_type = 10
elif len(c_str) > 0:
time_type = 11
else:
raise Exception("Error determining time-dependence.")
# Python function style Hamiltonian
elif len(h_func) > 0:
if len(c_func) == len(c_str) == 0:
time_type = 20
elif len(c_func) > 0:
time_type = 22
else:
raise Exception("Error determining time-dependence.")
return time_type, [h_const, h_func, h_str], [c_const, c_func, c_str]
def _td_wrap_array_str(H, c_ops, args, times):
"""
Wrap numpy-array based time-dependence in the string-based time dependence
format
"""
n = 0
H_new = []
c_ops_new = []
args_new = {}
if not isinstance(H, list):
H_new = H
else:
for Hk in H:
if isinstance(Hk, list) and isinstance(Hk[1], np.ndarray):
H_op, H_td = Hk
td_array_name = "_td_array_%d" % n
H_td_str = '(0 if (t > %f) else %s[int(round(%d * (t/%f)))])' %\
(times[-1], td_array_name, len(times) - 1, times[-1])
args_new[td_array_name] = H_td
H_new.append([H_op, H_td_str])
n += 1
else:
H_new.append(Hk)
if not isinstance(c_ops, list):
c_ops_new = c_ops
else:
for ck in c_ops:
if isinstance(ck, list) and isinstance(ck[1], np.ndarray):
c_op, c_td = ck
td_array_name = "_td_array_%d" % n
c_td_str = '(0 if (t > %f) else %s[int(round(%d * (t/%f)))])' %\
(times[-1], td_array_name, len(times) - 1, times[-1])
args_new[td_array_name] = c_td
c_ops_new.append([c_op, c_td_str])
n += 1
else:
c_ops_new.append(ck)
if not args_new:
args_new = args
elif isinstance(args, dict):
args_new.update(args)
else:
raise ValueError("Time-dependent array format requires args to " +
"be a dictionary")
return H_new, c_ops_new, args_new