def report_stats(result: OptimResult, N: int): result.stats.report() target_state = StandardGHZState(N).get_state_tensor() final_fidelity = qutip.fidelity(target_state, result.evo_full_final)**2 print(f"final_fidelity: {final_fidelity:.5f}") print(f"Final gradient normal {result.grad_norm_final:.3e}") print(f"Terminated due to {result.termination_reason}")
def report_stats(result: OptimResult, N: int, target_state_symmetric: bool = True, ghz_state: BaseGHZState = None, ): result.stats.report() if ghz_state is None: ghz_state = StandardGHZState(N) target_state = ghz_state.get_state_tensor(target_state_symmetric) final_fidelity = qutip.fidelity(target_state, result.evo_full_final) ** 2 print(f"Final fidelity: {final_fidelity:.5f}") print(f"Final gradient normal: {result.grad_norm_final:.3e}") print(f"Terminated due to: {result.termination_reason}")
def make_gym_env(): if issubclass(QubitEnv, TIEvolvingQubitEnvQ): ghz_state = StandardGHZStateQ(N) else: ghz_state = StandardGHZState(N) env = QubitEnv(N=N, V=C6, geometry=geometry, t_list=np.linspace(0, t, t_num), Omega_range=OMEGA_RANGE, Delta_range=DELTA_RANGE, ghz_state=ghz_state, verbose=ENV_VERBOSE) return env
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
def plot_optimresult(result: OptimResult, N: int, t: float, C6: float, characteristic_V: float, geometry: BaseGeometry, ghz_state: BaseGHZState = None): if ghz_state is None: ghz_state = StandardGHZState(N) final_Omega = np.hstack((result.final_amps[:, 0], result.final_amps[-1, 0])) final_Delta = np.hstack((result.final_amps[:, 1], result.final_amps[-1, 1])) time = result.time / characteristic_V final_Omega *= characteristic_V final_Delta *= characteristic_V e_qs = EvolvingQubitSystem( N=N, V=C6, geometry=geometry, Omega=get_hamiltonian_coeff_interpolation(time, final_Omega, "previous"), Delta=get_hamiltonian_coeff_interpolation(time, final_Delta, "previous"), t_list=np.linspace(0, t, 300), ghz_state=ghz_state ) solve_and_print_stats(e_qs)
def plot_optimresult(result: OptimResult, N: int, geometry: BaseGeometry, t: float, unnormalise_V: float, **kwargs): time = result.time final_Omega = np.hstack( (result.final_amps[:, 0], result.final_amps[-1, 0])) final_Delta = np.hstack( (result.final_amps[:, 1], result.final_amps[-1, 1])) t /= unnormalise_V time = result.time / unnormalise_V final_Omega *= unnormalise_V final_Delta *= unnormalise_V e_qs = EvolvingQubitSystem( N=N, V=C6, geometry=geometry, Omega=get_hamiltonian_coeff_interpolation(time, final_Omega, "previous"), Delta=get_hamiltonian_coeff_interpolation(time, final_Delta, "previous"), t_list=np.linspace(0, t, 300), ghz_state=StandardGHZState(N)) solve_and_print_stats(e_qs, **kwargs)
LATTICE_SPACING = 1.5e-6 print(f"C6: {C6:.3e}") characteristic_V = C6 / (LATTICE_SPACING ** 6) print(f"Characteristic V: {characteristic_V:.3e} Hz") norm_V = C6 / (LATTICE_SPACING ** 6) / characteristic_V norm_Omega_t, norm_Omega = np.array(Omega[0]) * characteristic_V, np.array(Omega[1]) / characteristic_V norm_Delta_t, norm_Delta = np.array(Delta[0]) * characteristic_V, np.array(Delta[1]) / characteristic_V norm_t = t * characteristic_V norm_H_d, norm_H_c, psi_0 = get_normalised_hamiltonian(N, norm_V) target_state = StandardGHZState(N).get_state_tensor(GHZ_SYMMETRIC) norm_t_list: np.ndarray = np.linspace(0, norm_t, 500) H = [norm_H_d, [norm_H_c[0], lambda t, args: 1], [norm_H_c[1], lambda t, args: 0.1]] objectives = [ krotov.Objective(initial_state=psi_0, target=target_state, H=H) ] def S(t): """Shape function for the field update""" return krotov.shapes.flattop(t, t_start=0, t_stop=norm_t, t_rise=norm_t / 10, t_fall=norm_t / 10, func='sinsq')
H += self.V / self.geometry.get_distance(i, j)**6 * n_i * n_j return H if __name__ == '__main__': from qubit_system.geometry.regular_lattice_1d import RegularLattice1D s_qs = StaticQubitSystem(N=4, V=1, geometry=RegularLattice1D(), Omega=1, Delta=np.linspace(-1, 1, 50)) s_qs.plot() from qubit_system.utils.ghz_states import StandardGHZState t = 1 N = 4 e_qs = EvolvingQubitSystem( N=N, V=1, geometry=RegularLattice1D(), Omega=get_hamiltonian_coeff_linear_interpolation( [0, t / 4, t * 3 / 4, t], [0, 1, 1, 0]), Delta=get_hamiltonian_coeff_linear_interpolation([0, t], [-1, 1]), t_list=np.linspace(0, 1, 100), ghz_state=StandardGHZState(N)) e_qs.solve() e_qs.plot(with_antisymmetric_ghz=True, show=True)
# pulse_scaling=1, pulse_offset=1, gen_stats=True, alg="GRAPE", init_pulse_type="RND", max_wall_time=30, max_iter=5000, fid_err_targ=1e-3, log_level=qutip.logging_utils.WARN, ) result.stats.report() final_fidelity = qutip.fidelity(target_state_, result.evo_full_final)**2 print(f"Final fidelity: {final_fidelity:.5f}") print(f"Final gradient normal: {result.grad_norm_final:.3e}") print(f"Terminated due to: {result.termination_reason}") return final_fidelity for target_state in [ StandardGHZState(N).get_state_tensor(True), AlternatingGHZState(N).get_state_tensor(True), ]: for geometry in [ RegularLattice1D(), RegularLattice2D((2, 4)), RegularLattice3D((2, 2, 2)), ]: optimise_and_evaluate_fidelity(target_state, geometry)