QUBEXP.py 91.4 KB
Newer Older
User expired's avatar
User expired committed
1
2
3
4
5
6
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Sep  8 13:31:08 2017

@author: oscar
7

User expired's avatar
User expired committed
8
9
10
11
v3.2.1:
    - Fixed the digitizer problem that was happening mainly under windows,
      giving a BSOD!
      
12
13
14
15
16
17
18
v3.2.0:
    - improved the double flux pulse function
    - added the function to create a flux pulse with overshoot
    - bugfix: the function to connect the LO generator for the readout didn't
    have a correct channel assignment
    - added timeout for anapicos in connect_signal_generators()

User expired's avatar
User expired committed
19
20
21
22
23
24
25
v3.1.1:
    -added a function that returns readout infos
    -added a function that returns sat-pulse infos
    
v3.1.0:
    - Trying to simplify processes

User expired's avatar
User expired committed
26
27
28
v3.0.0:
    - Library adapted to the new classes AWGSIGD, DIGSIGD and IQCALIBRATION

User expired's avatar
User expired committed
29
30
31
"""

from PULSE import Pulse
User expired's avatar
User expired committed
32
from UtilitiesLib import initialize_shared_gen,progressive_plot_2d,progressive_plot_3d
User expired's avatar
User expired committed
33
34
35
import numpy as np
from SIGGEN import Siggen
import time
User expired's avatar
User expired committed
36
import sys
User expired's avatar
User expired committed
37
38
39
40
import matplotlib.pyplot as plt
import numpy as np
import DataModule as dm
from SensorReader import SensorReader
User expired's avatar
User expired committed
41
from IQCALIBRATION import CalibrationParameters,load_calibration_file
User expired's avatar
User expired committed
42
from peak_detector import peak_detect,try_lorentzian_fit
User expired's avatar
User expired committed
43

User expired's avatar
User expired committed
44
print('QUBEXP v3.2.1')
User expired's avatar
User expired committed
45

User expired's avatar
User expired committed
46
class Qubexp(object):
User expired's avatar
User expired committed
47
    version='3.2.1'
User expired's avatar
User expired committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
    def __init__(self,CryoID,sgro=None,sgLO=None,sgLOch=None,sgIQLO=None,dig=None,awg=None):
        if CryoID != "Freezer" and CryoID != "Cooler":
            print('ERROR: Wrong CryoID inserted: Freezer or Cooler\n')
            raise Exception('CRYOIDERR')
        
        self.CryoID = CryoID
        
        
        self.sgro = sgro
        self.sgLO = sgLO
        self.sgLOch = sgLOch
        self.sgIQLO = sgIQLO
        self.sgLOpwr = 8
        self.dig = dig
        self.awg = awg
        self.__down_freq = 10 #MHz
        self.__acquisition_delay_fix = 0 #ns
        
User expired's avatar
User expired committed
66
67
        self.ro_pulse = [0.,-100.,0.,0.]        
        self.ex_pulse = [0.,-100.,0.,0.]        
User expired's avatar
User expired committed
68
        
User expired's avatar
User expired committed
69
70
71
        self._sgro = None
        self._sgex = None
        self._sgLO = None
User expired's avatar
User expired committed
72
#------------------------------------------------------------------------------------- q00 checks ---------------------------
User expired's avatar
User expired committed
73
74
75
76
    def __check_command(self,cmd):
        if cmd <0:
            print('ERROR in the command: {}\n'.format(cmd))
            raise Exception('CMDERR')
User expired's avatar
User expired committed
77

User expired's avatar
User expired committed
78
79
80
81
    def __check_pos_num(self,num,name):
        if num<0:
            print('ERROR: {} cannot be negative\n'.format(name))
            raise Exception('NEGNUM')
User expired's avatar
User expired committed
82
#------------------------------------------------------------------------------------- q01 parameters -----------------------        
User expired's avatar
User expired committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
    def returns_readout_pars(self):
        '''This function returns readout parameters as a dictionary.'''
        
        par_dict = dict(readout_frequency = self.ro_pulse[0],
                        readout_power = self.ro_pulse[1],
                        readout_duration = self.ro_pulse[2],
                        readout_delay = self.ro_pulse[3],
                        repetition_rate = self.repetition_period(),
                        
                        readout_generator=self.sgro,
                        readout_LO = self.sgLO,
                        LO_ch = self.sgLOch,
                        LO_pwr = self.sgLOpwr,
                        down_freq = self.__down_freq,
                        averages = self.averages,
                        acquisition_window_delayfix = self.__acquisition_delay_fix)
        
        return par_dict
    
    def returns_sat_pulse_pars(self):
        '''This function returns the saturation pulse parameters as a dictionary.'''
        
        par_dict = dict(sat_pulse_frequency = self.ex_pulse[0],
                        sat_pulse_power = self.ex_pulse[1],
                        sat_pulse_duration = self.ex_pulse[2],
                        sat_pulse_delay = self.ex_pulse[3],
                        
                        sat_generator = self.sgIQLO
                        )
        
        return par_dict
    
    
User expired's avatar
User expired committed
116
117
118
119
120
121
122
123
124
125
126
127
    def readout_frequency(self,Frequency=None):
        '''This is the readout frequency = LO frequency - readout signal frequency,
        expressed in MHz, if it is None it will be queried'''
        
        if Frequency is None:
            return self.__down_freq
        
        if Frequency <0:
            print('Frequency cannot be negative\n')            
            raise Exception('NEGNUM')
        
        self.__down_freq = Frequency
User expired's avatar
User expired committed
128
    
User expired's avatar
User expired committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
    def acquisition_delay_fix(self,Delay=None):
        '''This is the delay fix for the acquisition window, this value can be
        used to center the readout pulse in the windows.
        
         - Delay (ns)
         
         if it is None it will be queried
         '''
        
        
        if Delay is None:
            return self.__acquisition_delay_fix
        
        if Delay <0:
            print('Delay cannot be negative\n')            
            raise Exception('NEGNUM')
        
        if Delay % 10 != 0:
            Delay = Delay - (Delay%10)
            print('WARNING: Delay must be a multiple of 10, it will be floored: {}\n'.format(int(Delay)))
            
User expired's avatar
User expired committed
150
        
User expired's avatar
User expired committed
151
        self.__acquisition_delay_fix = int(Delay)
User expired's avatar
User expired committed
152

User expired's avatar
User expired committed
153
154
    def repetition_period(self,Rep_period=None):
        '''This is the repetition period between acquisitions/experiments.
User expired's avatar
User expired committed
155
        
User expired's avatar
User expired committed
156
157
158
159
         - Rep_period ($\mu$s)
         
         if it is None it will be queried
         '''
User expired's avatar
User expired committed
160
161
        
        
User expired's avatar
User expired committed
162
163
        if Rep_period is None:
            return self.__rep_period
User expired's avatar
User expired committed
164
        
User expired's avatar
User expired committed
165
166
167
        if  Rep_period < 0:
            print('Rep_period cannot be negative\n')            
            raise Exception('NEGNUM')
User expired's avatar
User expired committed
168
        
User expired's avatar
User expired committed
169
170
171
172
173
174
175
        self.__rep_period = Rep_period


#_------------------------------------------------------------------------------------ q02 plotting functions -----------------

    def __repeat_pulse(self,pulse,n):
        '''This function return a new pulse that is the N times repetition of the pulse given as argument.
User expired's avatar
User expired committed
176
        
User expired's avatar
User expired committed
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
        NOTE: pulse must be a PULSE object'''
        
        p=pulse.copy()
        if n >1:
            for count in range(n-1):
                p.insert(pulse)
        else:
            print('Error: repetition must be >1')
            raise Exception('REPERR')
        
        return p
        

    def plot_pulses(self,ex_pulses, awg_pulses, ro_pulse,notone=True,plotSF = 10):
        '''This is the function used to plot the pulse sequence, check the right order, it must be:
User expired's avatar
User expired committed
192
            
User expired's avatar
User expired committed
193
194
195
            ex_pulse (with signal generator)
            awg_pulse (with AWG)
            ro_pulse (with signal generator)
User expired's avatar
User expired committed
196
            
User expired's avatar
User expired committed
197
            if some of them is not used insert None as argument.
User expired's avatar
User expired committed
198
            
User expired's avatar
User expired committed
199
            '''
User expired's avatar
User expired committed
200
            
User expired's avatar
User expired committed
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
        #First I have to determine the longest pulse
        max_length = 0
        if ex_pulses is not None:
            
            for p in ex_pulses:
                max_length = np.max((max_length, np.sum(p.gettotallength('t','a')) ))
        
        if awg_pulses is not None:
            tot = 0
            for p in awg_pulses:
                tot += np.sum(p.gettotallength('t','a'))*p.par['repetitions']
            max_length = np.max((max_length,tot))
                
        if ro_pulse is not None:
            max_length = np.max((max_length,np.sum(ro_pulse.gettotallength('t','a'))))
        
        #Now I create a universal time-axis:
        time_axis = np.linspace(0,max_length,max_length*plotSF)
        
        #Now we have to print the pulses, adding a 0 padding to the shorter ones
        if ex_pulses is not None:
            
            for p in ex_pulses:
                if p.getlength() == 0 or p.par['repetitions'] == 0:
                    continue
                p2 = p.copy()
                p2.setsampling(plotSF)
                
                if notone is True:
                      p2.settone(0.,0.)
                
                if p2.par['repetitions'] > 1:
                    p2=self.__repeat_pulse(p2,p2.par['repetitions'])
                
                plt.plot(time_axis, np.hstack((p2.generate(),np.zeros((len(time_axis)-np.sum(p2.gettotallength('s','a'))) )))  ,'b')
       
        if awg_pulses is not None:
            tot = Pulse(SF=plotSF)
            for p in awg_pulses:
                if p.getlength() == 0 or p.par['repetitions'] == 0:
                    continue
                
                p2 = p.copy()
                if notone is True:
                    p2.settone(0,0)
                
                for count in range(p2.par['repetitions']):
                        tot.insert(p2)
                
            plt.plot(time_axis, np.hstack((tot.generate(),np.zeros((len(time_axis)-np.sum(tot.gettotallength('s','a'))) )))  ,'k')
              
        
        if ro_pulse is not None:
            p2 = ro_pulse.copy()
            p2.setsampling(plotSF)
User expired's avatar
User expired committed
256
            if notone is True:
User expired's avatar
User expired committed
257
258
259
                p2.settone(0.,0.)
            plt.plot(time_axis, np.hstack((p2.generate(),np.zeros((len(time_axis)-np.sum(p2.gettotallength('s','a'))) )))  ,'r')

User expired's avatar
User expired committed
260
261
    

User expired's avatar
User expired committed
262
#------------------------------------------------------------------------------------------------- q03 set-up functions -------------
User expired's avatar
User expired committed
263
264
265
266
267
268
269
270

    def set_readout_pulse(self,Frequency=None,Power=None,Duration=None,Delay=None):
        if Frequency is not None:
            self.__check_pos_num(Frequency,'Frequency')
            self.ro_pulse[0] = np.float(Frequency)
        
        if Power is not None:
            self.ro_pulse[1] = np.float(Power)
User expired's avatar
User expired committed
271
            
User expired's avatar
User expired committed
272
273
274
        if Duration is not None:
            self.__check_pos_num(Duration,'Duration')
            self.ro_pulse[2] = np.float(Duration)
User expired's avatar
User expired committed
275
            
User expired's avatar
User expired committed
276
277
278
279
280
281
282
283
284
285
286
287
288
        if Delay is not None:
            self.__check_pos_num(Delay,'Delay')
            self.ro_pulse[3] = np.float(Delay)
        
        self.disconnect_readout_generators()

    def set_excitation_pulse(self,Frequency=None,Power=None,Duration=None,Delay=None):
        if Frequency is not None:
            self.__check_pos_num(Frequency,'Frequency')
            self.ex_pulse[0] = np.float(Frequency)
        
        if Power is not None:
            self.ex_pulse[1] = np.float(Power)
User expired's avatar
User expired committed
289
            
User expired's avatar
User expired committed
290
291
292
293
294
295
296
297
298
        if Duration is not None:
            self.__check_pos_num(Duration,'Duration')
            self.ex_pulse[2] = np.float(Duration)
            
        if Delay is not None:
            self.__check_pos_num(Delay,'Delay')
            self.ex_pulse[3] = np.float(Delay)
        
        self.disconnect_excitation_generator()
User expired's avatar
User expired committed
299

User expired's avatar
User expired committed
300
301

    def connect_readout_generators(self,close_connection=False):
User expired's avatar
User expired committed
302
            
User expired's avatar
User expired committed
303
304
            if self._sgLO is None: # I assume that, if everything is properly done, if _sgLO is None, also _sgro is None
                self._sgLO = Siggen(self.sgLO,False)
305
306
                if self._sgLO.identify()[:3].lower() == 'ana':
                    self._sgLO.connection_timeout(1800) #30 min connection timeout for anapicos
User expired's avatar
User expired committed
307
                self._sgro = Siggen(self.sgro,False)
User expired's avatar
User expired committed
308
            
User expired's avatar
User expired committed
309
            
User expired's avatar
User expired committed
310
311
312
                self._sgLO.frequency(np.round(self.ro_pulse[0]+self.readout_frequency()/1e3 ,12),self.sgLOch)
                self._sgLO.power(self.sgLOpwr,self.sgLOch)                                  
                self._sgLO.frequency_reference('EXT')
313
                self._sgLO.output(1,channel=self.sgLOch)
User expired's avatar
User expired committed
314
            
User expired's avatar
User expired committed
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
           
            
                self._sgro.frequency(np.round(self.ro_pulse[0],12))
                self._sgro.power(self.ro_pulse[1])
                self._sgro.pulse_triggered(1,self.ro_pulse[2],self.ro_pulse[-1])
                self._sgro.frequency_reference('EXT')
                self._sgro.ALC(0)
                self._sgro.output(1)
                time.sleep(1)
            
            if close_connection is True:
                self.disconnect_readout_generators()

    def set_digitizer(self,points=None,acq_delay=None):
            if acq_delay is None:
                acq_delay = self.ro_pulse[3]
            
            if points is None:
                points = self.ro_pulse[2]*self.dig.SF()
                
            if self.repetition_period()*1e3 < acq_delay+self.acquisition_delay_fix() + points/self.dig.SF() :
                print('WARNING: REP_PERIOD must be larger than the total acquisition time (included delay)')

            
            self.dig._dig.writeRegisterByNumber(0,np.int( (self.acquisition_delay_fix())/10) )
User expired's avatar
User expired committed
340
341
            
        
User expired's avatar
User expired committed
342
343
344
345
            #Setting the averages
            self.dig._dig.writeRegisterByNumber(2,self.averages)
            self.dig._dig.writeRegisterByNumber(4,np.int(self.repetition_period()*100)) #Rep time
            self.dig.channel_trigger(self.ro_ch,"SW_CYCLE")
User expired's avatar
User expired committed
346
            self.dig.delay(self.ro_ch , acq_delay)
User expired's avatar
User expired committed
347
348
            self.dig.points(self.ro_ch,points,self.averages)
            self.dig.set_channel(self.ro_ch)
User expired's avatar
User expired committed
349
350
            
            self.dig.channel_trigger(self.ro_ref,"SW_CYCLE")
User expired's avatar
User expired committed
351
            self.dig.delay(self.ro_ref , acq_delay)
User expired's avatar
User expired committed
352
353
            self.dig.points(self.ro_ref,points,self.averages)
            self.dig.set_channel(self.ro_ref)
User expired's avatar
User expired committed
354
355
356
357
358
359
360
361
362
            
            
    def disconnect_readout_generators(self):
        if self._sgro is not None:
            self._sgro.close()
            self._sgLO.close()
            self._sgro = None
            self._sgLO = None
        
User expired's avatar
User expired committed
363
    def readout_setup(self,hvi,averages,channel=1,channel_amplitude=0.3,ref_channel=2,ref_channel_amplitude = 1., delay_fix=0,down_freq=10,rep_period=1000,LOpwr=8):
User expired's avatar
User expired committed
364
            self.hvi = hvi
User expired's avatar
User expired committed
365
366
367
368
369
            tmp = hvi.load()
            if tmp<0:
                print('ERROR in loading the HVI: {}\n'.format(tmp))
                raise Exception('HVIERR')
            
User expired's avatar
User expired committed
370
371
372
373
374
375
376
377
            self.acquisition_delay_fix(delay_fix)
            self.readout_frequency(down_freq)
            self.repetition_period(rep_period)
            if averages <1:
                print('averages cannot be less than 1\n')
                raise Exception('NEGNUM')
            self.averages = int(averages)
            self.dig.amplitude(channel,channel_amplitude)
User expired's avatar
User expired committed
378
            #self.dig.delay(channel,delay_fix)
User expired's avatar
User expired committed
379
380
            self.dig.set_channel(channel)
            
User expired's avatar
User expired committed
381
            self.dig.amplitude(ref_channel,ref_channel_amplitude)
User expired's avatar
User expired committed
382
            #self.dig.delay(ref_channel,delay_fix)
User expired's avatar
User expired committed
383
384
            self.dig.set_channel(ref_channel)
            
User expired's avatar
User expired committed
385
            self.ro_ch = int(channel)
User expired's avatar
User expired committed
386
            self.ro_ref = int(ref_channel)
User expired's avatar
User expired committed
387
388
389
            
            self.sgLOpwr = LOpwr
            
User expired's avatar
User expired committed
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
            
            self.set_digitizer() #optional
            
    def connect_excitation_generator(self,close_connection = False):
            
            if self._sgex is None:
                self._sgex = Siggen(self.sgIQLO,False)
                self._sgex.frequency(np.round(self.ex_pulse[0],12))
                self._sgex.power(self.ex_pulse[1])
                self._sgex.pulse_triggered(1,self.ex_pulse[2],self.ex_pulse[3])
                self._sgex.frequency_reference('EXT')
                self._sgex.ALC(0)
                self._sgex.output(1)
                time.sleep(0.2)
            
            if close_connection:
                self.disconnect_excitation_generator()
                
    def disconnect_excitation_generator(self):
       if self._sgex is not None:
           self._sgex.close()     
           self._sgex = None
            
User expired's avatar
User expired committed
413
414
            
    def set_awg_registers(self,delay,angle2,phase_corr):
User expired's avatar
User expired committed
415
        self.__check_command( self.awg._awg.writeRegisterByNumber(1,int(delay/10 + 30)) ) # NOTE: 30 for the AOU, +1 for the AWG
User expired's avatar
User expired committed
416
417
418
419
        self.__check_command( self.awg._awg.writeRegisterByNumber(2,self.set_phase_in_reg_deg(90,phase_corr)) )
        self.__check_command( self.awg._awg.writeRegisterByNumber(3,self.set_phase_in_reg_deg(angle2,0)) )
        self.__check_command( self.awg._awg.writeRegisterByNumber(4,self.set_phase_in_reg_deg(angle2+90,phase_corr)) )
        
User expired's avatar
User expired committed
420
421
422
#------------------------------------------------------------------------------------------------ q04 readout functions --------------            
            
        
User expired's avatar
User expired committed
423
    def readout_debug(self,points=None,acq_delay=None,close_connection=False):
User expired's avatar
User expired committed
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
        '''This function will play the sequences and read the data.
        The hvi and sequences must be previously loaded.
        
        if points and acq_delay are None, the defaults value from the readout pulse will be taken.
        
        This debug function will return all the acquired waves from the specified channel.
        
        It works only with the proper HVI - mind it!
        
        
        NOTE:
            - acq_delay is in ns (def None -> pulse delay)
            - rep_period is in $\mu$s (def 1000 = 1 ms)
            - SF is in Gigasamples/s (def 0.5)
        
        '''
        
        """
        NOTE on registers:
            - 0: Delay before acquisition 
            - 1: Counter, automatically set
            - 2: Averages number
            - 3: acquisition state: 1 means completed
            - 4: Delay after acquisition = repetition period 
            
            registers 0 and 4 are in 10ns, example: 10 means 10x10ns = 100ns
            All values must be integers, not floats/doubles
        
        """
User expired's avatar
User expired committed
453
454
455
456
        
        self.connect_readout_generators(close_connection)
        
        self.set_digitizer(points,acq_delay)
User expired's avatar
User expired committed
457
458
459
        #starting the HVI
        self.hvi.start()
    
User expired's avatar
User expired committed
460
        #Waiting for the acquisitions, R3 is 1 when acquisition is finished
User expired's avatar
User expired committed
461
462
463
464
465
466
        try:
            while( not self.dig._dig.readRegisterByNumber(3)[1]):
                time.sleep(0.001)
        except KeyboardInterrupt:
            print('Interrupted')
            raise KeyboardInterrupt
User expired's avatar
User expired committed
467
468
469
            
        tmp = self.dig.get_wave(self.ro_ch)
        self.dig._dig.writeRegisterByNumber(3,0) #at the end of the acquisition, R3 must be set to zero
User expired's avatar
User expired committed
470
                
User expired's avatar
User expired committed
471
        
User expired's avatar
User expired committed
472
        return tmp 
User expired's avatar
User expired committed
473
474
475
                
        
        
User expired's avatar
User expired committed
476
    def readout(self,acq_delay=None,volt_conversion=True,return_all=False):
User expired's avatar
User expired committed
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
        '''This function will play the sequences and read the data.
        The hvi and sequences must be previously loaded.
        
        
        
        This function will return the amplitude and phase (relative to a 0 phase 
        of a virtually created cosine) of the averaged data.
        
        return_all will return the acquired waves in addition to the usual output.
        
        It works only with the proper HVI - mind it!
        '''
        
        """
        NOTE on registers:
            - 0: Delay before acquisition
            - 1: Counter, automatically set
            - 2: Averages number
            - 3: acquisition state: 1 means completed
            - 4: Delay after acquisition = repetition period
            
            registers 0 and 4 are in 10ns, example: 10 means 10x10ns = 100ns
            All values must be integers, not floats/doubles
        
        """
User expired's avatar
User expired committed
502
        
User expired's avatar
User expired committed
503
        a = np.array(self.readout_debug(acq_delay=acq_delay ))
Christian Schneider's avatar
Christian Schneider committed
504
        idx_mask = np.array([x.size > 0 for x in a])
User expired's avatar
User expired committed
505
506
507
508
509
        
        if len(a[idx_mask]) == 0:
            print('Digitizer is not acquiring\n')
            raise Exception('DIGERR')
        
Christian Schneider's avatar
Christian Schneider committed
510
        if len(a[idx_mask]) < len(a):
User expired's avatar
User expired committed
511
512
513
514
            print('Warning: Too many averages n={}. Exceeded maximum of' +' n_max= {}'.format(len(a), len(a[idx_mask])))
            raise Exception('AVEERR')
        
        tot = np.sum(a, 0)/len(a)
User expired's avatar
User expired committed
515
516
        fa = np.fft.fft(tot)
        
User expired's avatar
User expired committed
517
        index = np.int(np.round(self.ro_pulse[2]*self.readout_frequency()/1e3,12))
User expired's avatar
User expired committed
518
519
520
521
522
523
524
525
526
527
        
        tmp = np.abs(fa[index])*2/len(tot)
        if volt_conversion is True:
            tmp = self.digits_to_Vp(tmp,self.dig.amplitude(self.ro_ch))
        
        if return_all is True:
            
            return tmp,a
        else:
            return tmp
User expired's avatar
User expired committed
528
    
User expired's avatar
User expired committed
529
    def readout_IQ_debug(self,points=None,acq_delay=None,close_connection=False):
User expired's avatar
User expired committed
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
        '''This function will play the sequences and read the data.
        The hvi and sequences must be previously loaded.
        
        if points and acq_delay are None, the defaults value from the readout pulse will be taken.
        
        This debug function will return all the acquired waves from the specified channel.
        
        It works only with the proper HVI - mind it!
        
        
        NOTE:
            - acq_delay is in ns (def None -> pulse delay)
            - rep_period is in $\mu$s (def 1000 = 1 ms)
            - SF is in Gigasamples/s (def 0.5)
        
        '''
        
        """
        NOTE on registers:
            - 0: Delay before acquisition 
            - 1: Counter, automatically set
            - 2: Averages number
            - 3: acquisition state: 1 means completed
            - 4: Delay after acquisition = repetition period 
            
            registers 0 and 4 are in 10ns, example: 10 means 10x10ns = 100ns
            All values must be integers, not floats/doubles
        
        """
User expired's avatar
User expired committed
559
560
561
562
        
        self.connect_readout_generators(close_connection)
        
        self.set_digitizer(points,acq_delay)
User expired's avatar
User expired committed
563
564
565
566
567
568
569
570
571
        
        points_per_period = np.int(self.dig.SF()*1e3/self.__down_freq )
        if points_per_period % 4 !=0:
            print('ERROR: down-mixed signal must be a multiple of 4: {}\n'.format(points_per_period))
            raise Exception('PPPERR')
    
    
        periods_to_ave = 1
    
User expired's avatar
User expired committed
572
        total_periods = np.int(self.ro_pulse[2]*self.dig.SF()/points_per_period)
User expired's avatar
User expired committed
573
574
575
576
577
578
579
        if periods_to_ave > total_periods:
            print('ERROR: periods_to_ave is larger than the total periods in the acquired wave')
            raise Exception('AVEERR')
        
        #starting the HVI
        self.hvi.start()
    
User expired's avatar
User expired committed
580
        #Waiting for the acquisitions, R3 is 1 at the end of the acquisition
User expired's avatar
User expired committed
581
582
583
584
585
586
587
588
589
        try:
            while( not self.dig._dig.readRegisterByNumber(3)[1]):
                time.sleep(0.001)
        except KeyboardInterrupt:
            print('Interrupted')
            raise KeyboardInterrupt
                
        a = self.dig.get_wave(self.ro_ch)
        b = self.dig.get_wave(self.ro_ref)
User expired's avatar
User expired committed
590
591
592
        
        self.dig._dig.writeRegisterByNumber(3,0) #at the end of the acquisition, R3 must be set to 0
        
User expired's avatar
User expired committed
593
594
        return a[0],b[0],points_per_period,total_periods
    
User expired's avatar
User expired committed
595
    def readout_IQ(self,volt_conversion=True,return_all=False):
User expired's avatar
User expired committed
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
        '''This function will play the sequences and read the data.
        The hvi and sequences must be previously loaded.
        
        
        
        This function will return the amplitude and phase (relative to a 0 phase 
        of a virtually created cosine) of the averaged data.
        
        return_all will return the acquired waves in addition to the usual output.
        
        It works only with the proper HVI - mind it!
        '''
        
        """
        NOTE on registers:
            - 0: Delay before acquisition
            - 1: Counter, automatically set
            - 2: Averages number
            - 3: acquisition state: 1 means completed
            - 4: Delay after acquisition = repetition period
            
            registers 0 and 4 are in 10ns, example: 10 means 10x10ns = 100ns
            All values must be integers, not floats/doubles
        
        """
        
        
        
User expired's avatar
User expired committed
624
        sig,ref,points_per_period,IQ_points = self.readout_IQ_debug()
User expired's avatar
User expired committed
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
        
        
        sig,ref = np.array(sig,dtype=np.float),np.array(ref,dtype=np.float)
        
    
        #IQ_points = np.int(total_periods/periods_to_ave)
        
        
        
        I,Q = np.ndarray(IQ_points),np.ndarray(IQ_points)
        ref90 = np.hstack((ref[int(points_per_period/4):],ref[-int(points_per_period/4):]))
        amp_ref_sq = np.sqrt(np.sum( (ref/len(ref))*ref)*2) #quick and quite accurate
        
        for i in range(IQ_points):
            I[i] = np.sum(sig[points_per_period*i:points_per_period*(i+1)]*ref[points_per_period*i:points_per_period*(i+1)])/(points_per_period)*2/amp_ref_sq
            Q[i] = np.sum(sig[points_per_period*i:points_per_period*(i+1)]*ref90[points_per_period*i:points_per_period*(i+1)])/(points_per_period)*2/amp_ref_sq
        
        if return_all is True:
            return I,Q,sig,ref
        else:
            return I,Q
User expired's avatar
User expired committed
646

User expired's avatar
User expired committed
647
    
User expired's avatar
User expired committed
648
#------------------------------------------------------------------------------------------- q06 experiments functions --------------    
User expired's avatar
User expired committed
649
    
User expired's avatar
User expired committed
650
651
    def readout_xy(self,freq,plot=True,fit_attempt=True,filename=None,Temp_reading=True,pars=None,close_connection=False):
        #check if freq is an iterable item, like a list or array
User expired's avatar
User expired committed
652
        try:
User expired's avatar
User expired committed
653
654
655
656
            iter(freq)
        except TypeError:
            freq = [np.float(freq)]

User expired's avatar
User expired committed
657
658
        
        
User expired's avatar
User expired committed
659
        self.connect_readout_generators()
User expired's avatar
User expired committed
660
        
User expired's avatar
User expired committed
661
662
663
664
665
666
667
668
669
670
671
        
        if len(freq)==1:
            #We don't want to change the readout pulse
            self._sgro.frequency(freq[0])
            self._sgLO.frequency(np.round(freq[0]+self.readout_frequency()/1e3,12),self.sgLOch)
            time.sleep(0.001)
            tmp = self.readout()
            if close_connection:
                self.disconnect_readout_generators()
            return tmp
        else:
User expired's avatar
User expired committed
672
            try:
User expired's avatar
User expired committed
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
                
                
                data = self.create_dm_before(Temp_reading)
                
                tmp = np.ndarray(len(freq))
                
                for i,f in enumerate( freq):
                    self.__check_pos_num(f,'Frequency')
                    self._sgro.frequency(np.round(f,10))
                    self._sgLO.frequency( np.round(f+self.readout_frequency()/1e3,12),self.sgLOch)
                    time.sleep(0.001)
                    
                    tmp[i] = self.readout()
                    
                    if plot is True:
                        progressive_plot_2d(freq[:i],tmp[:i],'-o')
                    
                data = self.create_dm_after(data,freq,tmp,Temp_reading)
            except KeyboardInterrupt:
                data = self.create_dm_after(data,freq[:i],tmp[:i],Temp_reading)
                
User expired's avatar
User expired committed
694
            except:
User expired's avatar
User expired committed
695
696
697
698
                print('Unknown error:\n'+sys.exc_info()[0])
                if close_connection:
                    self.disconnect_readout_generators()
                raise
User expired's avatar
User expired committed
699
                
User expired's avatar
User expired committed
700
701
            if fit_attempt:
                try:
User expired's avatar
User expired committed
702
                    par0 = peak_detect(data.x,data.y,7,plot=False)[0]
User expired's avatar
User expired committed
703
704
705
706
                    try_lorentzian_fit(data,par0)
                    del par0
                except:
                    print('Fit failed\n')
User expired's avatar
User expired committed
707
            
User expired's avatar
User expired committed
708
709
710
711
712
713
714
715
716
            if pars is not None:
                data.pars=pars
            
            if filename is not None:
                data.save(filename)
            
            if close_connection:
                self.disconnect_readout_generators()
            return data
User expired's avatar
User expired committed
717
718
719


        
User expired's avatar
User expired committed
720
721
722
723
724
725
726
    def qubit_spec_xy(self,freq,plot=True,fit_attempt=True,filename=None,Temp_reading=True,pars=None,close_connections=False):
        #check if freq is an iterable item, like a list or array
        try:
            iter(freq)
        except TypeError:
            freq = [np.float(freq)]
            
User expired's avatar
User expired committed
727
        
User expired's avatar
User expired committed
728
729
        self.connect_readout_generators()
        self.connect_excitation_generator()
User expired's avatar
User expired committed
730
731
732
        
        
        
User expired's avatar
User expired committed
733
734
735
736
737
738
739
740
741
742
743
744
        if len(freq)==1:
            
            self._sgex.frequency(freq[0])
            time.sleep(0.001)
            tmp = self.readout()
            if close_connections:
                self.disconnect_readout_generators()
                self.disconnect_excitation_generator()
            return tmp
        else:
            try:
                data = self.create_dm_before(Temp_reading )
745
                
User expired's avatar
User expired committed
746
                tmp = np.ndarray(len(freq))
747
                
User expired's avatar
User expired committed
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
                for i,f in enumerate( freq):
                    self.__check_pos_num(f,'Frequency')
                    self._sgex.frequency(np.round(f,10))
                    
                    time.sleep(0.001)
                    
                    tmp[i] = self.readout()
                    
                    if plot is True:
                        progressive_plot_2d(freq[:i],tmp[:i],'-o')
                    
                data = self.create_dm_after(data,freq,tmp,Temp_reading)
            except KeyboardInterrupt:
                data = self.create_dm_after(data,freq[:i],tmp[:i],Temp_reading)
                
            except:
                print('Unknown error:\n'+sys.exc_info()[0])
                if close_connections:
                    self.disconnect_readout_generators()
                    self.disconnect_excitation_generator()
                raise
                
            if fit_attempt:
771
                try:
User expired's avatar
User expired committed
772
                    par0 = peak_detect(data.x,data.y,7,plot=False)[0]
User expired's avatar
User expired committed
773
774
                    try_lorentzian_fit(data,par0)
                    del par0
775
776
777
                except:
                    print('Fit failed\n')
                    
User expired's avatar
User expired committed
778
            
779
            if pars is not None:
User expired's avatar
User expired committed
780
781
                data.pars=pars
            
782
783
            if filename is not None:
                data.save(filename)
User expired's avatar
User expired committed
784
785
786
787
            
            if close_connections:
                self.disconnect_readout_generators()
                self.disconnect_excitation_generator()
788
            return data
User expired's avatar
User expired committed
789
        
User expired's avatar
User expired committed
790
        
791
    def Rabi(self,sweep,readout_pulse,awg_pulse,plot=True,clear_mem=False,fit_attempt=True,filename=None,Temp_reading=True,pars=None,left_sb=False):
User expired's avatar
User expired committed
792
793
794
795
796
797
798
        #check cal-file
        if awg_pulse.par['calibration'] is None:
            print('calibration missing')
            raise Exception('CALMISSING')
        else:
            cal = awg_pulse.par['calibration']
        
User expired's avatar
User expired committed
799
        phase_corr = load_calibration_file(cal)
User expired's avatar
User expired committed
800
        #awg_freq = phase_corr.awg_parameters()[0]
User expired's avatar
User expired committed
801
        phase_corr = phase_corr.phase_corr
User expired's avatar
User expired committed
802
803
804
805
        
        #configure the excitation pulse
        pulse = awg_pulse.copy()
        pulse.sampling(self.awg.SF())
User expired's avatar
User expired committed
806
807
        pulse_delay = pulse.delay()
        pulse.delay(0)
User expired's avatar
User expired committed
808
809
810
        
        #set the readout delay
        ro = readout_pulse.copy()
User expired's avatar
User expired committed
811
        ro.delay(awg_pulse.duration()+readout_pulse.delay())
User expired's avatar
User expired committed
812
813
814
        
        #configure the AWG and the queue
        channel_used = [pulse.par['awg_chan1'],pulse.par['awg_chan2']]
User expired's avatar
User expired committed
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
        for i in channel_used:
            self.awg.clear_channel_queue(i)
        
        
        #Load the waves in the AWG
        if clear_mem is True:
            self.awg.clear_waves()
        
        self.awg.apply_correction(cal)
        if pulse.par['AM'] is True:
            self.awg.insert_array(pulse.generate(),'awgpulse')
        else:
            pulse.frequency(np.round( self.awg.frequency(channel_used[0])/1e3 ,12))
            pulse.phase(self.awg.phase(channel_used[0]))
            self.awg.insert_array(pulse.generate(),'awgpulse0')
830
831
832
833
834
835
            
            if left_sb is False:
                pulse.phase(self.awg.phase(channel_used[1]))
            else:
                pulse.phase(self.awg.phase(channel_used[1])+180 )
                
User expired's avatar
User expired committed
836
837
838
839
840
841
            self.awg.insert_array(pulse.generate(),'awgpulse90')
        
        self.awg.load_waves_in_AWG_memory()
        
        if pulse.par["AM"] is True:
            pulse.frequency(0)
User expired's avatar
User expired committed
842
843
            for i in channel_used:
                self.awg.clear_channel_queue(i)
User expired's avatar
User expired committed
844
                self.awg.queue_in_channel(i,'awgpulse',Delay=pulse_delay)
User expired's avatar
User expired committed
845
                self.awg.mode(i,'SIN')
User expired's avatar
User expired committed
846
847
                self.awg.modulation(i,1)
        else:
User expired's avatar
User expired committed
848
849
850
851
852
853
854
855
            for i in channel_used:
                self.awg.clear_channel_queue(i)
                
                self.awg.mode(i,'AWG')
                self.awg.modulation(i,0)
            
            self.awg.queue_in_channel(channel_used[0],'awgpulse0',Delay=pulse_delay)
            self.awg.queue_in_channel(channel_used[1],'awgpulse90',Delay=pulse_delay)
User expired's avatar
User expired committed
856
857
        
        
User expired's avatar
User expired committed
858
        self.set_awg_registers(pulse.total_duration(),0,phase_corr)
User expired's avatar
User expired committed
859
860
861
        self.generate_readout_pulse(ro)
        
        meas = np.ndarray(len(sweep))
User expired's avatar
User expired committed
862
        data = self.create_dm_before(Temp_reading)
User expired's avatar
User expired committed
863
864
865
866
867
868
869
870
871
872
873
874
        try:
            for i,amplitude in enumerate(sweep):
                self.awg.apply_correction(cal,amplitude)
                self.awg.set_channel()
                
                time.sleep(0.01)
                    
                tmp = self.readout(ro)
                meas[i]= tmp
                if plot is True:
                    progressive_plot_2d(sweep[:i],meas[:i],'-o')
                
User expired's avatar
User expired committed
875
            data = self.create_dm_after(data,sweep,meas,Temp_reading)
User expired's avatar
User expired committed
876
        except KeyboardInterrupt:
User expired's avatar
User expired committed
877
            data = self.create_dm_after(data,sweep[:i],meas[:i],Temp_reading)
User expired's avatar
User expired committed
878
        
User expired's avatar
User expired committed
879
880
881
882
        #cleaning the queue
        for i in channel_used:
                self.awg.clear_channel_queue(i)
                
User expired's avatar
User expired committed
883
884
885
886
887
888
889
890
891
892
893
894
895
896
        
        if fit_attempt is True:
            try:
                data.fit(dm.cos_fit,[data.y.min()-data.y.max(),data.x[data.y.argmin()],0,data.y.min()/2+data.y.max()/2],labels=dm.cos_fit_pars_labels,plot_init=False)
            except:
                print('Fit failed')
                
        if pars is not None:
            data.insert_par(pars)

                
        if filename is not None:
            data.save(filename)
            
User expired's avatar
User expired committed
897
898
        return data
            
User expired's avatar
User expired committed
899
900
901
902
903
904
905
906
907
        
    def T1(self,sweep,readout_pulse,pi_pulse,plot=True,clear_mem=False,fit_attempt=True,filename=None,Temp_reading=True,pars=None):
        #check cal-file
        if pi_pulse.par['calibration'] is None:
            print('calibration missing')
            raise Exception('CALMISSING')
        else:
            cal = pi_pulse.par['calibration']
        
User expired's avatar
User expired committed
908
        phase_corr = load_calibration_file(cal)
User expired's avatar
User expired committed
909
        #awg_freq = phase_corr.awg_parameters()[0]
User expired's avatar
User expired committed
910
        phase_corr = phase_corr.phase_corr
User expired's avatar
User expired committed
911
912
913
        
        #configure the excitation pulse
        pulse = pi_pulse.copy()
User expired's avatar
User expired committed
914
915
916
        pulse_delay = pulse.delay()
        pulse.delay(0)
        
User expired's avatar
User expired committed
917
918
919
920
921
922
        pulse.sampling(self.awg.SF())
        
        
        #set the readout delay
        ro = readout_pulse.copy()
        start_delay = pi_pulse.total_duration()+readout_pulse.delay()
User expired's avatar
User expired committed
923
        self.generate_readout_pulse(ro)
User expired's avatar
User expired committed
924
925
        #configure the AWG and the queue
        channel_used = [pulse.par['awg_chan1'],pulse.par['awg_chan2']]
User expired's avatar
User expired committed
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
        for i in channel_used:
            self.awg.clear_channel_queue(i)
        
        
        #Load the waves in the AWG
        if clear_mem is True:
            self.awg.clear_waves()
        
        
        self.awg.apply_correction(cal,pi_pulse.par['sigpow'])
        if pulse.par['AM'] is True:   
            self.awg.insert_array(pulse.generate(),'pipulse')
        else:
            pulse.frequency(np.round( self.awg.frequency(channel_used[0])/1e3,12))
            pulse.phase(self.awg.phase(channel_used[0]))
            self.awg.insert_array(pulse.generate(),'pipulse0')
            
            pulse.phase(self.awg.phase(channel_used[1]))
            self.awg.insert_array(pulse.generate(),'pipulse90')
            
            
        self.awg.load_waves_in_AWG_memory()

User expired's avatar
User expired committed
949
        if pulse.par["AM"]is True:
User expired's avatar
User expired committed
950
            pulse.frequency(0)
User expired's avatar
User expired committed
951
952
953
            for i in channel_used:
                self.awg.clear_channel_queue(i)
                self.awg.queue_in_channel(i,'pipulse')
User expired's avatar
User expired committed
954
                self.awg.mode(i,'SIN')
User expired's avatar
User expired committed
955
956
                self.awg.modulation(i,1)
        else:
User expired's avatar
User expired committed
957
958
959
960
961
962
963
964
            for i in channel_used:
                self.awg.clear_channel_queue(i)
                
                self.awg.mode(i,'AWG')
                self.awg.modulation(i,0)
            
            self.awg.queue_in_channel(channel_used[0],'pipulse0',Delay=pulse_delay)
            self.awg.queue_in_channel(channel_used[1],'pipulse90',Delay=pulse_delay)        
User expired's avatar
User expired committed
965
        
User expired's avatar
User expired committed
966
        self.set_awg_registers(pulse.total_duration(),0,phase_corr)
User expired's avatar
User expired committed
967
        self.awg.set_channel()
User expired's avatar
User expired committed
968
        
User expired's avatar
User expired committed
969
970
971
        
        
        meas = np.ndarray(len(sweep))
User expired's avatar
User expired committed
972
        data = self.create_dm_before(Temp_reading)
User expired's avatar
User expired committed
973
974
975
976
977
978
979
980
981
982
983
        try:
            for i,pulses_delay in enumerate(sweep):
                ro.delay(start_delay+pulses_delay)
                self.generate_readout_pulse(ro)        
                time.sleep(0.01)
                    
                tmp = self.readout(ro)
                meas[i]= tmp
                if plot is True:
                    progressive_plot_2d(sweep[:i],meas[:i],'-o')
                
User expired's avatar
User expired committed
984
            data = self.create_dm_after(data,sweep,meas,Temp_reading)
User expired's avatar
User expired committed
985
        except KeyboardInterrupt:
User expired's avatar
User expired committed
986
            data = self.create_dm_after(data,sweep[:i],meas[:i],Temp_reading)
User expired's avatar
User expired committed
987
        
User expired's avatar
User expired committed
988
989
990
        #cleaning the queue
        for i in channel_used:
                self.awg.clear_channel_queue(i)
User expired's avatar
User expired committed
991
992
993
994
995
996
997
998
999
1000
        
        if fit_attempt is True:
            try:
                data.fit(dm.exp_fit_rev,[data.y.min(),data.y.max(),0,10e3],labels=dm.exp_fit_pars_labels,plot_init=False)
            except:
                print('Fit failed')

        if pars is not None:
            data.insert_par(pars)
 
For faster browsing, not all history is shown. View entire blame