Exemplo n.º 1
0
 def build_tile_grid(self, M, N, adj):
     '''Build the grid layout from the tile range and adjacency'''
     
     # forget tiles
     for key, tile in self.canvas.tiles.items():
         tile.setParent(None)
     self.canvas.tiles = {}
     
     # release grid widgets
     while self.tile_layout.count():
         self.tile_layout.takeAt(0).widget().deleteLater()
         
     # determine new canvas size
     width = N*TILE_SIZE
     height = M*TILE_SIZE
     
     # update Canvas size parameters
     self.canvas.w = width
     self.canvas.h = height
     self.canvas.scaling = 1.
     self.canvas.setGeometry(0, 0, width, height)
     
     # convert adjacency dict to tup format if index keyed, assume valid ind
     if type(adj.keys()[0]) is int:
         adj = {linear_to_tuple(k, M, N):\
             [linear_to_tuple(a, M, N) for a in adj[k]] for k in adj}
     
     self.adj = adj
Exemplo n.º 2
0
    def build_tile_grid(self, M, N, adj):
        '''Build the grid layout from the tile range and adjacency'''

        # forget tiles
        for key, tile in self.canvas.tiles.items():
            tile.setParent(None)
        self.canvas.tiles = {}

        # release grid widgets
        while self.tile_layout.count():
            self.tile_layout.takeAt(0).widget().deleteLater()

        # determine new canvas size
        width = N * TILE_SIZE
        height = M * TILE_SIZE

        # update Canvas size parameters
        self.canvas.w = width
        self.canvas.h = height
        self.canvas.scaling = 1.
        self.canvas.setGeometry(0, 0, width, height)

        # convert adjacency dict to tup format if index keyed, assume valid ind
        if type(adj.keys()[0]) is int:
            adj = {linear_to_tuple(k, M, N):\
                [linear_to_tuple(a, M, N) for a in adj[k]] for k in adj}

        self.adj = adj
Exemplo n.º 3
0
    def load_chimera(self, fname):
        '''Load and set the chimera structure'''

        try:
            M, N, adj = load_chimera_file(fname)
        except IOError:
            return

        self.M = M
        self.N = N

        self.adj = {linear_to_tuple(k, M, N):\
            [linear_to_tuple(a, M, N) for a in adj[k]] for k in adj}

        # forget old grid layout
        for tile in self.canvas.tiles:
            self.canvas.tiles[tile].setParent(None)
        self.canvas.tiles = {}

        while self.layout.count():
            item = self.layout.takeAt(0)
            item.widget().deleteLater()

        # resize canvas
        width = N * TILE_SIZE
        height = M * TILE_SIZE

        self.canvas.w = width
        self.canvas.h = height
        self.canvas.scaling = 1.
        self.canvas.setGeometry(0, 0, width, height)

        # set up tiles and default nodes
        for m in range(M):
            for n in range(N):
                tile = TileWidget(m, n, parent=self.canvas)
                tile.set_nodes(self.adj)
                self.layout.addWidget(tile, m, n)
                self.canvas.tiles[(m, n)] = tile
                tile.show()

        self.canvas.update()
Exemplo n.º 4
0
    def load_chimera(self, fname):
        '''Load and set the chimera structure'''
        
        try:
            M, N, adj = load_chimera_file(fname)
        except IOError:
            return
        
        self.M = M
        self.N = N

        self.adj = {linear_to_tuple(k, M, N):\
            [linear_to_tuple(a, M, N) for a in adj[k]] for k in adj}
        
        # forget old grid layout
        for tile in self.canvas.tiles:
            self.canvas.tiles[tile].setParent(None)
        self.canvas.tiles = {}

        while self.layout.count():
            item = self.layout.takeAt(0)
            item.widget().deleteLater()

        # resize canvas
        width = N*TILE_SIZE
        height = M*TILE_SIZE
        
        self.canvas.w = width
        self.canvas.h = height
        self.canvas.scaling = 1.
        self.canvas.setGeometry(0, 0, width, height)
        
        # set up tiles and default nodes
        for m in range(M):
            for n in range(N):
                tile = TileWidget(m, n, parent=self.canvas)
                tile.set_nodes(self.adj)
                self.layout.addWidget(tile, m, n)
                self.canvas.tiles[(m, n)] = tile
                tile.show()
        
        self.canvas.update()
