def test_crab(self): """ control.pulseoptim: Hadamard gate using CRAB algorithm Apply guess and ramping pulse assert that goal is achieved and fidelity error is below threshold assert that starting amplitude is zero """ # Hadamard H_d = sigmaz() H_c = [sigmax()] U_0 = identity(2) U_targ = hadamard_transform(1) n_ts = 12 evo_time = 10 # Run the optimisation result = cpo.opt_pulse_crab_unitary(H_d, H_c, U_0, U_targ, n_ts, evo_time, fid_err_targ=1e-5, alg_params={'crab_pulse_params':{'randomize_coeffs':False, 'randomize_freqs':False}}, init_coeff_scaling=0.5, guess_pulse_type='GAUSSIAN', guess_pulse_params={'variance':0.1*evo_time}, guess_pulse_scaling=1.0, guess_pulse_offset=1.0, amp_lbound=None, amp_ubound=None, ramping_pulse_type='GAUSSIAN_EDGE', ramping_pulse_params={'decay_time':evo_time/100.0}, gen_stats=True) assert_(result.goal_achieved, msg="Hadamard goal not achieved. " "Terminated due to: {}, with infidelity: {}".format( result.termination_reason, result.fid_err)) assert_almost_equal(result.fid_err, 0.0, decimal=3, err_msg="Hadamard infidelity too high") assert_almost_equal(result.final_amps[0, 0], 0.0, decimal=3, err_msg="lead in amplitude not zero") # Repeat with Qobj integration result = cpo.opt_pulse_crab_unitary(H_d, H_c, U_0, U_targ, n_ts, evo_time, fid_err_targ=1e-5, alg_params={'crab_pulse_params':{'randomize_coeffs':False, 'randomize_freqs':False}}, dyn_params={'oper_dtype':Qobj}, init_coeff_scaling=0.5, guess_pulse_type='GAUSSIAN', guess_pulse_params={'variance':0.1*evo_time}, guess_pulse_scaling=1.0, guess_pulse_offset=1.0, amp_lbound=None, amp_ubound=None, ramping_pulse_type='GAUSSIAN_EDGE', ramping_pulse_params={'decay_time':evo_time/100.0}, gen_stats=True) assert_(result.goal_achieved, msg="Hadamard goal not achieved" "(Qobj integration). " "Terminated due to: {}, with infidelity: {}".format( result.termination_reason, result.fid_err))
def test_08_crab(self): """ control.pulseoptim: Hadamard gate using CRAB algorithm Apply guess and ramping pulse assert that goal is achieved and fidelity error is below threshold assert that starting amplitude is zero """ # Hadamard H_d = sigmaz() H_c = [sigmax()] U_0 = identity(2) U_targ = hadamard_transform(1) n_ts = 12 evo_time = 10 # Run the optimisation result = cpo.opt_pulse_crab_unitary(H_d, H_c, U_0, U_targ, n_ts, evo_time, fid_err_targ=1e-5, alg_params={'crab_pulse_params':{'randomize_coeffs':False, 'randomize_freqs':False}}, init_coeff_scaling=0.5, guess_pulse_type='GAUSSIAN', guess_pulse_params={'variance':0.1*evo_time}, guess_pulse_scaling=1.0, guess_pulse_offset=1.0, amp_lbound=None, amp_ubound=None, ramping_pulse_type='GAUSSIAN_EDGE', ramping_pulse_params={'decay_time':evo_time/100.0}, gen_stats=True) assert_(result.goal_achieved, msg="Hadamard goal not achieved. " "Terminated due to: {}, with infidelity: {}".format( result.termination_reason, result.fid_err)) assert_almost_equal(result.fid_err, 0.0, decimal=3, err_msg="Hadamard infidelity too high") assert_almost_equal(result.final_amps[0, 0], 0.0, decimal=3, err_msg="lead in amplitude not zero") # Repeat with Qobj integration result = cpo.opt_pulse_crab_unitary(H_d, H_c, U_0, U_targ, n_ts, evo_time, fid_err_targ=1e-5, alg_params={'crab_pulse_params':{'randomize_coeffs':False, 'randomize_freqs':False}}, dyn_params={'oper_dtype':Qobj}, init_coeff_scaling=0.5, guess_pulse_type='GAUSSIAN', guess_pulse_params={'variance':0.1*evo_time}, guess_pulse_scaling=1.0, guess_pulse_offset=1.0, amp_lbound=None, amp_ubound=None, ramping_pulse_type='GAUSSIAN_EDGE', ramping_pulse_params={'decay_time':evo_time/100.0}, gen_stats=True) assert_(result.goal_achieved, msg="Hadamard goal not achieved" "(Qobj integration). " "Terminated due to: {}, with infidelity: {}".format( result.termination_reason, result.fid_err))
def test_crab(self, propagation): tol = 1e-5 evo_time = 10 result = cpo.opt_pulse_crab_unitary( hadamard.system, hadamard.controls, hadamard.initial, hadamard.target, num_tslots=12, evo_time=evo_time, fid_err_targ=tol, **propagation, alg_params={'crab_pulse_params': {'randomize_coeffs': False, 'randomize_freqs': False}}, init_coeff_scaling=0.5, guess_pulse_type='GAUSSIAN', guess_pulse_params={'variance': evo_time * 0.1}, guess_pulse_scaling=1.0, guess_pulse_offset=1.0, amp_lbound=None, amp_ubound=None, ramping_pulse_type='GAUSSIAN_EDGE', ramping_pulse_params={'decay_time': evo_time * 0.01}, gen_stats=True) error = " ".join(["Infidelity: {:7.4e}".format(result.fid_err), "reason:", result.termination_reason]) assert result.goal_achieved, error assert abs(result.fid_err) < tol assert abs(result.final_amps[0, 0]) < tol, "Lead-in amplitude nonzero."
def get_optimised_controls(N: int, n_ts: int, norm_t: float, norm_V: float, target_state_symmetric: bool = True, optim_kwargs: dict = None, ghz_state: BaseGHZState = None, alg="GRAPE") -> OptimResult: norm_H_d, norm_H_c, psi_0 = get_normalised_hamiltonian(N, norm_V) if ghz_state is None: ghz_state = StandardGHZState(N) target_state = ghz_state.get_state_tensor(target_state_symmetric) optim_shared_kwargs = dict( amp_lbound=0, amp_ubound=3, # amp_lbound=0, amp_ubound=2e9 * norm_scaling, gen_stats=True, max_wall_time=300, max_iter=10000, fid_err_targ=1e-10, log_level=qutip.logging_utils.WARN, ) optim_kwargs_ = {**optim_shared_kwargs} if alg == "GRAPE": optim_kwargs_['init_pulse_type'] = "RND" else: optim_kwargs_['guess_pulse_type'] = "RND" optim_kwargs_ = {**optim_kwargs_, **optim_kwargs} if alg == "GRAPE": norm_result = cpo.optimize_pulse_unitary( norm_H_d, norm_H_c, psi_0, target_state, n_ts, norm_t, # pulse_scaling=1e9 * norm_scaling, pulse_offset=1e9 * norm_scaling, # pulse_scaling=0.5, # optim_method="FMIN_BFGS", **optim_kwargs_ ) else: norm_result = cpo.opt_pulse_crab_unitary( norm_H_d, norm_H_c, psi_0, target_state, n_ts, norm_t, # num_coeffs=10, # guess_pulse_scaling=0.1, # guess_pulse_scaling=1e9 * norm_scaling, guess_pulse_offset=1e9 * norm_scaling, **optim_kwargs_ ) return norm_result
def get_optimised_controls(N: int, n_ts: int, alg: str, norm_geometry: BaseGeometry) -> OptimResult: norm_H_d, norm_H_c, psi_0 = get_normalised_hamiltonian(N, norm_geometry) target_state = StandardGHZState(N).get_state_tensor() norm_scaling = 0.5 / characteristic_V optim_shared_kwargs = dict( amp_lbound=AMP_BOUNDS[0], amp_ubound=AMP_BOUNDS[1], # amp_lbound=0, amp_ubound=2e9 * norm_scaling, gen_stats=True, max_wall_time=MAX_WALL_TIME, max_iter=10000000, fid_err_targ=1e-10, log_level=qutip.logging_utils.WARN, ) if alg == "GRAPE": norm_result = cpo.optimize_pulse_unitary( norm_H_d, norm_H_c, psi_0, target_state, n_ts, norm_t, # pulse_scaling=1e9 * norm_scaling, pulse_offset=1e9 * norm_scaling, # pulse_scaling=0.5, # optim_method="FMIN_BFGS", init_pulse_type="RND", **optim_shared_kwargs) else: norm_result = cpo.opt_pulse_crab_unitary( norm_H_d, norm_H_c, psi_0, target_state, n_ts, norm_t, num_coeffs=20, guess_pulse_scaling=0.1, # guess_pulse_scaling=1e9 * norm_scaling, guess_pulse_offset=1e9 * norm_scaling, guess_pulse_type="RND", **optim_shared_kwargs) return norm_result
# In this step, the actual optimization is performed. At each iteration the Nelder-Mead algorithm calculates a new set of coefficients that improves the currently worst set among all set of coefficients. For details see [1,2] and a textbook about static search methods. The algorithm continues until one of the termination conditions defined above has been reached. If undesired results are achieved, rerun the algorithm and/or try to change the number of coefficients to be optimized for, as this is a very crucial parameter. # In[37]: fid_list = [] func_call_list = [] for max_fid_func_calls in range(200): result = cpo.opt_pulse_crab_unitary( H_d, H_c, psi_0, psi_targ, n_ts, evo_time, fid_err_targ=fid_err_targ, max_iter=max_iter, max_wall_time=max_wall_time, init_coeff_scaling=5.0, num_coeffs=2, method_params={'xtol': 1e-3}, guess_pulse_type='SINE', guess_pulse_action='modulate', # out_file_ext=f_ext, log_level=log_level, gen_stats=True) fid_list.append(result.fid_err) func_call_list.append(result.stats.num_fidelity_func_calls) # ### Report the results # Firstly the performace statistics are reported, which gives a breakdown of the processing times. In this example it can be seen that the majority of time is spent calculating the propagators, i.e. exponentiating the combined Hamiltonian. # # The optimised U(T) is reported as the 'final evolution', which is essentially the string representation of the Qobj that holds the full time evolution at the point when the optimisation is terminated.
DATA_FEXT = 'txt' f_end = "{}_n_ts{}".format(example_name, n_ts) f_ext = "{}.{}".format(f_end, DATA_FEXT) # Run the optimisation print("\n***********************************") print("Starting pulse optimisation") result = cpo.opt_pulse_crab_unitary(H_d, H_c, U_0, U_targ, n_ts, evo_time, fid_err_targ=fid_err_targ, max_iter=max_iter, max_wall_time=max_wall_time, init_coeff_scaling=0.5, alg_params={'crab_pulse_params':{'randomize_coeffs':False, 'randomize_freqs':False}}, # optim_method='fmin_l_bfgs_b', # optim_method='l-bfgs-b', # method_params={'xtol':1e-8}, guess_pulse_type='GAUSSIAN', guess_pulse_params={'variance':0.1*evo_time}, guess_pulse_scaling=1.0, guess_pulse_offset=1.0, # guess_pulse_params={'variance':0.1*evo_time}, amp_lbound=None, amp_ubound=None, ramping_pulse_type='GAUSSIAN_EDGE', ramping_pulse_params={'decay_time':evo_time/100.0}, out_file_ext=f_ext, log_level=log_level, gen_stats=True) #, print("\n***********************************") print("Optimising complete. Stats follow:") result.stats.report() print("\nFinal evolution\n{}\n".format(result.evo_full_final))