Пример #1
0
 def __init__(self,
              cell=None,
              nballs=10,
              step=0,
              gc=None,
              input=None,
              logfile=sys.stdout,
              velfile=None,
              velint=0,
              kT=None,
              hist=False):
     self.cell = cell
     if input is not None:
         self.load(input)
     else:
         self.balls = lattice(nballs, cell)
         if kT is not None:
             # ra.seed(1)
             self.thermalize(kT)
     self.balls.interact(cell)
     self.step = step
     self.gc = gc
     self.logfile = logfile
     self.velfile = velfile
     self.velinterval = velint
     self.kT = kT
     self.hist = hist
     self.histx = Hist(-5, +5, 0.05)
     self.histy = Hist(-5, +5, 0.05)
Пример #2
0
 def __init__(self,
              cell=None,
              nballs=10,
              step=0,
              gc=None,
              input=None,
              logfile=sys.stdout,
              velfile=None,
              kT=None,
              hist=False):
     if hist:
         self.colorscheme = AbsoluteVelocity(max=5 / 2)
         self.histx = Hist(-5, +5, 0.05, self.colorscheme)
         self.histy = Hist(-5, +5, 0.05, self.colorscheme)
         self.hist = True
     else:
         self.colorscheme = None
         self.hist = False
     if input is not None:
         self.load(input)
     else:
         self.lattice(cell, nballs)
         if kT is not None:
             ra.seed(1)
             self.thermalize(kT)
         self.last1 = self.last2 = None
     # initialize force
     self.step = step
     self.gc = gc
     self.logfile = logfile
     self.velfile = velfile
     self.kT = kT
     self.hist = hist
Пример #3
0
def n_dimensional_poisson_hists(rho, Us, statemap, trials, mu=None, **kwargs):
    """Multidimensional measurement outcomes. Only for 2 qubits right now"""
    Us = np.array(Us)
    rho = np.array(rho)
    statemap = np.array(statemap)
    assert Us.shape[0] == rho.shape[0]
    num_sub = statemap.shape[1]  # number of subspaces

    numU = Us.shape[0]  # number of histograms
    trials = trials * np.ones(numU)
    if mu is None:
        mu = generateExpectedCounts(num_sub, 2, 20)
        mu = np.tile(mu, (2, 1))

    hists = []
    for k in range(numU):
        measrho = Us[k].dot(rho[k]).dot(Us[k].conj().T)
        hists.append(Hist())
        diag = np.real(np.diag(measrho))
        pops = np.sum(statemap.T * diag, axis=1)
        hists[-1].simulate_multi(nspecies=2,
                                 trials=trials[k],
                                 statepops=pops,
                                 mu=[[2, 20], [2, 20]])
    return hists
Пример #4
0
def one_dimensional_poisson_hists(rho, Us, P_j, trials, mu=None, **kwargs):
    Us = np.array(Us)
    rho = np.array(rho)
    dim = rho.shape[-1]
    P_j = np.array(P_j)
    assert Us.shape[0] == rho.shape[0]
    num_sub = P_j.shape[0]  # number of subspaces
    numU = Us.shape[0]  # number of histograms

    P_ij = np.zeros((numU, num_sub, dim, dim)) + 0j
    for i in range(numU):
        for j in range(num_sub):
            P_ij[i, j, :, :] = Us[i].dot(P_j[j]).dot(Us[i].conj().T)

    pops = np.real(
        np.trace(np.einsum('iak,ijkb->ijab', rho, P_ij), axis1=-1, axis2=-2))

    trials = trials * np.ones(numU)
    if mu is None:
        mu = generateExpectedCounts(num_sub, 2, 35)

    hists = []
    for k in range(numU):
        #measrho = Us[k].dot(rho[k]).dot(Us[k].conj().T)
        hists.append(Hist())
        #diag = np.real(np.diag(measrho))
        #pops = np.sum(statemap.T*diag, axis=1)
        hists[-1].simPoisson(trials=trials[k], pops=pops[k], mu=mu)
    return hists
