Exemple #1
0
def approx_solve(fname, gammas=[0.], k=-1, adj=None):
    ''' '''
    # process the QCADesigner file
    try:
        cells, spacing, zones, J, _ = parse_qca_file(fname, one_zone=True)
    except:
        print('Failed to process QCA file: {0}'.format(fname))
        return None

    # convert J to specified adjacency
    J = convert_adjacency(cells, spacing, J, adj=adj)

    # normalize J
    J /= np.max(np.abs(J))

    # group each type of cell: normal = NORMAL or OUTPUT
    drivers, fixeds, normals, outputs = [], [], [], []
    for i, c in enumerate(cells):
        if c['cf'] == CELL_FUNCTIONS['QCAD_CELL_INPUT']:
            drivers.append(i)
        elif c['cf'] == CELL_FUNCTIONS['QCAD_CELL_FIXED']:
            fixeds.append(i)
        else:
            if c['cf'] == CELL_FUNCTIONS['QCAD_CELL_OUTPUT']:
                outputs.append(i)
            normals.append(i)

    print(fixeds)
    # map from output cell labels to indices in normals list
    output_map = {i: n for n, i in enumerate(normals) if i in outputs}

    J_d = J[drivers, :][:, normals]  # matrix for mapping drivers to h
    J_f = J[fixeds, :][:, normals]  # matrix for mapping fixed cells to h
    J_n = J[normals, :][:, normals]  # J internal to set of normal cells

    P_f = [(cells[i]['pol'] if 'pol' in cells[i] else 0.)
           for i in fixeds]  # polarization of fixed cells

    h0 = np.dot(P_f, J_f).reshape([
        -1,
    ])  # h contribution from fixed cells

    # for each polarization, solve for all gammas
    for pol in gen_pols(len(drivers)):
        h = h0 + np.dot(pol, J_d)
        for gamma in gammas:
            t = time()
            e_vals, e_vecs, modes = rp_solve(h, J_n, gam=gamma, verbose=True)
            print('\n')
            print(e_vals[0:2], time() - t)
            if False:
                t = time()
                e_vals, e_vecs = solve(h,
                                       J_n,
                                       gamma=gamma,
                                       minimal=False,
                                       exact=False)
                print(e_vals[0:2], time() - t)
Exemple #2
0
def load_qca(fname):

    cells, spacing, zones, J, feedback = parse_qca_file(fname, one_zone=True)

    J = convert_adjacency(cells, spacing, J, adj='full')
    J = -J / np.max(np.abs(J))

    if SAVE_COEF:
        fname = os.path.splitext(os.path.basename(fname))[0]
        fname += os.path.extsep + 'coef'
        to_coef(fname, J)

    return J
Exemple #3
0
def load_qca(fname):

    cells, spacing, zones, J, feedback = parse_qca_file(fname, one_zone=True)

    J = convert_adjacency(cells, spacing, J, adj="full")
    J = -J / np.max(np.abs(J))

    if SAVE_COEF:
        fname = os.path.splitext(os.path.basename(fname))[0]
        fname += os.path.extsep + "coef"
        to_coef(fname, J)

    return J
Exemple #4
0
def exact_solve(fname, gammas = [0.], k=-1, adj=None):
    '''Exactly solve the first k eigenstates for a QCA circuit for all possible
    input configurations and all specified transverse fields. Assumes all cells
    have the same transverse field.'''
    
    # process the QCADesigner file
    try:
        cells, spacing, zones, J, _ = parse_qca_file(fname, one_zone=True)
    except:
        print('Failed to process QCA file: {0}'.format(fname))
        return None
        
    # convert J to specified adjacency
    J = convert_adjacency(cells, spacing, J, adj=adj)
    
    # normalize J
    J /= np.max(np.abs(J))
        
    # group each type of cell: normal = NORMAL or OUTPUT
    drivers, fixeds, normals, outputs = [], [], [], []
    for i,c in enumerate(cells):
        if c['cf'] == CELL_FUNCTIONS['QCAD_CELL_INPUT']:
            drivers.append(i)
        elif c['cf'] == CELL_FUNCTIONS['QCAD_CELL_FIXED']:
            fixeds.append(i)
        else:
            if c['cf'] == CELL_FUNCTIONS['QCAD_CELL_OUTPUT']:
                outputs.append(i)
            normals.append(i)
    
    # map from output cell labels to indices in normals list
    output_map = {i: n for n, i in enumerate(normals) if i in outputs}
    
    J_d = J[drivers, :][:, normals]     # matrix for mapping drivers to h
    J_f = J[fixeds, :][:, normals]      # matrix for mapping fixed cells to h
    J_n = J[normals, :][:, normals]     # J internal to set of normal cells
    
    P_f = [(cells[i]['pol'] if 'pol' in cells[i] else 0.) for i in fixeds]  # polarization of fixed cells
    
    h0 = np.dot(P_f, J_f).reshape([-1,])    # h contribution from fixed cells

    # for each polarization, solve for all gammas
    for pol in gen_pols(len(drivers)):
        h = h0 + np.dot(pol, J_d)
        for gamma in gammas:
            e_vals, e_vecs = solve(h, J_n, gamma=gamma, minimal=True, exact=False)
            # e_vecs[:,i] is the i^th eigenstate
            pols = state_to_pol(e_vecs)
            # pols[:,i] gives the polarizations of all cells for the i^th e-vec
            print('GS: {0:.4f}'.format(e_vals[0]))
            print('pols: {0}'.format(pols[:,0]))
