예제 #1
0
def mc_method_meas(Lph, Tph, meas, smp, q=2):
    s = (int(Lph / 2) + 1) % 2
    # effective inverse temperature (renormalized)
    beta2 = np.log((q**2 + 1) / (2 * q))
    # array of exponents for numerator and denominator
    exp_sumA, exp_sum0 = np.zeros(Tph + 1, float), np.zeros(Tph + 1, float)
    # initial conditions
    exp_sumA[0], exp_sum0[0] = smp, smp
    # initial conditions
    P0, PA = np.zeros(smp, int), np.zeros(smp, int)
    # initial conditions: denominator configuration
    Omega0 = np.zeros(smp * Lph, bool)
    # initial conditions: numerator configuration
    OmegaA = np.repeat(True, 2 * smp)
    OmegaA[::2] = False
    OmegaA = np.repeat(OmegaA, int(Lph / 2))
    # time evolution
    for t in range(Tph):
        # adding measurements
        C = np.tile(meas[t], smp)
        K = np.random.randint(2, size=np.count_nonzero(C)) > 0
        C[C] = K
        # updating configurations
        Omega0[C], OmegaA[C] = NOT(Omega0[C]), NOT(OmegaA[C])
        P0, Omega0 = unitary_update(P0, Omega0, t, s, smp, Lph)
        PA, OmegaA = unitary_update(PA, OmegaA, t, s, smp, Lph)
        # storing the information in array of exponentials
        exp_sumA[t + 1] = np.sum(np.exp(-beta2 * PA))
        exp_sum0[t + 1] = np.sum(np.exp(-beta2 * P0))
    # computing Renyi entropy
    RE = -np.log(exp_sumA) + np.log(exp_sum0)
    # taking last value
    S = RE[-1]
    return S
예제 #2
0
파일: geom.py 프로젝트: nahman/lightbeam
def get_masks(rsqh, R2):
    maskh = np.full(rsqh.shape, False)
    maskh[rsqh <= R2] = True

    mask_in = AND(maskh[1:, 1:],
                  AND(maskh[1:, :-1], AND(maskh[:-1, 1:], maskh[:-1, :-1])))
    mask_out = AND(
        NOT(maskh[1:, 1:]),
        AND(NOT(maskh[1:, :-1]), AND(NOT(maskh[:-1, 1:]),
                                     NOT(maskh[:-1, :-1]))))
    mask_b = NOT(OR(mask_in, mask_out))

    return mask_in, mask_b
예제 #3
0
파일: optics.py 프로젝트: nahman/lightbeam
 def get_boundary(self,z):
     ''' given z, get mask which will select pixels that lie on top of the primitive boundary
         you must set the sampling first before you call this!'''
     
     xhg,yhg = self.xymesh.xhg,self.xymesh.yhg
     maskh = self._contains(xhg,yhg,z)
     mask_r = (NOT ( AND(AND(maskh[1:,1:] == maskh[:-1,1:], maskh[1:,1:] == maskh[1:,:-1]),maskh[:-1,:-1]==maskh[:-1,1:])))
     return mask_r
예제 #4
0
def PredictionToString(pred, is_test=False):
    ArrayToString = lambda x: ' '.join(str(y) for y in x)
    content = pred.T.ravel()
    index = np.array(range(len(content))) + 1
    content_pre = np.concatenate([[0], content[:-1]])
    content_post = np.concatenate([content[1:], [0]])
    index_pre = index[AND(content, NOT(content_pre))]
    index_post = index[AND(content, NOT(content_post))]
    length = index_post - index_pre + 1
    if is_test:
        assert len(index_pre) == len(index_post)
        for l in length:
            assert l >= 1
    picked = []
    for i in range(len(index_pre)):
        picked.append(index_pre[i])
        picked.append(length[i])
    return ArrayToString(picked)