Exemplo n.º 5
0
    def updateChimera(self, filename):
        '''Process a chimera specification file and update the widget'''

        try:
            M, N, adj = load_chimera_file(filename)
        except IOError:
            print('Failed to load given file...')
            return

        # forget old grid layout
        for tile in self.tiles:
            self.tiles[tile].setParent(None)
        self.tiles = {}

        while self.layout.count():
            item = self.layout.takeAt(0)
            item.widget().deleteLater()

        # resize canvas
        width = N*settings.CHIMERA_TILE_SIZE
        height = M*settings.CHIMERA_TILE_SIZE

        self.canvas.setGeometry(0, 0, width, height)

        # convert adjacency dict to tuple format
        adj = {linear_to_tuple(k, M, N):\
            [linear_to_tuple(a, M, N) for a in adj[k]] for k in adj}

        self.M = M
        self.N = N
        self.adj = adj

        for m in xrange(M):
            for n in xrange(N):
                tile = ChimeraTile(self, m, n, adj=adj)
                self.tiles[(m, n)] = tile
                self.layout.addWidget(tile, m, n)
                tile.show()

        self.canvas.update()
Exemplo n.º 6
0
    def updateChimera(self, filename):
        '''Process a chimera specification file and update the widget'''

        try:
            M, N, adj = load_chimera_file(filename)
        except IOError:
            print('Failed to load given file...')
            return

        # forget old grid layout
        for tile in self.tiles:
            self.tiles[tile].setParent(None)
        self.tiles = {}

        while self.layout.count():
            item = self.layout.takeAt(0)
            item.widget().deleteLater()

        # resize canvas
        width = N * settings.CHIMERA_TILE_SIZE
        height = M * settings.CHIMERA_TILE_SIZE

        self.canvas.setGeometry(0, 0, width, height)

        # convert adjacency dict to tuple format
        adj = {linear_to_tuple(k, M, N):\
            [linear_to_tuple(a, M, N) for a in adj[k]] for k in adj}

        self.M = M
        self.N = N
        self.adj = adj

        for m in xrange(M):
            for n in xrange(N):
                tile = ChimeraTile(self, m, n, adj=adj)
                self.tiles[(m, n)] = tile
                self.layout.addWidget(tile, m, n)
                tile.show()

        self.canvas.update()
Exemplo n.º 7
0
    def run_heur_embedding(self, full_adj=True):
        '''Setup and run the Heuristic algorithm'''

        # update embedding type in case direct call
        self.use_dense = False

        active_cells, qca_adj = self.get_reduced_qca_adj()
        S_size = len(qca_adj)
        A_size = len(self.chimera_adj)

        # construct S
        S = {}
        for i in range(S_size):
            c1 = active_cells[i]
            for j in range(S_size):
                c2 = active_cells[j]
                v = 1 if c2 in qca_adj[c1] else 0
                S[(i, j)] = v
                S[(j, i)] = v

        # construct A
        A = set()
        for qb1 in self.chimera_adj:
            for qb2 in self.chimera_adj[qb1]:
                l1 = tuple_to_linear(qb1,
                                     self.M,
                                     self.N,
                                     L=self.L,
                                     index0=True)
                l2 = tuple_to_linear(qb2,
                                     self.M,
                                     self.N,
                                     L=self.L,
                                     index0=True)
                A.add((l1, l2))

        try:
            print 'Running heuristic embedding'
            models = find_embedding(S, S_size, A, A_size)
        except Exception as e:
            print(e.message())

        print 'Embedding finished'
        self.good = len(models) == S_size

        # map models to standard format
        mapper = lambda ind: linear_to_tuple(
            ind, self.M, self.N, L=self.L, index0=True)
        self.models = {
            active_cells[i]: [mapper(c) for c in models[i]]
            for i in xrange(S_size)
        }
