Exemplo n.º 1
0
    def adjacency(self):
        """dict[variable, set]: Adjacency structure for the composed sampler."""
        try:
            return self._adjacency
        except AttributeError:
            pass

        if self.embedding is None:
            return None

        self._adjacency = adj = target_to_source(
            self.target_structure.adjacency, self.embedding)

        return adj
Exemplo n.º 2
0
    def _set_graph_related_init(self, embedding=None, source_adjacency=None):
        # Must have embedding xor source_adjacency
        if (embedding is None) == (source_adjacency is None):
            raise TypeError(
                '_set_graph_related_init() must take either an embedding or a source_adjacency argument,'
                ' but not both.')

        # Populate embedding and adjacency attributes
        if embedding is not None:
            self.embedding = self.properties['embedding'] = embedding
            self.adjacency = target_to_source(self.child.adjacency, embedding)

        else:
            self.adjacency = source_adjacency

            # Find embedding with source_adjacency
            __, target_edgelist, target_adjacency = self.child.structure
            source_edgelist = []

            for k, edges in source_adjacency.items():
                source_edgelist.append((k, k))
                for e in edges:
                    source_edgelist.append((k, e))

            embedding = minorminer.find_embedding(source_edgelist,
                                                  target_edgelist)
            self.embedding = self.properties['embedding'] = embedding

        # Populate nodelist and edgelist
        try:
            nodelist = sorted(self.adjacency)
            edgelist = sorted(_adjacency_to_edges(self.adjacency))
        except TypeError:
            # python3 does not allow sorting of unlike types, so if nodes have
            # different type names just choose an arbitrary order
            nodelist = list(self.adjacency)
            edgelist = list(_adjacency_to_edges(self.adjacency))
        self.nodelist = nodelist
        self.edgelist = edgelist