Пример #5
0
def nDimPoissonHists(rho, input_state, Us, P_j, trials, train_frac, mu=None,
                     **simkwargs):
    """
    Generates n-dimensional histograms for simulated examples.
    
    Args:
        rho:         array of fiducial state for reference experiment and 
                     target states for probing experiments
        input_state: array of fiducial state for reference experiment and 1 for 
                     probing experiment
        Us:          knonw unitaries
        P_j:         underlying POVM elements
        trials:      number of trials per experiment
        train_frac:  fraction of reference histograms used for trainind data
        mu:          average number of photon counts for each underlying POVM
                     outcome
        **simkwargs: keyword arguements for properties of state prep (fid)
                     
    Returns:
        hists: histograms for reference and probing experiments
    
    """
    
    Us = np.array(Us)
    rho = np.array(rho)
    assert Us.shape[0] == rho.shape[0]
    dim = P_j.shape[2]
    num_sub = P_j.shape[1]  # number of subspaces
    num_total = Us.shape[0]  # number of histograms
    
    # rotated projectors
    P_ij = np.zeros((num_total, num_sub, dim, dim))+0j
    for i in range(num_total):
        for j in range(num_sub):
            P_ij[i,j,:,:] = (Us[i].conj().T).dot(P_j[j]).dot(Us[i])

    pops = np.real(np.trace(np.einsum('iak,ijkb->ijab', rho, P_ij), 
                            axis1=-1, axis2=-2))

    # Reduce number of trials by 10% of trials for binnning
    trials = trials*np.ones(num_total) # old code
    ref_ind = np.where(list(np.array(input_state[i]).size > 1 
                            for i in range(num_total)))
    trials[ref_ind] = (1/(1-train_frac))*trials[ref_ind] 
    
    if mu is None:
        mu = generateExpectedCounts(num_sub, 2, 20)
        mu = np.tile(mu, (2, 1))

    hists = []
    for k in range(num_total):
        hists.append(Hist())
        hists[-1].simulateMulti(dim_hist=2, trials=trials[k],
                                state_pops=pops[k], mu=[[2, 20],[2, 20]])
    return hists
Пример #6
0
def oneDimPoissonHists(rho, input_state, Us, P_j, trials, train_frac, mu=None,
                       **simkwargs):
    """
    Generates 1D histograms for simulated examples.
    
    Args:
        rho:         array of fiducial state for reference experiment and 
                     target states for probing experiments
        input_state: array of fiducial state for reference experiment and 1 for 
                     probing experiment
        Us:          knonw unitaries
        P_j:         underlying POVM elements
        trials:      number of trials per experiment
        train_frac:  fraction of reference histograms used for trainind data
        mu:          average number of photon counts for each underlying POVM
                     outcome
        **simkwargs: keyword arguements for properties of state prep (fid)
                     
    Returns:
        hists: histograms for reference and probing experiments
    
    """
    Us = np.array(Us)
    rho = np.array(rho)
    dim = rho.shape[-1]
    P_j = np.array(P_j)
    assert Us.shape[0] == rho.shape[0]
    num_sub = P_j.shape[0]      # number of subspaces
    num_total = Us.shape[0]     # total number of experiments

    # engineered POVM elements
    P_ij = np.zeros((num_total, num_sub, dim, dim))+0j
    for i in range(num_total):
        for j in range(num_sub):
            P_ij[i,j,:,:] = (Us[i].conj().T).dot(P_j[j]).dot(Us[i])

    pops = np.real(np.trace(np.einsum('iak,ijkb->ijab', rho, P_ij), 
                            axis1=-1, axis2=-2))

    # Reduce number of trials by 10% of trials for binnning
    trials = trials*np.ones(num_total) 
    ref_ind = np.where(list(np.array(input_state[i]).size > 1 
                            for i in range(num_total)))
    trials[ref_ind] = (1/(1-train_frac))*trials[ref_ind] 
    
    if mu is None:
        # in High-fidelity universal gate-set paper it says 30 photons
        mu = generateExpectedCounts(num_sub, 2, 20) 

    hists = []
    for k in range(num_total):
        hists.append(Hist())
        hists[-1].simPoisson(trials=trials[k], pops=pops[k], mu=mu)
        
    return hists