def cellular_automaton_method(L, tm, measured, smp_std, q=2):
    s = (int(L / 2) + 1) % 2
    beta2 = np.log((q**2 + 1) / (2 * q))
    exp_sumA, exp_sum0 = np.zeros(tm + 1, float), np.zeros(tm + 1, float)
    exp_sumA[0], exp_sum0[0] = smp_std, smp_std
    P0, PA = np.zeros(smp_std, int), np.zeros(smp_std, int)
    Omega0 = np.zeros(smp_std * L, bool)
    OmegaA = np.repeat(True, 2 * smp_std)
    OmegaA[::2] = False
    OmegaA = np.repeat(OmegaA, int(L / 2))
    for t in range(tm):
        C = np.tile(measured[t], smp_std)
        K = np.random.randint(2, size=np.count_nonzero(C)) > 0
        C[C] = K
        Omega0[C], OmegaA[C] = NOT(Omega0[C]), NOT(OmegaA[C])
        P0, Omega0 = unitary_update(P0, Omega0, t, s, smp_std, L)
        PA, OmegaA = unitary_update(PA, OmegaA, t, s, smp_std, L)
        exp_sumA[t + 1] = np.sum(np.exp(-beta2 * PA))
        exp_sum0[t + 1] = np.sum(np.exp(-beta2 * P0))
    RE = -np.log(exp_sumA) + np.log(exp_sum0)
    return RE[-1]
예제 #6
0
 def comp(self, cond):
     return NOT(cond)
