def go(self):
        if self.liveplot_enabled:
            self.plotter.clear()

        print "Prep Instruments"
        self.readout.set_frequency(self.cfg['readout']['frequency'])
        self.readout.set_power(self.cfg['readout']['power'])
        self.readout.set_ext_pulse(mod=True)
        self.readout_shifter.set_phase(self.cfg['readout']['start_phase'] + self.cfg['readout']['phase_slope'] * (
            self.cfg['readout']['frequency'] - self.cfg['readout']['bare_frequency']), self.cfg['readout']['frequency'])

        self.drive.set_frequency(self.cfg['qubit']['frequency'] - self.cfg['pulse_info'][self.pulse_type]['iq_freq'])
        self.drive.set_power(self.cfg['drive']['power'])
        self.drive.set_ext_pulse(mod=True)
        self.drive.set_output(True)
        self.readout_atten.set_attenuator(self.cfg['readout']['dig_atten'])

        try:
            self.cfg['freq_flux']['flux']=self.extra_args['flux']
        except:
            pass

        try:
            self.cfg['freq_flux']['freq_flux_slope']=self.extra_args['freq_flux_slope']
        except:
            pass

        try:
            self.cfg['freq_flux']['flux_offset']+=self.extra_args['flux_offset']
        except:
            pass


        if self.cfg['freq_flux']['current']:
            self.flux_volt.ramp_current(self.cfg['freq_flux']['flux'])
        elif self.cfg['freq_flux']['voltage']:
            self.flux_volt.ramp_volt(self.cfg['freq_flux']['flux'])

        self.awg.set_amps_offsets(self.cfg['cal']['iq_amps'], self.cfg['cal']['iq_offsets'])

        if self.pre_run is not None:
            self.pre_run()

        if self.adc==None:
            print "Prep Card"
            adc = Alazar(self.cfg['alazar'])
        else:
            adc = self.adc

        expt_data = None
        current_data = None
        for ii in tqdm(arange(max(1, self.cfg[self.expt_cfg_name]['averages'] / 100))):
            tpts, ch1_pts, ch2_pts = adc.acquire_avg_data_by_record(prep_function=self.awg_prep,
                                                                    start_function=self.awg.run,
                                                                    excise=self.cfg['readout']['window'])

            mag = sqrt(ch1_pts**2+ch2_pts**2)
            if not self.cfg[self.expt_cfg_name]['use_pi_calibration']:

                if expt_data is None:
                    if self.cfg['readout']['channel']==1:
                        expt_data = ch1_pts
                    elif self.cfg['readout']['channel']==2:
                        expt_data = ch2_pts
                else:
                    if self.cfg['readout']['channel']==1:
                        expt_data = (expt_data * ii + ch1_pts) / (ii + 1.0)
                    elif self.cfg['readout']['channel']==2:
                        expt_data = (expt_data * ii + ch2_pts) / (ii + 1.0)


            else:
                if self.cfg['readout']['channel']==1:
                    zero_amp = mean(ch1_pts[-2])
                    pi_amp = mean(ch1_pts[-1])
                    current_data= (ch1_pts[:-2]-zero_amp)/(pi_amp-zero_amp)
                elif self.cfg['readout']['channel']==2:
                    zero_amp = mean(ch2_pts[-2])
                    pi_amp = mean(ch2_pts[-1])
                    current_data= (ch2_pts[:-2]-zero_amp)/(pi_amp-zero_amp)
                if expt_data is None:
                    expt_data = current_data
                else:
                    expt_data = (expt_data * ii + current_data) / (ii + 1.0)


            expt_avg_data = mean(expt_data, 1)


            if self.liveplot_enabled:
                self.plotter.plot_z(self.prefix + ' Data', expt_data.T)
                self.plotter.plot_xy(self.prefix + ' XY', self.pulse_sequence.expt_pts, expt_avg_data)

            # print ii * min(self.cfg[self.expt_cfg_name]['averages'], 100)

            if self.data_file != None:
                self.slab_file = SlabFile(self.data_file)
            else:
                self.slab_file = self.datafile()
            with self.slab_file as f:
                f.add('expt_2d', expt_data)
                f.add('expt_avg_data', expt_avg_data)
                f.add('expt_pts', self.expt_pts)
                f.close()

        if self.post_run is not None:
            self.post_run(self.expt_pts, expt_avg_data)
    def take_data(self):

        ####
        if self.cfg["visdom_plot_livedata"]:

            viz = visdom.Visdom()
            assert viz.check_connection(), "Visdom server not connected!"
            # added two environments "seq_builder.json", and "live_plot.json" in C:\Users\slab\.visdom
            eid = "live_plot"
            viz.close(win=None, env=eid)

            win1 = viz.line( X=np.arange(0, 1), Y=np.arange(0, 1), env=eid,
                opts=dict(height=400, width=700, title='expt_avg_data', showlegend=True, xlabel='expt_pts'))
            win2 = viz.line( X=np.arange(0, 1), Y=np.arange(0, 1), env=eid,
                opts=dict(height=400, width=700, title='expt_avg_data2', showlegend=True, xlabel='expt_pts'))
            win3 = viz.line( X=np.arange(0, 1), Y=np.arange(0, 1), env=eid,
                opts=dict(height=400, width=700, title='single_record (of only first run)', showlegend=True, xlabel='time ns'))
            win4 = viz.scatter(X=array([[1,2,3],[1,2,3]]).transpose(), env=eid,
                        opts=dict(height=400, width=700, title='g/e/f cal single shot', showlegend=True, xlabel=''))

        ####

        if self.pre_run is not None:
            self.pre_run()

        TEST_REDPITAYA = False

        if TEST_REDPITAYA:
            self.awg_run()

        if not TEST_REDPITAYA:

            if self.adc == None:
                print("Prep Card")
                adc = Alazar(self.cfg['alazar'])
            else:
                adc = self.adc

            # debug_alazer = False
            # if debug_alazer:
            #
            #     # save raw time trace, 100 runs only
            #     tpts, single_data1, single_data2 = adc.acquire_singleshot_data2()
            #
            #     self.slab_file = self.datafile(data_file=self.data_file)
            #     with self.slab_file as f:
            #         f.add('tpts', tpts)
            #         f.add('single_data1', single_data1)
            #         f.add('single_data2', single_data2)
            #         f.close()
            #
            #     # hack for post_run
            #     expt_avg_data = self.expt_pts
            #
            # else:

            if not self.cfg['readout']['save_single-shot_data']:

                expt_data = None
                current_data = None
                for ii in tqdm(arange(max(1, self.cfg[self.expt_cfg_name]['averages'] / 100))):
                    tpts, ch1_pts, ch2_pts = adc.acquire_avg_data_by_record(prep_function=self.awg_prep,
                                                                            start_function=self.awg_run,
                                                                            excise=self.cfg['readout']['window'])

                    if not self.cfg[self.expt_cfg_name]['use_pi_calibration']:

                        if expt_data is None:
                            if self.cfg['readout']['channel'] == 1:
                                expt_data = ch1_pts
                            elif self.cfg['readout']['channel'] == 2:
                                expt_data = ch2_pts
                        else:
                            if self.cfg['readout']['channel'] == 1:
                                expt_data = (expt_data * ii + ch1_pts) / (ii + 1.0)
                            elif self.cfg['readout']['channel'] == 2:
                                expt_data = (expt_data * ii + ch2_pts) / (ii + 1.0)

                        if self.cfg['readout']['heterodyne_freq'] == 0:
                            # homodyne
                            expt_avg_data = mean(expt_data, 1)
                        else:
                            # heterodyne
                            heterodyne_freq = self.cfg['readout']['heterodyne_freq']
                            # ifft by numpy default has the correct 1/N normalization
                            expt_data_fft_amp = np.abs(np.fft.ifft(expt_data))
                            hetero_f_ind = int(round(heterodyne_freq * tpts.size * 1e-9))  # position in ifft

                            # todo: do the proper sum here
                            # expt_avg_data = np.average(expt_data_fft_amp[:, (hetero_f_ind - 1):(hetero_f_ind + 1)], axis=1)
                            expt_avg_data = expt_data_fft_amp[:, hetero_f_ind]

                    else:
                        # average first, then divide by pi_calibration values
                        if expt_data is None:
                            if self.cfg['readout']['channel'] == 1:
                                expt_data = ch1_pts[:-2]
                                zero_amp_curr = mean(ch1_pts[-2])
                                pi_amp_curr = mean(ch1_pts[-1])
                            elif self.cfg['readout']['channel'] == 2:
                                expt_data = ch2_pts[:-2]
                                zero_amp_curr = mean(ch2_pts[-2])
                                pi_amp_curr = mean(ch2_pts[-1])
                            zero_amp = zero_amp_curr
                            pi_amp = pi_amp_curr
                        else:
                            if self.cfg['readout']['channel'] == 1:
                                expt_data = (expt_data * ii + ch1_pts[:-2]) / (ii + 1.0)
                                zero_amp_curr = mean(ch1_pts[-2])
                                pi_amp_curr = mean(ch1_pts[-1])
                            elif self.cfg['readout']['channel'] == 2:
                                expt_data = (expt_data * ii + ch2_pts[:-2]) / (ii + 1.0)
                                zero_amp_curr = mean(ch2_pts[-2])
                                pi_amp_curr = mean(ch2_pts[-1])
                            zero_amp = (zero_amp * ii + zero_amp_curr) / (ii + 1.0)
                            pi_amp = (pi_amp * ii + pi_amp_curr) / (ii + 1.0)

                        # todo: add heterodyne with pi_cal
                        expt_avg_data = mean((expt_data - zero_amp) / (pi_amp - zero_amp), 1)

                    # self.slab_file = self.datafile(data_file=self.data_file)

                    with self.slab_file as f:
                        f.add('expt_2d', expt_data)
                        f.add('expt_avg_data', expt_avg_data)
                        f.add('expt_pts', self.expt_pts)

                        # save pi_cal amps, to be able to monitor fluctuations
                        if self.cfg[self.expt_cfg_name]['use_pi_calibration']:
                            f.append_pt('zero_amps', zero_amp_curr)
                            f.append_pt('pi_amps', pi_amp_curr)

                        f.close()


            else:  # here saves all single shot data

                if self.prefix == 'Vacuum_Rabi':

                    print('vacuum_rabi')
                    het_IFreqList = [self.cfg['readout']['heterodyne_freq']]
                    het_read_freq_list = [0]

                elif self.cfg['readout']['is_multitone_heterodyne']:
                    het_carrier_freq = self.cfg['readout']['heterodyne_carrier_freq']
                    het_read_freq_list = array(self.cfg['readout']['heterodyne_freq_list'])
                    het_IFreqList = het_read_freq_list - het_carrier_freq
                else:
                    het_carrier_freq = self.readout_freq - self.cfg['readout']['heterodyne_freq']
                    het_read_freq_list = array([self.readout_freq])
                    het_IFreqList = het_read_freq_list - het_carrier_freq

                avgPerAcquisition = int(min(self.cfg[self.expt_cfg_name]['averages'], 100))
                numAcquisition = int(np.ceil(self.cfg[self.expt_cfg_name]['averages'] / 100))

                # (ch1/2, exp_pts, heterodyne_freq, cos/sin, all averages)
                ss_data = zeros((2, len(self.expt_pts), len(het_IFreqList), 2, avgPerAcquisition * numAcquisition))

                # (ch1/2, heterodyne_freq, cos/sin, all averages)
                ss_cal_g = zeros((2, len(het_IFreqList), 2, avgPerAcquisition * numAcquisition))
                ss_cal_e = zeros((2, len(het_IFreqList), 2, avgPerAcquisition * numAcquisition))
                ss_cal_f = zeros((2, len(het_IFreqList), 2, avgPerAcquisition * numAcquisition))

                if self.cfg['readout']['save_trajectory_data']:

                    # (ch1/2, exp_pts, heterodyne_freq, cos/sin, all averages, traj)
                    traj_data = zeros((2, len(self.expt_pts), len(het_IFreqList), 2, avgPerAcquisition * numAcquisition, self.cfg['readout']['pts_per_traj']))

                    # (ch1/2, heterodyne_freq, cos/sin, all averages, traj)
                    traj_cal_g = zeros((2, len(het_IFreqList), 2, avgPerAcquisition * numAcquisition, self.cfg['readout']['pts_per_traj']))
                    traj_cal_e = zeros((2, len(het_IFreqList), 2, avgPerAcquisition * numAcquisition, self.cfg['readout']['pts_per_traj']))
                    traj_cal_f = zeros((2, len(het_IFreqList), 2, avgPerAcquisition * numAcquisition, self.cfg['readout']['pts_per_traj']))

                for ii in tqdm(arange(numAcquisition)):

                    if not self.cfg['readout']['is_hetero_phase_ref']:

                        # single_data1/2: index: (hetero_freqs, cos/sin, all_seqs)
                        single_data1, single_data2, single_record1, single_record2 = \
                            adc.acquire_singleshot_heterodyne_multitone_data(het_IFreqList, prep_function=self.awg_prep,
                                                                             start_function=self.awg_run,
                                                                             excise=self.cfg['readout']['window'])

                        # saving the raw time traces
                        # single_data1, single_data2, single_record1, single_record2 = \
                        #     adc.acquire_singleshot_heterodyne_multitone_data(het_IFreqList, prep_function=self.awg_prep,
                        #                                                      start_function=self.awg_run,
                        #                                                      excise=None, save_raw_data=True)

                    elif self.cfg['readout']['save_trajectory_data']:

                        excise = [ 0, self.cfg['readout']['window'][1] ]
                        single_data1, single_data2, single_record1, single_record2, single_traj1, single_traj2= \
                            adc.acquire_singleshot_heterodyne_multitone_data_phase_ref_save_traj(het_IFreqList,
                                                                                       self.cfg['readout'][
                                                                                           'hetero_phase_ref_freq'],
                                                                                       prep_function=self.awg_prep,
                                                                                       start_function=self.awg_run,
                                                                                       excise=excise,
                                                                                       isCompensatePhase=True,
                                                                                       save_raw_data=False, pts_per_traj=self.cfg['readout']['pts_per_traj'])
                    else:

                        # single_data1/2: index: (hetero_freqs, cos/sin, all_seqs)
                        single_data1, single_data2, single_record1, single_record2 = \
                            adc.acquire_singleshot_heterodyne_multitone_data_phase_ref(het_IFreqList,
                                                                             self.cfg['readout']['hetero_phase_ref_freq'],
                                                                             prep_function=self.awg_prep,
                                                                             start_function=self.awg_run,
                                                                             excise=self.cfg['readout']['window'],
                                                                             isCompensatePhase=True,
                                                                             save_raw_data=False)

                    single_data = array([single_data1, single_data2])
                    # index: (ch1/2, hetero_freqs, cos / sin, avgs, seq(exp_pts))
                    single_data = np.reshape(single_data,
                                             (single_data.shape[0], single_data.shape[1], single_data.shape[2],
                                              int(self.cfg['alazar'][
                                                  'recordsPerAcquisition'] / self.pulse_sequence.sequence_length),
                                              self.pulse_sequence.sequence_length))

                    # index: (ch1/2, exp_pts, hetero_freqs, cos / sin, avgs)
                    single_data = np.transpose(single_data, (0, 4, 1, 2, 3))

                    if (self.cfg[self.expt_cfg_name]['use_g-e-f_calibration']):

                        ss_data[:, :, :, :, (ii * avgPerAcquisition):((ii + 1) * avgPerAcquisition)] = single_data[:,
                                                                                                       0:-3]
                        ss_cal_g[:, :, :, (ii * avgPerAcquisition):((ii + 1) * avgPerAcquisition)] = single_data[:, -3]
                        ss_cal_e[:, :, :, (ii * avgPerAcquisition):((ii + 1) * avgPerAcquisition)] = single_data[:, -2]
                        ss_cal_f[:, :, :, (ii * avgPerAcquisition):((ii + 1) * avgPerAcquisition)] = single_data[:, -1]

                    elif (self.cfg[self.expt_cfg_name]['use_pi_calibration']):

                        ss_data[:, :, :, :, (ii * avgPerAcquisition):((ii + 1) * avgPerAcquisition)] = single_data[:,
                                                                                                       0:-2]
                        ss_cal_g[:, :, :, (ii * avgPerAcquisition):((ii + 1) * avgPerAcquisition)] = single_data[:, -2]
                        ss_cal_e[:, :, :, (ii * avgPerAcquisition):((ii + 1) * avgPerAcquisition)] = single_data[:, -1]

                    else:
                        ss_data[:, :, :, :, (ii * avgPerAcquisition):((ii + 1) * avgPerAcquisition)] = single_data[:, :]

                    ####
                    if self.cfg['readout']['save_trajectory_data']:

                        # index: (hetero_freqs, cos/sin, all_seqs, traj)
                        single_traj = array([single_traj1, single_traj2])

                        # index: (ch1/2, hetero_freqs, cos / sin, avgs, seq(exp_pts), traj)
                        single_traj = np.reshape(single_traj,
                                                 (single_traj.shape[0], single_traj.shape[1], single_traj.shape[2],
                                                  self.cfg['alazar']['recordsPerAcquisition'] / self.pulse_sequence.sequence_length,
                                                  self.pulse_sequence.sequence_length, single_traj.shape[-1]))

                        # index: (ch1/2, exp_pts, hetero_freqs, cos / sin, avgs, traj)
                        single_traj = np.transpose(single_traj, (0, 4, 1, 2, 3, 5))
                        # print single_traj.shape

                        if (self.cfg[self.expt_cfg_name]['use_g-e-f_calibration']):

                            traj_data[:, :, :, :, (ii * avgPerAcquisition):((ii + 1) * avgPerAcquisition), :] = single_traj[
                                                                                                           :,
                                                                                                           0:-3, :]
                            traj_cal_g[:, :, :, (ii * avgPerAcquisition):((ii + 1) * avgPerAcquisition), :] = single_traj[:,
                                                                                                         -3, :]
                            traj_cal_e[:, :, :, (ii * avgPerAcquisition):((ii + 1) * avgPerAcquisition), :] = single_traj[:,
                                                                                                         -2, :]
                            traj_cal_f[:, :, :, (ii * avgPerAcquisition):((ii + 1) * avgPerAcquisition), :] = single_traj[:,
                                                                                                         -1, :]

                        elif (self.cfg[self.expt_cfg_name]['use_pi_calibration']):

                            traj_data[:, :, :, :, (ii * avgPerAcquisition):((ii + 1) * avgPerAcquisition), :] = single_traj[
                                                                                                           :,
                                                                                                           0:-2, :]
                            traj_cal_g[:, :, :, (ii * avgPerAcquisition):((ii + 1) * avgPerAcquisition), :] = single_traj[:,
                                                                                                         -2, :]
                            traj_cal_e[:, :, :, (ii * avgPerAcquisition):((ii + 1) * avgPerAcquisition), :] = single_traj[:,
                                                                                                         -1, :]

                        else:
                            traj_data[:, :, :, :, (ii * avgPerAcquisition):((ii + 1) * avgPerAcquisition), :] = single_traj[
                                                                                                           :, :, :]

                    ####

                    # old way for easy plotting
                    # only calculate for 1st het_readout_freq
                    if het_IFreqList[0] == 0:
                        # take cos of ch1/ch2
                        expt_avg_data = mean(ss_data[0, :, 0, 0, 0:((ii + 1) * avgPerAcquisition)], -1)
                        expt_avg_data2 = mean(ss_data[1, :, 0, 0, 0:((ii + 1) * avgPerAcquisition)], -1)

                        if self.cfg[self.expt_cfg_name]['use_pi_calibration']:
                            zero_amp_avg = mean(ss_cal_g[0, 0, 0, 0:((ii + 1) * avgPerAcquisition)], -1)
                            zero_amp_avg2 = mean(ss_cal_g[1, 0, 0, 0:((ii + 1) * avgPerAcquisition)], -1)
                            pi_amp_avg = mean(ss_cal_e[0, 0, 0, 0:((ii + 1) * avgPerAcquisition)], -1)
                            pi_amp_avg2 = mean(ss_cal_e[1, 0, 0, 0:((ii + 1) * avgPerAcquisition)], -1)

                            zero_amp_curr = mean(
                                ss_cal_g[0, 0, 0, (ii * avgPerAcquisition):((ii + 1) * avgPerAcquisition)], -1)
                            zero_amp_curr2 = mean(
                                ss_cal_g[1, 0, 0, (ii * avgPerAcquisition):((ii + 1) * avgPerAcquisition)], -1)
                            pi_amp_curr = mean(
                                ss_cal_e[0, 0, 0, (ii * avgPerAcquisition):((ii + 1) * avgPerAcquisition)], -1)
                            pi_amp_curr2 = mean(
                                ss_cal_e[1, 0, 0, (ii * avgPerAcquisition):((ii + 1) * avgPerAcquisition)], -1)

                            expt_avg_data = (expt_avg_data - zero_amp_avg) / (pi_amp_avg - zero_amp_avg)
                            expt_avg_data2 = (expt_avg_data2 - zero_amp_avg2) / (pi_amp_avg2 - zero_amp_avg2)

                    else:
                        # take cos/sin of ch1
                        expt_avg_data = mean(ss_data[0, :, 0, 0, 0:((ii + 1) * avgPerAcquisition)], -1)
                        expt_avg_data2 = mean(ss_data[0, :, 0, 1, 0:((ii + 1) * avgPerAcquisition)], -1)

                        if self.cfg[self.expt_cfg_name]['use_pi_calibration']:
                            zero_amp_avg = mean(ss_cal_g[0, 0, 0, 0:((ii + 1) * avgPerAcquisition)], -1)
                            zero_amp_avg2 = mean(ss_cal_g[0, 0, 1, 0:((ii + 1) * avgPerAcquisition)], -1)
                            pi_amp_avg = mean(ss_cal_e[0, 0, 0, 0:((ii + 1) * avgPerAcquisition)], -1)
                            pi_amp_avg2 = mean(ss_cal_e[0, 0, 1, 0:((ii + 1) * avgPerAcquisition)], -1)

                            zero_amp_curr = mean(
                                ss_cal_g[0, 0, 0, (ii * avgPerAcquisition):((ii + 1) * avgPerAcquisition)], -1)
                            zero_amp_curr2 = mean(
                                ss_cal_g[0, 0, 1, (ii * avgPerAcquisition):((ii + 1) * avgPerAcquisition)],
                                -1)
                            pi_amp_curr = mean(
                                ss_cal_e[0, 0, 0, (ii * avgPerAcquisition):((ii + 1) * avgPerAcquisition)], -1)
                            pi_amp_curr2 = mean(
                                ss_cal_e[0, 0, 1, (ii * avgPerAcquisition):((ii + 1) * avgPerAcquisition)], -1)

                            expt_avg_data = (expt_avg_data - zero_amp_avg) / (pi_amp_avg - zero_amp_avg)
                            expt_avg_data2 = (expt_avg_data2 - zero_amp_avg2) / (pi_amp_avg2 - zero_amp_avg2)

                    # this needs to stay here
                    # if self.data_file != None:
                    #     self.slab_file = SlabFile(self.data_file)
                    # else:
                    #     self.slab_file = self.datafile()
                    self.slab_file = self.datafile(data_file=self.data_file)

                    with self.slab_file as f:
                        f.add('ss_data', ss_data[:, :, :, :, 0:((ii + 1) * avgPerAcquisition)])
                        f.add('single_record1', single_record1)
                        f.add('single_record2', single_record2)

                        if (self.cfg[self.expt_cfg_name]['use_pi_calibration']):
                            f.add('ss_cal_g', ss_cal_g[:, :, :, 0:((ii + 1) * avgPerAcquisition)])
                            f.add('ss_cal_e', ss_cal_e[:, :, :, 0:((ii + 1) * avgPerAcquisition)])
                        if (self.cfg[self.expt_cfg_name]['use_g-e-f_calibration']):
                            f.add('ss_cal_g', ss_cal_g[:, :, :, 0:((ii + 1) * avgPerAcquisition)])
                            f.add('ss_cal_e', ss_cal_e[:, :, :, 0:((ii + 1) * avgPerAcquisition)])
                            f.add('ss_cal_f', ss_cal_f[:, :, :, 0:((ii + 1) * avgPerAcquisition)])

                        if self.cfg['readout']['save_trajectory_data']:
                            f.add('traj_data', traj_data[:, :, :, :, 0:((ii + 1) * avgPerAcquisition), :])
                            if (self.cfg[self.expt_cfg_name]['use_pi_calibration']):
                                f.add('traj_cal_g', traj_cal_g[:, :, :, 0:((ii + 1) * avgPerAcquisition), :])
                                f.add('traj_cal_e', traj_cal_e[:, :, :, 0:((ii + 1) * avgPerAcquisition), :])
                            if (self.cfg[self.expt_cfg_name]['use_g-e-f_calibration']):
                                f.add('traj_cal_g', traj_cal_g[:, :, :, 0:((ii + 1) * avgPerAcquisition), :])
                                f.add('traj_cal_e', traj_cal_e[:, :, :, 0:((ii + 1) * avgPerAcquisition), :])
                                f.add('traj_cal_f', traj_cal_f[:, :, :, 0:((ii + 1) * avgPerAcquisition), :])

                        f.add('expt_avg_data', expt_avg_data.flatten())
                        f.add('expt_avg_data2', expt_avg_data2.flatten())

                        f.add('expt_pts', self.expt_pts)
                        f.add('het_read_freq_list', het_read_freq_list)

                        if self.cfg[self.expt_cfg_name]['use_pi_calibration']:
                            f.append_pt('zero_amps', zero_amp_curr)
                            f.append_pt('pi_amps', pi_amp_curr)
                            f.append_pt('zero_amps2', zero_amp_curr2)
                            f.append_pt('pi_amps2', pi_amp_curr2)

                        f.close()

                    ####
                    if self.cfg["visdom_plot_livedata"]:
                        viz.updateTrace(X=self.expt_pts, Y=expt_avg_data, env=eid, win=win1, append=False)
                        viz.updateTrace(X=self.expt_pts, Y=expt_avg_data2, env=eid, win=win2, append=False)

                        # if (self.cfg[self.expt_cfg_name]['use_pi_calibration']):
                        #
                        #     f_idx = 0
                        #     viz.scatter(X=array([ss_cal_g[0, f_idx, 0, :],ss_cal_g[0, f_idx, 1, :]]).transpose(),
                        #                     Y=None, env=eid, win=win4, name='test')
                        #
                        # if (self.cfg[self.expt_cfg_name]['use_g-e-f_calibration']):
                        #     pass

                        if ii==0:
                            viz.updateTrace(X=array(range(len(single_record1))), Y=single_record2, env=eid, win=win3, name='2', append=False)
                            viz.updateTrace(X=array(range(len(single_record1))), Y=single_record1, env=eid, win=win3, name='1', append=False)
                    ####

            if self.post_run is not None:
                self.post_run(self.expt_pts, expt_avg_data)

            if self.cfg['stop_awgs'] == True:
                self.awg_prep()
                print('stop_awg = True, seqs stopped.')
            else:
                print('stop_awg = False, seqs left running.')

            # closes Alazar card and releases buffer
            adc.close()
    def go(self):
        if self.liveplot_enabled:
            self.plotter.clear()

        print("Prep Instruments")
        self.readout.set_frequency(self.cfg['readout']['frequency'])
        self.readout.set_power(self.cfg['readout']['power'])
        self.readout.set_ext_pulse(mod=True)
        # self.readout_shifter.set_phase(self.cfg['readout']['start_phase'] + self.cfg['readout']['phase_slope'] * (
        #     self.cfg['readout']['frequency'] - self.cfg['readout']['bare_frequency']), self.cfg['readout']['frequency'])

        self.drive.set_frequency(
            self.cfg['qubit']['frequency'] -
            self.cfg['pulse_info'][self.pulse_type]['iq_freq'])
        self.drive.set_power(self.cfg['drive']['power'])
        self.drive.set_ext_pulse(mod=True)
        self.drive.set_output(True)
        self.readout_atten.set_attenuator(self.cfg['readout']['dig_atten'])

        self.trigger.set_period(self.trigger_period)

        try:
            self.cfg['freq_flux']['flux'] = self.extra_args['flux']
        except:
            pass

        try:
            self.cfg['freq_flux']['freq_flux_slope'] = self.extra_args[
                'freq_flux_slope']
        except:
            pass

        try:
            self.cfg['freq_flux']['flux_offset'] += self.extra_args[
                'flux_offset']
        except:
            pass

        if self.cfg['freq_flux']['current']:
            try:
                self.flux_volt.ramp_current(self.cfg['freq_flux']['flux'])
            except:
                pass
        elif self.cfg['freq_flux']['voltage']:
            try:
                self.flux_volt.ramp_volt(self.cfg['freq_flux']['flux'])
            except:
                pass

        self.awg.set_amps_offsets(self.cfg['cal']['iq_amps'],
                                  self.cfg['cal']['iq_offsets'])

        if self.pre_run is not None:
            self.pre_run()

        if self.adc == None:
            print("Prep Card")
            adc = Alazar(self.cfg['alazar'])
        else:
            adc = self.adc

        expt_data = None
        current_data = None
        for ii in tqdm(
                arange(max(1,
                           self.cfg[self.expt_cfg_name]['averages'] / 100))):
            tpts, ch1_pts, ch2_pts = adc.acquire_avg_data_by_record(
                prep_function=self.awg_prep,
                start_function=self.awg.run,
                excise=self.cfg['readout']['window'])

            ### to skip the first 100 averages data point, because we saw a weird ramp in the RF output
            if ii == 0:
                time.sleep(0.1)
                continue

            mag = sqrt(ch1_pts**2 + ch2_pts**2)
            if not self.cfg[self.expt_cfg_name]['use_pi_calibration']:

                if expt_data is None:
                    if self.cfg['readout']['channel'] == 1:
                        expt_data = ch1_pts
                    elif self.cfg['readout']['channel'] == 2:
                        expt_data = ch2_pts
                else:
                    if self.cfg['readout']['channel'] == 1:
                        expt_data = (expt_data * ii + ch1_pts) / (ii + 1.0)
                    elif self.cfg['readout']['channel'] == 2:
                        expt_data = (expt_data * ii + ch2_pts) / (ii + 1.0)

                expt_avg_data = mean(expt_data, 1)

            else:

                if expt_data is None:
                    if self.cfg['readout']['channel'] == 1:
                        expt_data = ch1_pts
                    elif self.cfg['readout']['channel'] == 2:
                        expt_data = ch2_pts
                else:
                    if self.cfg['readout']['channel'] == 1:
                        expt_data = (expt_data * ii + ch1_pts) / (ii + 1.0)
                    elif self.cfg['readout']['channel'] == 2:
                        expt_data = (expt_data * ii + ch2_pts) / (ii + 1.0)

                expt_avg_data = mean(expt_data, 1)

                expt_avg_data = (expt_avg_data[:-2] - expt_avg_data[-2]) / (
                    expt_avg_data[-1] - expt_avg_data[-2])

            # temporary hack. Alazar card seems to shift two data points (problem disappeared after reboot computer)
            # expt_avg_data = np.concatenate((expt_avg_data[2:],expt_avg_data[:2]))
            # if self.cfg[self.expt_cfg_name]['use_pi_calibration']:
            #     expt_avg_data = (expt_avg_data[:-2]-expt_avg_data[-2])/(expt_avg_data[-1]-expt_avg_data[-2])

            # else:
            #     if self.cfg['readout']['channel']==1:
            #         zero_amp = mean(ch1_pts[-2])
            #         pi_amp = mean(ch1_pts[-1])
            #         current_data= (ch1_pts[:-2]-zero_amp)/(pi_amp-zero_amp)
            #     elif self.cfg['readout']['channel']==2:
            #         zero_amp = mean(ch2_pts[-2])
            #         pi_amp = mean(ch2_pts[-1])
            #         current_data= (ch2_pts[:-2]-zero_amp)/(pi_amp-zero_amp)
            #     if expt_data is None:
            #         expt_data = current_data
            #     else:
            #         expt_data = (expt_data * ii + current_data) / (ii + 1.0)

            # expt_avg_data = mean(expt_data, 1)

            if self.liveplot_enabled:
                self.plotter.plot_z(self.prefix + ' Data', expt_data.T)
                self.plotter.plot_xy(self.prefix + ' XY',
                                     self.pulse_sequence.expt_pts,
                                     expt_avg_data)

            # print ii * min(self.cfg[self.expt_cfg_name]['averages'], 100)

            if self.data_file == None:
                self.slab_file = self.datafile()
                with self.slab_file as f:
                    f.add('expt_2d', expt_data)
                    f.add('expt_avg_data', expt_avg_data)
                    f.add('expt_pts', self.expt_pts)
                    f.close()

        if self.data_file != None:
            self.slab_file = SlabFile(self.data_file)
            with self.slab_file as f:
                f.append_line('expt_avg_data', expt_avg_data)
                f.append_line('expt_pts', self.expt_pts)
                f.close()

        if not self.adc_predefined:
            adc.close()

        if self.post_run is not None:
            self.post_run(self.expt_pts, expt_avg_data)
    def take_data(self):

        ####
        if self.cfg["visdom_plot_livedata"]:

            viz = visdom.Visdom()
            assert viz.check_connection(), "Visdom server not connected!"
            # added two environments "seq_builder.json", and "live_plot.json" in C:\Users\slab\.visdom
            eid = "live_plot"
            viz.close(win=None, env=eid)

            win1 = viz.line(X=np.arange(0, 1),
                            Y=np.arange(0, 1),
                            env=eid,
                            opts=dict(height=400,
                                      width=700,
                                      title='expt_avg_data',
                                      showlegend=True,
                                      xlabel='expt_pts'))
            win2 = viz.line(X=np.arange(0, 1),
                            Y=np.arange(0, 1),
                            env=eid,
                            opts=dict(height=400,
                                      width=700,
                                      title='expt_avg_data2',
                                      showlegend=True,
                                      xlabel='expt_pts'))
            win3 = viz.line(X=np.arange(0, 1),
                            Y=np.arange(0, 1),
                            env=eid,
                            opts=dict(
                                height=400,
                                width=700,
                                title='single_record (of only first run)',
                                showlegend=True,
                                xlabel='time ns'))
        ####

        if self.pre_run is not None:
            self.pre_run()

        TEST_REDPITAYA = False

        if TEST_REDPITAYA:
            self.awg_run()

        if not TEST_REDPITAYA:

            if self.adc == None:
                print("Prep Card")
                adc = Alazar(self.cfg['alazar'])
            else:
                adc = self.adc

            # debug_alazer = False
            # if debug_alazer:
            #
            #     # save raw time trace, 100 runs only
            #     tpts, single_data1, single_data2 = adc.acquire_singleshot_data2()
            #
            #     self.slab_file = self.datafile(data_file=self.data_file)
            #     with self.slab_file as f:
            #         f.add('tpts', tpts)
            #         f.add('single_data1', single_data1)
            #         f.add('single_data2', single_data2)
            #         f.close()
            #
            #     # hack for post_run
            #     expt_avg_data = self.expt_pts
            #
            # else:

            if not self.cfg['readout']['save_single-shot_data']:

                expt_data = None
                current_data = None
                for ii in tqdm(
                        arange(
                            max(1, self.cfg[self.expt_cfg_name]['averages'] /
                                100))):
                    tpts, ch1_pts, ch2_pts = adc.acquire_avg_data_by_record(
                        prep_function=self.awg_prep,
                        start_function=self.awg_run,
                        excise=self.cfg['readout']['window'])

                    if not self.cfg[self.expt_cfg_name]['use_pi_calibration']:

                        if expt_data is None:
                            if self.cfg['readout']['channel'] == 1:
                                expt_data = ch1_pts
                            elif self.cfg['readout']['channel'] == 2:
                                expt_data = ch2_pts
                        else:
                            if self.cfg['readout']['channel'] == 1:
                                expt_data = (expt_data * ii + ch1_pts) / (ii +
                                                                          1.0)
                            elif self.cfg['readout']['channel'] == 2:
                                expt_data = (expt_data * ii + ch2_pts) / (ii +
                                                                          1.0)

                        if self.cfg['readout']['heterodyne_freq'] == 0:
                            # homodyne
                            expt_avg_data = mean(expt_data, 1)
                        else:
                            # heterodyne
                            heterodyne_freq = self.cfg['readout'][
                                'heterodyne_freq']
                            # ifft by numpy default has the correct 1/N normalization
                            expt_data_fft_amp = np.abs(np.fft.ifft(expt_data))
                            hetero_f_ind = int(
                                round(heterodyne_freq * tpts.size *
                                      1e-9))  # position in ifft

                            # todo: do the proper sum here
                            # expt_avg_data = np.average(expt_data_fft_amp[:, (hetero_f_ind - 1):(hetero_f_ind + 1)], axis=1)
                            expt_avg_data = expt_data_fft_amp[:, hetero_f_ind]

                    else:
                        # average first, then divide by pi_calibration values
                        if expt_data is None:
                            if self.cfg['readout']['channel'] == 1:
                                expt_data = ch1_pts[:-2]
                                zero_amp_curr = mean(ch1_pts[-2])
                                pi_amp_curr = mean(ch1_pts[-1])
                            elif self.cfg['readout']['channel'] == 2:
                                expt_data = ch2_pts[:-2]
                                zero_amp_curr = mean(ch2_pts[-2])
                                pi_amp_curr = mean(ch2_pts[-1])
                            zero_amp = zero_amp_curr
                            pi_amp = pi_amp_curr
                        else:
                            if self.cfg['readout']['channel'] == 1:
                                expt_data = (expt_data * ii +
                                             ch1_pts[:-2]) / (ii + 1.0)
                                zero_amp_curr = mean(ch1_pts[-2])
                                pi_amp_curr = mean(ch1_pts[-1])
                            elif self.cfg['readout']['channel'] == 2:
                                expt_data = (expt_data * ii +
                                             ch2_pts[:-2]) / (ii + 1.0)
                                zero_amp_curr = mean(ch2_pts[-2])
                                pi_amp_curr = mean(ch2_pts[-1])
                            zero_amp = (zero_amp * ii + zero_amp_curr) / (ii +
                                                                          1.0)
                            pi_amp = (pi_amp * ii + pi_amp_curr) / (ii + 1.0)

                        # todo: add heterodyne with pi_cal
                        expt_avg_data = mean(
                            (expt_data - zero_amp) / (pi_amp - zero_amp), 1)

                    # self.slab_file = self.datafile(data_file=self.data_file)

                    with self.slab_file as f:
                        f.add('expt_2d', expt_data)
                        f.add('expt_avg_data', expt_avg_data)
                        f.add('expt_pts', self.expt_pts)

                        # save pi_cal amps, to be able to monitor fluctuations
                        if self.cfg[self.expt_cfg_name]['use_pi_calibration']:
                            f.append_pt('zero_amps', zero_amp_curr)
                            f.append_pt('pi_amps', pi_amp_curr)

                        f.close()

            else:  # here saves all single shot data

                if self.prefix == 'Vacuum_Rabi':

                    print('vacuum_rabi')
                    het_IFreqList = [self.cfg['readout']['heterodyne_freq']]
                    het_read_freq_list = [0]

                elif self.cfg['readout']['is_multitone_heterodyne']:
                    het_carrier_freq = self.cfg['readout'][
                        'heterodyne_carrier_freq']
                    het_read_freq_list = array(
                        self.cfg['readout']['heterodyne_freq_list'])
                    het_IFreqList = het_read_freq_list - het_carrier_freq
                else:
                    het_carrier_freq = self.readout_freq - self.cfg['readout'][
                        'heterodyne_freq']
                    het_read_freq_list = array([self.readout_freq])
                    het_IFreqList = het_read_freq_list - het_carrier_freq

                avgPerAcquisition = int(
                    min(self.cfg[self.expt_cfg_name]['averages'], 100))
                numAcquisition = int(
                    np.ceil(self.cfg[self.expt_cfg_name]['averages'] / 100))

                # (ch1/2, exp_pts, heterodyne_freq, cos/sin, all averages)
                ss_data = zeros((2, len(self.expt_pts), len(het_IFreqList), 2,
                                 avgPerAcquisition * numAcquisition))

                # (ch1/2, heterodyne_freq, cos/sin, all averages)
                ss_cal_g = zeros((2, len(het_IFreqList), 2,
                                  avgPerAcquisition * numAcquisition))
                ss_cal_e = zeros((2, len(het_IFreqList), 2,
                                  avgPerAcquisition * numAcquisition))
                ss_cal_f = zeros((2, len(het_IFreqList), 2,
                                  avgPerAcquisition * numAcquisition))

                if self.cfg['readout']['save_trajectory_data']:

                    # (ch1/2, exp_pts, heterodyne_freq, cos/sin, all averages, traj)
                    traj_data = zeros(
                        (2, len(self.expt_pts), len(het_IFreqList), 2,
                         avgPerAcquisition * numAcquisition,
                         self.cfg['readout']['pts_per_traj']))

                    # (ch1/2, heterodyne_freq, cos/sin, all averages, traj)
                    traj_cal_g = zeros((2, len(het_IFreqList), 2,
                                        avgPerAcquisition * numAcquisition,
                                        self.cfg['readout']['pts_per_traj']))
                    traj_cal_e = zeros((2, len(het_IFreqList), 2,
                                        avgPerAcquisition * numAcquisition,
                                        self.cfg['readout']['pts_per_traj']))
                    traj_cal_f = zeros((2, len(het_IFreqList), 2,
                                        avgPerAcquisition * numAcquisition,
                                        self.cfg['readout']['pts_per_traj']))

                for ii in tqdm(arange(numAcquisition)):

                    if not self.cfg['readout']['is_hetero_phase_ref']:

                        # single_data1/2: index: (hetero_freqs, cos/sin, all_seqs)
                        single_data1, single_data2, single_record1, single_record2 = \
                            adc.acquire_singleshot_heterodyne_multitone_data(het_IFreqList, prep_function=self.awg_prep,
                                                                             start_function=self.awg_run,
                                                                             excise=self.cfg['readout']['window'])

                        # saving the raw time traces
                        # single_data1, single_data2, single_record1, single_record2 = \
                        #     adc.acquire_singleshot_heterodyne_multitone_data(het_IFreqList, prep_function=self.awg_prep,
                        #                                                      start_function=self.awg_run,
                        #                                                      excise=None, save_raw_data=True)

                    elif self.cfg['readout']['save_trajectory_data']:

                        excise = [0, self.cfg['readout']['window'][1]]
                        single_data1, single_data2, single_record1, single_record2, single_traj1, single_traj2= \
                            adc.acquire_singleshot_heterodyne_multitone_data_phase_ref_save_traj(het_IFreqList,
                                                                                       self.cfg['readout'][
                                                                                           'hetero_phase_ref_freq'],
                                                                                       prep_function=self.awg_prep,
                                                                                       start_function=self.awg_run,
                                                                                       excise=excise,
                                                                                       isCompensatePhase=True,
                                                                                       save_raw_data=False, pts_per_traj=self.cfg['readout']['pts_per_traj'])
                    else:

                        # single_data1/2: index: (hetero_freqs, cos/sin, all_seqs)
                        single_data1, single_data2, single_record1, single_record2 = \
                            adc.acquire_singleshot_heterodyne_multitone_data_phase_ref(het_IFreqList,
                                                                             self.cfg['readout']['hetero_phase_ref_freq'],
                                                                             prep_function=self.awg_prep,
                                                                             start_function=self.awg_run,
                                                                             excise=self.cfg['readout']['window'],
                                                                             isCompensatePhase=True,
                                                                             save_raw_data=False)

                    single_data = array([single_data1, single_data2])
                    # index: (ch1/2, hetero_freqs, cos / sin, avgs, seq(exp_pts))
                    single_data = np.reshape(
                        single_data,
                        (single_data.shape[0], single_data.shape[1],
                         single_data.shape[2],
                         int(self.cfg['alazar']['recordsPerAcquisition'] /
                             self.pulse_sequence.sequence_length),
                         self.pulse_sequence.sequence_length))

                    # index: (ch1/2, exp_pts, hetero_freqs, cos / sin, avgs)
                    single_data = np.transpose(single_data, (0, 4, 1, 2, 3))

                    if (self.cfg[self.expt_cfg_name]['use_g-e-f_calibration']):

                        ss_data[:, :, :, :, (ii * avgPerAcquisition):(
                            (ii + 1) * avgPerAcquisition)] = single_data[:,
                                                                         0:-3]
                        ss_cal_g[:, :, :, (ii * avgPerAcquisition):(
                            (ii + 1) * avgPerAcquisition)] = single_data[:, -3]
                        ss_cal_e[:, :, :, (ii * avgPerAcquisition):(
                            (ii + 1) * avgPerAcquisition)] = single_data[:, -2]
                        ss_cal_f[:, :, :, (ii * avgPerAcquisition):(
                            (ii + 1) * avgPerAcquisition)] = single_data[:, -1]

                    elif (self.cfg[self.expt_cfg_name]['use_pi_calibration']):

                        ss_data[:, :, :, :, (ii * avgPerAcquisition):(
                            (ii + 1) * avgPerAcquisition)] = single_data[:,
                                                                         0:-2]
                        ss_cal_g[:, :, :, (ii * avgPerAcquisition):(
                            (ii + 1) * avgPerAcquisition)] = single_data[:, -2]
                        ss_cal_e[:, :, :, (ii * avgPerAcquisition):(
                            (ii + 1) * avgPerAcquisition)] = single_data[:, -1]

                    else:
                        ss_data[:, :, :, :, (ii * avgPerAcquisition):(
                            (ii + 1) * avgPerAcquisition)] = single_data[:, :]

                    ####
                    if self.cfg['readout']['save_trajectory_data']:

                        # index: (hetero_freqs, cos/sin, all_seqs, traj)
                        single_traj = array([single_traj1, single_traj2])

                        # index: (ch1/2, hetero_freqs, cos / sin, avgs, seq(exp_pts), traj)
                        single_traj = np.reshape(
                            single_traj,
                            (single_traj.shape[0], single_traj.shape[1],
                             single_traj.shape[2],
                             self.cfg['alazar']['recordsPerAcquisition'] /
                             self.pulse_sequence.sequence_length,
                             self.pulse_sequence.sequence_length,
                             single_traj.shape[-1]))

                        # index: (ch1/2, exp_pts, hetero_freqs, cos / sin, avgs, traj)
                        single_traj = np.transpose(single_traj,
                                                   (0, 4, 1, 2, 3, 5))
                        # print single_traj.shape

                        if (self.cfg[self.expt_cfg_name]
                            ['use_g-e-f_calibration']):

                            traj_data[:, :, :, :, (ii * avgPerAcquisition):(
                                (ii + 1) *
                                avgPerAcquisition), :] = single_traj[:,
                                                                     0:-3, :]
                            traj_cal_g[:, :, :, (ii * avgPerAcquisition):(
                                (ii + 1) *
                                avgPerAcquisition), :] = single_traj[:, -3, :]
                            traj_cal_e[:, :, :, (ii * avgPerAcquisition):(
                                (ii + 1) *
                                avgPerAcquisition), :] = single_traj[:, -2, :]
                            traj_cal_f[:, :, :, (ii * avgPerAcquisition):(
                                (ii + 1) *
                                avgPerAcquisition), :] = single_traj[:, -1, :]

                        elif (self.cfg[self.expt_cfg_name]
                              ['use_pi_calibration']):

                            traj_data[:, :, :, :, (ii * avgPerAcquisition):(
                                (ii + 1) *
                                avgPerAcquisition), :] = single_traj[:,
                                                                     0:-2, :]
                            traj_cal_g[:, :, :, (ii * avgPerAcquisition):(
                                (ii + 1) *
                                avgPerAcquisition), :] = single_traj[:, -2, :]
                            traj_cal_e[:, :, :, (ii * avgPerAcquisition):(
                                (ii + 1) *
                                avgPerAcquisition), :] = single_traj[:, -1, :]

                        else:
                            traj_data[:, :, :, :, (ii * avgPerAcquisition):(
                                (ii + 1) *
                                avgPerAcquisition), :] = single_traj[:, :, :]

                    ####

                    # old way for easy plotting
                    # only calculate for 1st het_readout_freq
                    if het_IFreqList[0] == 0:
                        # take cos of ch1/ch2
                        expt_avg_data = mean(
                            ss_data[0, :, 0, 0,
                                    0:((ii + 1) * avgPerAcquisition)], -1)
                        expt_avg_data2 = mean(
                            ss_data[1, :, 0, 0,
                                    0:((ii + 1) * avgPerAcquisition)], -1)

                        if self.cfg[self.expt_cfg_name]['use_pi_calibration']:
                            zero_amp_avg = mean(
                                ss_cal_g[0, 0, 0,
                                         0:((ii + 1) * avgPerAcquisition)], -1)
                            zero_amp_avg2 = mean(
                                ss_cal_g[1, 0, 0,
                                         0:((ii + 1) * avgPerAcquisition)], -1)
                            pi_amp_avg = mean(
                                ss_cal_e[0, 0, 0,
                                         0:((ii + 1) * avgPerAcquisition)], -1)
                            pi_amp_avg2 = mean(
                                ss_cal_e[1, 0, 0,
                                         0:((ii + 1) * avgPerAcquisition)], -1)

                            zero_amp_curr = mean(
                                ss_cal_g[0, 0, 0, (ii * avgPerAcquisition):(
                                    (ii + 1) * avgPerAcquisition)], -1)
                            zero_amp_curr2 = mean(
                                ss_cal_g[1, 0, 0, (ii * avgPerAcquisition):(
                                    (ii + 1) * avgPerAcquisition)], -1)
                            pi_amp_curr = mean(
                                ss_cal_e[0, 0, 0, (ii * avgPerAcquisition):(
                                    (ii + 1) * avgPerAcquisition)], -1)
                            pi_amp_curr2 = mean(
                                ss_cal_e[1, 0, 0, (ii * avgPerAcquisition):(
                                    (ii + 1) * avgPerAcquisition)], -1)

                            expt_avg_data = (expt_avg_data - zero_amp_avg) / (
                                pi_amp_avg - zero_amp_avg)
                            expt_avg_data2 = (expt_avg_data2 - zero_amp_avg2
                                              ) / (pi_amp_avg2 - zero_amp_avg2)

                    else:
                        # take cos/sin of ch1
                        expt_avg_data = mean(
                            ss_data[0, :, 0, 0,
                                    0:((ii + 1) * avgPerAcquisition)], -1)
                        expt_avg_data2 = mean(
                            ss_data[0, :, 0, 1,
                                    0:((ii + 1) * avgPerAcquisition)], -1)

                        if self.cfg[self.expt_cfg_name]['use_pi_calibration']:
                            zero_amp_avg = mean(
                                ss_cal_g[0, 0, 0,
                                         0:((ii + 1) * avgPerAcquisition)], -1)
                            zero_amp_avg2 = mean(
                                ss_cal_g[0, 0, 1,
                                         0:((ii + 1) * avgPerAcquisition)], -1)
                            pi_amp_avg = mean(
                                ss_cal_e[0, 0, 0,
                                         0:((ii + 1) * avgPerAcquisition)], -1)
                            pi_amp_avg2 = mean(
                                ss_cal_e[0, 0, 1,
                                         0:((ii + 1) * avgPerAcquisition)], -1)

                            zero_amp_curr = mean(
                                ss_cal_g[0, 0, 0, (ii * avgPerAcquisition):(
                                    (ii + 1) * avgPerAcquisition)], -1)
                            zero_amp_curr2 = mean(
                                ss_cal_g[0, 0, 1, (ii * avgPerAcquisition):(
                                    (ii + 1) * avgPerAcquisition)], -1)
                            pi_amp_curr = mean(
                                ss_cal_e[0, 0, 0, (ii * avgPerAcquisition):(
                                    (ii + 1) * avgPerAcquisition)], -1)
                            pi_amp_curr2 = mean(
                                ss_cal_e[0, 0, 1, (ii * avgPerAcquisition):(
                                    (ii + 1) * avgPerAcquisition)], -1)

                            expt_avg_data = (expt_avg_data - zero_amp_avg) / (
                                pi_amp_avg - zero_amp_avg)
                            expt_avg_data2 = (expt_avg_data2 - zero_amp_avg2
                                              ) / (pi_amp_avg2 - zero_amp_avg2)

                    # this needs to stay here
                    # if self.data_file != None:
                    #     self.slab_file = SlabFile(self.data_file)
                    # else:
                    #     self.slab_file = self.datafile()
                    self.slab_file = self.datafile(data_file=self.data_file)

                    with self.slab_file as f:
                        f.add(
                            'ss_data',
                            ss_data[:, :, :, :,
                                    0:((ii + 1) * avgPerAcquisition)])
                        f.add('single_record1', single_record1)
                        f.add('single_record2', single_record2)

                        if (self.cfg[self.expt_cfg_name]['use_pi_calibration']
                            ):
                            f.add(
                                'ss_cal_g',
                                ss_cal_g[:, :, :,
                                         0:((ii + 1) * avgPerAcquisition)])
                            f.add(
                                'ss_cal_e',
                                ss_cal_e[:, :, :,
                                         0:((ii + 1) * avgPerAcquisition)])
                        if (self.cfg[self.expt_cfg_name]
                            ['use_g-e-f_calibration']):
                            f.add(
                                'ss_cal_g',
                                ss_cal_g[:, :, :,
                                         0:((ii + 1) * avgPerAcquisition)])
                            f.add(
                                'ss_cal_e',
                                ss_cal_e[:, :, :,
                                         0:((ii + 1) * avgPerAcquisition)])
                            f.add(
                                'ss_cal_f',
                                ss_cal_f[:, :, :,
                                         0:((ii + 1) * avgPerAcquisition)])

                        if self.cfg['readout']['save_trajectory_data']:
                            f.add(
                                'traj_data',
                                traj_data[:, :, :, :,
                                          0:((ii + 1) * avgPerAcquisition), :])
                            if (self.cfg[self.expt_cfg_name]
                                ['use_pi_calibration']):
                                f.add(
                                    'traj_cal_g',
                                    traj_cal_g[:, :, :,
                                               0:((ii + 1) *
                                                  avgPerAcquisition), :])
                                f.add(
                                    'traj_cal_e',
                                    traj_cal_e[:, :, :,
                                               0:((ii + 1) *
                                                  avgPerAcquisition), :])
                            if (self.cfg[self.expt_cfg_name]
                                ['use_g-e-f_calibration']):
                                f.add(
                                    'traj_cal_g',
                                    traj_cal_g[:, :, :,
                                               0:((ii + 1) *
                                                  avgPerAcquisition), :])
                                f.add(
                                    'traj_cal_e',
                                    traj_cal_e[:, :, :,
                                               0:((ii + 1) *
                                                  avgPerAcquisition), :])
                                f.add(
                                    'traj_cal_f',
                                    traj_cal_f[:, :, :,
                                               0:((ii + 1) *
                                                  avgPerAcquisition), :])

                        f.add('expt_avg_data', expt_avg_data.flatten())
                        f.add('expt_avg_data2', expt_avg_data2.flatten())

                        f.add('expt_pts', self.expt_pts)
                        f.add('het_read_freq_list', het_read_freq_list)

                        if self.cfg[self.expt_cfg_name]['use_pi_calibration']:
                            f.append_pt('zero_amps', zero_amp_curr)
                            f.append_pt('pi_amps', pi_amp_curr)
                            f.append_pt('zero_amps2', zero_amp_curr2)
                            f.append_pt('pi_amps2', pi_amp_curr2)

                        f.close()

                    ####
                    if self.cfg["visdom_plot_livedata"]:
                        viz.updateTrace(X=self.expt_pts,
                                        Y=expt_avg_data,
                                        env=eid,
                                        win=win1,
                                        append=False)
                        viz.updateTrace(X=self.expt_pts,
                                        Y=expt_avg_data2,
                                        env=eid,
                                        win=win2,
                                        append=False)
                        if ii == 0:
                            viz.updateTrace(X=array(range(
                                len(single_record1))),
                                            Y=single_record2,
                                            env=eid,
                                            win=win3,
                                            name='2',
                                            append=False)
                            viz.updateTrace(X=array(range(
                                len(single_record1))),
                                            Y=single_record1,
                                            env=eid,
                                            win=win3,
                                            name='1',
                                            append=False)
                    ####

            if self.post_run is not None:
                self.post_run(self.expt_pts, expt_avg_data)

            if self.cfg['stop_awgs'] == True:
                self.awg_prep()
                print('stop_awg = True, seqs stopped.')
            else:
                print('stop_awg = False, seqs left running.')

            # closes Alazar card and releases buffer
            adc.close()
    def go(self):
        # if self.liveplot_enabled:
        #     self.plotter.clear()

        print("Prep Instruments")

        try:
            self.readout.set_frequency(self.readout_freq)
            self.readout.set_power(self.cfg['readout']['power'])
            self.readout.set_ext_pulse(mod=True)
            self.readout.set_output(True)
        except:
            print("No readout found.")

        try:
            self.readout_shifter.set_phase(self.cfg['readout']['start_phase'] + self.cfg['readout']['phase_slope'] * (
                self.cfg['readout']['frequency'] - self.cfg['readout']['bare_frequency']), self.cfg['readout']['frequency'])
        except:
            print("Digital phase shifter not loaded.")

        try:
            self.drive.set_frequency(self.cfg['qubit']['frequency'] - self.cfg['pulse_info'][self.pulse_type]['iq_freq'])
            self.drive.set_power(self.cfg['drive']['power'])
            self.drive.set_ext_pulse(mod=False)
            self.drive.set_output(True)
        except:
            print("No drive found")

        try:
            self.flux_drive.set_frequency(self.cfg['sidebands']['blue'] + self.cfg['flux_pulse_info'][self.pulse_type]['iq_freq'])
            self.flux_drive.set_power(self.cfg['drive']['power'])
            self.flux_drive.set_ext_pulse(mod=False)
            self.flux_drive.set_output(False)
        except:
            print("No flux drive found")

        try:
            self.readout_atten.set_attenuator(self.cfg['readout']['dig_atten'])
        except:
            print("Digital attenuator not loaded.")

        try:
            self.cfg['freq_flux']['flux']=self.extra_args['flux']
        except:
            pass

        try:
            self.cfg['freq_flux']['freq_flux_slope']=self.extra_args['freq_flux_slope']
        except:
            pass

        try:
            self.cfg['freq_flux']['flux_offset']+=self.extra_args['flux_offset']
        except:
            pass


        try:
            if self.cfg['freq_flux']['current']:
                self.flux_volt.ramp_current(self.cfg['freq_flux']['flux'])
            elif self.cfg['freq_flux']['voltage']:
                self.flux_volt.ramp_volt(self.cfg['freq_flux']['flux'])

        except:
            print("Voltage source not loaded.")

        try:
            self.awg.set_amps_offsets(self.cfg['cal']['iq_amps'], self.cfg['cal']['iq_offsets'])
        except:
            print("self.awg not loaded.")

        if self.pre_run is not None:
            self.pre_run()

        # What is this for?
        TEST_REDPITAYA = False

        if TEST_REDPITAYA:
            self.awg_run()


        if not TEST_REDPITAYA:

            if self.cfg['readout']['adc'] == 'redpitaya':
                print("Using Red Pitaya ADC")

                if not self.cfg[self.expt_cfg_name]['use_pi_calibration']:
                    num_experiments = len(self.pulse_sequence.expt_pts)
                else:
                    num_experiments = len(self.pulse_sequence.expt_pts) + 2

                testing_redpitaya = False

                if not testing_redpitaya:
                    expt_avg_data_raw1, expt_avg_data_raw2 = setup_redpitaya_adc(num_experiments=num_experiments,
                                                            window=self.cfg['readout']['window'],
                                                            shots=self.cfg[self.expt_cfg_name]['averages'],
                                                            plot_data=self.cfg['readout']['plot'],start_function=self.awg_run,stop_function=self.awg_prep)
                    if self.cfg['readout']['channel'] == 1:
                        expt_avg_data_raw = expt_avg_data_raw1
                    else:
                        expt_avg_data_raw = expt_avg_data_raw2

                ## This is for testing the redpitaya behaviour
                if testing_redpitaya:
                    correct_counter = 0
                    repeat = 100
                    for ii in range(repeat):
                        expt_avg_data_raw = setup_redpitaya_adc(num_experiments=num_experiments,
                                                                window=self.cfg['readout']['window'],
                                                                shots=self.cfg[self.expt_cfg_name]['averages'],
                                                                plot_data=False,start_function=self.awg_run,stop_function=self.awg_prep())
                        if ((expt_avg_data_raw[1:] - expt_avg_data_raw[:-1]) > 0).all():
                            correct_counter += 1

                    print("correct percent: " + str(correct_counter / float(repeat)))

                if self.cfg[self.expt_cfg_name]['use_pi_calibration']:
                    zero_amp = expt_avg_data_raw[-2]
                    pi_amp = expt_avg_data_raw[-1]
                    expt_avg_data = (expt_avg_data_raw[:-2] - zero_amp) / (pi_amp - zero_amp)

                    # #saves pi calibration data
                    # expt_avg_data = expt_avg_data_raw
                else:
                    expt_avg_data = expt_avg_data_raw

                print(shape(expt_avg_data))
                expt_avg_data = expt_avg_data.flatten()
                print(shape(expt_avg_data))

                if self.data_file != None:
                    self.slab_file = SlabFile(self.data_file)

                    with self.slab_file as f:
                        f.append_line('expt_avg_data', expt_avg_data)
                        f.append_line('expt_pts', self.expt_pts)
                        f.close()

                else:
                    self.slab_file = self.datafile()

                    with self.slab_file as f:
                        f.add('expt_avg_data', expt_avg_data)
                        f.add('expt_pts', self.expt_pts)
                        f.close()

            else:
                if self.adc==None:
                    print("Prep Card")
                    adc = Alazar(self.cfg['alazar'])
                else:
                    adc = self.adc

                expt_data = None
                current_data = None
                for ii in tqdm(arange(max(1, self.cfg[self.expt_cfg_name]['averages'] / 100))):
                    tpts, ch1_pts, ch2_pts = adc.acquire_avg_data_by_record(prep_function=self.awg_prep,
                                                                            start_function=self.awg_run,
                                                                            excise=self.cfg['readout']['window'])

                    mag = sqrt(ch1_pts**2+ch2_pts**2)
                    if not self.cfg[self.expt_cfg_name]['use_pi_calibration']:

                        if expt_data is None:
                            if self.cfg['readout']['channel']==1:
                                expt_data = ch1_pts
                            elif self.cfg['readout']['channel']==2:
                                expt_data = ch2_pts
                        else:
                            if self.cfg['readout']['channel']==1:
                                expt_data = (expt_data * ii + ch1_pts) / (ii + 1.0)
                            elif self.cfg['readout']['channel']==2:
                                expt_data = (expt_data * ii + ch2_pts) / (ii + 1.0)


                    else:
                        if self.cfg['readout']['channel']==1:
                            zero_amp = mean(ch1_pts[-2])
                            pi_amp = mean(ch1_pts[-1])
                            current_data= (ch1_pts[:-2]-zero_amp)/(pi_amp-zero_amp)
                        elif self.cfg['readout']['channel']==2:
                            zero_amp = mean(ch2_pts[-2])
                            pi_amp = mean(ch2_pts[-1])

                            current_data= (ch2_pts[:-2]-zero_amp)/(pi_amp-zero_amp)
                        if expt_data is None:
                            expt_data = current_data
                        else:
                            expt_data = (expt_data * ii + current_data) / (ii + 1.0)


                    expt_avg_data = mean(expt_data, 1)


                # if self.liveplot_enabled:
                #     self.plotter.plot_z(self.prefix + ' Data', expt_data.T)
                #     self.plotter.plot_xy(self.prefix + ' XY', self.pulse_sequence.expt_pts, expt_avg_data)

                # print ii * min(self.cfg[self.expt_cfg_name]['averages'], 100)

                if self.data_file != None:
                    self.slab_file = SlabFile(self.data_file)
                else:
                    self.slab_file = self.datafile()
                with self.slab_file as f:
                    f.add('expt_2d', expt_data)
                    f.add('expt_avg_data', expt_avg_data)
                    f.add('expt_pts', self.expt_pts)
                    f.close()

            # if self.data_file != None:
            #     self.slab_file = SlabFile(self.data_file)
            #     with self.slab_file as f:
            #         f.append_line('expt_avg_data', expt_avg_data)
            #         f.append_line('expt_pts', self.expt_pts)
            #         f.close()



            if self.post_run is not None:
                self.post_run(self.expt_pts, expt_avg_data)
    def go(self):
        #self.plotter.clear()

        print("Prep Instruments")
        self.readout.set_frequency(self.cfg['readout']['frequency'])
        self.readout.set_power(self.cfg['readout']['power'])
        self.readout.set_ext_pulse(mod=True)


        self.drive.set_frequency(self.cfg['qubit']['frequency'] - self.cfg['pulse_info'][self.pulse_type]['iq_freq'])
        self.drive.set_power(self.cfg[self.expt_cfg_name]['power'])
        self.drive.set_ext_pulse(mod=True)
        self.drive.set_output(True)

        self.awg.set_amps_offsets(self.cfg['cal']['iq_amps'], self.cfg['cal']['iq_offsets'])

        print("Prep Card")
        adc = Alazar(self.cfg['alazar'])

        attenpts = arange(self.cfg[self.expt_cfg_name]['atten_start'], self.cfg[self.expt_cfg_name]['atten_stop'], self.cfg[self.expt_cfg_name]['atten_step'])
        freqpts = arange(self.cfg[self.expt_cfg_name]['freq_start'], self.cfg[self.expt_cfg_name]['freq_stop'], self.cfg[self.expt_cfg_name]['freq_step'])
        num_bins = self.cfg[self.expt_cfg_name]['num_bins']

        for xx, atten in enumerate(attenpts):
            self.im.atten.set_attenuator(atten)
            max_contrast_data_ch1 = zeros(len(freqpts))
            max_contrast_data_ch2 = zeros(len(freqpts))
            #self.plotter.clear('max contrast')

            print("atten at: %s" %atten)

            for yy, freq in enumerate(freqpts):
                self.readout.set_frequency(freq)
                #self.readout_shifter.set_phase(self.cfg['readout']['start_phase'] , freq)
                # self.readout_shifter.set_phase((self.cfg['readout']['start_phase'] + self.cfg['readout']['phase_slope'] * (freq - self.cfg['readout']['frequency']))%360, freq)
                tpts, ch1_pts, ch2_pts = adc.acquire_avg_data_by_record(prep_function=self.awg.stop_and_prep, start_function=self.awg.run,excise=self.cfg['readout']['window'])
                # self.plotter.plot_z("current",ch1_pts)
                # with self.datafile() as f:
                #     f.append('time_trace', ch1_pts)
                ss_data = zeros((len(self.expt_pts), num_bins))
                sss_data = zeros((len(self.expt_pts), num_bins))

                ss1, ss2 = adc.acquire_singleshot_data(prep_function=self.awg.stop_and_prep, start_function=self.awg.run,
                                                       excise=self.cfg['readout']['window'])

                #with self.datafile() as f:
                #    f.append_line('ss1', ss1)
                #    f.append_line('ss2', ss2)


                ss1 = reshape(ss1, (int(self.cfg['alazar']['recordsPerAcquisition'] / len(self.expt_pts)), len(self.expt_pts))).T
                histo_range = (ss1.min() / 1.05, ss1.max() * 1.05)
                for jj, ss in enumerate(ss1):
                    sshisto, ssbins = np.histogram(ss, bins=num_bins, range=histo_range)
                    ss_data[jj] += sshisto
                    sss_data[jj] = cumsum(ss_data[[jj]])
                #     self.plotter.plot_xy('histogram %d' % jj, ssbins[:-1], ss_data[jj])
                #     self.plotter.plot_xy('cum histo %d' % jj, ssbins[:-1], sss_data[jj])

                max_contrast_data_ch1[yy] = abs(((sss_data[0] - sss_data[1]) / ss_data[0].sum())).max()

                ss2 = reshape(ss2, (int(self.cfg['alazar']['recordsPerAcquisition'] / len(self.expt_pts)), len(self.expt_pts))).T
                histo_range = (ss2.min() / 1.05, ss2.max() * 1.05)
                for jj, ss in enumerate(ss2):
                    sshisto, ssbins = np.histogram(ss, bins=num_bins, range=histo_range)
                    ss_data[jj] += sshisto
                    sss_data[jj] = cumsum(ss_data[[jj]])
                    #self.plotter.plot_xy('histogram %d' % jj, ssbins[:-1], ss_data[jj])
                    #self.plotter.plot_xy('cum histo %d' % jj, ssbins[:-1], sss_data[jj])

                #self.plotter.plot_xy('contrast', ssbins[:-1], abs(sss_data[0] - sss_data[1]) / ss_data[0].sum())
                max_contrast_data_ch2[yy] = abs(((sss_data[0] - sss_data[1]) / ss_data[0].sum())).max()
                #self.plotter.append_xy('max contrast', freq, max_contrast_data[yy])
            if len(attenpts)>1:
                print("plotting max contrast 2")
                pass
                # self.plotter.append_z('max contrast 2', max_contrast_data, start_step=(
                #  (attenpts[0], attenpts[1] - attenpts[0]),(freqpts[0] / 1.0e9, (freqpts[1] - freqpts[0]) / 1.0e9)))
            with self.datafile() as f:
                f.append_pt('atten', atten)
                f.append_line('freq', freqpts)
                f.append_line('max_contrast_data_ch1', max_contrast_data_ch1)
                f.append_line('max_contrast_data_ch2', max_contrast_data_ch2)
