Beispiel #1
0
    def relabel_variables(self, mapping, inplace=True):
        """Relabel variables of a binary polynomial as specified by mapping.

        Args:
            mapping (dict):
                Dict mapping current variable labels to new ones. If an
                incomplete mapping is provided, unmapped variables retain their
                current labels.

            inplace (bool, optional, default=True):
                If True, the binary polynomial is updated in-place; otherwise, a
                new binary polynomial is returned.

        Returns:
            :class:`.BinaryPolynomial`: A binary polynomial with the variables
            relabeled. If `inplace` is set to True, returns itself.

        """
        if not inplace:
            return self.copy().relabel_variables(mapping, inplace=True)

        for submap in iter_safe_relabels(mapping, self.variables):

            for oldterm, bias in list(self.items()):
                newterm = frozenset((submap.get(v, v) for v in oldterm))

                if newterm != oldterm:
                    self[newterm] = bias
                    del self[oldterm]

        return self
Beispiel #2
0
    def _relabel(self, mapping):
        for submap in iter_safe_relabels(mapping, self):
            for old, new in submap.items():
                if old == new:
                    continue

                idx = self._label_to_idx.pop(old, old)

                if new != idx:
                    self._label_to_idx[new] = idx
                    self._idx_to_label[idx] = new  # overwrites old idx
                else:
                    self._idx_to_label.pop(idx, None)
Beispiel #3
0
    def relabel(self, mapping):

        for submap in iter_safe_relabels(mapping, self):

            label = self._label
            index = self.index

            for old, new in submap.items():
                if old not in self:
                    continue

                label[index[old]] = new
                index[new] = index[old]
                del index[old]
Beispiel #4
0
    def relabel_variables(self, mapping: Mapping[Variable, Variable]):
        adj = self._adj

        for submap in iter_safe_relabels(mapping, self.variables):
            for old, new in submap.items():
                if old == new:
                    continue

                # replace the linear bias
                adj[new] = {new: adj[old].pop(old)}

                # copy the quadratic biases
                for v in adj[old]:
                    adj[new][v] = adj[v][new] = adj[v].pop(old)

                # remove the old adj for old
                del adj[old]
Beispiel #5
0
    def _relabel(self, mapping):
        """Relabel the variables in-place.

        This method is semi-public. it is intended to be used by
        classes that have :class:`.Variables` as an attribute, not by the
        the user.
        """
        for submap in iter_safe_relabels(mapping, self):
            for old, new in submap.items():
                if old == new:
                    continue

                idx = self._label_to_idx.pop(old, old)

                if new != idx:
                    self._label_to_idx[new] = idx
                    self._idx_to_label[idx] = new  # overwrites old idx
                else:
                    self._idx_to_label.pop(idx, None)
Beispiel #6
0
    def relabel_variables(self, mapping, inplace=True):
        """Relabel variables of a binary quadratic model as specified by mapping.

        Args:
            mapping (dict):
                Dict mapping current variable labels to new ones. If an
                incomplete mapping is provided, unmapped variables retain their
                current labels.

            inplace (bool, optional, default=True):
                If True, the binary quadratic model is updated in-place;
                otherwise, a new binary quadratic model is returned.

        Returns:
            A binary quadratic model with the variables relabeled. If `inplace`
            is set to True, returns itself.

        """
        if not inplace:
            return self.copy().relabel_variables(mapping, inplace=True)

        adj = self._adj

        for submap in iter_safe_relabels(mapping, self.linear):
            for old, new in submap.items():
                if old == new:
                    continue

                # replace the linear bias
                adj[new] = {new: adj[old].pop(old)}

                # copy the quadratic biases
                for v in adj[old]:
                    adj[new][v] = adj[v][new] = adj[v].pop(old)

                # remove the old adj for old
                del adj[old]

        return self