예제 #7
0
def generate_lattice_couplings(L,T,meas,G,beta,q=2,plot=False,ax=[]):
    
    # =============================================================================
    # Generating couplings for the effective lattice    
    # L,T -- dimensions of the lattice 
    # meas -- boolean matrix of measurement occurence
    # G -- (large) parameter controling the gap between relevant and irrelevant states  
    # q -- qudit dimension 
    # plot -- if True, the lattice couplings would be visualized
    # =============================================================================
    
    # 01 -- parameters
    
    # 1.01 gaps
    G1,G2,G3 = G+1e-3,G+1e-4,G+1e-5
    # 1.02 effective temperature
    beta = np.log((q**2+1)/q)
    # 1.03 renormalize coupling
    J_eff = -0.5-G1+1/(2*beta)*np.log(np.cosh(beta/2+2*beta*G1)/np.cosh(beta/2))
    
    # 02 -- x and y coordinates of the lattice vertices
    basis_x = np.tile(np.arange(L),T)
    basis_y = np.repeat(np.arange(T),L)
    X,Y = np.meshgrid(basis_x,basis_y)
    
    # 03 -- full adjacency matrix for the lattice
    adj_matrix  = AND(np.abs(X-X.T)==1,np.abs(Y-Y.T)==0)
    adj_matrix += AND(np.abs(X-X.T)==0,np.abs(Y-Y.T)==1)
    adj_matrix += np.triu((X-X.T)*(Y-Y.T)==2*(Y%2)-1)
    adj_matrix = adj_matrix+adj_matrix.T
    
    # 04 -- top/bottom horizontal couplings
    top_edge    = AND(Y==T-1,AND(np.abs(X-X.T)==1,np.abs(Y-Y.T)==0))
    bottom_edge = AND(Y==0  ,AND(np.abs(X-X.T)==1,np.abs(Y-Y.T)==0))
    if L%2==1:
        bottom_edge[L-2,L-1],bottom_edge[L-1,L-2] = False,False
        
    # 05 -- angle couplings     
    inkline_lines = AND(adj_matrix,np.abs(Y-Y.T)==1)
    
    # 06 -- horizontal couplings
    horizon_lines = AND(adj_matrix,np.abs(Y-Y.T)==0)
    
    # 07 -- special boundary edges
    if L%2==1:
        spec_bd_lines = np.triu(AND(inkline_lines,AND(X==0,Y%2==0)))+np.triu(AND(inkline_lines,AND(np.abs(X.T-X)==1,AND(X==L-1,Y.T%2==1))))
        spec_bd_lines = spec_bd_lines + spec_bd_lines.T
    if L%2==0:
        spec_bd_lines = np.triu(AND(inkline_lines,AND(X==L-1,Y%2==1)))+np.triu(AND(inkline_lines,AND(np.abs(X.T-X)==1,AND(X==0,Y%2==1))))
        spec_bd_lines = spec_bd_lines + spec_bd_lines.T
    
    # 08 -- central link separating subsystems A and B
    x0 = int(L/2)-1
    central_link = np.zeros([T*L,T*L],bool)
    central_link[x0,x0+1],central_link[x0+1,x0] = True,True

    # 09 -- edges that should be excluded from the lattice
    right_edge  = OR(AND(X==L-1,X.T==L-1),AND(AND(OR(X==L-1,X.T==L-1),np.abs(Y-Y.T)==0),OR((Y%2)==0,(Y.T)%2==0)))
    left_edge   = OR(AND(X==0,X.T==0),AND(AND(OR(X==0,X.T==0),np.abs(Y-Y.T)==0),OR((Y%2)==1,(Y.T)%2==1)))
    if L%2==1:
        edge_exclusion = right_edge
    if L%2==0:
        edge_exclusion = left_edge

    # 10 -- lattice couplings ----------------------------------------------------
    
    # 10.1 -- base square lattice
    edges_1 = np.zeros([L*T,L*T],complex)
    edges_1[XOR(inkline_lines,AND(inkline_lines,edge_exclusion))] = -0.5
    
    # 10.2 -- projector prohibiting '\lambda'-configurations
    
    # 10.2.1 horizontal edges
    edges_2 = np.zeros([L*T,L*T],complex)
    edges_2[AND(XOR(horizon_lines,AND(horizon_lines,edge_exclusion)),NOT(top_edge))] = G1
    # 10.2.2 angle edges
    edges_3 = np.zeros([L*T,L*T],complex)
    edges_3[XOR(spec_bd_lines,XOR(inkline_lines,AND(inkline_lines,edge_exclusion)))] = -G1

    # 10.3 -- boundary conditions
    edges_4 = np.zeros([L*T,L*T],complex)
    edges_4[spec_bd_lines] = -G3  
 
    # 10.4 -- combining couplings and applying measurements 
    J_matrix = edges_1 + edges_2 + edges_3 + edges_4
    J_matrix,K_matrix = add_measurements(meas,J_matrix,L,T,G1,J_eff)
    
    # 10.5 -- final state boundary conditions
    edges_5 = np.zeros([L*T,L*T],complex)
    edges_5[XOR(bottom_edge,central_link)] = -G2
    
    # 10.5.1 -- numerator: division on two halves
    edges_6 = np.zeros([L*T,L*T],complex)
    edges_6[central_link] = G2
    # 10.5.2 -- denominator: no division
    edges_7 = np.zeros([L*T,L*T],complex)
    edges_7[central_link] = -G2

    # 10.6 final setting the couplings
    J_matrix1 = J_matrix + edges_5 + edges_6
    J_matrix2 = J_matrix + edges_5 + edges_7

    # 11 -- plotting the lattice (optional)
    if plot:
        J_matrix_display = J_matrix1.copy()
        show_lattice(ax,J_matrix_display==-0.5-G1,L,T,col='k',label=r'$-1/2-\Gamma_1$')
        show_lattice(ax,J_matrix_display==G1,L,T,col='b',label=r'$\Gamma_1$')
        show_lattice(ax,J_matrix_display==J_eff,L,T,col='r',label=r'$J_R$')
        show_lattice(ax,J_matrix_display==-0.5-G3,L,T,col='brown',label=r'$-1/2-\Gamma_3$')
        show_lattice(ax,J_matrix_display== G1-G2,L,T,col='cyan',label=r'$\Gamma_1-\Gamma_2$')
        show_lattice(ax,J_matrix_display==-G2,L,T,col='green',label=r'$-\Gamma_2$')
        show_lattice(ax,J_matrix_display== G1+G2,L,T,col='violet',label=r'$\Gamma_1+\Gamma_2$')
        show_lattice(ax,J_matrix_display==+G2,L,T,col='violet',label=r'$\Gamma_2$')
        show_lattice(ax,AND(K_matrix,NOT(edges_5 + edges_6)!=0),L,T,col='k',ls='--',label=r'$0$')
        plt.legend(loc=(0.005*L,-0.1),ncol=3,numpoints=None)
        plt.ylim(-T-1,0.1)
    
    # 12 -- adding regularization
    J_matrix1 += 1e-10*np.int_(adj_matrix)
    J_matrix2 += 1e-10*np.int_(adj_matrix)
    
    return J_matrix1,J_matrix2