Exemplo n.º 8
0
    def run_heur_embedding(self, full_adj=True):
        '''Setup and run the Heuristic algorithm'''

        # update embedding type in case direct call
        self.embed_method = 'heur'

        active_cells, qca_adj = self.get_reduced_qca_adj()
        S_size = len(qca_adj)
        A_size = len(self.chimera_adj)

        # construct S, the problem adjacency edge list
        S = set()
        smap = {c: i for i, c in enumerate(active_cells)}
        for c1, adj in qca_adj.items():
            for c2 in adj:
                if c1 < c2:
                    S.add((smap[c1], smap[c2]))

        # construct A, the chimera adjacency edge list
        A = set()
        for qb1 in self.chimera_adj:
            for qb2 in self.chimera_adj[qb1]:
                l1 = tuple_to_linear(qb1,
                                     self.M,
                                     self.N,
                                     L=self.L,
                                     index0=True)
                l2 = tuple_to_linear(qb2,
                                     self.M,
                                     self.N,
                                     L=self.L,
                                     index0=True)
                A.add((l1, l2))

        try:
            print 'Running heuristic embedding'
            #models = find_embedding(S, S_size, A, A_size)
            models = find_embedding(S, A)
        except Exception as e:
            print(e.message())

        print 'Embedding finished'
        self.good = len(models) == S_size

        # map models to standard format
        mapper = lambda ind: linear_to_tuple(
            ind, self.M, self.N, L=self.L, index0=True)
        self.models = {
            active_cells[i]: [mapper(c) for c in model]
            for i, model in enumerate(models)
        }
Exemplo n.º 9
0
    def run_heur_embedding(self, full_adj=True):
        '''Setup and run the Heuristic algorithm'''

        # update embedding type in case direct call
        self.use_dense = False

        active_cells, qca_adj = self.get_reduced_qca_adj()
        S_size = len(qca_adj)
        A_size = len(self.chimera_adj)

        # construct S
        S = {}
        for i in range(S_size):
            c1 = active_cells[i]
            for j in range(S_size):
                c2 = active_cells[j]
                v = 1 if c2 in qca_adj[c1] else 0
                S[(i, j)] = v
                S[(j, i)] = v

        # construct A
        A = set()
        for qb1 in self.chimera_adj:
            for qb2 in self.chimera_adj[qb1]:
                l1 = tuple_to_linear(qb1, self.M, self.N, L=self.L, index0=True)
                l2 = tuple_to_linear(qb2, self.M, self.N, L=self.L, index0=True)
                A.add((l1, l2))

        try:
            print 'Running heuristic embedding'
            models = find_embedding(S, S_size, A, A_size)
        except Exception as e:
            print(e.message())

        print 'Embedding finished'
        self.good = len(models) == S_size

        # map models to standard format
        mapper = lambda ind: linear_to_tuple(ind, self.M, self.N,
                                             L=self.L, index0=True)
        self.models = {active_cells[i]: [mapper(c) for c in models[i]]
            for i in xrange(S_size)}
Exemplo n.º 10
0
    def load_coefs(self, fname):
        '''Load and set a coef file in the current chimera structure'''

        try:
            fp = open(fname)
        except:
            print('Failed to open coef file...')
            return

        # purge first line, don't need the number of qubits
        fp.readline()

        # extract h and J coefficients
        h = {}
        J = {}
        mapper = lambda qb: linear_to_tuple(qb, self.M, self.N, L=4)
        for line in fp:
            if len(line) < 3 or '#' in line:
                continue
            data = line.split()
            qb1 = int(data[0])
            qb2 = int(data[1])
            val = float(data[2])
            if qb1 == qb2:
                h[mapper(qb1)] = val
            else:
                if mapper(qb1) in J:
                    J[mapper(qb1)][mapper(qb2)] = val
                else:
                    J[mapper(qb1)] = {mapper(qb2): val}
        fp.close()

        # color chimera graph
        self.color_chimera(h, J)

        self.canvas.update()
Exemplo n.º 11
0
 def load_coefs(self, fname):
     '''Load and set a coef file in the current chimera structure'''
     
     try:
         fp = open(fname)
     except:
         print('Failed to open coef file...')
         return
     
     # purge first line, don't need the number of qubits
     fp.readline()
     
     # extract h and J coefficients
     h = {}
     J = {}
     mapper = lambda qb: linear_to_tuple(qb, self.M, self.N, L=4)
     for line in fp:
         if len(line) < 3 or '#' in line:
             continue
         data = line.split()
         qb1 = int(data[0])
         qb2 = int(data[1])
         val = float(data[2])
         if qb1 == qb2:
             h[mapper(qb1)] = val
         else:
             if mapper(qb1) in J:
                 J[mapper(qb1)][mapper(qb2)] = val
             else:
                 J[mapper(qb1)] = {mapper(qb2): val}
     fp.close()
     
     # color chimera graph
     self.color_chimera(h, J)
     
     self.canvas.update()