|
| 1 | +#========================================== |
| 2 | +# Copy of the data collection functions from the cnot3 stepsize test |
| 3 | +==========================================# |
| 4 | +using QuantumGateDesign, DelimitedFiles |
| 5 | + |
| 6 | + |
| 7 | +function collect_data(prob::SchrodingerProb, controls::ControlsType, |
| 8 | + pcof::AbstractVector{<: Real}, order::Integer, max_walltime::Real, |
| 9 | + filename_base::AbstractString, N_timestep_saves::Integer, initial_nsteps=2 |
| 10 | + ) |
| 11 | + prob = copy(prob) # Copy problem, just to make sure there are no mutability issues. |
| 12 | + |
| 13 | + initial_time = time() |
| 14 | + max_walltime *= 60*60 # convert walltime from hours to seconds |
| 15 | + |
| 16 | + filename_csv = filename_base * ".csv" |
| 17 | + filename_final_states_csv = filename_base * "_finalStates.csv" |
| 18 | + |
| 19 | + header = hcat( |
| 20 | + "nsteps", "stepsize", "elapsed_time", "avg_N_gmres_iter", |
| 21 | + "N_converged_gmres", "avg_gmres_residual", "max_gmres_residual", |
| 22 | + "R_abs_err_L1", "R_abs_err_L2", "R_rel_err_L1", "R_rel_err_L2", |
| 23 | + "R_abs_err_Linf", |
| 24 | + ) |
| 25 | + println(stdout, header) |
| 26 | + |
| 27 | + writedlm(filename_csv, rpad.(header, 24), ',') |
| 28 | + |
| 29 | + final_states_vec = Vector{ComplexF64}(undef, length(prob.u0)) |
| 30 | + final_states_vec .= NaN |
| 31 | + gmres_tracker = GMRESTracker() |
| 32 | + |
| 33 | + # Run simulation |
| 34 | + prob.nsteps = initial_nsteps |
| 35 | + stepsize = prob.tf / prob.nsteps |
| 36 | + |
| 37 | + # Run simulation once just to get compilation out of the way |
| 38 | + dummy_history = eval_forward(prob, controls, pcof, order=order) |
| 39 | + |
| 40 | + is_first_step = true |
| 41 | + history_2h = nothing |
| 42 | + history_h = nothing |
| 43 | + elapsed_time = 0.0 |
| 44 | + |
| 45 | + # Loop until time runs out (with estimator for when we will go overtime on next simulation) |
| 46 | + while (time()-initial_time) < (max_walltime - 2*elapsed_time) |
| 47 | + # Run simulation |
| 48 | + t1 = time() |
| 49 | + history_h = eval_forward(prob, controls, pcof, order=order, |
| 50 | + gmres_tracker=gmres_tracker, verbose=true) |
| 51 | + t2 = time() |
| 52 | + elapsed_time = t2 - t1 |
| 53 | + |
| 54 | + # Collect data into a row |
| 55 | + if !is_first_step |
| 56 | + R = RichardsonExtrapolation(history_h[:,1:2:end,:], history_2h, order) |
| 57 | + csv_row = hcat( |
| 58 | + prob.nsteps, stepsize, elapsed_time, |
| 59 | + avg_N_iterations(gmres_tracker), gmres_tracker.N_converged, |
| 60 | + avg_residual(gmres_tracker), R.abs_err_L1, R.abs_err_L2, |
| 61 | + R.rel_err_L1, R.rel_err_L2, R.abs_err_Linf, |
| 62 | + ) |
| 63 | + else |
| 64 | + csv_row = hcat( |
| 65 | + prob.nsteps, stepsize, elapsed_time, |
| 66 | + avg_N_iterations(gmres_tracker), gmres_tracker.N_converged, |
| 67 | + avg_residual(gmres_tracker), NaN, NaN, NaN, NaN, NaN, |
| 68 | + ) |
| 69 | + is_first_step = false |
| 70 | + end |
| 71 | + |
| 72 | + # Log data (CSV) |
| 73 | + open(filename_csv, "a+") do io |
| 74 | + writedlm(io, rpad.(csv_row, 24), ',') |
| 75 | + end |
| 76 | + final_states_vec .= reshape(history_h[:,end,:], :) |
| 77 | + open(filename_final_states_csv, "a+") do io |
| 78 | + writedlm(io, rpad.(transpose(final_states_vec), 53), ',') |
| 79 | + end |
| 80 | + println(stdout, csv_row) # Print row |
| 81 | + |
| 82 | + # Prepare for next iteration |
| 83 | + history_2h = history_h |
| 84 | + prob.nsteps *= 2 |
| 85 | + stepsize = prob.tf / prob.nsteps |
| 86 | + end |
| 87 | + |
| 88 | + return readdlm(filename_csv, ',') |
| 89 | +end |
| 90 | + |
| 91 | + |
| 92 | +function collect_data_grad(prob::SchrodingerProb, controls::ControlsType, |
| 93 | + pcof::AbstractVector{<: Real}, target::AbstractMatrix{<: Number}, order::Integer, max_walltime::Real, |
| 94 | + filename_base::AbstractString, N_timestep_saves::Integer |
| 95 | + ) |
| 96 | + prob = copy(prob) # Copy problem, just to make sure there are no mutability issues. |
| 97 | + |
| 98 | + initial_time = time() |
| 99 | + max_walltime *= 60*60 # convert walltime from hours to seconds |
| 100 | + |
| 101 | + filename_csv = filename_base * ".csv" |
| 102 | + filename_final_states_csv = filename_base * "_finalStates.csv" |
| 103 | + |
| 104 | + header = hcat( |
| 105 | + "nsteps", "stepsize", "elapsed_time", "forward_time", "adjoint_time", |
| 106 | + "grad_accum_time", "avg_N_gmres_iter_fwd", "N_converged_gmres_fwd", |
| 107 | + "avg_gmres_residual_fwd", "avg_N_gmres_iter_adj", "N_converged_gmres_adj", |
| 108 | + "avg_gmres_residual_adj", "R_abs_err_L1", "R_abs_err_L2", "R_rel_err_L1", |
| 109 | + "R_rel_err_L2", "R_abs_err_Linf", |
| 110 | + ) |
| 111 | + println(stdout, header) |
| 112 | + |
| 113 | + writedlm(filename_csv, rpad.(header, 24), ',') |
| 114 | + |
| 115 | + final_states_vec = Vector{ComplexF64}(undef, length(prob.u0)) |
| 116 | + final_states_vec .= NaN |
| 117 | + |
| 118 | + forward_gmres_tracker = GMRESTracker() |
| 119 | + adjoint_gmres_tracker = GMRESTracker() |
| 120 | + |
| 121 | + # Run simulation |
| 122 | + prob.nsteps = 2 |
| 123 | + stepsize = prob.tf / prob.nsteps |
| 124 | + |
| 125 | + # Run simulation once just to get compilation out of the way |
| 126 | + dummy_history = eval_forward(prob, controls, pcof, order=order) |
| 127 | + |
| 128 | + timer = QuantumGateDesign.DiscreteAdjointTimes() |
| 129 | + is_first_step = true |
| 130 | + history_2h = nothing |
| 131 | + history_h = nothing |
| 132 | + elapsed_time = 0.0 |
| 133 | + N_derivatives = div(order,2) |
| 134 | + grad = zeros(get_number_of_control_parameters(controls)) |
| 135 | + |
| 136 | + # Loop until time runs out (with estimator for when we will go overtime on next simulation) |
| 137 | + while (time()-initial_time) < (max_walltime - 2*elapsed_time) |
| 138 | + # Run simulation |
| 139 | + history = zeros(prob.real_system_size, 1+N_derivatives, 1+prob.nsteps, prob.N_initial_conditions) |
| 140 | + lambda_history = zeros(prob.real_system_size, 1+N_derivatives, 1+prob.nsteps, prob.N_initial_conditions) |
| 141 | + adjoint_forcing = zeros(prob.real_system_size, 1+prob.nsteps, prob.N_initial_conditions) |
| 142 | + |
| 143 | + QuantumGateDesign.discrete_adjoint!( |
| 144 | + grad, history, lambda_history, adjoint_forcing, prob, controls, |
| 145 | + pcof, target, order=order, timer=timer, |
| 146 | + forward_gmres_tracker=forward_gmres_tracker, |
| 147 | + adjoint_gmres_tracker=adjoint_gmres_tracker, |
| 148 | + ) |
| 149 | + history_h = QuantumGateDesign.real_to_complex(history[:,1,:,:]) |
| 150 | + |
| 151 | + # Collect data into a row |
| 152 | + if !is_first_step |
| 153 | + R = RichardsonExtrapolation(history_h[:,1:2:end,:], history_2h, order) |
| 154 | + csv_row = hcat( |
| 155 | + prob.nsteps, stepsize, QuantumGateDesign.total_time(timer), |
| 156 | + timer.forward, timer.adjoint, timer.grad_accum, |
| 157 | + avg_N_iterations(forward_gmres_tracker), |
| 158 | + forward_gmres_tracker.N_converged, |
| 159 | + avg_residual(forward_gmres_tracker), |
| 160 | + avg_N_iterations(adjoint_gmres_tracker), |
| 161 | + adjoint_gmres_tracker.N_converged, |
| 162 | + avg_residual(adjoint_gmres_tracker), R.abs_err_L1, |
| 163 | + R.abs_err_L2, R.rel_err_L1, R.rel_err_L2, R.abs_err_Linf, |
| 164 | + ) |
| 165 | + else |
| 166 | + # Rerun to update timing (now that precompilation is out of the way) |
| 167 | + QuantumGateDesign.discrete_adjoint!( |
| 168 | + grad, history, lambda_history, adjoint_forcing, prob, controls, |
| 169 | + pcof, target, order=order, timer=timer, |
| 170 | + forward_gmres_tracker=forward_gmres_tracker, |
| 171 | + adjoint_gmres_tracker=adjoint_gmres_tracker, |
| 172 | + ) |
| 173 | + |
| 174 | + csv_row = hcat( |
| 175 | + prob.nsteps, stepsize, QuantumGateDesign.total_time(timer), |
| 176 | + timer.forward, timer.adjoint, timer.grad_accum, |
| 177 | + avg_N_iterations(forward_gmres_tracker), |
| 178 | + forward_gmres_tracker.N_converged, |
| 179 | + avg_residual(forward_gmres_tracker), |
| 180 | + avg_N_iterations(adjoint_gmres_tracker), |
| 181 | + adjoint_gmres_tracker.N_converged, |
| 182 | + avg_residual(adjoint_gmres_tracker), NaN, |
| 183 | + NaN, NaN, NaN, NaN, |
| 184 | + ) |
| 185 | + is_first_step = false |
| 186 | + end |
| 187 | + |
| 188 | + # Log data (CSV) |
| 189 | + open(filename_csv, "a+") do io |
| 190 | + writedlm(io, rpad.(csv_row, 24), ',') |
| 191 | + end |
| 192 | + final_states_vec .= reshape(history_h[:,end,:], :) |
| 193 | + open(filename_final_states_csv, "a+") do io |
| 194 | + writedlm(io, rpad.(transpose(final_states_vec), 53), ',') |
| 195 | + end |
| 196 | + println(stdout, csv_row) # Print row |
| 197 | + |
| 198 | + # Prepare for next iteration |
| 199 | + history_2h = history_h |
| 200 | + prob.nsteps *= 2 |
| 201 | + stepsize = prob.tf / prob.nsteps |
| 202 | + end |
| 203 | + |
| 204 | + return readdlm(filename_csv, ',') |
| 205 | +end |
| 206 | + |
| 207 | + |
| 208 | + |
| 209 | +""" |
| 210 | +Turn state vector history into a row matrix with the correct number of timesteps |
| 211 | +saved, for putting into a csv file. |
| 212 | +
|
| 213 | +If the number of timesteps to be saved is greater than the number of timesteps |
| 214 | +in the provided history, use NaN in place of the 'missing' timestep saves |
| 215 | +""" |
| 216 | +function parse_history_for_csv(history::AbstractArray{ComplexF64, 3}, N_timestep_saves::Union{Missing, Integer}) |
| 217 | + if ismissing(N_timestep_saves) |
| 218 | + return copy(reshape(history, 1, :)) |
| 219 | + end |
| 220 | + |
| 221 | + parsed_history = Array{ComplexF64, 3}(undef, size(history, 1), 1+N_timestep_saves, size(history, 3)) |
| 222 | + parsed_history .= NaN |
| 223 | + |
| 224 | + nsteps = size(history, 2) - 1 |
| 225 | + |
| 226 | + if nsteps >= N_timestep_saves |
| 227 | + if (nsteps % N_timestep_saves != 0) |
| 228 | + throw(ArgumentError("Number of timesteps to save ($N_timestep_saves) does not divide the number of timesteps ($nsteps)")) |
| 229 | + end |
| 230 | + stride = div(nsteps, N_timestep_saves) |
| 231 | + parsed_history .= history[:,1:stride:end,:] |
| 232 | + else |
| 233 | + if (N_timestep_saves % nsteps != 0) |
| 234 | + throw(ArgumentError("Number of timesteps ($nsteps) does not divide the number of timesteps to save ($N_timestep_saves)")) |
| 235 | + end |
| 236 | + stride = div(N_timestep_saves, nsteps) |
| 237 | + parsed_history[:,1:stride:end,:] .= history |
| 238 | + end |
| 239 | + |
| 240 | + return reshape(parsed_history, 1, :) |
| 241 | +end |
0 commit comments