def responsePlots(num_trials=5000, mu=[2, 20, 40]):
    """Plots histograms of states contained in POVM elements."""

    fontsi = 12

    dark_pops = [1, 0, 0]  # only 2 dark state
    bright_pops = [0, 0, 1]  # only 2 bright state
    mid_pops = [0, 1, 0]  # only 1 bright state

    # 2 dark histogram
    dark_hist = Hist()
    dark_counts = dark_hist.simPoisson(num_trials, pops=dark_pops, mu=mu)
    plt.bar(dark_hist.bin_bounds[0][0:-1],
            dark_hist.hist / dark_hist.trials,
            width=1,
            color=(29 / 255, 111 / 255, 169 / 255),
            edgecolor='black',
            align='edge')

    # 1 bright 1 dark histogram
    mid_hist = Hist()
    mid_counts = mid_hist.simPoisson(num_trials, pops=mid_pops, mu=mu)
    plt.bar(mid_hist.bin_bounds[0][0:-1],
            mid_hist.hist / mid_hist.trials,
            width=1,
            color=(241 / 255, 157 / 255, 25 / 255),
            edgecolor='black',
            align='edge')

    # 2 bright histogram
    bright_hist = Hist()
    bright_counts = bright_hist.simPoisson(num_trials, pops=bright_pops, mu=mu)
    plt.bar(bright_hist.bin_bounds[0][0:-1],
            bright_hist.hist / bright_hist.trials,
            width=1,
            color=(183 / 255, 73 / 255, 25 / 255),
            edgecolor='black',
            align='edge')

    # plot everything
    a = plt.gca()
    mpl.rc('font', family='Times New Roman')
    plt.axis([0, 60, 0, np.max(dark_hist.hist / dark_hist.trials) * 1.05])
    plt.ylabel('Relative frequency', fontsize=fontsi)
    plt.xlabel('Counts', fontsize=fontsi)
    plt.legend(['0 bright', '1 bright', '2 bright'], loc=5, fontsize=fontsi)
Пример #8
0
def main():
    username = input('Введите логин VK: ')

    client_get_id = ClientGetID(username)
    user_id = client_get_id.execute()

    if client_get_id.is_success():
        print("ID: ", user_id)
    else:
        print('Не существует пользователя с таким ID.')
        return 0

    # find age list
    friends_ages_list = ClientGetFriendsAges(user_id).execute()
    if not friends_ages_list:
        print('Друзей не найдено')
        return 0
    else:
        print("Ages: ", friends_ages_list)
        # write gist
        username_friend_gist = Hist(friends_ages_list)
        username_friend_gist.printGist()

    # show gist
    title = "Гистограмма"
    title_x = "Возраст"
    title_y = "Количество друзей"
    username_friend_gist.showBar(title, title_x, title_y)
