Beispiel #1
0
    def add_sample(self, sample, energy=None, data=None, h=None, J=None):
        """Loads a sample and associated energy into the response.

        Args:
            sample (dict): A sample as would be returned by a discrete
                model solver. Should be a dict of the form
                {var: value, ...}. The values should be spin-valued, that is
                -1 or 1.
            energy (float/int, optional): The energy associated with the
                given sample.
            data (dict, optional): A dict containing any additional
                data about the sample. Default {}.
            h (dict) and J (dict): Define an Ising problem that can be
                used to calculate the energy associated with `sample`.

        Notes:
            Solutions are stored in order of energy, lowest first.

        Raises:
            TypeError: If `sample` is not a dict.
            TypeError: If `energy` is not an int or float.
            TypeError: If `data` is not a dict.
            ValueError: If any of the values in `sample` are not -1
                or 1.
            TypeError: If energy is not provided, h and J must be.

        Examples:
            >>> response = SpinResponse()
            >>> response.add_sample({0: -1}, 1)
            >>> response.add_sample({0: 1}, -1, data={'n': 1})
            >>> response.add_sample({0: 1}, h={0: -1}, J={})
            >>> list(response.energies())
            [-1, -1]

        """
        if not isinstance(sample, dict):
            raise TypeError("expected 'sample' to be a dict")

        # check that the sample is spin-valued
        if any(val not in (-1, 1) for val in itervalues(sample)):
            raise ValueError(
                'given sample is not spin-valued. Values should be -1 or 1')

        # if energy is not provided, but h, J are, then we can calculate
        # the energy for the sample.
        if energy is None:
            if h is None or J is None:
                raise TypeError("most provide 'energy' or 'h' and 'J'")
            energy = ising_energy(h, J, sample)

        if data is None:
            data = {}

        TemplateResponse.add_sample(self, sample, energy, data)
Beispiel #2
0
    def add_sample(self,
                   sample,
                   energy=None,
                   num_occurences=1,
                   h=None,
                   J=None,
                   **kwargs):
        """Loads a sample and associated energy into the response.

        Args:
            todo

        Notes:
            Solutions are stored in order of energy, lowest first.

        Raises:
            TypeError: If `sample` is not a dict.
            TypeError: If `energy` is not an int or float.
            TypeError: If `data` is not a dict.
            ValueError: If any of the values in `sample` are not -1
                or 1.
            TypeError: If energy is not provided, h and J must be.

        Examples:
            >>> response = SpinResponse()
            >>> response.add_sample({0: -1}, 1)
            >>> response.add_sample({0: 1}, -1, data={'n': 1})
            >>> response.add_sample({0: 1}, h={0: -1}, J={})
            >>> list(response.energies())
            [-1, -1]

        """
        if not isinstance(sample, dict):
            raise TypeError("expected 'sample' to be a dict")

        # if energy is not provided, but h, J are, then we can calculate
        # the energy for the sample.
        if energy is None:
            if h is None or J is None:
                raise TypeError("most provide 'energy' or 'h' and 'J'")
            energy = ising_energy(sample, h, J)

        TemplateResponse.add_sample(self,
                                    sample,
                                    energy,
                                    num_occurences=num_occurences,
                                    **kwargs)
