Python Zamanlama Problemi

Başlatan Python_, 01 Nisan 2021 - 21:35:27

« önceki - sonraki »

0 Üyeler ve 1 Ziyaretçi konuyu incelemekte.

Python_

Merhaba,
Problem su:
nodeInport adinda bir class yazdim. Bu classin amaci su: Iki tane inportum var. Ainport ve Binport.  Qubit adinda objeler bu inportlara geldiginde, zamani ve bu qubitlerin id'lerini kaydediyorum. Daha sonra bunlari physqueue ismini verdigim hafizaya kaydediyorum. Bir kere bu hafizaya qubitleri kaydettigimde, diger inporta da  "Bu qubiti kaydettim" diye mesaj yolluyorum. Bunu da dataqueue adindaki listeye kucuk bir erteleme ile yapiyorum. Ama sorun su ki: zamanlama olayini cozemedim. Bu algoritmaya gore siralama su olmali:

physqueue' ya kaydedildigi an<lastt <dataqueueya gonderildigi an.

Fakat zamanlama korkunc. Hemen hicbir zaman dogru kaydedilmiyor.
Classim burada:


class nodeinport:
   
    def __init__(self, t0, tmem, inqubits, physqueuesize, clinkdt, otherport=None, name=''): #should variables define according to order..?
        self.DEBUG=True ### HACK !!
        self.lastt=t0
        self.tmem=tmem  # hafiza suresi
        self.name=name
#start hack ?
        if self.tmem == 0: # sonsuz hafiza suresi
            def updateqmem(t):
                pass #Infinite memory, nothing to do
        else :
            def updateqmem(t):
                """/!\ Does NOT update self.lastt"""
                perase=-expm1(-(t-self.lastt)/self.tmem)
                for i,q in enumerate(self.physqueue):
                    if random()<perase:
                        q[1].lost()
                        print(f'qubit name={name} {q[1].idt} is lost at t={t}')
                        del self.physqueue[i]
        self.updateqmem=updateqmem
#end hack ?
        self.inqubits=inqubits  #gelen qubitler
        self.physqueuesize=physqueuesize  # hafiza icin max qubit sayisi
        self.physqueue=[]  #hafiza
        self.dataqueue=[]  #hafizadaya kaydettikten sonra mesaj gindermek icin kaydettigim yer
        self.clinkdt=clinkdt  #dataqueue ya veriyi clinkdt kadar erteleme ile kaydediyorum
       # self.intermediatetime = 0
       # self.dataqueuetime = 0
        self.otherport=otherport
        if otherport != None:
            if otherport.otherport==None :
                otherport.otherport=self
                otherport.clinkdt=clinkdt
            else: raise ValueError(f"{otherport} already connected to someone else")
            self.nextqubitin()
            self.otherport.nextqubitin()
       
           
    def __next__(self):
        while True:
            if len(self.physqueue)<=1:
                self.nextqubitin()
            if len(self.dataqueue)<=1:
                self.otherport.nextqubitin() #TODO : look at the termination contition # this will be finished today
           # if  self.lastt >= self.dataqueue[0][0]: #TODO: check len(self.dataqueue) == 0 or
           #     self.lastt = self.dataqueue[0][0]
           #     td = self.dataqueue[0][0]
           #     self.updateqmem(td)
           #     self.lastt = td
           #     self.dataqueue.pop(0)
                if len(self.dataqueue)>0 and self.lastt >= self.dataqueue[0][0]:
                    sys.exit("******ERROR: qubit arrival time can not be equal or bigger than time of classical message******")
                #td = self.dataqueue[0][0]
                #self.updateqmem(td)
                #self.lastt = td
                #self.dataqueue.pop(0)
            td, cdata = self.dataqueue.pop(0) #otherport
            self.updateqmem(td)
            self.lastt = td
            #self.updateqmem(td) #if I close it, timing for A is ok but for b is still wrong
            #self.lastt=td
            while len(self.physqueue)>0 and self.physqueue[0][1].idt<cdata:#TODO Check
                self.physqueue.pop(0) # not ...lost() because the other is lost anyway
            if  len(self.physqueue)>0 and self.physqueue[0][1].idt==cdata:
                tq, qb = self.physqueue.pop(0)     
                return [td, qb] #td  why did we put td instead of tq? I changed td as tq!!!!
       
    def nextqubitin(self):
            tq, q = next(self.inqubits)
            self.updateqmem(tq)
            self.lastt=tq
            if self.physqueuesize==0 or len(self.physqueue)<=self.physqueuesize:
                self.physqueue.append([tq, q]) #actually keep tq lets try q.idt
                #self.dataqueue.append([tq, q])
                self.otherport.dataqueue.append([tq+self.clinkdt, q.idt])#send classical message to other port
                #self.lastt = self.physqueue.append[0][0]
                #td,a = self.dataqueue.pop(0)
                #self.lastt = tq
                while self.lastt > self.physqueue[0][0]:
                    self.updateqmem(self.physqueue[0][0])
                    self.lastt= self.physqueue[0][0]
                    self.physqueue.pop(0)
                   
                #while self.lastt >= self.otherport.dataqueue[0][0]: #or self.lastt >= self.physqueue[0][0]
                    #if(self.physqueue[0][0]>self.otherport.dataqueue[0][0]):
                          #self.physqueue[0][0]=self.physqueue[0][0]+0.5
                          #self.otherport.dataqueue[0][0]=self.otherport.dataqueue[0][0]+0.5
                          #  self.lastt=self.lastt-0.5 
                #while len(self.dataqueue) > 0 and self.lastt >= self.dataqueue[0][0]:
                #        self.lastt=self.lastt-0.5
                    #self.dataqueue[0][0]=self.dataqueue[0][0]+0.5
                    #self.dataqueue[0][0]=self.dataqueue[0][0]+self.clinkdt
                 
                       
            #self.lastt=tq   
            if self.DEBUG :
                print(f'{self.name}.nextqubitin')
                self.showstate()
    def showstate(self):
        print(f'{self.name}, t={self.lastt}',f'Physqueue:{self.physqueue}',f'Dataqueue:{self.dataqueue}')
       
    def __iter__(self):
        return(self)   