예제 #8
0
from numpy import logical_or as OR
from numpy import logical_not as NOT

### BIT 0

f = pd.DataFrame({
    's0': [0, 0, 1, 1],
    'r0': [0, 1, 0, 1],
}).astype(bool)

inputs = list(f.columns)

# d0 = (r0 && not(s0)) || (not(r0) && s0)
f = f.assign(d0=XOR(f.r0, f.s0))
# c1 = (r0 && not(s0))
f = f.assign(c1=AND(f.r0, NOT(f.s0)))

f = f.sort_values(by=inputs, ascending=True, ignore_index=True).astype(int)

f.columns = [F"\\cee{{{c[0]}_{c[1]}}}" for c in f.columns]

print(f.to_latex(column_format="cc|cc", index=False, escape=False))

### BIT 1

f = pd.DataFrame({
    's1': [0, 0, 0, 0, 1, 1, 1, 1],
    'r1': [0, 0, 1, 1, 0, 0, 1, 1],
    'c1': [0, 1, 0, 1, 0, 1, 0, 1],
}).astype(bool)
def trnglr_lattice(L, Lph, T, p, prob, plot=False):
    #-------------------------------------------------------------------------
    basisx = np.hstack(
        (np.tile(np.hstack((np.arange(L), np.arange(-p, L - 1 + p))),
                 int(T / 2)), np.arange(L)))
    basisy = np.hstack((np.repeat(np.arange(T),
                                  np.tile([L, L - 1 + 2 * p],
                                          int(T / 2))), np.repeat(T, L)))
    S = len(basisx)
    #--------------------------------------------------------------------------
    X, Y = np.meshgrid(basisx, basisy)
    Rset = OR(AND(AND(X - X.T == 1,
                      np.abs(Y - Y.T) == 1), Y % 2 == 1),
              AND(AND(X - X.T == 0,
                      np.abs(Y - Y.T) == 1), Y % 2 == 0))
    Lset = OR(AND(AND(X - X.T == -1,
                      np.abs(Y - Y.T) == 1), Y % 2 == 0),
              AND(AND(X - X.T == 0,
                      np.abs(Y - Y.T) == 1), Y % 2 == 1))
    #---
    Rset0 = np.triu(Rset)
    Rset0[Rset0] = np.random.choice([True, False],
                                    np.count_nonzero(Rset0),
                                    p=(1 - prob, prob))
    Rset0 += Rset0.T
    #---
    Lset0 = np.triu(Lset)
    Lset0[Lset0] = np.random.choice([True, False],
                                    np.count_nonzero(Lset0),
                                    p=(1 - prob, prob))
    Lset0 += Lset0.T
    #---
    Hset0 = symmetrize(np.dot(np.triu(Rset0), np.tril(Lset0)))
    #---
    Rset1 = np.triu(Rset)
    Rset1 += Rset1.T
    #---
    Lset1 = np.triu(Lset)
    Lset1 += Lset1.T
    #---
    Hset1 = symmetrize(np.dot(np.triu(Rset1), np.tril(Lset1)))
    #---
    not_Rset = XOR(Rset0, Rset1)
    not_Lset = XOR(Lset0, Lset1)
    not_Hset = XOR(Hset0, Hset1)
    #---

    diag_links_renorm = symmetrize(
        OR(AND(np.dot(np.tril(not_Hset), np.triu(not_Rset)), Lset0),
           AND(np.dot(np.triu(not_Hset), np.triu(not_Lset)), Rset0)))
    horizontal_links = AND(
        XOR(allOR([Rset1, Lset1, Hset1]),
            OR(diag_links_renorm, allOR([not_Rset, not_Lset, not_Hset]))),
        Hset0)
    empty_links = allOR([not_Rset, not_Lset, not_Hset])
    fm_links = AND(AND(np.abs(X - X.T) == 1,
                       np.abs(Y - Y.T) == 0),
                   AND(Y == 0, X * X.T != int(L / 2) * (int(L / 2) - 1)))
    antifm_links = AND(AND(np.abs(X - X.T) == 1,
                           np.abs(Y - Y.T) == 0),
                       AND(Y == 0, X * X.T == int(L / 2) * (int(L / 2) - 1)))
    bc_links = symmetrize(
        AND(
            AND(
                OR(AND(X - X.T == 0,
                       np.abs(Y - Y.T) == 1),
                   (X - X.T) * (Y - Y.T) == 1 - 2 * (Y % 2)),
                AND(Y % 2 != p, OR(X == -p, X == L - 1))), NOT(empty_links)))
    diagonal_links = AND(
        AND(
            XOR(allOR([Rset1, Lset1, Hset1]),
                OR(diag_links_renorm, allOR([not_Rset, not_Lset, not_Hset]))),
            allOR([Rset0, Lset0])), NOT(bc_links))

    sole_measurements = int(np.count_nonzero(diag_links_renorm) / 2)
    double_measurements = int(
        np.count_nonzero(not_Hset) / 2) - sole_measurements
    #-----------------------------------------------------------------------------------------------------------------------------------------

    empty_links_diag = AND(empty_links, allOR([Rset1, Lset1]))
    measured = np.zeros([T, Lph], bool)
    for i in range(S):
        v = np.zeros(S, bool)
        v[i + 1:] = empty_links_diag[i][i + 1:]
        if p == 0 and np.count_nonzero(v) > 0:
            measured[basisy[i]][2 * basisx[i] + basisx[v] - basisx[i] -
                                (basisy[i] + 1) % 2 + 1 - basisy[i] % 2] = True
        if p == 1 and np.count_nonzero(v) > 0:
            measured[basisy[i]][2 * basisx[i] + basisx[v] - basisx[i] +
                                1] = True

    #-----------------------------------------------------------------------------------------------------------------------------------------

    sets = [
        diagonal_links, horizontal_links, diag_links_renorm, empty_links,
        fm_links, antifm_links, bc_links
    ]

    if plot:
        visualize(basisx, basisy, horizontal_links, c='b')
        visualize(basisx, basisy, diagonal_links, c='k')
        visualize(basisx, basisy, empty_links, c='r', ls="--")
        visualize(basisx, basisy, diag_links_renorm, c='g', ls=":")
        visualize(basisx, basisy, fm_links, c='orange', ls=":")
        visualize(basisx, basisy, antifm_links, c='cyan', ls=":")
        visualize(basisx, basisy, bc_links, c='violet', ls=":")

    return basisx, basisy, sets, measured, sole_measurements, double_measurements
