diff --git a/scripts/IBM_T1_T2_H_gate.py b/scripts/IBM_T1_T2_H_gate.py deleted file mode 100644 index 5810e7da..00000000 --- a/scripts/IBM_T1_T2_H_gate.py +++ /dev/null @@ -1,113 +0,0 @@ -import sys -sys.path.append('./src/') - -import ray -from ray.rllib.algorithms.ddpg import DDPGConfig -from ray.tune.registry import register_env -from relaqs.environments.gate_synth_env_rllib_Haar import GateSynthEnvRLlibHaarNoisy -from relaqs.save_results import SaveResults -from relaqs.plot_data import plot_data -import numpy as np - -from relaqs.quantum_noise_data.get_data import get_month_of_single_qubit_data, get_month_of_all_qubit_data -from relaqs import quantum_noise_data -from relaqs import QUANTUM_NOISE_DATA_DIR - -from qutip.operators import * - - -def env_creator(config): - return GateSynthEnvRLlibHaarNoisy(config) - -def save_grad_to_file(resultdict): - try: - policydict = resultdict["default_policy"] - stats = policydict["learner_stats"] - grad_gnorm = stats["grad_gnorm"] - with open("gradfile", "a") as f: - f.write(f"{grad_gnorm}\n") - except KeyError: - print(f"Failed to extract grad_gnorm from: {resultdict}") - -def inject_logging(alg, logging_func): - og_ts = alg.training_step - def new_training_step(): - result = og_ts() - # do logging here - logging_func(result) - return result - alg.training_step = new_training_step - -def run(n_training_iterations=1, save=True, plot=True): - ray.init() - try: - register_env("my_env", env_creator) - - - # ---------------------> Configure algorithm and Environment <------------------------- - alg_config = DDPGConfig() - alg_config.framework("torch") - - H = 1/np.sqrt(2) * np.array([[1, 1], [1, -1]]) - env_config = GateSynthEnvRLlibHaarNoisy.get_default_env_config() - env_config["U_target"] = H - - - # ---------------------> Get quantum noise data <------------------------- - path_to_file = "C:\\Users\\haknamu\\Documents\\PythonWorkSpace\\GateSynth\\rl-repo\\src\\relaqs\\quantum_noise_data\\april\\ibmq_belem_month_is_4.json" - t1_list, t2_list = get_month_of_all_qubit_data(path_to_file) #in seconds - # ------------------------------------------------------------------------ - - env_config["relaxation_rates_list"] = [np.reciprocal(t1_list).tolist(), np.reciprocal(t2_list).tolist()] # using real T1 data - env_config["relaxation_ops"] = [sigmam(),sigmaz()] - env_config["observation_space_size"] = 2*16 + 1 + 2 + 1 # 2*16 = (complex number)*(density matrix elements = 4)^2, + 1 for fidelity + 2 for relaxation rate + 1 for detuning - alg_config.environment("my_env", env_config=env_config) - - alg_config.rollouts(batch_mode="complete_episodes") - alg_config.train_batch_size = GateSynthEnvRLlibHaarNoisy.get_default_env_config()["steps_per_Haar"] - - ### working 1-3 sets - alg_config.actor_lr = 4e-5 - alg_config.critic_lr = 5e-4 - - alg_config.actor_hidden_activation = "relu" - alg_config.critic_hidden_activation = "relu" - alg_config.num_steps_sampled_before_learning_starts = 1000 - alg_config.actor_hiddens = [30,30,30] - alg_config.exploration_config["scale_timesteps"] = 10000 - print(alg_config.algo_class) - print(alg_config["framework"]) - - alg = alg_config.build() - inject_logging(alg, save_grad_to_file) - # --------------------------------------------------------------------- - list_of_results = [] - # ---------------------> Train Agent <------------------------- - for _ in range(n_training_iterations): - result = alg.train() - list_of_results.append(result['hist_stats']) - # ------------------------------------------------------------- - - # ---------------------> Save Results <------------------------- - if save is True: - env = alg.workers.local_worker().env - sr = SaveResults(env, alg, results=list_of_results) - save_dir = sr.save_results() - print("Results saved to:", save_dir) - # -------------------------------------------------------------- - - # ---------------------> Plot Data <------------------------- - if plot is True: - assert save is True, "If plot=True, then save must also be set to True" - plot_data(save_dir, episode_length=alg._episode_history[0].episode_length) - print("Plots Created") - # -------------------------------------------------------------- - finally: - ray.shutdown() - -if __name__ == "__main__": - n_training_iterations = 100 - save = True - plot = True - run(n_training_iterations, save, plot) - diff --git a/scripts/IBM_noise.py b/scripts/IBM_noise.py new file mode 100644 index 00000000..9498848d --- /dev/null +++ b/scripts/IBM_noise.py @@ -0,0 +1,72 @@ +import ray +import numpy as np +from ray.rllib.algorithms.ddpg import DDPGConfig +from relaqs.environments.noisy_single_qubit_env import NoisySingleQubitEnv +from relaqs.save_results import SaveResults +from relaqs.plot_data import plot_data +from relaqs.api import gates +from relaqs.api.utils import sample_noise_parameters +from qutip.operators import sigmaz, sigmam + +def run(n_training_iterations=1, save=True, plot=True): + ray.init() + + # ---------------------> Configure algorithm and Environment <------------------------- + alg_config = DDPGConfig() + alg_config.framework("torch") + env_config = NoisySingleQubitEnv.get_default_env_config() + + # Set noise parameters + noise_file = "april/ibmq_belem_month_is_4.json" + t1_list, t2_list, detuning_list = sample_noise_parameters(noise_file) + env_config["relaxation_rates_list"] = [np.reciprocal(t1_list).tolist(), np.reciprocal(t2_list).tolist()] + env_config["delta"] = detuning_list + env_config["relaxation_ops"] = [sigmam(), sigmaz()] + env_config["observation_space_size"] += 1 # for T2 + + # Set target gate + target_gate = gates.X() + env_config["U_target"] = target_gate.get_matrix() + + alg_config.environment(NoisySingleQubitEnv, env_config=env_config) + + alg_config.rollouts(batch_mode="complete_episodes") + alg_config.train_batch_size = env_config["steps_per_Haar"] + + ### working 1-3 sets + alg_config.actor_lr = 4e-5 + alg_config.critic_lr = 5e-4 + + alg_config.actor_hidden_activation = "relu" + alg_config.critic_hidden_activation = "relu" + alg_config.num_steps_sampled_before_learning_starts = 1000 + alg_config.actor_hiddens = [30,30,30] + alg_config.exploration_config["scale_timesteps"] = 10000 + + alg = alg_config.build() + # --------------------------------------------------------------------- + + # ---------------------> Train Agent <------------------------- + results = [alg.train() for _ in range(n_training_iterations)] + # ---------------------> Save Results <------------------------- + if save is True: + env = alg.workers.local_worker().env + sr = SaveResults(env, alg, target_gate_string=str(target_gate)) + save_dir = sr.save_results() + print("Results saved to:", save_dir) + # -------------------------------------------------------------- + + # ---------------------> Plot Data <------------------------- + if plot is True: + assert save is True, "If plot=True, then save must also be set to True" + print("epiosde length", alg._episode_history[0].episode_length) + plot_data(save_dir, episode_length=alg._episode_history[0].episode_length, figure_title="IBM April Belem Noise, noisy X, gamma/5") + print("Plots Created") + # -------------------------------------------------------------- + +if __name__ == "__main__": + n_training_iterations = 50 + save = True + plot = True + run(n_training_iterations, save, plot) + \ No newline at end of file diff --git a/scripts/run_and_save_v3.py b/scripts/run_and_save_v3.py index 0c77f3a2..2143feed 100644 --- a/scripts/run_and_save_v3.py +++ b/scripts/run_and_save_v3.py @@ -23,7 +23,7 @@ def run(env_class=SingleQubitEnv, n_training_iterations=1, save=True, plot=True) alg_config.environment(env_class, env_config=env_config) alg_config.rollouts(batch_mode="complete_episodes") - alg_config.train_batch_size = env_config["steps_per_Haar"] # TOOD use env_config + alg_config.train_batch_size = env_config["steps_per_Haar"] ### working 1-3 sets alg_config.actor_lr = 4e-5 diff --git a/src/relaqs/environments/noisy_single_qubit_env.py b/src/relaqs/environments/noisy_single_qubit_env.py index dff1d856..1812bdcc 100644 --- a/src/relaqs/environments/noisy_single_qubit_env.py +++ b/src/relaqs/environments/noisy_single_qubit_env.py @@ -56,8 +56,11 @@ def get_relaxation_rate(self): return sampled_rate_list def get_observation(self): - normalizedDetuning = [(self.detuning - min(self.detuning_list)+1E-15)/(max(self.detuning_list)-min(self.detuning_list)+1E-15)] - return np.append([self.compute_fidelity()]+[x//6283185 for x in self.relaxation_rate]+normalizedDetuning, self.unitary_to_observation(self.U)) #6283185 assuming 500 nanosecond relaxation is max + normalized_detuning = [(self.detuning - min(self.detuning_list) + 1E-15) / (max(self.detuning_list) - min(self.detuning_list) + 1E-15)] + return np.append([self.compute_fidelity()] + + [x // 6283185 for x in self.relaxation_rate] + + normalized_detuning, + self.unitary_to_observation(self.U)) #6283185 assuming 500 nanosecond relaxation is max def hamiltonian(self, detuning, alpha, gamma_magnitude, gamma_phase): return (detuning + alpha)*Z + gamma_magnitude*(np.cos(gamma_phase)*X + np.sin(gamma_phase)*Y) diff --git a/src/relaqs/plot_data.py b/src/relaqs/plot_data.py index 1341b0b1..2edd7230 100644 --- a/src/relaqs/plot_data.py +++ b/src/relaqs/plot_data.py @@ -101,6 +101,10 @@ def plot_data(save_dir, episode_length, figure_title=''): avg_final_infelity_per_episode = np.round(avg_final_infelity_per_episode, rounding_precision) avg_sum_of_rewards_per_episode = np.round(avg_sum_of_rewards_per_episode, rounding_precision) + + if len(avg_final_fidelity_per_episode) >= 100: + print("Average final fidelity over last 100 episodes", np.mean(avg_final_fidelity_per_episode[-100:])) + # -------------------------------> Plotting <------------------------------------- rcParams['font.family'] = 'serif' mpl.style.use('seaborn-v0_8')