Exemple #5
0
def approx_solve(fname, gammas=[0.0], k=-1, adj=None):
    """ """
    # process the QCADesigner file
    try:
        cells, spacing, zones, J, _ = parse_qca_file(fname, one_zone=True)
    except:
        print("Failed to process QCA file: {0}".format(fname))
        return None

    # convert J to specified adjacency
    J = convert_adjacency(cells, spacing, J, adj=adj)

    # normalize J
    J /= np.max(np.abs(J))

    # group each type of cell: normal = NORMAL or OUTPUT
    drivers, fixeds, normals, outputs = [], [], [], []
    for i, c in enumerate(cells):
        if c["cf"] == CELL_FUNCTIONS["QCAD_CELL_INPUT"]:
            drivers.append(i)
        elif c["cf"] == CELL_FUNCTIONS["QCAD_CELL_FIXED"]:
            fixeds.append(i)
        else:
            if c["cf"] == CELL_FUNCTIONS["QCAD_CELL_OUTPUT"]:
                outputs.append(i)
            normals.append(i)

    print(fixeds)
    # map from output cell labels to indices in normals list
    output_map = {i: n for n, i in enumerate(normals) if i in outputs}

    J_d = J[drivers, :][:, normals]  # matrix for mapping drivers to h
    J_f = J[fixeds, :][:, normals]  # matrix for mapping fixed cells to h
    J_n = J[normals, :][:, normals]  # J internal to set of normal cells

    P_f = [(cells[i]["pol"] if "pol" in cells[i] else 0.0) for i in fixeds]  # polarization of fixed cells

    h0 = np.dot(P_f, J_f).reshape([-1])  # h contribution from fixed cells

    # for each polarization, solve for all gammas
    for pol in gen_pols(len(drivers)):
        h = h0 + np.dot(pol, J_d)
        for gamma in gammas:
            t = time()
            e_vals, e_vecs, modes = rp_solve(h, J_n, gam=gamma, verbose=True)
            print("\n")
            print(e_vals[0:2], time() - t)
            if False:
                t = time()
                e_vals, e_vecs = solve(h, J_n, gamma=gamma, minimal=False, exact=False)
                print(e_vals[0:2], time() - t)
Exemple #6
0
def from_qca_file(fname, adj=None):
    ''' '''

    try:
        cells, spacing, zones, J, _ = parse_qca_file(fname, one_zone=True)
    except:
        print('Failed to process QCA file: {0}'.format(fname))
        return

    # convert J to specified adjacency
    J = convert_adjacency(cells, spacing, J, adj=adj)

    # normalize J
    J /= np.max(np.abs(J))

    # group each type of cell: normal = NORMAL or OUTPUT
    drivers, fixeds, normals, outputs = [], [], [], []
    for i, c in enumerate(cells):
        if c['cf'] == CELL_FUNCTIONS['QCAD_CELL_INPUT']:
            drivers.append(i)
        elif c['cf'] == CELL_FUNCTIONS['QCAD_CELL_FIXED']:
            fixeds.append(i)
        else:
            if c['cf'] == CELL_FUNCTIONS['QCAD_CELL_OUTPUT']:
                outputs.append(i)
            normals.append(i)

    # map from output cell labels to indices in normals list

    J_d = J[drivers, :][:, normals]  # matrix for mapping drivers to h
    J_f = J[fixeds, :][:, normals]  # matrix for mapping fixed cells to h
    J_n = J[normals, :][:, normals]  # J internal to set of normal cells

    P_f = [(cells[i]['pol'] if 'pol' in cells[i] else 0.)
           for i in fixeds]  # polarization of fixed cells

    h0 = np.dot(P_f, J_f).reshape([
        -1,
    ])  # h contribution from fixed cells

    # for each polarization, solve for gamma=0 and extimate remaining spectrum
    for pol in gen_pols(len(drivers)):
        h = h0 + np.dot(pol, J_d)
        yield h, J_n
