generated from fastai/nbdev_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.py
156 lines (127 loc) · 4.19 KB
/
core.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
# AUTOGENERATED! DO NOT EDIT! File to edit: 00_core.ipynb (unless otherwise specified).
__all__ = ['ihu', 'y_der', 'check_for_stupidity']
# Cell
from scipy.integrate import solve_ivp
from numpy.random import random_sample
from numpy import array, linspace
from math import sqrt
from .herm import rand_herm_ndarray, test_herm_ndarray
from .unitary import rand_unitary_ndarray, test_unitary_ndarray
# Cell
def ihu(ham_func, u0, evolution_time, no_time_step_for_eval, **kwargs):
r"""evolves the unitary
Parameters
----------
ham_func : function
Function which produces the Hamiltonian
as it appears at time t
form ham_func(t)
u0 : ndarray
Unitary at time 0
evolution_time : float
Evolution time
no_time_step_for_eval : int
no of time steps for evaluation in the
final output
**kwargs
Arbitrary optional keyword arguments.
w : float
Defaults to 6.28
Returns
-------
Bunch object with the following fields defined:
t : [ndarray, shape (n_points,)]
Time points.
y : [ndarray, shape (n, n_points)]
Values of the solution at t.
sol: [OdeSolution or None]
Found solution as OdeSolution instance; None if
dense_output was set to False.
t_events: [list of ndarray or None]
Contains for each event type a list of arrays at which an event of
that type event was detected. None if events was None.
nfev: [int]
Number of evaluations of the right-hand side.
njev: [int]
Number of evaluations of the Jacobian.
nlu :[int]
Number of LU decompositions.
status [int]
Reason for algorithm termination:
• -1: Integration step failed.
• 0: The solver successfully reached the end of tspan.
• 1: A termination event occurred.
message: [string]
Human-readable description of the termination reason.
success: [bool]
True if the solver reached the interval end or a
termination event occurred (status
>= 0).
u_series :[ndarray, shape (sqrt(n), sqrt(n), n_points)]
Values of the solution at t.
"""
u_shape = u0.shape
check_for_stupidity(ham_func, u0, )
T = evolution_time
Nt = no_time_step_for_eval
dt = T/Nt
y0 = u0.ravel()
t_eval_span = linspace(0, T, Nt)
result = solve_ivp(fun=y_der, t_span=(0, T), y0=y0,
t_eval=t_eval_span, args=(u_shape, ham_func))
# Will expose other options later on
print(result.message)
u_series = result.y.reshape(u_shape[0], u_shape[0], result.y.shape[-1])
return u_series, result
# Cell
def y_der(t, y, u_shape, ham_func):
r"""Basically dy/dt
Does some stuff.
Parameters
----------
t : float
Current time value during the integration
y : ndarray
flattened unitary at the current time
Returns
-------
dydt : ndarray
flattened derivative of the unitary
"""
# u_shape = int(sqrt(y.shape[0]))
u_curr = y.reshape(u_shape[0], u_shape[0])
hamiltonian_curr = ham_func(t)
dudt = -1j*hamiltonian_curr@u_curr
dydt = dudt.ravel()
return dydt
# Cell
def check_for_stupidity(ham_func, u0, ):
r"""Perform checks for invalid inputs and the lot"""
u0_shape = u0.shape
ham_shape = ham_func(0.2).shape
try:
assert u0_shape[0] == u0_shape[1]
except AssertionError as e1:
print('Initial unitary is not a square matrix')
raise AssertionError
try:
assert ham_shape[0] == ham_shape[1]
except AssertionError as e2:
print('Hamiltonian is not a square matrix')
raise AssertionError
try:
assert u0_shape == ham_shape
except AssertionError as e3:
print('Hamiltonian shape is not same as that of the initial unitary.')
raise AssertionError
try:
assert test_unitary_ndarray(u0) is True
except AssertionError as e4:
print('Initial unitary is not unitary')
raise AssertionError
try:
assert test_herm_ndarray(ham_func(0.42)) is True
except AssertionError as e5:
print('Hamiltonian is not Hermitian')
raise AssertionError
return None