def test(): # Hypersurface shape and pygor box_dim = 2 box_a = np.array([-1300., -1000.]) box_b = 500. * np.ones(box_dim) shape = pygor_dummy.Box(ndim=box_dim, a=box_a, b=box_b) th_leak = -500. * np.ones(2) shape = pygor_dummy.Leakage(shape, th_leak) origin = 0. * np.ones(box_dim) # Dummy function for extra measurement box_peaks = ([-1400., -1100], [-750, -900]) box_goodscore = ([-1400., -1100], [-1100, -900]) do_extra_meas = DummyExtMeas(box_peaks, box_goodscore) pg, active_gate_idxs, lb_short, ub_short, max_current, min_current, set_big_jump, set_small_jump = config.setup_pygor_dummy( shape) # Poff detector step_back = 100 # important param for measuring d len_after_pinchoff = 100 threshold_high = 0.8 threshold_low = 0.2 d_r = 10 detector_pinchoff = util.PinchoffDetectorThreshold( threshold_low) # pichoff detector detector_conducting = util.ConductingDetectorThreshold( threshold_high) # reverse direction tester = Tester(pg, lb_short, ub_short, detector_pinchoff, d_r=d_r, len_after_pinchoff=len_after_pinchoff, logging=True, detector_conducting=detector_conducting, set_big_jump=set_big_jump, set_small_jump=set_small_jump) # Initial observations num_init = 10 u_all = random_hypersphere(box_dim, num_init) u_all, r_all, d_all, poff_all, detected_all, time_all, extra_meas_all = rw.get_full_data( u_all, tester, step_back, origin=origin) # GP hypersurface model ### # Gaussian process for r ### num_active_gates = box_dim l_prior_mean = 0.2 * np.ones(num_active_gates) l_prior_var = 0.1 * 0.1 * np.ones(num_active_gates) r_min, r_max = 0.0, np.sqrt(num_active_gates) * 2000. v_prior_mean = ((r_max - r_min) / 4.0)**2 v_prior_var = v_prior_mean**2 noise_var_r = np.square(d_r / 2.0) gp_r = GP_util.create_GP(num_active_gates, 'Matern52', v_prior_mean, l_prior_mean, (r_max - r_min) / 2.0) GP_util.set_GP_prior(gp_r, l_prior_mean, l_prior_var, None, None) # do not set prior for kernel var GP_util.fix_hyperparams(gp_r, False, True) # GP with initial observations #gp_r.create_model(u_all, r_all[:,np.newaxis], noise_var_r, noise_prior='fixed') #gp_r.optimize(num_restarts=20, opt_messages=False, print_result=True) #plot_example(lb_short, ub_short, shape, gp_r, u_all, r_all, origin, 'initial') #print(np.array(sampler.history_all).shape) # (num_samples, num_iter, ndim) #plot_example(lb_short, ub_short, shape, gp_r, u_all, r_all, origin, 'after') num_samples = 100 do_random_meas(box_dim, num_samples, tester, step_back, origin, lb_short, ub_short, gp_r, do_extra_meas=do_extra_meas, save_dir=None)
def main(): conf_name = 'dummy' if conf_name == 'config1': conf_func = config.config1 ip = "http://129.67.86.107:8000/RPC2" save_dir = Path('./save_Dominic_ABL_group_optparam_run2') settletime, settletime_big = 0.01, 0.03 # settletime_big is for shuttling, 'None' disables shuttling # ['c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9', 'c10'] origin = -100. threshold_low = 0.0 # for pinchoff detector d_r = 10 # length of one step elif conf_name == 'config2': # ['c3', 'c4', 'c8', 'c10', 'c11', 'c12', 'c16'] conf_func = config.config2 ip = 'http://129.67.85.38:8000/RPC2' save_dir = Path('./save_Basel2_group') settletime, settletime_big = 0.02, 0.02 # settletime_big is for shuttling, 'None' disables shuttling origin = -100. threshold_low = 2.e-11 d_r = 10 # length of one step elif conf_name == 'dummy': import pygor_dummy box_dim = 5 box_a = -1000. * np.ones(box_dim) box_b = 1000. * np.ones(box_dim) shape = pygor_dummy.Box(ndim=box_dim, a=box_a, b=box_b) th_leak = np.array([-500., -400., -300., 0., 0.]) shape = pygor_dummy.Leakage(shape, th_leak) save_dir = Path('./save_dummy') origin = -100. threshold_low = 0.2 d_r = 10 # length of one step else: raise ValueError('Unsupported setup') save_dir.mkdir(exist_ok=True) if conf_name != 'dummy': pg, active_gate_idxs, lb_short, ub_short, max_current, min_current, set_big_jump, set_small_jump = config.setup_pygor(conf_func, ip, settletime, settletime_big) else: pg, active_gate_idxs, lb_short, ub_short, max_current, min_current, set_big_jump, set_small_jump = config.setup_pygor_dummy(shape) threshold_high = 0.8 * max_current num_active_gates = len(active_gate_idxs) # gate names are for nothing active_gate_names = ["c{}".format(i+1) for i in active_gate_idxs] print(active_gate_names) ''' Disable grouped gates new_wires = [[1, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 1], [0, 0, 1, 0, 0, 1, 0], [0, 0, 0, 1, 1, 0, 0]] pg = PygorRewire(pg, new_wires) lb_short = lb_short[:4] ub_short = ub_short[:4] num_active_gates = len(new_wires) ''' # choose the origin if np.isscalar(origin): origin = origin*np.ones(num_active_gates) else: if len(origin) != num_active_gates: raise ValueError('Wrong array shape of the origin.') origin.dump(str(save_dir / 'origin.npy')) # important algorithm parameters #threshold_low = 0.2 * (max_current - min_current) + min_current step_back = 100 # important param for measuring d len_after_pinchoff=50 num_samples = 100 # number of initial samples (by Latin hypercube design) detector_pinchoff = util.PinchoffDetectorThreshold(threshold_low) # pichoff detector detector_conducting = util.ConductingDetectorThreshold(threshold_high) # reverse direction # create a Callable object, input: unit_vector, output: distance between the boundary and the origin tester = Tester(pg, lb_short, ub_short, detector_pinchoff, d_r=d_r, len_after_pinchoff=len_after_pinchoff, logging=True, detector_conducting=detector_conducting, set_big_jump = set_big_jump, set_small_jump = set_small_jump) ### # Set a problem and a model ### #do_extra_meas = lambda vols, th: config_model.do_extra_meas(pg, vols, th) do_extra_meas = None ### # Gaussian process for r ### l_prior_mean = 0.4 * np.ones(num_active_gates) l_prior_var = 0.1*0.1 * np.ones(num_active_gates) r_min, r_max = 0.0, np.sqrt(num_active_gates)* 2000. v_prior_mean = ((r_max-r_min)/4.0)**2 #v_prior_var = v_prior_mean**2 #noise_var_r = np.square(d_r/2.0) gp_r = GP_util.create_GP(num_active_gates, 'Matern52', v_prior_mean, l_prior_mean, (r_max-r_min)/2.0) GP_util.set_GP_prior(gp_r, l_prior_mean, l_prior_var, None, None) # do not set prior for kernel var GP_util.fix_hyperparams(gp_r, False, True) ### # Gaussian process classifiers for predicting each probability component ### l_prior_mean = 500. * np.ones(num_active_gates) l_prior_var = 100.**2 * np.ones(num_active_gates) v_prior_mean = 50. v_prior_var = 20.**2 gpc_dict = dict() gpc_dict['valid'] = GP_util.create_GP(num_active_gates, 'Matern52', lengthscale=l_prior_mean, const_kernel=True, GP=GPC) gpc_dict['peak'] = GP_util.create_GP(num_active_gates, 'Matern52', lengthscale=l_prior_mean, const_kernel=True, GP=GPC) # when a point is valid gpc_dict['goodscore'] = GP_util.create_GP(num_active_gates, 'Matern52', lengthscale=l_prior_mean, const_kernel=True, GP=GPC) # when a point is valid and has peaks GP_util.set_GP_prior(gpc_dict['valid'], l_prior_mean, l_prior_var, v_prior_mean, v_prior_var) # do not set prior for kernel var GP_util.set_GP_prior(gpc_dict['peak'], l_prior_mean, l_prior_var, v_prior_mean, v_prior_var) # do not set prior for kernel var GP_util.set_GP_prior(gpc_dict['goodscore'], l_prior_mean, l_prior_var, v_prior_mean, v_prior_var) # do not set prior for kernel var do_random_meas(num_active_gates, num_samples, tester, step_back, origin, lb_short, ub_short, gp_r, gpc_dict, do_extra_meas=do_extra_meas, save_dir=save_dir) print('Time for random sampling: ', time.time()-tic)
delimiter=',', fmt='%4.2f') np.savetxt(save_dir / 'd_sorted.csv', d_all, delimiter=',', fmt='%4.2f') np.savetxt(save_dir / 'dtot_sorted.csv', d_tot[:, np.newaxis], delimiter=',', fmt='%4.2f') #sys.exit() # pygor if conf_name != 'dummy': pg, active_gate_idxs, lb, ub, max_current, min_current, set_big_jump, set_small_jump = config.setup_pygor( conf_func, ip, settletime, settletime_big) else: pg, active_gate_idxs, lb, ub, max_current, min_current, set_big_jump, set_small_jump = config.setup_pygor_dummy( shape) active_gate_names = ["c{}".format(i + 1) for i in active_gate_idxs] num_active_gates = len(active_gate_names) print(active_gate_names) # choose the origin if np.isscalar(origin): origin = origin * np.ones(num_active_gates) else: if len(origin) != num_active_gates: raise ValueError('Wrong array shape of origin.') # pinchoff detector find_pinchoff_agian = False if find_pinchoff_agian: threshold = 0.2 * (max_current - min_current) + min_current d_r = 2.5 # length of one step
def main(): conf_name = 'config1' if conf_name == 'config1': conf_func = config.config1 ip = "http://129.67.86.107:8000/RPC2" save_dir = Path('./save_Dominic_redo_Basel2_correct') settletime, settletime_big = 0.01, 0.03 # settletime_big is for shuttling, 'None' disables shuttling # ['c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9', 'c10'] #origin = -100. origin = 0. threshold_low = 0.0 # for pinchoff detector d_r = 10 # length of one step elif conf_name == 'config2': # ['c3', 'c4', 'c8', 'c10', 'c11', 'c12', 'c16'] conf_func = config.config2 ip = "http://129.67.85.235:8000/RPC2" save_dir = Path('./save_Florian_redo') settletime, settletime_big = 0.01, 0.03 # settletime_big is for shuttling, 'None' disables shuttling #origin = -100. origin = 0. settletime = 0.01 threshold_low = 2.e-11 d_r = 10 # length of one step elif conf_name == 'dummy': import pygor_dummy box_dim = 5 box_a = -1000. * np.ones(box_dim) box_b = 1000. * np.ones(box_dim) shape = pygor_dummy.Box(ndim=box_dim, a=box_a, b=box_b) th_leak = np.array([-500., -400., -300., 0., 0.]) shape = pygor_dummy.Leakage(shape, th_leak) save_dir = Path('./save_dummy') origin = -100. threshold_low = 0.2 d_r = 10 # length of one step else: raise ValueError('Unsupported setup') save_dir.mkdir(exist_ok=True) if conf_name != 'dummy': pg, active_gate_idxs, lb_short, ub_short, max_current, min_current, set_big_jump, set_small_jump = config.setup_pygor(conf_func, ip, settletime, settletime_big) else: pg, active_gate_idxs, lb_short, ub_short, max_current, min_current, set_big_jump, set_small_jump = config.setup_pygor_dummy(shape) threshold_high = 0.8 * max_current active_gate_names = ["c{}".format(i+1) for i in active_gate_idxs] num_active_gates = len(active_gate_names) print(active_gate_names) # choose the origin if np.isscalar(origin): origin = origin*np.ones(num_active_gates) else: if len(origin) != num_active_gates: raise ValueError('Wrong array shape of origin.') # important algorithm parameters len_after_pinchoff=100 detector_pinchoff = util.PinchoffDetectorThreshold(threshold_low) # pichoff detector detector_conducting = util.ConductingDetectorThreshold(threshold_high) # reverse direction # create a Callable object, input: unit_vector, output: distance between the boundary and the origin tester = Tester(pg, lb_short, ub_short, detector_pinchoff, d_r=d_r, len_after_pinchoff=len_after_pinchoff, logging=True, detector_conducting=detector_conducting, set_big_jump = set_big_jump, set_small_jump = set_small_jump) #do_extra_meas = lambda vols: config_model.do_extra_meas(pg, vols) do_extra_meas = None # load voltages of a previous experiment load_dir = Path('./save_Florian_randompoints') #load_dir = Path('./save_Dominic_hs') #load_dir = Path('./save_Dominic_randomHS_origin0_fixed') vols_prev = np.load(load_dir/'vols_poff.npy', allow_pickle=True) num_points = len(vols_prev) vols_poff_all = list() detected_all = list() for i, v in enumerate(vols_prev): v = convert_Basel2_to_Basel1(v) v_origin = v - origin u = v_origin / np.sqrt(np.sum(np.square(v_origin))) # voltage -> unit vector # Get measurements r, vols_pinchoff, found, t_firstjump = tester.get_r(u, origin=origin) # Measure the distance vols_poff_all.append(vols_pinchoff) detected_all.append(found) print(i) print(v) print(vols_pinchoff) np.array(vols_poff_all).dump(str(save_dir / 'vols_poff_after.npy')) np.array(vols_prev).dump(str(save_dir / 'vols_poff_prev.npy')) np.array(detected_all).dump(str(save_dir / 'detected_after.npy'))
def main(): conf_name = 'config2' if conf_name == 'config1': conf_func = config.config1 ip = "http://129.67.86.107:8000/RPC2" save_dir = Path('./save_Dominic_ABL_group_optparam_run2/135_2') settletime, settletime_big = 0.01, 0.03 # settletime_big is for shuttling, 'None' disables shuttling # ['c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9', 'c10'] threshold_low = 0.0 # for pinchoff detector d_r = 10 # length of one step elif conf_name == 'config2': # ['c3', 'c4', 'c8', 'c10', 'c11', 'c12', 'c16'] conf_func = config.config2 #ip = "http://129.67.85.235:8000/RPC2" ip = 'http://129.67.85.38:8000/RPC2' save_dir = Path('./save_Basel2_group/105') settletime, settletime_big = 0.02, 0.02 # settletime_big is for shuttling, 'None' disables shuttling threshold_low = 2.e-11 d_r = 10 # length of one step elif conf_name == 'dummy': import pygor_dummy box_dim = 5 box_a = -1000. * np.ones(box_dim) box_b = 1000. * np.ones(box_dim) shape = pygor_dummy.Box(ndim=box_dim, a=box_a, b=box_b) th_leak = np.array([-500., -400., -300., 0., 0.]) shape = pygor_dummy.Leakage(shape, th_leak) save_dir = Path('./save_dummy') threshold_low = 0.2 d_r = 10 # length of one step else: raise ValueError('Unsupported setup') save_dir.mkdir(exist_ok=True) if conf_name != 'dummy': pg, active_gate_idxs, lb_short, ub_short, max_current, min_current, set_big_jump, set_small_jump = config.setup_pygor( conf_func, ip, settletime, settletime_big) else: pg, active_gate_idxs, lb_short, ub_short, max_current, min_current, set_big_jump, set_small_jump = config.setup_pygor_dummy( shape) threshold_high = 0.8 * max_current active_gate_names = ["c{}".format(i + 1) for i in active_gate_idxs] num_active_gates = len(active_gate_names) print(active_gate_names) # important algorithm parameters len_after_pinchoff = 100 detector_pinchoff = util.PinchoffDetectorThreshold( threshold_low) # pichoff detector detector_conducting = util.ConductingDetectorThreshold( threshold_high) # reverse direction # create a Callable object, input: unit_vector, output: distance between the boundary and the origin tester = Tester(pg, lb_short, ub_short, detector_pinchoff, d_r=d_r, len_after_pinchoff=len_after_pinchoff, logging=True, detector_conducting=detector_conducting, set_big_jump=set_big_jump, set_small_jump=set_small_jump) #do_extra_meas = lambda vols, th: config_model.do_extra_meas(pg, vols, th) do_extra_meas = None #v_target = np.array([-1877.11448379, -315.09271412, -588.03713859, -1743.6191362 , -587.8001855 , -693.38667762, -832.91995001, -186.99101894]) #new_wires = [[1, 0, 0, 0, 0, 0, 0, 0], #[0, 1, 0, 0, 0, 0, 0, 1], #[0, 0, 1, 0, 0, 0, 1, 0], #[0, 0, 0, 1, 1, 1, 0, 0]] new_wires = [[1, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 1], [0, 0, 1, 0, 0, 1, 0], [0, 0, 0, 1, 1, 0, 0]] new_wires = np.array(new_wires) v_target = np.array( [-1290.10731323, -1521.90819125, -813.40589252, -1069.5955569]) v_target = np.matmul(new_wires.T, v_target) origin = np.minimum(v_target + 200., 0.) print('Origin: ', origin) np.array(v_target).dump(str(save_dir / 'v_local.npy')) np.array(origin).dump(str(save_dir / 'origin_local.npy')) num_trials = 500 sigma = 100. #name_g1, name_g2 = 'c5', 'c9' name_g1, name_g2 = 'c8', 'c12' resol = 64 w1 = w2 = 75 idx_g1, idx_g2 = active_gate_names.index(name_g1), active_gate_names.index( name_g2) lb_g1, lb_g2 = lb_short[idx_g1], lb_short[idx_g2] ub_g1, ub_g2 = ub_short[idx_g1], ub_short[idx_g2] v_all = list() meas_all = list() time_all = list() for i in range(num_trials): ''' delta = np.random.normal(scale=sigma, size=num_active_gates) v_origin = v_target + delta - origin print('Random vol:', v_origin+origin) u = v_origin / np.sqrt(np.sum(np.square(v_origin))) # voltage -> unit vector ''' u = random_hypersphere(num_active_gates, 1)[0] # Get measurements t = time.time() r, vols_pinchoff, found, t_firstjump = tester.get_r( u, origin=origin) # Measure the distance t1 = time.time() - t print('Poff at ', vols_pinchoff) if found: # measurement print('Found') meas = measure_2d.scan2d(pg, name_g1, name_g2, resol, vols_pinchoff, w1, w2, lb_g1, lb_g2, ub_g1, ub_g2, str(i), pic_dpi=None, mult1=1., mult2=1., save_dir=save_dir) ''' meas = do_extra_meas(vols_pinchoff, 0.0) # highres = meas[-6] if len(meas) > 10 else None # lowres = meas[4] if len(meas) > 4 else None if lowres: plt.imsave(str(save_dir/(str(i)+'lowres.png')), lowres, origin='lower', cmap='viridis') if highres: plt.imsave(str(save_dir/(str(i)+'highres.png')), highres, origin='lower', cmap='viridis') ''' else: print('Not found') meas = [] t2 = time.time() - t time_all.append((t1, t2)) v_all.append(vols_pinchoff) meas_all.append(meas) np.array(v_all).dump(str(save_dir / 'vols_poff.npy')) #np.array(meas_all).dump(str(save_dir/'meas.npy')) np.save(str(save_dir / 'meas'), meas_all) np.array(time_all).dump(str(save_dir / 'time.npy'))