Exemple #7
0
def main(fname):
    
    try:
        cells, spacing, zones, J, _ = parse_qca_file(fname, one_zone=True)
    except:
        print('Failed to process QCA file: {0}'.format(fname))
        return None
    
    # convert J to specified adjacency
    J = 1.*(convert_adjacency(cells, spacing, J, adj=ADJ) != 0)
    
    G = nx.Graph(J)
    for k in G:
        G.node[k]['w'] = 1./len(G[k])**2
        
    pos = graphviz_layout(G)
    nx.draw(G, pos=pos, with_labels=True)
    plt.show()
    
    chlebikova(G)
Exemple #8
0
def main(fname):

    try:
        cells, spacing, zones, J, _ = parse_qca_file(fname, one_zone=True)
    except:
        print('Failed to process QCA file: {0}'.format(fname))
        return None

    # convert J to specified adjacency
    J = 1. * (convert_adjacency(cells, spacing, J, adj=ADJ) != 0)

    G = nx.Graph(J)
    for k in G:
        G.node[k]['w'] = 1. / len(G[k])**2

    pos = graphviz_layout(G)
    nx.draw(G, pos=pos, with_labels=True)
    plt.show()

    chlebikova(G)
def from_qca_file(fname, adj=None):
    ''' '''

    try:
        cells, spacing, zones, J, _ =  parse_qca_file(fname, one_zone=True)
    except:
        print('Failed to process QCA file: {0}'.format(fname))
        return

    # convert J to specified adjacency
    J = convert_adjacency(cells, spacing, J, adj=adj)

    # normalize J
    J /= np.max(np.abs(J))

    # group each type of cell: normal = NORMAL or OUTPUT
    drivers, fixeds, normals, outputs = [], [], [], []
    for i,c in enumerate(cells):
        if c['cf'] == CELL_FUNCTIONS['QCAD_CELL_INPUT']:
            drivers.append(i)
        elif c['cf'] == CELL_FUNCTIONS['QCAD_CELL_FIXED']:
            fixeds.append(i)
        else:
            if c['cf'] == CELL_FUNCTIONS['QCAD_CELL_OUTPUT']:
                outputs.append(i)
            normals.append(i)

    # map from output cell labels to indices in normals list

    J_d = J[drivers, :][:, normals]     # matrix for mapping drivers to h
    J_f = J[fixeds, :][:, normals]      # matrix for mapping fixed cells to h
    J_n = J[normals, :][:, normals]     # J internal to set of normal cells

    P_f = [(cells[i]['pol'] if 'pol' in cells[i] else 0.) for i in fixeds]  # polarization of fixed cells

    h0 = np.dot(P_f, J_f).reshape([-1,])    # h contribution from fixed cells

    # for each polarization, solve for gamma=0 and extimate remaining spectrum
    for pol in gen_pols(len(drivers)):
        h = h0 + np.dot(pol, J_d)
        yield h, J_n
Exemple #10
0
def compare_spectrum(fname, gmin=0.01, gmax=.5, adj='lim'):
    ''' '''
    
    try:
        cells, spacing, zones, J, _ = parse_qca_file(fname, one_zone=True)
    except:
        print('Failed ot process QCA file: {0}'.format(fname))
        return None
        
    # convert J to specified adjacency
    J = convert_adjacency(cells, spacing, J, adj=adj)
    
    # normalize J
    J /= np.max(np.abs(J))
        
    # group each type of cell: normal = NORMAL or OUTPUT
    drivers, fixeds, normals, outputs = [], [], [], []
    for i,c in enumerate(cells):
        if c['cf'] == CELL_FUNCTIONS['QCAD_CELL_INPUT']:
            drivers.append(i)
        elif c['cf'] == CELL_FUNCTIONS['QCAD_CELL_FIXED']:
            fixeds.append(i)
        else:
            if c['cf'] == CELL_FUNCTIONS['QCAD_CELL_OUTPUT']:
                outputs.append(i)
            normals.append(i)
    
    # map from output cell labels to indices in normals list
    output_map = {i: n for n, i in enumerate(normals) if i in outputs}
    
    J_d = J[drivers, :][:, normals]     # matrix for mapping drivers to h
    J_f = J[fixeds, :][:, normals]      # matrix for mapping fixed cells to h
    J_n = J[normals, :][:, normals]     # J internal to set of normal cells
    
    P_f = [(cells[i]['pol'] if 'pol' in cells[i] else 0.) for i in fixeds]  # polarization of fixed cells
    
    h0 = np.dot(P_f, J_f).reshape([-1,])    # h contribution from fixed cells
    
    gammas = np.linspace(gmin, gmax, STEPS)
    
    for pol in gen_pols(len(drivers)):
        h = h0 + np.dot(pol, J_d)
        SP_E, RP_E = [], []
        times = []
        for gamma in gammas:
            print(gamma)
            t = time()
            rp_vals, rp_vecs, modes = rp_solve(h, J_n, gam=gamma, 
                                               cache_dir=CACHE)
            times.append(time()-t)
            sp_vals, sp_vecs = solve(h, J_n, gamma=gamma)
            SP_E.append(sp_vals)
            RP_E.append(rp_vals)
        LSP = min(len(x) for x in SP_E)
        L = min(LSP, min(len(x) for x in RP_E))
        SP_E = np.array([x[:L] for x in SP_E])
        RP_E = np.array([x[:L] for x in RP_E])
        
        plt.figure('spectrum')
        plt.plot(gammas, SP_E, linewidth=2)
        plt.plot(gammas, RP_E, 'x', markersize=8, markeredgewidth=2)
        plt.xlabel('Gamma', fontsize=FS)
        plt.ylabel('Energy', fontsize=FS)
        plt.title('Circuit Spectrum', fontsize=FS)
        plt.show(block=False)
        
        plt.figure('runtimes')
        plt.plot(gammas, times, 'b', linewidth=2)
        plt.xlabel('Gammas', fontsize=FS)
        plt.ylabel('Runtime (s)', fontsize=FS)
        plt.title('RP-Solver runtime', fontsize=FS)
        plt.show()