Beispiel #3
0
def ising_simulated_annealing(h, J, beta_range=None, num_sweeps=1000):
    """Tries to find the spins that minimize the given Ising problem.

    Args:
        h (dict): A dictionary of the linear biases in the Ising
            problem. Should be of the form {v: bias, ...} for each
            variable v in the Ising problem.
        J (dict): A dictionary of the quadratic biases in the Ising
            problem. Should be a dict of the form {(u, v): bias, ...}
            for each edge (u, v) in the Ising problem. If J[(u, v)] and
            J[(v, u)] exist then the biases are added.
        beta_range (tuple, optional): A 2-tuple defining the
            beginning and end of the beta schedule (beta is the
            inverse temperature). The schedule is applied linearly
            in beta. Default is chosen based on the total bias associated
            with each node.
        num_sweeps (int, optional): The number of sweeps or steps.
            Default is 1000.

    Returns:
        dict: A sample as a dictionary of spins.
        float: The energy of the returned sample.

    Raises:
        TypeError: If the values in `beta_range` are not numeric.
        TypeError: If `num_sweeps` is not an int.
        TypeError: If `beta_range` is not a tuple.
        ValueError: If the values in `beta_range` are not positive.
        ValueError: If `beta_range` is not a 2-tuple.
        ValueError: If `num_sweeps` is not positive.

    https://en.wikipedia.org/wiki/Simulated_annealing

    """

    if beta_range is None:
        beta_init = .1

        sigmas = {v: abs(h[v]) for v in h}
        for u, v in J:
            sigmas[u] += abs(J[(u, v)])
            sigmas[v] += abs(J[(u, v)])

        if sigmas:
            beta_final = 2. * max(itervalues(sigmas))
        else:
            beta_final = 0.0

    else:
        if not isinstance(beta_range, (tuple, list)):
            raise TypeError("'beta_range' should be a tuple of length 2")
        if any(not isinstance(b, (int, float)) for b in beta_range):
            raise TypeError("values in 'beta_range' should be numeric")
        if any(b <= 0 for b in beta_range):
            raise ValueError("beta values in 'beta_range' should be positive")
        if len(beta_range) != 2:
            raise ValueError("'beta_range' should be a tuple of length 2")
        beta_init, beta_final = beta_range
    if not isinstance(num_sweeps, int):
        raise TypeError("'sweeps' should be a positive int")
    if num_sweeps <= 0:
        raise ValueError("'sweeps' should be a positive int")

    # We want the schedule to be linear in beta (inverse temperature)
    betas = [
        beta_init + i * (beta_final - beta_init) / (num_sweeps - 1.)
        for i in range(num_sweeps)
    ]

    # set up the adjacency matrix. We can rely on every node in J already being in h
    adj = {n: set() for n in h}
    for n0, n1 in J:
        adj[n0].add(n1)
        adj[n1].add(n0)

    # we will use a vertex coloring for the graph and update the nodes by color. A quick
    # greedy coloring will be sufficient.
    __, colors = greedy_coloring(adj)

    # let's make our initial guess (randomly)
    spins = {v: random.choice((-1, 1)) for v in h}

    # there are exactly as many betas as sweeps
    for beta in betas:

        # we want to know the gain in energy for flipping each of the spins.
        # We can calculate all of the linear terms simultaneously
        energy_diff_h = {v: -2 * spins[v] * h[v] for v in h}

        # for each color, do updates
        for color in colors:

            nodes = colors[color]

            # we now want to know the energy change for flipping the spins within
            # the color class
            energy_diff_J = {}
            for v0 in nodes:
                ediff = 0
                for v1 in adj[v0]:
                    if (v0, v1) in J:
                        ediff += spins[v0] * spins[v1] * J[(v0, v1)]
                    if (v1, v0) in J:
                        ediff += spins[v0] * spins[v1] * J[(v1, v0)]

                energy_diff_J[v0] = -2. * ediff

            # now decide whether to flip spins according to the
            # following scheme:
            #   p ~ Uniform(0, 1)
            #   log(p) < -beta * (energy_diff)
            for v in nodes:
                logp = math.log(random.uniform(0, 1))
                if logp < -1. * beta * (energy_diff_h[v] + energy_diff_J[v]):
                    # flip the variable in the spins
                    spins[v] *= -1

    return spins, ising_energy(spins, h, J)
Beispiel #4
0
    def sample_ising(self, h, J):
        """Solves the Ising problem exactly.

        Args:
            h (dict/list): The linear terms in the Ising problem. If a
                dict, should be of the form {v: bias, ...} where v is
                a variable in the Ising problem, and bias is the linear
                bias associated with v. If a list, should be of the form
                [bias, ...] where the indices of the biases are the
                variables in the Ising problem.
            J (dict): A dictionary of the quadratic terms in the Ising
                problem. Should be of the form {(u, v): bias} where u,
                v are variables in the Ising problem and bias is the
                quadratic bias associated with u, v.

        Returns:
            :obj:`SpinResponse`

        Notes:
            Becomes slow for problems with 18 or more variables.

        """

        # it will be convenient to have J in a nested-dict form.
        adjJ = {v: {} for v in h}
        for (u, v), bias in iteritems(J):
            if v not in adjJ[u]:
                adjJ[u][v] = bias
            else:
                adjJ[u][v] += bias

            if u not in adjJ[v]:
                adjJ[v][u] = bias
            else:
                adjJ[v][u] += bias

        # initialize the response
        response = SpinResponse()

        # generate the first sample and add it to the response
        sample = {v: -1 for v in h}
        energy = ising_energy(h, J, sample)
        response.add_sample(sample.copy(), energy)

        # now we iterate, flipping one bit at a time until we have
        # traversed all samples. This is a gray code.
        # https://en.wikipedia.org/wiki/Gray_code
        for i in range(1, 1 << len(h)):
            v = _ffs(i)

            # flip the bit in the sample
            sample[v] *= -1

            # get the energy difference
            quad_diff = sum(adjJ[v][u] * sample[u] for u in adjJ[v])

            # calculate the new energy as a difference from the old
            energy += 2 * sample[v] * (h[v] + quad_diff)

            response.add_sample(sample.copy(), energy)
        return response
