Skip to content
Snippets Groups Projects
Select Git revision
  • a1cfdf1d42cb42dce571f9acf3caad0284edd0ee
  • master default protected
  • enxhi_issue460_remove_TOAR-I_access
  • michael_issue459_preprocess_german_stations
  • sh_pollutants
  • develop protected
  • release_v2.4.0
  • michael_issue450_feat_load-ifs-data
  • lukas_issue457_feat_set-config-paths-as-parameter
  • lukas_issue454_feat_use-toar-statistics-api-v2
  • lukas_issue453_refac_advanced-retry-strategy
  • lukas_issue452_bug_update-proj-version
  • lukas_issue449_refac_load-era5-data-from-toar-db
  • lukas_issue451_feat_robust-apriori-estimate-for-short-timeseries
  • lukas_issue448_feat_load-model-from-path
  • lukas_issue447_feat_store-and-load-local-clim-apriori-data
  • lukas_issue445_feat_data-insight-plot-monthly-distribution
  • lukas_issue442_feat_bias-free-evaluation
  • lukas_issue444_feat_choose-interp-method-cams
  • 414-include-crps-analysis-and-other-ens-verif-methods-or-plots
  • lukas_issue384_feat_aqw-data-handler
  • v2.4.0 protected
  • v2.3.0 protected
  • v2.2.0 protected
  • v2.1.0 protected
  • Kleinert_etal_2022_initial_submission
  • v2.0.0 protected
  • v1.5.0 protected
  • v1.4.0 protected
  • v1.3.0 protected
  • v1.2.1 protected
  • v1.2.0 protected
  • v1.1.0 protected
  • IntelliO3-ts-v1.0_R1-submit
  • v1.0.0 protected
  • v0.12.2 protected
  • v0.12.1 protected
  • v0.12.0 protected
  • v0.11.0 protected
  • v0.10.0 protected
  • IntelliO3-ts-v1.0_initial-submit
41 results

test_datastore.py

Blame
  • embedding.py 4.55 KiB
    '''
    Author: Edoardo Pasetto
    '''
    
    import numpy as np
    from dwave.system import DWaveSampler
    from dwave_networkx import pegasus_graph
    from dwave.embedding.pegasus import find_clique_embedding
    from dwave.embedding import is_valid_embedding
    import networkx as nx
    from minorminer import find_embedding
    
    #%%
    embedding_path=''    # insert the path where the embeddings are going to be stored 
    embedding_ind_path=embedding_path+'independent_graph\\'
    
    #%%
    
    def get_clique_emebedding(dim):
        '''
        searches for an embedding of a clique of dimension specified by dim
        '''
    
        var_list=list(range(dim))
    
    
        sampler=DWaveSampler()
        topology=sampler.properties['topology']
        active_qubits=sampler.properties['qubits']
        active_couplers=sampler.properties['couplers']
    
        new_pegasus=pegasus_graph(topology['shape'][0], node_list=active_qubits, edge_list=active_couplers)
        embedding=find_clique_embedding(k=var_list, target_graph= new_pegasus)
    
        return embedding
    
    
    def load_embedding(path):
        '''
        loads an embedding from a .txt file saved using the function save_embedding
        returns the embedding as dict that can then be passed to an EmbeddingComposite
        '''
        r=open(path)
        lines=r.readlines()
    
        r.close()
    
        retrieved_embedding={}
        for l in range(len(lines)):
            data=np.fromstring(lines[l], sep=',').astype('int32')
            data=list(data)
            key=data[0]
            value=data[1:]
    
            retrieved_embedding[key]=value
    
    
    
        return retrieved_embedding
    
    
    
    
    
    def check_clique_embedding(emb):
        '''
        checks if a given clique embedding is valid
        works only for cliques, for a more general case see check_embedding
        '''
    
        sampler=DWaveSampler()
        topology=sampler.properties['topology']
        active_qubits=sampler.properties['qubits']
        active_couplers=sampler.properties['couplers']
    
        new_pegasus=pegasus_graph(topology['shape'][0], node_list=active_qubits, edge_list=active_couplers)
    
        var_list=list(emb.keys())
        complete_graph= nx.complete_graph(var_list)
    
        res=is_valid_embedding(emb, complete_graph, new_pegasus)
    
    
        return res
    
    
    def check_embedding(embedding,bqm):
        '''
        checks if an embedding is valid for a given problem, which is defined by a bqm
        it generates a graph from bqm and then checks if the graph can be minor-embedded in the QPU using the given embedding
        '''
        sampler=DWaveSampler()
        topology=sampler.properties['topology']
        active_qubits=sampler.properties['qubits']
        active_couplers=sampler.properties['couplers']
    
        new_pegasus=pegasus_graph(topology['shape'][0], node_list=active_qubits, edge_list=active_couplers)
    
        var_list=list(bqm.variables)
    
        G=nx.Graph()
        G.add_nodes_from(var_list)
        G.add_edges_from(list(bqm.quadratic.keys()))
    
        res=is_valid_embedding(embedding, G, new_pegasus)
    
    
        return res
    
    
    
    
    def save_embedding(emb, path):
        '''
        saves the embedding as .txt file in the given path:
            for each row the first number is the logical qubit, while the other numbers in the row are the physical qubits used to create the chain
        '''
    
    
        f=open(path+'embedding.txt','w+')
        for k in emb.keys():
            f.write(str(k))
    
            for val in emb[k]:
                f.write(',')
                f.write(str(val))
    
            f.write('\n')
        f.close()
    
    
    
    
    
    #%%
    
    
    def find_independents_cliques_embedding(N,q):
        '''
        Parameters
        ----------
        N : int
            number of independents cliques with no
            shared variables (in the QKS code this corresponds to the number of episodes E)
        q : int
            dimension of the single clique
    
        Returns
        embedding on the pegasus graph
    
        '''
        sampler=DWaveSampler()
        topology=sampler.properties['topology']
        active_qubits=sampler.properties['qubits']
        active_couplers=sampler.properties['couplers']
    
        new_pegasus=pegasus_graph(topology['shape'][0], node_list=active_qubits, edge_list=active_couplers)
    
        nodes_table=np.zeros((N,q))
        for i in range(len(nodes_table)):
            for j in range(nodes_table.shape[1]):
                nodes_table[i,j]=q*i + j
    
        nodes_table.astype('int32')
    
    
        nodes_dict={i:list(nodes_table[i,:]) for i in range(nodes_table.shape[0])}
    
        G=nx.Graph()
    
        for k in nodes_dict.keys():
            #iterate through the independent sets of nodes/variables
            c_nodes=nodes_dict[k]
            G.add_nodes_from(c_nodes)
    
            edges_list=[]
    
            for i in range(len(c_nodes)):
                for j in range(i+1,len(c_nodes)):
                    edges_list.append((c_nodes[i],c_nodes[j]))
    
            G.add_edges_from(edges_list)
    
        emb=find_embedding(S=G, T=new_pegasus)
    
        return emb, nodes_dict
    
    
    #%%