Exemple #11
0
def compare_spectrum(fname, gmin=0.01, gmax=0.5, adj="lim"):
    """ """

    try:
        cells, spacing, zones, J, _ = parse_qca_file(fname, one_zone=True)
    except:
        print("Failed ot process QCA file: {0}".format(fname))
        return None

    # convert J to specified adjacency
    J = convert_adjacency(cells, spacing, J, adj=adj)

    # normalize J
    J /= np.max(np.abs(J))

    # group each type of cell: normal = NORMAL or OUTPUT
    drivers, fixeds, normals, outputs = [], [], [], []
    for i, c in enumerate(cells):
        if c["cf"] == CELL_FUNCTIONS["QCAD_CELL_INPUT"]:
            drivers.append(i)
        elif c["cf"] == CELL_FUNCTIONS["QCAD_CELL_FIXED"]:
            fixeds.append(i)
        else:
            if c["cf"] == CELL_FUNCTIONS["QCAD_CELL_OUTPUT"]:
                outputs.append(i)
            normals.append(i)

    # map from output cell labels to indices in normals list
    output_map = {i: n for n, i in enumerate(normals) if i in outputs}

    J_d = J[drivers, :][:, normals]  # matrix for mapping drivers to h
    J_f = J[fixeds, :][:, normals]  # matrix for mapping fixed cells to h
    J_n = J[normals, :][:, normals]  # J internal to set of normal cells

    P_f = [(cells[i]["pol"] if "pol" in cells[i] else 0.0) for i in fixeds]  # polarization of fixed cells

    h0 = np.dot(P_f, J_f).reshape([-1])  # h contribution from fixed cells

    gammas = np.linspace(gmin, gmax, STEPS)

    for pol in gen_pols(len(drivers)):
        h = h0 + np.dot(pol, J_d)
        SP_E, RP_E = [], []
        times = []
        for gamma in gammas:
            print(gamma)
            t = time()
            rp_vals, rp_vecs, modes = rp_solve(h, J_n, gam=gamma, cache_dir=CACHE)
            times.append(time() - t)
            sp_vals, sp_vecs = solve(h, J_n, gamma=gamma)
            SP_E.append(sp_vals)
            RP_E.append(rp_vals)
        LSP = min(len(x) for x in SP_E)
        L = min(LSP, min(len(x) for x in RP_E))
        SP_E = np.array([x[:L] for x in SP_E])
        RP_E = np.array([x[:L] for x in RP_E])

        plt.figure("spectrum")
        plt.plot(gammas, SP_E, linewidth=2)
        plt.plot(gammas, RP_E, "x", markersize=8, markeredgewidth=2)
        plt.xlabel("Gamma", fontsize=FS)
        plt.ylabel("Energy", fontsize=FS)
        plt.title("Circuit Spectrum", fontsize=FS)
        plt.show(block=False)

        plt.figure("runtimes")
        plt.plot(gammas, times, "b", linewidth=2)
        plt.xlabel("Gammas", fontsize=FS)
        plt.ylabel("Runtime (s)", fontsize=FS)
        plt.title("RP-Solver runtime", fontsize=FS)
        plt.show()