Bu classi kullanabilmek icin calistirdigim kod asagidadir:


src=source(rate=.4,eta=1)
FAqubits=link(eta=.8, deltat=1.5, inputs=src.portA)
FBqubits=link(eta=0.7, deltat=3.2, inputs=src.portB)
Ainport=nodeinport(t0=0, tmem=50, inqubits=FAqubits, physqueuesize=5, clinkdt=4,name='A')
Binport=nodeinport(t0=0, tmem=50, inqubits=FBqubits, physqueuesize=5, clinkdt=4, otherport=Ainport, name='B')#otherport=Ainport,

#next(Ainport)
#next(Binport)
Aout=[x for x in Ainport] # Probably not the best thing, to generate one after the other,but it works
Bout=[x for x in Binport]

print("="*20)
print(f'qubits out of A {[x[1].idt for x in Aout]}')
print(f'qubits out of B {[x[1].idt for x in Bout]}')
print('State of the queus at the end:')
for port in [Ainport, Binport]:
    #next(port)
    port.showstate()
print("the end******************************************************")


Sonuclar soyle(sadece son kismini buraya koyuyorum):
Alıntı Yap
A, t=14.0 Physqueue:[[16.5, <__main__.qubit object at 0x7ff824479670>]] Dataqueue:[[19.7, (12.5,)]]
B.nextqubitin
B, t=15.7 Physqueue:[[18.2, <__main__.qubit object at 0x7ff8243c7e80>]] Dataqueue:[[5.5, (0.0,)], [8.0, (2.5,)], [10.5, (5.0,)], [13.0, (7.5,)], [15.5, (10.0,)], [18.0, (12.5,)], [20.5, (15.0,)]]
A.nextqubitin
A, t=16.5 Physqueue:[[19.0, <__main__.qubit object at 0x7ff8243c78e0>]] Dataqueue:[[22.2, (15.0,)]]
B.nextqubitin
B, t=18.2 Physqueue:[[20.7, <__main__.qubit object at 0x7ff8243c7cd0>]] Dataqueue:[[5.5, (0.0,)], [8.0, (2.5,)], [10.5, (5.0,)], [13.0, (7.5,)], [15.5, (10.0,)], [18.0, (12.5,)], [20.5, (15.0,)], [23.0, (17.5,)]]
====================
qubits out of A []
qubits out of B []
State of the queus at the end:
A, t=22.2 Physqueue:[[19.0, <__main__.qubit object at 0x7ff8243c78e0>]] Dataqueue:[[24.7, (17.5,)]]
B, t=18.2 Physqueue:[[20.7, <__main__.qubit object at 0x7ff8243c7cd0>]] Dataqueue:[[5.5, (0.0,)], [8.0, (2.5,)], [10.5, (5.0,)], [13.0, (7.5,)], [15.5, (10.0,)], [18.0, (12.5,)], [20.5, (15.0,)], [23.0, (17.5,)]]
the end******************************************************

B icin qubitin geldigi zaman 18.2 ikenn dataqueue daki ilk zaman 5.5
A icin durum biraz daha iyi gozukurken, bazen o da degisebiliyor...
zamanlama olayini ayarlayamadim bir turlu. Bir tarafini yapiyorum digeri bozuluyor..
Isterseniz diger kullandigim source ve link fonksiyonunu da buraya yazabilirim