Beispiel #5
0
    def add_samples_from(self,
                         samples,
                         energies=None,
                         num_occurences=1,
                         h=None,
                         J=None,
                         **kwargs):
        """Loads samples and associated energies from iterators.

        Args:
            todo

        Notes:
            Solutions are stored in order of energy, lowest first.

        Raises:
            TypeError: If any `sample` in `samples` is not a dict.
            TypeError: If any `energy`  in `energies` is not an int
            or float.
            TypeError: If any `data` in `sample_data` is not a dict.
            ValueError: If any of the values in `sample` are not -1
            or 1.
            TypeError: If energy is not provided, h and J must be.

        Examples:
            >>> samples = [{0: -1}, {0: 1}, {0: -1}]
            >>> energies = [1, -1, 1]
            >>> sample_data = [{'t': .2}, {'t': .5}, {'t': .1}]

            >>> response = SpinResponse()
            >>> response.add_samples_from(samples, energies)
            >>> list(response.samples())
            [{0: 1}, {0: -1}, {0: -1}]

            >>> response = SpinResponse()
            >>> response.add_samples_from(samples, energies, sample_data)
            >>> list(response.samples())
            [{0: 1}, {0: -1}, {0: -1}]

            >>> items = [({0: -1}, -1), ({0: -1}, 1)]
            >>> response = SpinResponse()
            >>> response.add_samples_from(*zip(*items))
            >>> list(response.samples())
            [{0: 1}, {0: -1}]

            >>> response = SpinResponse()
            >>> response.add_samples_from(samples, h={0: -1}, J={}})
            >>> list(response.energies())
            [-1, 1, 1]

        """

        samples = list(samples)

        if not all(isinstance(sample, dict) for sample in samples):
            raise TypeError("expected each sample in 'samples' to be a dict")

        if energies is None:
            if h is None or J is None:
                raise TypeError("most provide 'energy' or 'h' and 'J'")
            energies = [ising_energy(sample, h, J) for sample in samples]

        TemplateResponse.add_samples_from(self,
                                          samples,
                                          energies,
                                          num_occurences=num_occurences,
                                          **kwargs)
Beispiel #6
0
    def add_samples_from(self,
                         samples,
                         energies=None,
                         sample_data=None,
                         h=None,
                         J=None):
        """Loads samples and associated energies from iterators.

        Args:
            samples (iterator): An iterable object that yields
                samples. Each sample should be a dict of the form
                {var: value, ...}.
            energies (iterator): An iterable object that yields
                energies associated with each sample.
                sample_data (iterator, optional): An iterable object
                that yields data about each sample as  dict. Default
                empty dicts.
            h (dict) and J (dict): Define an Ising problem that can be
                used to calculate the energy associated with `sample`.

        Notes:
            Solutions are stored in order of energy, lowest first.

        Raises:
            TypeError: If any `sample` in `samples` is not a dict.
            TypeError: If any `energy`  in `energies` is not an int
            or float.
            TypeError: If any `data` in `sample_data` is not a dict.
            ValueError: If any of the values in `sample` are not -1
            or 1.
            TypeError: If energy is not provided, h and J must be.

        Examples:
            >>> samples = [{0: -1}, {0: 1}, {0: -1}]
            >>> energies = [1, -1, 1]
            >>> sample_data = [{'t': .2}, {'t': .5}, {'t': .1}]

            >>> response = SpinResponse()
            >>> response.add_samples_from(samples, energies)
            >>> list(response.samples())
            [{0: 1}, {0: -1}, {0: -1}]

            >>> response = SpinResponse()
            >>> response.add_samples_from(samples, energies, sample_data)
            >>> list(response.samples())
            [{0: 1}, {0: -1}, {0: -1}]

            >>> items = [({0: -1}, -1), ({0: -1}, 1)]
            >>> response = SpinResponse()
            >>> response.add_samples_from(*zip(*items))
            >>> list(response.samples())
            [{0: 1}, {0: -1}]

            >>> response = SpinResponse()
            >>> response.add_samples_from(samples, h={0: -1}, J={}})
            >>> list(response.energies())
            [-1, 1, 1]

        """

        samples = list(samples)

        if not all(isinstance(sample, dict) for sample in samples):
            raise TypeError("expected each sample in 'samples' to be a dict")

        if any(
                any(val not in (-1, 1) for val in itervalues(sample))
                for sample in samples):
            raise ValueError(
                'given sample is not spin-valued. Values should be -1 or 1')

        if energies is None:
            if h is None or J is None:
                raise TypeError("most provide 'energy' or 'h' and 'J'")
            energies = [ising_energy(h, J, sample) for sample in samples]

        TemplateResponse.add_samples_from(self, samples, energies, sample_data)