def fit_temp_pow_sweep(fname): """ Osmond Wen, December 10, 2019 decription: finds the resonant frequencies from the combined temperature and power sweep performed by powsweep.sweep_temp_pow """ fr, Qr = {}, {} with h5.File(fname) as fyle: fr_list_0 = fyle["S21/fr_list"][:] full_location = "tempsweep/2019-12-06-14-35-28" for temperature in np.array(fyle[full_location].keys()): print 'Temperature: ', temperature powernams = np.array(fyle[full_location+"/"+temperature].keys()) powervals = powernams.astype(np.float) powernams = powernams[powervals.argsort()] fr[float(temperature)] = {} Qr[float(temperature)] = {} for power in powernams: print ' Power: ', power df = pd.read_hdf(fname, key=full_location+"/"+temperature+"/"+power) fr[float(temperature)][float(power)] = {} Qr[float(temperature)][float(power)] = {} for resID, fr_nominal in enumerate(fr_list_0): print ' Resonator: ', resID df_thisKID = df[df.resID == resID] f_thisKID = df_thisKID.f.values z_thisKID = df_thisKID.z.values fr[float(temperature)][float(power)][resID],\ Qr[float(temperature)][float(power)][resID],\ _,_,_,_,_ = \ fitres.finefit(f_thisKID, z_thisKID, fr_nominal) return fr, Qr
def vna_file_fit(filename,pickedres,show=False,save=False): pickedres = np.array(pickedres) VNA_f, VNA_z = read_vna(filename, decimation=1) VNA_f = VNA_f*1e-3 # from MHz to GHz frs = np.zeros(len(pickedres)) Qrs = np.zeros(len(pickedres)) for MKIDnum in range(len(pickedres)): MKID_index = np.argmin(abs(VNA_f-pickedres[MKIDnum])) index_range = 5000 MKID_f = VNA_f[max(MKID_index-index_range,0):min(MKID_index+index_range,len(VNA_f))] MKID_z = VNA_z[max(MKID_index-index_range,0):min(MKID_index+index_range,len(VNA_f))] #MKID_f = VNA_f[(VNA_f>=pickedres[MKIDnum]-)*(VNA_f<=pickedres[MKIDnum]+)] frs[MKIDnum], Qrs[MKIDnum], Qc_hat, a, phi, tau, Qc = fitres.finefit(MKID_f, MKID_z, pickedres[MKIDnum]) if show: fit_z = fitres.resfunc3(MKID_f, frs[MKIDnum], Qrs[MKIDnum], Qc_hat, a, phi, tau) #MKID_z_corrected = 1-((1-MKID_z/(a*np.exp(-2j*np.pi*(MKID_f-frs[MKIDnum])*tau)))*(np.cos(phi)/np.exp(1j*phi))) #fit_z_corrected = 1-(Qrs[MKIDnum]/Qc)/(1+2j*Qrs[MKIDnum]*(MKID_f-frs[MKIDnum])/frs[MKIDnum]) plt.plot(MKID_f,20*np.log10(abs(MKID_z))) plt.plot(MKID_f,20*np.log10(abs(fit_z))) plt.show() if save: fit_z = fitres.resfunc3(MKID_f, frs[MKIDnum], Qrs[MKIDnum], Qc_hat, a, phi, tau) #MKID_z_corrected = 1-((1-MKID_z/(a*np.exp(-2j*np.pi*(MKID_f-frs[MKIDnum])*tau)))*(np.cos(phi)/np.exp(1j*phi))) #fit_z_corrected = 1-(Qrs[MKIDnum]/Qc)/(1+2j*Qrs[MKIDnum]*(MKID_f-frs[MKIDnum])/frs[MKIDnum]) plt.plot(MKID_f,20*np.log10(abs(MKID_z))) plt.plot(MKID_f,20*np.log10(abs(fit_z))) plt.savefig(filename[:-3]+'_res'+str(MKIDnum)+'.png') plt.close() return frs, Qrs
def fit_single_sweep(fname, sweeptype, sdate, sweep, h5_rewrite = False): with h5.File(fname) as fyle: fr_list_0 = fyle["S21/fr_list"][:] df = pd.read_hdf(fname, "{}/{}/{}".format(sweeptype, sdate, sweep)) num_res = len(fr_list_0) fr_list, Qr_list, Qc_mag_list, a_list, phi_list, tau_list, Qc_list = \ [None]*num_res, [None]*num_res, [None]*num_res, [None]*num_res, [None]*num_res, [None]*num_res, [None]*num_res for resID, fr_nominal in enumerate(fr_list_0): print sweep, fr_nominal df_temp = df[df.resID == resID] fwindow = 5e-4 # (max(df_temp.f) - min(df_temp.f)) / 2.0 f_curr = df_temp.f.values z_curr = df_temp.z.values # print f_curr # print z_curr try: fr_list[resID], \ Qr_list[resID], \ Qc_mag_list[resID], \ a_list[resID], \ phi_list[resID], \ tau_list[resID], \ Qc_list[resID] = \ fitres.finefit(f_curr, z_curr, fr_nominal) except: print "couldn't find a fit" fr_list[resID], Qr_list[resID], Qc_mag_list[resID], a_list[resID], phi_list[resID], tau_list[resID], Qc_list[resID] = \ 0, 0, 0, 0, 0, 0, 0 if h5_rewrite == True: with h5.File(fname, "r+") as fyle: if "{}/{}/{}/fr_list".format(sweeptype,sdate,sweep) in fyle: fyle.__delitem__("{}/{}/{}/fr_list".format(sweeptype,sdate,sweep)) if "{}/{}/{}/Qr_list".format(sweeptype,sdate,sweep) in fyle: fyle.__delitem__("{}/{}/{}/Qr_list".format(sweeptype,sdate,sweep)) if "{}/{}/{}/Qc_list".format(sweeptype,sdate,sweep) in fyle: fyle.__delitem__("{}/{}/{}/Qc_list".format(sweeptype,sdate,sweep)) fyle["{}/{}/{}/fr_list".format(sweeptype,sdate,sweep)] = fr_list fyle["{}/{}/{}/Qr_list".format(sweeptype,sdate,sweep)] = Qr_list fyle["{}/{}/{}/Qc_list".format(sweeptype,sdate,sweep)] = Qc_list result = pd.DataFrame({"fr_list":fr_list, "Qr_list":Qr_list, "Qc_mag_list":Qc_mag_list, "a_list":a_list, "phi_list":phi_list, "tau_list":tau_list, "Qc_list": Qc_list}) result["sweep"] = float(sweep) return result
def vna_file_fit(filename, pickedres, show=False, save=False): pickedres = np.array(pickedres) VNA_f, VNA_z, _ = read_vna(filename, decimation=1) VNA_f = VNA_f * 1e-3 #VNA_f in units of GHz after this line frs = np.zeros(len(pickedres)) Qrs = np.zeros(len(pickedres)) for MKIDnum in range(len(pickedres)): window_f = (pickedres[MKIDnum] + 10 * pickedres[MKIDnum] / 3e5) window_index = np.argmin(abs(VNA_f - window_f)) MKID_index = np.argmin(abs(VNA_f - pickedres[MKIDnum])) index_range = window_index - MKID_index MKID_f = VNA_f[max(MKID_index - index_range, 0):min(MKID_index + index_range, len(VNA_f))] MKID_z = VNA_z[max(MKID_index - index_range, 0):min(MKID_index + index_range, len(VNA_f))] frs[MKIDnum], Qrs[MKIDnum], Qc_hat, a, phi, tau, Qc = fitres.finefit( MKID_f, MKID_z, pickedres[MKIDnum]) if show: fit_z = fitres.resfunc3(MKID_f, frs[MKIDnum], Qrs[MKIDnum], Qc_hat, a, phi, tau) #MKID_z_corrected = 1-((1-MKID_z/(a*np.exp(-2j*np.pi*(MKID_f-frs[MKIDnum])*tau)))*(np.cos(phi)/np.exp(1j*phi))) #fit_z_corrected = 1-(Qrs[MKIDnum]/Qc)/(1+2j*Qrs[MKIDnum]*(MKID_f-frs[MKIDnum])/frs[MKIDnum]) plt.figure('how does the fit look') plt.plot(MKID_f, 20 * np.log10(abs(MKID_z))) plt.plot(MKID_f, 20 * np.log10(abs(fit_z))) plt.show() raw_input('press enter to move on') plt.close('all') if save: fit_z = fitres.resfunc3(MKID_f, frs[MKIDnum], Qrs[MKIDnum], Qc_hat, a, phi, tau) #MKID_z_corrected = 1-((1-MKID_z/(a*np.exp(-2j*np.pi*(MKID_f-frs[MKIDnum])*tau)))*(np.cos(phi)/np.exp(1j*phi))) #fit_z_corrected = 1-(Qrs[MKIDnum]/Qc)/(1+2j*Qrs[MKIDnum]*(MKID_f-frs[MKIDnum])/frs[MKIDnum]) plt.plot(MKID_f, 20 * np.log10(abs(MKID_z))) plt.plot(MKID_f, 20 * np.log10(abs(fit_z))) plt.savefig(filename[:-3] + '_res' + str(MKIDnum) + '.png') plt.close() return frs, Qrs, Qc_hat, a, phi, tau, Qc
def fit_temp_res(fname, sweepnum, pickedres=None): with h5py.File(fname, 'r') as fyle: timestamps = fyle['tempsweep'].keys() chosen_tempsweep = timestamps[ sweepnum] # Just the first temperature sweep saved in this file print chosen_tempsweep MCTemps1 = np.array(fyle['tempsweep/' + chosen_tempsweep].keys()) MCTemps1 = MCTemps1[MCTemps1 != 'MB'] MCTemps1 = MCTemps1[MCTemps1 != 'RES'] df1 = pd.read_hdf(fname, key='tempsweep/' + chosen_tempsweep + '/' + MCTemps1[0]) if pickedres is not None: if isinstance(pickedres, int): MKIDnum = [pickedres] else: MKIDnum = pickedres else: MKIDnum = np.unique(df1['resID']) ID_of_T = np.zeros((len(MCTemps1), len(MKIDnum))) fr_of_T = np.zeros((len(MCTemps1), len(MKIDnum))) Qr_of_T = np.zeros((len(MCTemps1), len(MKIDnum))) Qc_of_T = np.zeros((len(MCTemps1), len(MKIDnum))) a_of_T = (1 + 1j) * np.zeros((len(MCTemps1), len(MKIDnum))) phi_of_T = np.zeros((len(MCTemps1), len(MKIDnum))) tau_of_T = (1 + 1j) * np.zeros((len(MCTemps1), len(MKIDnum))) Qc_hat_mag_of_T = np.zeros((len(MCTemps1), len(MKIDnum))) print 'Doing resonance fits...' for Tn in range(len(MCTemps1)): temperature_Tn = MCTemps1[Tn] df1 = pd.read_hdf(fname, key='tempsweep/' + chosen_tempsweep + '/' + temperature_Tn) resID_index = df1['resID'] for Kn in range(len(MKIDnum)): fr_0 = np.mean(df1['f0'][resID_index == MKIDnum[Kn]][0]) if Tn > 0: if fr_of_T[Tn - 1, Kn] != 0: fr_0 = fr_of_T[Tn - 1, Kn] f1 = df1['f'] f_array = np.array(f1[resID_index == MKIDnum[Kn]]) z1 = df1['z'] z_array = np.array(z1[resID_index == MKIDnum[Kn]]) try: fitted = fitres.finefit(f_array, z_array, fr_0) except: plt.plot(f_array, abs(z_array)) plt.title('MKID ' + str(MKIDnum[Kn]) + ', ' + str(MCTemps1[Tn]) + ' K') plt.show() fitted = [0, 0, 0, 0, 0, 0, 0] ID_of_T[Tn, Kn] = MKIDnum[Kn] fr_of_T[Tn, Kn] = fitted[0] Qr_of_T[Tn, Kn] = fitted[1] Qc_of_T[Tn, Kn] = fitted[6] a_of_T[Tn, Kn] = fitted[3] phi_of_T[Tn, Kn] = fitted[4] tau_of_T[Tn, Kn] = fitted[5] Qc_hat_mag_of_T[Tn, Kn] = fitted[2] df2 = pd.DataFrame() df2['resID'] = ID_of_T.flatten(order='F') df2['fr'] = fr_of_T.flatten(order='F') df2['Qr'] = Qr_of_T.flatten(order='F') df2['Qc'] = Qc_of_T.flatten(order='F') df2['a'] = a_of_T.flatten(order='F') df2['phi'] = phi_of_T.flatten(order='F') df2['tau'] = tau_of_T.flatten(order='F') df2['Qc_hat_mag'] = Qc_hat_mag_of_T.flatten(order='F') df2.to_hdf(fname, key='/tempsweep/' + chosen_tempsweep + '/RES')
def resonator_basis(noise_timestream,readout_f,VNA_f,VNA_z,char_f,char_z): def rotate_to_ideal(z,f,fr,a,tau,phi): return 1-((1-z/(a*np.exp(-2j*np.pi*(f-fr)*tau)))*(np.cos(phi)/np.exp(1j*phi))) char_res_idx = int(len(char_f)/2) # Define region around this resonance, and fit for parameters index_range = 1000 readout_index = PUf.find_closest(VNA_f,readout_f) MKID_f = VNA_f[max(readout_index-index_range,0):min(readout_index+index_range,len(VNA_f))] MKID_z = VNA_z[max(readout_index-index_range,0):min(readout_index+index_range,len(VNA_f))] fit_fr, fit_Qr, fit_Qc_hat, fit_a, fit_phi, fit_tau, fit_Qc = fitres.finefit(MKID_f, MKID_z, readout_f) fit_Qi = fit_Qr*fit_Qc/(fit_Qc-fit_Qr) print(fit_Qr,fit_Qc) # Transform VNA to ideal space MKID_z_ideal = rotate_to_ideal(MKID_z,MKID_f,fit_fr,fit_a,fit_tau,fit_phi) # Transform the VNA fit to ideal space fit_f = np.linspace(MKID_f[0],MKID_f[-1],10000) fit_z = fitres.resfunc3(fit_f, fit_fr, fit_Qr, fit_Qc_hat, fit_a, fit_phi, fit_tau) fit_z_ideal = rotate_to_ideal(fit_z,fit_f,fit_fr,fit_a,fit_tau,fit_phi) # find some important indices in the f vector first_char_fit_idx = PUf.find_closest(fit_f,char_f[0]) last_char_fit_idx = PUf.find_closest(fit_f,char_f[-1]) res_f_idx = PUf.find_closest(MKID_f,readout_f) res_fit_idx = PUf.find_closest(fit_f,readout_f) # Find VNA-data subset that covers the characterization-data region in frequency space char_region_f = fit_f[first_char_fit_idx:last_char_fit_idx] char_region_z = fit_z[first_char_fit_idx:last_char_fit_idx] char_region_z_ideal = fit_z_ideal[first_char_fit_idx:last_char_fit_idx] # Get the angle (in complex space) of the characterization data real_fit = np.polyfit(char_f,char_z.real,1) imag_fit = np.polyfit(char_f,char_z.imag,1) char_angle = np.angle((real_fit[0]*(char_region_f[-1]-char_region_f[0]))+1j*(imag_fit[0]*(char_region_f[-1]-char_region_f[0]))) # Get the angle (in complex space) of the VNA data real_fit = np.polyfit(char_region_f,char_region_z.real,1) imag_fit = np.polyfit(char_region_f,char_region_z.imag,1) char_region_angle = np.angle((real_fit[0]*(char_region_f[-1]-char_region_f[0]))+1j*(imag_fit[0]*(char_region_f[-1]-char_region_f[0]))) # Get the angle (in complex ideal space) of the VNA data real_fit = np.polyfit(char_region_f,char_region_z_ideal.real,1) imag_fit = np.polyfit(char_region_f,char_region_z_ideal.imag,1) char_region_ideal_angle = np.angle((real_fit[0]*(char_region_f[-1]-char_region_f[0]))+1j*(imag_fit[0]*(char_region_f[-1]-char_region_f[0]))) # Rotate characterization data to VNA data char_z_rotated = (char_z-char_z[char_res_idx])\ *np.exp(1j*(-1*char_angle+char_region_angle))\ +fit_z[res_fit_idx] char_z_rotated_ideal = rotate_to_ideal(char_z_rotated,char_f,fit_fr,fit_a,fit_tau,fit_phi) timestream_rotated = (noise_timestream-np.mean(noise_timestream,dtype=complex))\ *np.exp(1j*(-1*char_angle+char_region_angle))\ +fit_z[res_fit_idx] timestream_rotated_ideal = rotate_to_ideal(timestream_rotated,readout_f,fit_fr,fit_a,fit_tau,fit_phi) dS21 = timestream_rotated_ideal-np.mean(timestream_rotated_ideal,dtype=complex) # Extra rotation for imaginary direction --> frequency tangent direction # This is necessary if data_freqs[0] != primary_fr # Note that char_f[res_idx] == data_freqs[0] should always be true # data_freqs[0] != primary_fr means our vna fit before data taking gave a different fr than our vna fit now phase_adjust = 1 # phase_adjust = np.exp(1j*(np.sign(char_region_ideal_angle)*0.5*np.pi-char_region_ideal_angle)) frequency = phase_adjust*dS21.imag*fit_Qc/(2*fit_Qr**2) dissipation = phase_adjust*dS21.real*fit_Qc/(fit_Qr**2) # print(type(frequency[2]),type(dissipation[2]),type(fit_Qc)) ideal = {} ideal['f'] = MKID_f ideal['z'] = MKID_z_ideal ideal['char z'] = char_z_rotated_ideal ideal['timestream'] = timestream_rotated_ideal ideal['fit f'] = fit_f ideal['fit z'] = fit_z_ideal resonator = {} resonator['fr'] = fit_fr resonator['Qr'] = fit_Qr resonator['Qc'] = fit_Qc if False: # PUf.plot_noise_and_vna(noise_timestream,MKID_z,f_idx=res_f_idx,char_zs=char_z,title='S21') PUf.plot_noise_and_vna(timestream_rotated_ideal,MKID_z_ideal,\ f_idx=res_f_idx,char_zs=char_z_rotated_ideal,title='ideal space') plt.show(False) # plt.pause(5) raw_input('press enter to close all plots') plt.close('all') # plt.figure(2) # fit_fr_idx = PUf.find_closest(MKID_f,fit_fr) # # Plotting the corrected data (moved to ideal S21 space) # plt.gca().set_aspect('equal', adjustable='box') # plt.axvline(x=0,c='k') # plt.axhline(y=0,c='k') # plt.plot(MKID_z_ideal.real,MKID_z_ideal.imag,label='ideal space vna') # plt.plot(char_z_rotated_ideal.real,char_z_rotated_ideal.imag,label='calibrated calibration data') # plt.plot(MKID_z_ideal[res_f_idx].real,MKID_z_ideal[res_f_idx].imag,'o',label='vna closest to readout frequency') # plt.plot(MKID_z_ideal[fit_fr_idx].real,MKID_z_ideal[fit_fr_idx].imag,'o',label='vna closest to new fr') # plt.plot(timestream_rotated_ideal[::10].real,timestream_rotated_ideal[::10].imag,ls='',marker='.') # # plt.legend() # plt.show() return frequency, dissipation, ideal, resonator