Пример #9
0
class System:
    def __init__(self,
                 cell=None,
                 nballs=10,
                 step=0,
                 gc=None,
                 input=None,
                 logfile=sys.stdout,
                 velfile=None,
                 velint=0,
                 kT=None,
                 hist=False):
        self.cell = cell
        if input is not None:
            self.load(input)
        else:
            self.balls = lattice(nballs, cell)
            if kT is not None:
                # ra.seed(1)
                self.thermalize(kT)
        self.balls.interact(cell)
        self.step = step
        self.gc = gc
        self.logfile = logfile
        self.velfile = velfile
        self.velinterval = velint
        self.kT = kT
        self.hist = hist
        self.histx = Hist(-5, +5, 0.05)
        self.histy = Hist(-5, +5, 0.05)

    def thermalize(self, kT):
        self.balls.randomize(kT)
        # Remove total translation
        velsum = np.sum(self.balls.vel, axis=0)
        velsum /= self.balls.N
        self.balls.vel -= velsum

    def OneStep(self, dt):
        # Progress Momenta (half)
        self.balls.accel(dt / 2.0)
        # Progress Position
        self.balls.forward(dt)
        self.balls.resetforce()
        # Force
        self.pot, virsum = self.balls.interact(self.cell)
        # Progress Momenta (half)
        self.balls.accel(dt / 2.0)
        # temperature scaling
        # if self.kT is not None:
        if False:
            kin = self.balls.kinetic()
            dof = self.balls.N * self.cell.shape[0]
            factor = (self.kT * dof / 2.0) / kin
            factor = 1.0 - (1.0 - factor) * 0.001
            self.balls.rescale(factor)
        # Data output
        if self.logfile is not None:
            dof = self.balls.dim * self.balls.N
            kin = self.balls.kinetic()
            kT = 2.0 * kin / dof
            z = 1.0 - virsum / (dof * kT)
            self.logfile.write(
                "%s %s %s %s %s %s\n" %
                (self.step, kT, z, kin + self.pot, kin, self.pot))
        if self.velfile is not None and self.step % self.velinterval == 0:
            for i in range(0, N):
                self.velfile.write("%s\n" %
                                   sqrt(2.0 * self.balls[i].kinetic()))
        # histogram
        for v in self.balls.vel:
            self.histx.accum(v[0], 1.0)
            if len(v.shape) > 1:
                self.histy.accum(v[1], 1.0)
        self.step += 1

    def draw(self):
        if self.gc is not None:
            dim = self.cell.shape[0]
            if 2 < dim:
                avgvel = 1.0
                if self.kT is not None:
                    avgvel = sqrt(dim * self.kT)
                self.balls.draw(self.cell, self.gc, avgvel)
            else:
                self.balls.draw(self.cell, self.gc, 0.0)
            if dim > 1:
                canvasx = self.cell[0] * self.gc.zoom
                canvasy = self.cell[1] * self.gc.zoom
                if self.hist:
                    self.histx.draw(0, canvasy, canvasx, canvasy / 2)
                    self.histx.draw(canvasx,
                                    canvasy,
                                    canvasx / 2,
                                    canvasy,
                                    vertical=True)
            else:
                canvasx = self.cell[0] * self.gc.zoom
                canvasy = self.gc.zoom
                if self.hist:
                    self.histx.draw(0, canvasy, canvasx, canvasy / 2)
#                    self.histx.draw(canvasx,canvasy,canvasx/2,canvasy,vertical=True)

    def load(self, file):
        self.balls = []
        s = file.readline()
        self.cell = unserialize(s)
        s = file.readline()
        x = unserialize(s)
        x = int(x[0])
        for i in range(0, x):
            self.balls.append(Particle(file=file))
        s = file.readline()

    # serialize
    def __str__(self):
        s = serialize(self.cell)
        s += "%s\n" % len(self.balls)
        for i in range(0, len(self.balls)):
            s += "%s\n" % self.balls[i]
        return s

    def save(self, file):
        file.write("%s\n" % self)
