From 67ad27d299d02fd1f127f8d9bdb2176e98b1164b Mon Sep 17 00:00:00 2001 From: Collin Farquhar Date: Fri, 21 Jun 2024 12:56:06 -0600 Subject: [PATCH 1/7] rm, old env name, and particular path --- scripts/IBM_T1_T2_H_gate.py | 113 ------------------------------------ 1 file changed, 113 deletions(-) delete mode 100644 scripts/IBM_T1_T2_H_gate.py 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) - From fd8a65fdc88d09a9cb5b7248327af8c286b5faa6 Mon Sep 17 00:00:00 2001 From: Collin Farquhar Date: Fri, 21 Jun 2024 12:59:23 -0600 Subject: [PATCH 2/7] remove comment --- scripts/run_and_save_v3.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From f6c0393ed822c561e6704c7450ff12b375e74d62 Mon Sep 17 00:00:00 2001 From: Collin Farquhar Date: Fri, 21 Jun 2024 13:03:15 -0600 Subject: [PATCH 3/7] wip --- scripts/IBM_noise.py | 71 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 scripts/IBM_noise.py diff --git a/scripts/IBM_noise.py b/scripts/IBM_noise.py new file mode 100644 index 00000000..a8358193 --- /dev/null +++ b/scripts/IBM_noise.py @@ -0,0 +1,71 @@ +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()] + + # 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") + 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 From b979ebb0944303b3251f761a9b4ddeedf28d236e Mon Sep 17 00:00:00 2001 From: Collin Farquhar Date: Fri, 21 Jun 2024 15:50:54 -0600 Subject: [PATCH 4/7] cosmetic --- src/relaqs/environments/noisy_single_qubit_env.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/relaqs/environments/noisy_single_qubit_env.py b/src/relaqs/environments/noisy_single_qubit_env.py index dff1d856..b4a8bdac 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 + 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 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) From 32edf222e205a5c1c7e34a13419a41faee6988c3 Mon Sep 17 00:00:00 2001 From: Collin Farquhar Date: Fri, 21 Jun 2024 15:52:01 -0600 Subject: [PATCH 5/7] naming convention --- src/relaqs/environments/noisy_single_qubit_env.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/relaqs/environments/noisy_single_qubit_env.py b/src/relaqs/environments/noisy_single_qubit_env.py index b4a8bdac..1812bdcc 100644 --- a/src/relaqs/environments/noisy_single_qubit_env.py +++ b/src/relaqs/environments/noisy_single_qubit_env.py @@ -56,10 +56,10 @@ 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)] + 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] + - normalizedDetuning, + normalized_detuning, self.unitary_to_observation(self.U)) #6283185 assuming 500 nanosecond relaxation is max def hamiltonian(self, detuning, alpha, gamma_magnitude, gamma_phase): From 27106d1ca41ff5e90f39828b28700daaa20aac69 Mon Sep 17 00:00:00 2001 From: Collin Farquhar Date: Fri, 21 Jun 2024 17:18:40 -0600 Subject: [PATCH 6/7] working. obs space change for T2 --- scripts/IBM_noise.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/IBM_noise.py b/scripts/IBM_noise.py index a8358193..9498848d 100644 --- a/scripts/IBM_noise.py +++ b/scripts/IBM_noise.py @@ -22,6 +22,7 @@ def run(n_training_iterations=1, save=True, plot=True): 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() @@ -59,7 +60,7 @@ def run(n_training_iterations=1, save=True, plot=True): 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") + 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") # -------------------------------------------------------------- From e3d52f08db109960a03ee5e67eb7e68b22392901 Mon Sep 17 00:00:00 2001 From: Collin Farquhar Date: Fri, 21 Jun 2024 18:07:10 -0600 Subject: [PATCH 7/7] print avg final fidelity --- src/relaqs/plot_data.py | 4 ++++ 1 file changed, 4 insertions(+) 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')