Beispiel #7
0
    def go(self):
        # if self.liveplot_enabled:
        #     self.plotter.clear()

        print("Prep Instruments")

        try:
            self.readout.set_frequency(self.readout_freq)
            self.readout.set_power(self.cfg['readout']['power'])
            self.readout.set_ext_pulse(mod=True)
            self.readout.set_output(True)
        except:
            print("No readout found.")

        try:
            self.readout_shifter.set_phase(
                self.cfg['readout']['start_phase'] +
                self.cfg['readout']['phase_slope'] *
                (self.cfg['readout']['frequency'] -
                 self.cfg['readout']['bare_frequency']),
                self.cfg['readout']['frequency'])
        except:
            print("Digital phase shifter not loaded.")

        try:
            self.drive.set_frequency(
                self.cfg['qubit']['frequency'] -
                self.cfg['pulse_info'][self.pulse_type]['iq_freq'])
            self.drive.set_power(self.cfg['drive']['power'])
            self.drive.set_ext_pulse(mod=False)
            self.drive.set_output(True)
        except:
            print("No drive found")

        try:
            self.flux_drive.set_frequency(
                self.cfg['sidebands']['blue'] +
                self.cfg['flux_pulse_info'][self.pulse_type]['iq_freq'])
            self.flux_drive.set_power(self.cfg['drive']['power'])
            self.flux_drive.set_ext_pulse(mod=False)
            self.flux_drive.set_output(False)
        except:
            print("No flux drive found")

        try:
            self.readout_atten.set_attenuator(self.cfg['readout']['dig_atten'])
        except:
            print("Digital attenuator not loaded.")

        try:
            self.cfg['freq_flux']['flux'] = self.extra_args['flux']
        except:
            pass

        try:
            self.cfg['freq_flux']['freq_flux_slope'] = self.extra_args[
                'freq_flux_slope']
        except:
            pass

        try:
            self.cfg['freq_flux']['flux_offset'] += self.extra_args[
                'flux_offset']
        except:
            pass

        try:
            if self.cfg['freq_flux']['current']:
                self.flux_volt.ramp_current(self.cfg['freq_flux']['flux'])
            elif self.cfg['freq_flux']['voltage']:
                self.flux_volt.ramp_volt(self.cfg['freq_flux']['flux'])

        except:
            print("Voltage source not loaded.")

        try:
            self.awg.set_amps_offsets(self.cfg['cal']['iq_amps'],
                                      self.cfg['cal']['iq_offsets'])
        except:
            print("self.awg not loaded.")

        if self.pre_run is not None:
            self.pre_run()

        # What is this for?
        TEST_REDPITAYA = False

        if TEST_REDPITAYA:
            self.awg_run()

        if not TEST_REDPITAYA:

            if self.cfg['readout']['adc'] == 'redpitaya':
                print("Using Red Pitaya ADC")

                if not self.cfg[self.expt_cfg_name]['use_pi_calibration']:
                    num_experiments = len(self.pulse_sequence.expt_pts)
                else:
                    num_experiments = len(self.pulse_sequence.expt_pts) + 2

                testing_redpitaya = False

                if not testing_redpitaya:
                    expt_avg_data_raw1, expt_avg_data_raw2 = setup_redpitaya_adc(
                        num_experiments=num_experiments,
                        window=self.cfg['readout']['window'],
                        shots=self.cfg[self.expt_cfg_name]['averages'],
                        plot_data=self.cfg['readout']['plot'],
                        start_function=self.awg_run,
                        stop_function=self.awg_prep)
                    if self.cfg['readout']['channel'] == 1:
                        expt_avg_data_raw = expt_avg_data_raw1
                    else:
                        expt_avg_data_raw = expt_avg_data_raw2

                ## This is for testing the redpitaya behaviour
                if testing_redpitaya:
                    correct_counter = 0
                    repeat = 100
                    for ii in range(repeat):
                        expt_avg_data_raw = setup_redpitaya_adc(
                            num_experiments=num_experiments,
                            window=self.cfg['readout']['window'],
                            shots=self.cfg[self.expt_cfg_name]['averages'],
                            plot_data=False,
                            start_function=self.awg_run,
                            stop_function=self.awg_prep())
                        if ((expt_avg_data_raw[1:] - expt_avg_data_raw[:-1]) >
                                0).all():
                            correct_counter += 1

                    print("correct percent: " +
                          str(correct_counter / float(repeat)))

                if self.cfg[self.expt_cfg_name]['use_pi_calibration']:
                    zero_amp = expt_avg_data_raw[-2]
                    pi_amp = expt_avg_data_raw[-1]
                    expt_avg_data = (expt_avg_data_raw[:-2] -
                                     zero_amp) / (pi_amp - zero_amp)

                    # #saves pi calibration data
                    # expt_avg_data = expt_avg_data_raw
                else:
                    expt_avg_data = expt_avg_data_raw

                print(shape(expt_avg_data))
                expt_avg_data = expt_avg_data.flatten()
                print(shape(expt_avg_data))

                if self.data_file != None:
                    self.slab_file = SlabFile(self.data_file)

                    with self.slab_file as f:
                        f.append_line('expt_avg_data', expt_avg_data)
                        f.append_line('expt_pts', self.expt_pts)
                        f.close()

                else:
                    self.slab_file = self.datafile()

                    with self.slab_file as f:
                        f.add('expt_avg_data', expt_avg_data)
                        f.add('expt_pts', self.expt_pts)
                        f.close()

            else:
                if self.adc == None:
                    print("Prep Card")
                    adc = Alazar(self.cfg['alazar'])
                else:
                    adc = self.adc

                expt_data = None
                current_data = None
                for ii in tqdm(
                        arange(
                            max(1, self.cfg[self.expt_cfg_name]['averages'] /
                                100))):
                    tpts, ch1_pts, ch2_pts = adc.acquire_avg_data_by_record(
                        prep_function=self.awg_prep,
                        start_function=self.awg_run,
                        excise=self.cfg['readout']['window'])

                    mag = sqrt(ch1_pts**2 + ch2_pts**2)
                    if not self.cfg[self.expt_cfg_name]['use_pi_calibration']:

                        if expt_data is None:
                            if self.cfg['readout']['channel'] == 1:
                                expt_data = ch1_pts
                            elif self.cfg['readout']['channel'] == 2:
                                expt_data = ch2_pts
                        else:
                            if self.cfg['readout']['channel'] == 1:
                                expt_data = (expt_data * ii + ch1_pts) / (ii +
                                                                          1.0)
                            elif self.cfg['readout']['channel'] == 2:
                                expt_data = (expt_data * ii + ch2_pts) / (ii +
                                                                          1.0)

                    else:
                        if self.cfg['readout']['channel'] == 1:
                            zero_amp = mean(ch1_pts[-2])
                            pi_amp = mean(ch1_pts[-1])
                            current_data = (ch1_pts[:-2] -
                                            zero_amp) / (pi_amp - zero_amp)
                        elif self.cfg['readout']['channel'] == 2:
                            zero_amp = mean(ch2_pts[-2])
                            pi_amp = mean(ch2_pts[-1])

                            current_data = (ch2_pts[:-2] -
                                            zero_amp) / (pi_amp - zero_amp)
                        if expt_data is None:
                            expt_data = current_data
                        else:
                            expt_data = (expt_data * ii + current_data) / (ii +
                                                                           1.0)

                    expt_avg_data = mean(expt_data, 1)

                # if self.liveplot_enabled:
                #     self.plotter.plot_z(self.prefix + ' Data', expt_data.T)
                #     self.plotter.plot_xy(self.prefix + ' XY', self.pulse_sequence.expt_pts, expt_avg_data)

                # print ii * min(self.cfg[self.expt_cfg_name]['averages'], 100)

                if self.data_file != None:
                    self.slab_file = SlabFile(self.data_file)
                else:
                    self.slab_file = self.datafile()
                with self.slab_file as f:
                    f.add('expt_2d', expt_data)
                    f.add('expt_avg_data', expt_avg_data)
                    f.add('expt_pts', self.expt_pts)
                    f.close()

            # if self.data_file != None:
            #     self.slab_file = SlabFile(self.data_file)
            #     with self.slab_file as f:
            #         f.append_line('expt_avg_data', expt_avg_data)
            #         f.append_line('expt_pts', self.expt_pts)
            #         f.close()

            if self.post_run is not None:
                self.post_run(self.expt_pts, expt_avg_data)
    def go(self):
        # self.plotter.clear()

        print("Prep Instruments")
        self.readout.set_output(True)
        self.readout.set_power(self.cfg['readout']['power'])
        if (self.cfg[self.expt_cfg_name]['pulsed']):
            self.readout.set_ext_pulse(mod=True)
        else:
            self.readout.set_ext_pulse(mod=False)

        if (self.cfg[self.expt_cfg_name]['pi_pulse']):
            self.drive.set_output(True)
            self.drive.set_ext_pulse(mod=True)
        else:
            self.drive.set_output(False)
            self.drive.set_ext_pulse(mod=False)


        # self.drive.set_ext_pulse(mod=False)
        self.readout_atten.set_attenuator(self.cfg['readout']['dig_atten'])

        self.awg.set_amps_offsets(self.cfg['cal']['iq_amps'], self.cfg['cal']['iq_offsets'])

        self.awg.run()

        print("Prep Card")
        adc = Alazar(self.cfg['alazar'])

        self.tek2 = InstrumentManager()["TEK2"]


        self.slab_file = self.datafile()
        with self.slab_file as f:
            f.append_line('expt_pts', self.expt_pts)
            f.close()

        for freq in self.expt_pts:
            self.readout.set_frequency(freq)
            self.readout_shifter.set_phase((self.cfg['readout']['start_phase'] + self.cfg['readout']['phase_slope'] * (freq - self.cfg['readout']['frequency']))%360, freq)
            # print self.readout_shifter.get_phase()

            ## taking data

            expt_data_1 = None

            expt_data_2 = None

            for ii in tqdm(arange(max(1, self.cfg[self.expt_cfg_name]['averages'] / 10000))):
                tpts, ch1_pts, ch2_pts = adc.acquire_avg_data_by_record(prep_function=self.awg_prep,
                                                                        start_function=self.awg.run,
                                                                        excise=self.cfg['readout']['window'])

                mag = sqrt(ch1_pts**2+ch2_pts**2)

                if expt_data_1 is None:
                    expt_data_1 = ch1_pts
                    expt_data_2 = ch2_pts
                else:
                    expt_data_1 = (expt_data_1 * ii + ch1_pts) / (ii + 1.0)
                    expt_data_2 = (expt_data_2 * ii + ch2_pts) / (ii + 1.0)



                expt_avg_data_1 = mean(expt_data_1, 1)
                expt_avg_data_2 = mean(expt_data_2, 1)
                # if self.liveplot_enabled:
                #     self.plotter.plot_z(self.prefix + ' Data', expt_data.T)
                #     self.plotter.plot_xy(self.prefix + ' XY', array([0,1]), expt_avg_data)

                # print ii * min(self.cfg[self.expt_cfg_name]['averages'], 100)


            self.slab_file = self.datafile()
            with self.slab_file as f:
                f.append_line('expt_avg_data_1', expt_avg_data_1)
                f.append_line('expt_avg_data_2', expt_avg_data_2)
                f.close()