Пример #10
0
class System:
    def __init__(self,
                 cell=None,
                 nballs=10,
                 step=0,
                 gc=None,
                 input=None,
                 logfile=sys.stdout,
                 velfile=None,
                 kT=None,
                 hist=False):
        if hist:
            self.colorscheme = AbsoluteVelocity(max=5 / 2)
            self.histx = Hist(-5, +5, 0.05, self.colorscheme)
            self.histy = Hist(-5, +5, 0.05, self.colorscheme)
            self.hist = True
        else:
            self.colorscheme = None
            self.hist = False
        if input is not None:
            self.load(input)
        else:
            self.lattice(cell, nballs)
            if kT is not None:
                ra.seed(1)
                self.thermalize(kT)
            self.last1 = self.last2 = None
        # initialize force
        self.step = step
        self.gc = gc
        self.logfile = logfile
        self.velfile = velfile
        self.kT = kT
        self.hist = hist

    def lattice(self, cell, nballs):
        self.balls = []
        self.cell = cell
        dim = len(cell)
        if dim == 1:
            self.lattice1d(nballs)
            self.walls = [Wall(coeff=[1.0, 0.0]), Wall(coeff=[1.0, cell[0]])]
            self.area = 2.0 * 1.0
            self.volume = cell[0]

        elif dim == 2:
            self.lattice2d(nballs)
            # d次元の壁はd+1個の係数で指示する。
            # 最後の要素以外は単位ベクトルでなければいけない。
            self.walls = [
                Wall(coeff=[1.0, 0.0, 0.0]),
                Wall(coeff=[1.0, 0.0, cell[0]]),
                Wall(coeff=[0.0, 1.0, 0.0]),
                Wall(coeff=[0.0, 1.0, cell[1]])
            ]
            self.area = 2.0 * (cell[0] + cell[1])
            self.volume = cell[0] * cell[1]
        elif dim == 3:
            self.lattice3d(nballs)
            self.walls = [
                Wall(coeff=[1.0, 0.0, 0.0, 0.0]),
                Wall(coeff=[1.0, 0.0, 0.0, cell[0]]),
                Wall(coeff=[0.0, 1.0, 0.0, 0.0]),
                Wall(coeff=[0.0, 1.0, 0.0, cell[1]]),
                Wall(coeff=[0.0, 0.0, 1.0, 0.0]),
                Wall(coeff=[0.0, 0.0, 1.0, cell[1]])
            ]
            self.area = 2.0 * (cell[0] * cell[1] + cell[1] * cell[2] +
                               cell[0] * cell[2])
            self.volume = cell[0] * cell[1] * cell[2]

    def thermalize(self, kT):
        dim = len(self.cell)
        N = len(self.balls)
        # 1粒子だけにエネルギーを与える。
        self.balls[0].randomize(sqrt(dim * kT * float(N)))

    def rescale(self, factor):
        for b in self.balls:
            b.rescale(factor)

    def lattice1d(self, nballs):
        n = nballs
        x = 0.1
        while 0 < n:
            self.balls += [HardDisk([x], [0.0])]
            n -= 1
            x += 1.12

    def lattice2d(self, nballs):
        n = nballs
        x = 0.1
        y = 0.1
        while 0 < n:
            self.balls += [
                HardDisk([x, y], [0.0, 0.0], colorscheme=self.colorscheme)
            ]
            n -= 1
            x += 1.12
            if self.cell[0] < x:
                x -= self.cell[0] - (1.12 / 2.0)
                y += 1.12 * sqrt(3.0) / 2.0

    def lattice3d(self, nballs):
        n = nballs
        x = 0.1
        y = 0.1
        z = 0.1
        while 0 < n:
            self.balls += [HardDisk([x, y, z], [0.0, 0.0, 0.0])]
            n -= 1
            x += 1.12
            if self.cell[0] < x:
                x -= self.cell[0] - (1.12 / 2.0)
                y += 1.12 * sqrt(3.0) / 2.0
                if self.cell[0] < y:
                    y = 0.1
                    z += 1.12 * sqrt(3.0) / 2.0

    def OneCollision(self, deltat):
        dtmin = deltat
        object1 = 0
        object2 = 0
        # 粒子のいずれかが壁にぶつかるまでの最短時間を調べる。
        for b in self.balls:
            for w in self.walls:
                if b != self.last1 or w != self.last2:
                    dt = b.collideWall(w)
                    if 0 < dt < dtmin:
                        dtmin = dt
                        object1 = b
                        object2 = w
        # 粒子同士が衝突するまでの最短時間を調べる。
        N = len(self.balls)
        for i in range(0, N):
            for j in range(i + 1, N):
                if self.balls[i] != self.last1 or self.balls[j] != self.last2:
                    dt = self.balls[i].collide(self.balls[j])
                    if 0 < dt < dtmin:
                        dtmin = dt
                        object1 = self.balls[i]
                        object2 = self.balls[j]
        # 粒子をdtminだけ進める。
        for b in self.balls:
            b.forward(dtmin)
        impulse = 0.0
        # 衝突相手が粒子なら
        if isinstance(object2, HardDisk):
            (v1, t1, v2, t2) = object1.reflect(object2)
            if self.velfile is not None:
                self.velfile.write("%s %s\n" % (v1, t1))
                self.velfile.write("%s %s\n" % (v2, t2))
            self.last1 = object1
            self.last2 = object2
        # 壁に衝突する場合は、壁への力積から圧力が出せる。
        elif isinstance(object1, HardDisk):
            impulse = object1.reflectWall(object2)
            self.last1 = object1
            self.last2 = object2
        # 最後に衝突した物体と、消費した時間を返す。
        return (dtmin, impulse)

    def OneStep(self, dt):
        bunbo = dt * self.area
        sumpulse = 0.0
        ncollision = 0
        while 0.0 < dt:
            # 次の衝突またはdtまで粒子を進める。
            (progress, impulse) = self.OneCollision(dt)
            dt -= progress
            sumpulse += impulse
            if impulse == 0.0:
                ncollision += 1
            if self.hist:
                for b in self.balls:
                    self.histx.accum(b.vel[0], progress)
                    if len(b.vel) > 1:
                        self.histy.accum(b.vel[1], progress)
        # Data output
        if self.logfile is not None:
            dim = len(self.cell)
            N = len(self.balls)
            kin = 0.0
            for b in self.balls:
                kin += b.kinetic()
            kT = 2.0 * kin / (dim * N)
            #z = 1.0 + virsum / (dim * N * kT )
            pressure = sumpulse / bunbo
            self.logfile.write("%s %s %s %s %s\n" %
                               (self.step, kT, pressure * self.volume /
                                (N * kT), kin, ncollision))
        # histogram
        if self.hist:
            for b in self.balls:
                self.histx.accum(b.vel[0], 1.0)
                if len(b.vel) > 1:
                    self.histy.accum(b.vel[1], 1.0)
        self.step += 1

    def draw(self):
        if self.gc is not None:
            dim = len(self.cell)
            if 2 < dim:
                avgvel = 0.0
                if self.kT > 0.0:
                    avgvel = sqrt(dim * self.kT)
                tmp = list(self.balls)
                tmp.sort(key=lambda x: -x.pos[2])
                for b in tmp:
                    b.draw(self.cell, self.gc, avgvel)
            else:
                for b in self.balls:
                    b.draw(self.cell, self.gc, 0.0)
            if dim > 1:
                canvasx = self.cell[0] * self.gc.zoom
                canvasy = self.cell[1] * self.gc.zoom
                if self.hist:
                    self.histx.draw(0, canvasy, canvasx, canvasy / 2)
                    self.histx.draw(canvasx,
                                    canvasy,
                                    canvasx / 2,
                                    canvasy,
                                    vertical=True)
            else:
                canvasx = self.cell[0] * self.gc.zoom
                canvasy = self.gc.zoom
                if self.hist:
                    self.histx.draw(0, canvasy, canvasx, canvasy / 2)
