forked from naezzell/topological-spin-transport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclosed_sys_optimize_transport.jl
279 lines (242 loc) · 8.23 KB
/
closed_sys_optimize_transport.jl
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
##
using OpenQuantumTools, OrdinaryDiffEq, Plots, LaTeXStrings
using OpenQuantumBase
using Optim, Suppressor
using QuantumControl
using QuantumPropagators.Controls: substitute
include("helperFuncs.jl")
const PROJECTDIR = dirname(Base.active_project())
projectdir(names...) = joinpath(PROJECTDIR, names...)
datadir(names...) = projectdir("data", names...)
##
##
J = 1
nt = 1
Γ = 0.1;
tf = 100;
tstops = range(0, tf, length=100);
adj_mat = build_triangular_chain_adjacency_matrix(nt);
x_part, z_part = form_static_ham_ops(adj_mat, J, Γ);
h0 = z_part - x_part;
#z1_term = σz ⊗ σi ⊗ σi ⊗ σi ⊗ σi;
z1_term = σz ⊗ σi ⊗ σi;
u = PauliVec[3][1];
d = PauliVec[3][2];
r = (u + d) / sqrt(2);
l = (u - d) / sqrt(2);
#init_state = (u ⊗ u ⊗ r)
init_state = (u ⊗ u ⊗ r)
#init_state = (u ⊗ u ⊗ r ⊗ d ⊗ u)
#init_state = (d ⊗ u ⊗ r)
#init_state = (u ⊗ d ⊗ r ⊗ u ⊗ d + d ⊗ u ⊗ r ⊗ d ⊗ u) / sqrt(2);
#init_state = (d ⊗ d ⊗ r ⊗ u ⊗ d - u ⊗ u ⊗ r ⊗ u ⊗ d) / sqrt(2) ;
print("init spin: $(compute_all_pseudospin(init_state))\n")
print("init energy: $(real(adjoint(init_state) * h0 * init_state))\n")
#targ_state = d ⊗ u ⊗ u ⊗ u ⊗ u;
#print("targ spin: $(compute_all_pseudospin(targ_state))\n")
#print("targ energy: $(real(adjoint(targ_state) * h0 * targ_state))\n")
tilings = enumerate_triangular_clock_tilings(nt);
#init_state = tilings[1];
targ_state = tilings[1];
#desired_spin = compute_all_pseudospin(targ_state);
pert_h = h0 + z1_term;
##
##
function sin_cost(x)
coeffs = [(s) -> 1, (s) -> -1, (s) -> (x[1] * sin(x[2] * tf * s))]
ham_terms = [z_part, x_part, z1_term]
H = DenseHamiltonian(coeffs, ham_terms, unit=:ħ)
sol = solve_closed_annealing_obj(H, init_state, tf);
#final_spins = compute_all_pseudospin(sol[end]);
#diff = norm(final_spins - (1.5 + 0.8660254037844383im) * [1.0, 1.0, 1.0])
diff = norm(sol[end] - targ_state)
return diff
end
function sin_cost2(x)
coeffs = [(s) -> 1, (s) -> -1, (s) -> (x[1] * sin(6.56 * tf * s))]
ham_terms = [z_part, x_part, z1_term]
H = DenseHamiltonian(coeffs, ham_terms, unit=:ħ)
sol = solve_closed_annealing_obj(H, init_state, tf);
final_spins = compute_all_pseudospin(sol[end]);
diff = norm(final_spins - desired_spin);
#diff = norm(final_spins - (1.5 + 0.8660254037844383im) * [1.0, 1.0, 1.0])
#diff = norm(sol[end] - targ_state)
return diff
end
function lin_cost(x)
coeffs = [(s) -> 1, (s) -> -1, (s) -> x[1] * s * tf]
ham_terms = [z_part, x_part, z1_term]
H = DenseHamiltonian(coeffs, ham_terms, unit=:ħ)
sol = solve_closed_annealing_obj(H, init_state, tf);
#final_spins = compute_all_pseudospin(sol[end]);
#diff = norm(final_spins - (1.5 + 0.8660254037844383im) * [1.0, 1.0, 1.0])
diff = norm(sol[end] - targ_state)
return diff
end
##
##
result = Optim.optimize(lin_cost, rand(1), NelderMead(),
Optim.Options(iterations=100))
##
##
result = Optim.optimize(sin_cost, rand(2), NelderMead(),
Optim.Options(iterations=100))
##
##
function get_sin_spin_dynamics(opt_x)
coeffs = [(s) -> 1, (s) -> -1, (s) -> opt_x[1] * sin(opt_x[2] * tf * s)]
ham_terms = [z_part, x_part, z1_term]
H = DenseHamiltonian(coeffs, ham_terms, unit=:ħ)
sol = solve_closed_annealing_obj(H, init_state, tf);
times = sol.t
states = sol.u
func_vals = opt_x[1] * sin.(opt_x[2] * times)
spins = [compute_all_pseudospin(states[i]) for i=1:length(times)]
return times, spins, func_vals
end
function get_sin2_spin_dynamics(opt_x)
coeffs = [(s) -> 1, (s) -> -1, (s) -> opt_x[1] * sin(6.56 * tf * s)]
ham_terms = [z_part, x_part, z1_term]
H = DenseHamiltonian(coeffs, ham_terms, unit=:ħ)
sol = solve_closed_annealing_obj(H, init_state, tf);
print(compute_fidelity(sol[end], targ_state))
times = sol.t
states = sol.u
func_vals = opt_x[1] * sin.(6.56 * times)
spins = [compute_all_pseudospin(states[i]) for i=1:length(times)]
return times, spins, func_vals
end
function get_linear_spin_dynamics(m)
coeffs = [(s) -> 1, (s) -> -1, (s) -> m * s * tf]
ham_terms = [z_part, x_part, z1_term]
H = DenseHamiltonian(coeffs, ham_terms, unit=:ħ)
sol = solve_closed_annealing_obj(H, init_state, tf);
times = sol.t
states = sol.u
func_vals = m * times
spins = [compute_all_pseudospin(states[i]) for i=1:length(times)]
return times, spins, func_vals
end
function get_linear_spin_dynamics(m, input_times)
coeffs = [(s) -> 1, (s) -> -1, (s) -> m * s * tf]
ham_terms = [z_part, x_part, z1_term]
H = DenseHamiltonian(coeffs, ham_terms, unit=:ħ)
sol = solve_closed_annealing_obj(H, init_state, tf, Exprb32(), solve_times);
times = sol.t
states = sol.u
func_vals = m * times
spins = [compute_all_pseudospin(states[i]) for i=1:length(times)]
return times, spins, func_vals
end
function get_constant_spin_dynamics(c)
coeffs = [(s) -> 1, (s) -> -1, (s) -> c]
ham_terms = [z_part, x_part, z1_term]
H = DenseHamiltonian(coeffs, ham_terms, unit=:ħ)
sol = solve_closed_annealing_obj(H, init_state, tf);
times = sol.t
states = sol.u
func_vals = [c for i=1:length(times)]
spins = [compute_all_pseudospin(states[i]) for i=1:length(times)]
return times, spins, func_vals
end
function get_constant_spin_dynamics(c, solve_times)
coeffs = [(s) -> 1, (s) -> -1, (s) -> c]
ham_terms = [z_part, x_part, z1_term]
H = DenseHamiltonian(coeffs, ham_terms, unit=:ħ)
sol = solve_closed_annealing_obj(H, init_state, tf, Exprb32(), solve_times);
times = sol.t
states = sol.u
func_vals = [c for i=1:length(times)]
spins = [compute_all_pseudospin(states[i]) for i=1:length(times)]
return times, spins, func_vals
end
##
# set up as quantum optimal control problem
##
ϵ(t) = QuantumControl.Shapes.flattop(t, T=tf, t_rise=0.3, func=:blackman);
"""Two-level-system Hamiltonian."""
function tls_hamiltonian(ϵ=ϵ)
H0 = z_part - x_part
H1 = z1_term
return hamiltonian(H0, (H1, ϵ))
end;
H = tls_hamiltonian();
tlist = collect(range(0, tf, length=500));
function plot_control(pulse::Vector, tlist)
plot(tlist, pulse, xlabel="time", ylabel="amplitude", legend=false)
end
plot_control(ϵ::Function, tlist) = plot_control([ϵ(t) for t in tlist], tlist);
#init_state = q_translate_state("10000", normal=true);
#targ_state = q_translate_state("00001", normal=true);
objectives = [Objective(initial_state=init_state, generator=H, target_state=targ_state)]
##
##
guess_dynamics = propagate_objective(
objectives[1],
problem.tlist;
storage=true,
#observables=(Ψ -> abs.(Ψ) .^ 2,)
)
times = problem.tlist;
spins = [compute_all_pseudospin(guess_dynamics[:, k]) for k=1:500];
fvals = [0 for k=1:500];
compute_fidelity(guess_dynamics[:,end], targ_state)
##
##
problem = ControlProblem(
objectives=objectives,
lambda_a=1/10,
update_shape=(t -> QuantumControl.Shapes.flattop(t, T=tf, t_rise=0.3, func=:blackman)),
tlist=tlist,
iter_stop=1000,
J_T=QuantumControl.Functionals.J_T_ss,
check_convergence=res -> begin
((res.J_T < 1e-3) && (res.converged = true) && (res.message = "J_T < 10⁻³"))
end
);
##
##
val = rand();
opt_result = @optimize_or_load(
datadir("TLSOCT#J_T=J_T_ss#iter_stop=1000#method=krotov$(val).jld2"),
problem,
method = :krotov,
);
##
##
opt_dynamics = propagate_objective(
substitute(objectives[1], IdDict(ϵ => opt_result.optimized_controls[1])),
problem.tlist;
storage=true
)
print(compute_fidelity(opt_dynamics[:,end], targ_state))
fig = plot_control(opt_result.optimized_controls[1], tlist)
##
##
times = problem.tlist
fvals = opt_result.optimized_controls[1]
spins = [compute_all_pseudospin(opt_dynamics[:,i]) for i=1:length(times)]
##
##
guess_dynamics = propagate_objective(
objectives[1],
problem.tlist;
storage=true,
#observables=(Ψ -> abs.(Ψ) .^ 2,)
)
times = problem.tlist;
spins = [compute_all_pseudospin(guess_dynamics[:, k]) for k=1:500];
fvals = [0 for k=1:500];
compute_fidelity(guess_dynamics[:,end], targ_state)
##
# Extract dominant frequency by FFT
##
# center the data to remove DC component
c_data = fvals .- mean(fvals)
# get magnitudes of fft
y = abs.(rfft(c_data));
# get frequencies
freqs = fftfreq(length(y), 1 / (times[2] - times[1]))
max_peak = findmax(y);
peaked_freq = freqs[max_peak[2]];
##