def calibrate_duplexer_phase(pulse_pars):
    mod_freq = pulse_pars['mod_frequency']
    G_phi_skew =pulse_pars['G_phi_skew']
    D_phi_skew = pulse_pars['D_phi_skew']
    G_alpha =pulse_pars['G_alpha']
    D_alpha = pulse_pars['D_alpha']



    # cal_elts.cos_seq(.1, mod_freq, ['ch1', 'ch2', 'ch3', 'ch4'],
    #                              phases = [0, 90, 180, 270],
    #                              marker_channels=['ch4_marker1', 'ch4_marker2'])
    cal_elts.cos_seq(.1, mod_freq, ['ch1', 'ch2', 'ch3', 'ch4'],
                             phases = [0, 180],
                             marker_channels=['ch4_marker1', 'ch4_marker2'],
                             alphas=[G_alpha,D_alpha],
                             phi_skews=[G_phi_skew, D_phi_skew])

    AWG.start()
    f = Qubit_LO.frequency()+mod_freq
    MC.set_sweep_function(Dux.in1_out1_phase)
    MC.set_detector_function(det.Signal_Hound_fixed_frequency(SH,
                             frequency=f))


    # MC.set_sweep_points(np.arange(8000, 20000, 100))
    # MC.run('Duplexer_phase_sweep')
    # ma.MeasurementAnalysis()

    ad_func_pars = {'adaptive_function': minimize_scalar,
                    'bracket': [5000, 12000, 15000]}
    MC.set_adaptive_function_parameters(ad_func_pars)
    MC.run(name='adaptive_duplexer_phase_cal', mode='adaptive')
    ma.MeasurementAnalysis()
def calibrate_duplexer_phase_2D(pulse_pars,
                                Dux_phase_1_default=Dux_phase_1_default,
                                Dux_att_1_default=Dux_att_1_default):
    mod_freq = pulse_pars['mod_frequency']
    G_phi_skew =pulse_pars['G_phi_skew']
    D_phi_skew = pulse_pars['D_phi_skew']
    G_alpha =pulse_pars['G_alpha']
    D_alpha = pulse_pars['D_alpha']

    VIP_mon_2_dux.Mux_G_phase(Dux_phase_1_default)
    VIP_mon_2_dux.Mux_G_att(Dux_att_1_default)
    Dux.in1_out1_attenuation(VIP_mon_2_dux.Mux_G_att())
    Dux.in1_out1_phase(VIP_mon_2_dux.Mux_G_phase())

    cal_elts.cos_seq(.1, mod_freq, ['ch1', 'ch2', 'ch3', 'ch4'],
                             phases = [0, 180],
                             marker_channels=['ch4_marker1', 'ch4_marker2'],
                             alphas=[G_alpha,D_alpha],
                             phi_skews=[G_phi_skew, D_phi_skew])

    AWG.start()
    f = Qubit_LO.frequency()+mod_freq
    MC.set_sweep_functions([Dux.in2_out1_phase, Dux.in2_out1_attenuation])
    MC.set_detector_function(det.Signal_Hound_fixed_frequency(SH,
                             frequency=f))

    ad_func_pars = {'adaptive_function': nelder_mead,
                    'x0': [10000,VIP_mon_2_dux.Mux_G_att()],
                    'initial_step': [1000,0.05],
                    'no_improv_break': 35,
                    'sigma':.5,
                    'minimize': True,
                    'maxiter': 500}
    MC.set_adaptive_function_parameters(ad_func_pars)
    MC.run(name='adaptive_duplexer_phase_cal_2D', mode='adaptive')
    ma.OptimizationAnalysis(close_fig=True)
    a=ma.MeasurementAnalysis(auto=False)
    phase= int(a.data_file['Analysis']['optimization_result'].attrs['in2_out1_phase'])
    attenuation = a.data_file['Analysis']['optimization_result'].attrs['in2_out1_attenuation']
    print('phase', )
    VIP_mon_2_dux.Mux_D_att(attenuation)
    VIP_mon_2_dux.Mux_D_phase(phase)
    print('phase set',VIP_mon_2_dux.Mux_D_phase())