#                    self.histx.draw(canvasx,canvasy,canvasx/2,canvasy,vertical=True)

    def load(self, file):
        s = file.readline()
        self.cell = unserialize(s)

        self.balls = []
        s = file.readline()
        x = unserialize(s)
        x = int(x[0])
        for i in range(0, x):
            self.balls.append(HardDisk(file=file))

        self.walls = []
        s = file.readline()
        x = unserialize(s)
        x = int(x[0])
        for i in range(0, x):
            self.walls.append(Wall(file=file))

        s = file.readline()
        x = unserialize(s)
        self.area = float(x[0])

        s = file.readline()
        x = unserialize(s)
        self.volume = float(x[0])

        s = file.readline()

    # serialize
    def __str__(self):
        s = serialize(self.cell)
        s += "%s\n" % len(self.balls)
        for i in range(0, len(self.balls)):
            s += "%s\n" % self.balls[i]
        s += "%s\n" % len(self.walls)
        for i in range(0, len(self.walls)):
            s += "%s\n" % self.walls[i]
        s += "%s\n" % self.area
        s += "%s\n" % self.volume
        return s

    def save(self, file):
        file.write("%s\n" % self)
Пример #11
0
from librip.gens import gen_random
from histogram import Hist

a = [a for a in gen_random(1, 50, 300)]

username_friend_gist = Hist(a)
username_friend_gist.printGist()

# show gist
title = "Гистограмма"
title_x = "Числа"
title_y = "Количество чисел"
username_friend_gist.showBar(title, title_x, title_y)