Beispiel #9
0
    def go(self):
        if self.liveplot_enabled:
            self.plotter.clear()

        print("Prep Instruments")
        self.readout.set_frequency(self.cfg['readout']['frequency'])
        self.readout.set_power(self.cfg['readout']['power'])
        self.readout.set_ext_pulse(mod=True)

        self.drive.set_frequency(
            self.cfg['qubit']['frequency'] -
            self.cfg['pulse_info'][self.pulse_type]['iq_freq'])
        self.drive.set_power(self.cfg[self.expt_cfg_name]['power'])
        self.drive.set_ext_pulse(mod=False)
        self.drive.set_output(True)

        try:
            self.awg.set_amps_offsets(self.cfg['cal']['iq_amps'],
                                      self.cfg['cal']['iq_offsets'])
        except:
            print("self.awg not loaded.")

        print("Prep Card")
        adc = Alazar(self.cfg['alazar'])

        attenpts = arange(self.cfg[self.expt_cfg_name]['atten_start'],
                          self.cfg[self.expt_cfg_name]['atten_stop'],
                          self.cfg[self.expt_cfg_name]['atten_step'])
        freqpts = arange(self.cfg[self.expt_cfg_name]['freq_start'],
                         self.cfg[self.expt_cfg_name]['freq_stop'],
                         self.cfg[self.expt_cfg_name]['freq_step'])
        num_bins = self.cfg[self.expt_cfg_name]['num_bins']

        for xx, atten in enumerate(attenpts):
            #self.im.atten.set_attenuator(atten)
            try:
                self.readout_atten.set_attenuator(atten)
            except:
                print("Digital attenuator not loaded.")

            max_contrast_data = zeros(len(freqpts))

            if self.liveplot_enabled:
                self.plotter.clear('max contrast')

            for yy, freq in enumerate(freqpts):
                self.readout.set_frequency(freq)
                #self.readout_shifter.set_phase(self.cfg['readout']['start_phase'] , freq)
                self.readout_shifter.set_phase(
                    (self.cfg['readout']['start_phase'] +
                     self.cfg['readout']['phase_slope'] *
                     (freq - self.cfg['readout']['frequency'])) % 360, freq)

                #tpts, ch1_pts, ch2_pts = adc.acquire_avg_data_by_record(prep_function=self.awg.stop_and_prep, start_function=self.awg.run,excise=None)

                tpts, ch1_pts, ch2_pts = adc.acquire_avg_data_by_record(
                    prep_function=self.awg_prep,
                    start_function=self.awg_run,
                    excise=self.cfg['readout']['window'])

                if self.liveplot_enabled:
                    self.plotter.plot_z("current", ch1_pts)

                # with self.datafile() as f:
                #     f.append('time_trace', ch1_pts)
                ss_data = zeros((len(self.expt_pts), num_bins))
                sss_data = zeros((len(self.expt_pts), num_bins))

                # ss1, ss2 = adc.acquire_singleshot_data(prep_function=None, start_function=None,
                #                                        excise=self.cfg['readout']['window'])
                ss1, ss2 = adc.acquire_singleshot_data(
                    prep_function=self.awg_prep,
                    start_function=self.awg_run,
                    excise=self.cfg['readout']['window'])

                ss1 = reshape(ss1,
                              (self.cfg['alazar']['recordsPerAcquisition'] /
                               len(self.expt_pts), len(self.expt_pts))).T
                histo_range = (ss1.min() / 1.5, ss1.max() * 1.5)
                for jj, ss in enumerate(ss1):
                    sshisto, ssbins = np.histogram(ss,
                                                   bins=num_bins,
                                                   range=histo_range)
                    ss_data[jj] += sshisto
                    sss_data[jj] = cumsum(ss_data[[jj]])
                    if self.liveplot_enabled:
                        self.plotter.plot_xy('histogram %d' % jj, ssbins[:-1],
                                             ss_data[jj])
                        self.plotter.plot_xy('cum histo %d' % jj, ssbins[:-1],
                                             sss_data[jj])

                if self.liveplot_enabled:
                    self.plotter.plot_xy(
                        'contrast', ssbins[:-1],
                        abs(sss_data[0] - sss_data[1]) / ss_data[0].sum())
                max_contrast_data[yy] = abs(
                    ((sss_data[0] - sss_data[1]) / ss_data[0].sum())).max()
                if self.liveplot_enabled:
                    self.plotter.append_xy('max contrast', freq,
                                           max_contrast_data[yy])
            if len(attenpts) > 1:
                if self.liveplot_enabled:
                    print("plotting max contrast 2")
                    self.plotter.append_z(
                        'max contrast 2',
                        max_contrast_data,
                        start_step=((attenpts[0], attenpts[1] - attenpts[0]),
                                    (freqpts[0] / 1.0e9,
                                     (freqpts[1] - freqpts[0]) / 1.0e9)))

            with self.datafile() as f:
                f.append_pt('atten', atten)
                f.append_line('freq', freqpts)
                f.append_line('max_contrast_data', max_contrast_data)
                f.close()
    def go(self):
        if self.liveplot_enabled:
            self.plotter.clear()

        print("Prep Instruments")
        self.readout.set_frequency(self.cfg['readout']['frequency'])
        self.readout.set_power(self.cfg['readout']['power'])
        self.readout.set_ext_pulse(mod=True)
        # self.readout_shifter.set_phase(self.cfg['readout']['start_phase'] + self.cfg['readout']['phase_slope'] * (
        #     self.cfg['readout']['frequency'] - self.cfg['readout']['bare_frequency']), self.cfg['readout']['frequency'])

        self.drive.set_frequency(self.cfg['qubit']['frequency'] - self.cfg['pulse_info'][self.pulse_type]['iq_freq'])
        self.drive.set_power(self.cfg['drive']['power'])
        self.drive.set_ext_pulse(mod=True)
        self.drive.set_output(True)
        self.readout_atten.set_attenuator(self.cfg['readout']['dig_atten'])

        self.trigger.set_period(self.trigger_period)

        try:
            self.cfg['freq_flux']['flux']=self.extra_args['flux']
        except:
            pass

        try:
            self.cfg['freq_flux']['freq_flux_slope']=self.extra_args['freq_flux_slope']
        except:
            pass

        try:
            self.cfg['freq_flux']['flux_offset']+=self.extra_args['flux_offset']
        except:
            pass


        if self.cfg['freq_flux']['current']:
            try: self.flux_volt.ramp_current(self.cfg['freq_flux']['flux'])
            except:pass
        elif self.cfg['freq_flux']['voltage']:
            try: self.flux_volt.ramp_volt(self.cfg['freq_flux']['flux'])
            except: pass

        self.awg.set_amps_offsets(self.cfg['cal']['iq_amps'], self.cfg['cal']['iq_offsets'])

        if self.pre_run is not None:
            self.pre_run()

        if self.adc==None:
            print("Prep Card")
            adc = Alazar(self.cfg['alazar'])
        else:
            adc = self.adc

        expt_data = None
        current_data = None
        for ii in tqdm(arange(max(1, self.cfg[self.expt_cfg_name]['averages'] / 100))):
            tpts, ch1_pts, ch2_pts = adc.acquire_avg_data_by_record(prep_function=self.awg_prep,
                                                                    start_function=self.awg.run,
                                                                    excise=self.cfg['readout']['window'])

            ### to skip the first 100 averages data point, because we saw a weird ramp in the RF output
            if ii == 0:
                time.sleep(0.1)
                continue

            mag = sqrt(ch1_pts**2+ch2_pts**2)
            if not self.cfg[self.expt_cfg_name]['use_pi_calibration']:

                if expt_data is None:
                    if self.cfg['readout']['channel']==1:
                        expt_data = ch1_pts
                    elif self.cfg['readout']['channel']==2:
                        expt_data = ch2_pts
                else:
                    if self.cfg['readout']['channel']==1:
                        expt_data = (expt_data * ii + ch1_pts) / (ii + 1.0)
                    elif self.cfg['readout']['channel']==2:
                        expt_data = (expt_data * ii + ch2_pts) / (ii + 1.0)

                expt_avg_data = mean(expt_data, 1)


            else:

                if expt_data is None:
                    if self.cfg['readout']['channel']==1:
                        expt_data = ch1_pts
                    elif self.cfg['readout']['channel']==2:
                        expt_data = ch2_pts
                else:
                    if self.cfg['readout']['channel']==1:
                        expt_data = (expt_data * ii + ch1_pts) / (ii + 1.0)
                    elif self.cfg['readout']['channel']==2:
                        expt_data = (expt_data * ii + ch2_pts) / (ii + 1.0)

                expt_avg_data = mean(expt_data, 1)

                expt_avg_data = (expt_avg_data[:-2]-expt_avg_data[-2])/(expt_avg_data[-1]-expt_avg_data[-2])

            # temporary hack. Alazar card seems to shift two data points (problem disappeared after reboot computer)
            # expt_avg_data = np.concatenate((expt_avg_data[2:],expt_avg_data[:2]))
            # if self.cfg[self.expt_cfg_name]['use_pi_calibration']:
            #     expt_avg_data = (expt_avg_data[:-2]-expt_avg_data[-2])/(expt_avg_data[-1]-expt_avg_data[-2])

            # else:
            #     if self.cfg['readout']['channel']==1:
            #         zero_amp = mean(ch1_pts[-2])
            #         pi_amp = mean(ch1_pts[-1])
            #         current_data= (ch1_pts[:-2]-zero_amp)/(pi_amp-zero_amp)
            #     elif self.cfg['readout']['channel']==2:
            #         zero_amp = mean(ch2_pts[-2])
            #         pi_amp = mean(ch2_pts[-1])
            #         current_data= (ch2_pts[:-2]-zero_amp)/(pi_amp-zero_amp)
            #     if expt_data is None:
            #         expt_data = current_data
            #     else:
            #         expt_data = (expt_data * ii + current_data) / (ii + 1.0)


            # expt_avg_data = mean(expt_data, 1)


            if self.liveplot_enabled:
                self.plotter.plot_z(self.prefix + ' Data', expt_data.T)
                self.plotter.plot_xy(self.prefix + ' XY', self.pulse_sequence.expt_pts, expt_avg_data)

            # print ii * min(self.cfg[self.expt_cfg_name]['averages'], 100)

            if self.data_file == None:
                self.slab_file = self.datafile()
                with self.slab_file as f:
                    f.add('expt_2d', expt_data)
                    f.add('expt_avg_data', expt_avg_data)
                    f.add('expt_pts', self.expt_pts)
                    f.close()

        if self.data_file != None:
            self.slab_file = SlabFile(self.data_file)
            with self.slab_file as f:
                f.append_line('expt_avg_data', expt_avg_data)
                f.append_line('expt_pts', self.expt_pts)
                f.close()


        if not self.adc_predefined:
            adc.close()


        if self.post_run is not None:
            self.post_run(self.expt_pts, expt_avg_data)
    def go(self):
        # self.plotter.clear()

        print("Prep Instruments")
        self.readout.set_output(True)
        self.readout.set_power(self.cfg['readout']['power'])
        if (self.cfg[self.expt_cfg_name]['pulsed']):
            self.readout.set_ext_pulse(mod=True)
        else:
            self.readout.set_ext_pulse(mod=False)

        if (self.cfg[self.expt_cfg_name]['pi_pulse']):
            self.drive.set_output(True)
            self.drive.set_ext_pulse(mod=True)
        else:
            self.drive.set_output(False)
            self.drive.set_ext_pulse(mod=False)

        # self.drive.set_ext_pulse(mod=False)
        self.readout_atten.set_attenuator(self.cfg['readout']['dig_atten'])

        self.awg.set_amps_offsets(self.cfg['cal']['iq_amps'],
                                  self.cfg['cal']['iq_offsets'])

        self.awg.run()

        print("Prep Card")
        adc = Alazar(self.cfg['alazar'])

        self.tek2 = InstrumentManager()["TEK2"]

        self.slab_file = self.datafile()
        with self.slab_file as f:
            f.append_line('expt_pts', self.expt_pts)
            f.close()

        for freq in self.expt_pts:
            self.readout.set_frequency(freq)
            self.readout_shifter.set_phase(
                (self.cfg['readout']['start_phase'] +
                 self.cfg['readout']['phase_slope'] *
                 (freq - self.cfg['readout']['frequency'])) % 360, freq)
            # print self.readout_shifter.get_phase()

            ## taking data

            expt_data_1 = None

            expt_data_2 = None

            for ii in tqdm(
                    arange(
                        max(1, self.cfg[self.expt_cfg_name]['averages'] /
                            10000))):
                tpts, ch1_pts, ch2_pts = adc.acquire_avg_data_by_record(
                    prep_function=self.awg_prep,
                    start_function=self.awg.run,
                    excise=self.cfg['readout']['window'])

                mag = sqrt(ch1_pts**2 + ch2_pts**2)

                if expt_data_1 is None:
                    expt_data_1 = ch1_pts
                    expt_data_2 = ch2_pts
                else:
                    expt_data_1 = (expt_data_1 * ii + ch1_pts) / (ii + 1.0)
                    expt_data_2 = (expt_data_2 * ii + ch2_pts) / (ii + 1.0)

                expt_avg_data_1 = mean(expt_data_1, 1)
                expt_avg_data_2 = mean(expt_data_2, 1)
                # if self.liveplot_enabled:
                #     self.plotter.plot_z(self.prefix + ' Data', expt_data.T)
                #     self.plotter.plot_xy(self.prefix + ' XY', array([0,1]), expt_avg_data)

                # print ii * min(self.cfg[self.expt_cfg_name]['averages'], 100)

            self.slab_file = self.datafile()
            with self.slab_file as f:
                f.append_line('expt_avg_data_1', expt_avg_data_1)
                f.append_line('expt_avg_data_2', expt_avg_data_2)
                f.close()