예제 #10
0
def create_lattice(L, T):

    #--------------------------------------------------------------------------
    # forming adjacency matrix
    basis_x = np.tile(np.arange(L), T)
    basis_y = np.repeat(np.arange(T), L)
    X, Y = np.meshgrid(basis_x, basis_y)
    adj_matrix = AND(np.abs(X - X.T) == 1, np.abs(Y - Y.T) == 0)
    adj_matrix += AND(np.abs(X - X.T) == 0, np.abs(Y - Y.T) == 1)
    adj_matrix += np.triu((X - X.T) * (Y - Y.T) == 2 * (Y % 2) - 1)
    adj_matrix = adj_matrix + adj_matrix.T

    # -- different parts of the lattice ---

    right_edge = OR(
        AND(X == L - 1, X.T == L - 1),
        AND(AND(OR(X == L - 1, X.T == L - 1),
                np.abs(Y - Y.T) == 0), OR((Y % 2) == 0, (Y.T) % 2 == 0)))
    left_edge = OR(
        AND(X == 0, X.T == 0),
        AND(AND(OR(X == 0, X.T == 0),
                np.abs(Y - Y.T) == 0), OR((Y % 2) == 1, (Y.T) % 2 == 1)))
    top_edge = AND((X - X.T) == 1, AND(Y == T - 1, Y.T == T - 1))
    bottom_edge = AND((X - X.T) == 1, AND(Y == 0, Y.T == 0))
    if L % 2 == 1:
        bottom_edge[L - 2, L - 1], bottom_edge[L - 1, L - 2] = False, False
    inkline_lines = AND(adj_matrix, np.abs(Y - Y.T) == 1)
    horizon_lines = AND(adj_matrix, np.abs(Y - Y.T) == 0)
    if L % 2 == 1:
        spec_bd_lines = np.triu(AND(inkline_lines, AND(
            X == 0, Y % 2 == 0))) + np.triu(
                AND(inkline_lines,
                    AND(np.abs(X.T - X) == 1, AND(X == L - 1, Y.T % 2 == 1))))
        spec_bd_lines = spec_bd_lines + spec_bd_lines.T
    if L % 2 == 0:
        spec_bd_lines = np.triu(AND(inkline_lines, AND(
            X == L - 1, Y % 2 == 1))) + np.triu(
                AND(inkline_lines,
                    AND(np.abs(X.T - X) == 1, AND(X == 0, Y % 2 == 1))))
        spec_bd_lines = spec_bd_lines + spec_bd_lines.T

    x0 = int(L / 2) - 1
    central_link = np.zeros([T * L, T * L], bool)
    central_link[x0, x0 + 1], central_link[x0 + 1, x0] = True, True

    #--------------------------------------------------------------------------
    # setting couplings matrix

    # qudit dimension
    q = 2
    # effective temperature
    beta = np.log((q**2 + 1) / q)
    # default zero
    zero = 1e-8
    # projecting parameters
    G1, G2, G3 = 5, 5, 5
    E = mp.mpc(0)

    if L % 2 == 1:
        edge_exclusion = right_edge
    if L % 2 == 0:
        edge_exclusion = left_edge

    # ------ setting edges -------

    edges_1 = np.zeros([L * T, L * T], complex)
    edges_1[XOR(inkline_lines, AND(inkline_lines,
                                   edge_exclusion))] = +beta * 0.5
    E += -beta * 0.5 * np.count_nonzero(edges_1) / 2

    edges_2 = np.zeros([L * T, L * T], complex)
    edges_2[AND(XOR(horizon_lines, AND(horizon_lines, edge_exclusion)),
                NOT(top_edge))] = -beta * G1
    E += -beta * G1 * np.count_nonzero(edges_2) / 2
    #
    edges_3 = np.zeros([L * T, L * T], complex)
    edges_3[XOR(spec_bd_lines,
                XOR(inkline_lines, AND(inkline_lines,
                                       edge_exclusion)))] = +beta * G1
    #
    edges_4 = np.zeros([L * T, L * T], complex)
    edges_4[spec_bd_lines] = +beta * G3
    E += -beta * G3 * np.count_nonzero(edges_4) / 2
    #
    edges_5 = np.zeros([L * T, L * T], complex)
    edges_5[central_link] = -beta * G2
    E += -beta * G2 * np.count_nonzero(edges_5) / 2
    #
    edges_6 = np.zeros([L * T, L * T], complex)
    edges_6[XOR(bottom_edge, central_link)] = +beta * G2
    E += -beta * G2 * np.count_nonzero(edges_6) / 2
    #
    J_matrix = zero * np.float_(
        adj_matrix
    ) + edges_1 + edges_2 + edges_3 + edges_4 + edges_5 + edges_6  #+edges_5+edges_6
    print J_matrix[edges_6 != 0]
    #    E += -(int(T/2)+L%2)*mp.log(2)
    #    print 'x'
    #
    plt.subplot(2, 3, 1)
    show_lattice(adj_matrix, L, T, col='0.9')
    show_lattice(edges_1 != 0, L, T, col='k')
    plt.subplot(2, 3, 2)
    show_lattice(adj_matrix, L, T, col='0.9')
    show_lattice(edges_2 != 0, L, T, col='b')
    plt.subplot(2, 3, 3)
    show_lattice(adj_matrix, L, T, col='0.9')
    show_lattice(edges_3 != 0, L, T, col='r')
    plt.subplot(2, 3, 4)
    show_lattice(adj_matrix, L, T, col='0.9')
    show_lattice(edges_4 != 0, L, T, col='g')
    plt.subplot(2, 3, 5)
    show_lattice(adj_matrix, L, T, col='0.9')
    show_lattice(edges_5 != 0, L, T, col='orange')
    show_lattice(edges_6 != 0, L, T, col='cyan')
    plt.subplot(2, 3, 6)
    show_lattice(np.abs(J_matrix) > zero, L, T, col='0.')
    #
    log_partition_function(E, J_matrix, L, T)

    j_matrix = mp.matrix(mexp(-J_matrix).tolist())

    return E, j_matrix