Esempio n. 1
0
def test_spin_to_boolean():

    assert spin_to_boolean(-1) == 1
    assert spin_to_boolean(1) == 0
    assert spin_to_boolean((-1, 1)) == (1, 0)
    assert spin_to_boolean([-1, 1]) == [1, 0]
    assert spin_to_boolean({"a": -1, "b": 1}) == {"a": 1, "b": 0}
Esempio n. 2
0
    def convert_solution(self, solution, spin=False):
        """convert_solution.

        Convert the solution to the QUBO or QUSO to the solution to the Set
        Cover problem.

        Parameters
        ----------
        solution : iterable or dict.
            The QUBO or QUSO solution output. The QUBO solution output
            is either a list or tuple where indices specify the label of the
            variable and the element specifies whether it's 0 or 1 for QUBO
            (or -1 or 1 for QUSO), or it can be a dictionary that maps the
            label of the variable to is value.
        spin : bool (optional, defaults to False).
            `spin` indicates whether ``solution`` is the solution to the
            boolean {0, 1} formulation of the problem or the spin {1, -1}
            formulation of the problem. This parameter usually does not matter,
            and it will be ignored if possible. The only time it is used is if
            ``solution`` contains all 1's. In this case, it is unclear whether
            ``solution`` came from a spin or boolean formulation of the
            problem, and we will figure it out based on the ``spin`` parameter.

        Returns
        -------
        res : set.
            A set of which sets are included in the set cover. So if this
            function returns ``{0, 2, 3}``, then the set cover is the sets
            ``V[0]``, ``V[2]``, and ``V[3]``.

        """
        if is_solution_spin(solution, spin):
            solution = spin_to_boolean(solution)
        return set(i for i in range(self._N) if solution[i])
Esempio n. 3
0
    def convert_solution(self, solution, spin=False):
        r"""convert_solution.

        Convert the solution to the QUBO or QUSO to the solution to the BILP
        problem.

        Parameters
        ----------
        solution : iterable or dict.
            The QUBO or QUSO solution output. The QUBO solution output
            is either a list or tuple where indices specify the label of the
            variable and the element specifies whether it's 0 or 1 for QUBO
            (or 1 or -1 for QUSO), or it can be a dictionary that maps the
            label of the variable to is value.
        spin : bool (optional, defaults to False).
            `spin` indicates whether ``solution`` is the solution to the
            boolean {0, 1} formulation of the problem or the spin {1, -1}
            formulation of the problem. This parameter usually does not matter,
            and it will be ignored if possible. The only time it is used is if
            ``solution`` contains all 1's. In this case, it is unclear whether
            ``solution`` came from a spin or boolean formulation of the
            problem, and we will figure it out based on the ``spin`` parameter.

        Return
        ------
        res : np.array.
            An array representing the :math:`\mathbf{x}` vector.

        """
        if is_solution_spin(solution, spin):
            solution = spin_to_boolean(solution)
        return np.array([int(bool(solution[i])) for i in range(self._N)])
Esempio n. 4
0
    def state(self):
        """state.

        A copy of the current state of the system.

        Returns
        -------
        state : dict.
            Dictionary that maps boolean labels to their values in {0, 1}.

        """
        return spin_to_boolean(self._state)
Esempio n. 5
0
    def to_boolean(self):
        """to_boolean.

        Convert the result to a boolean result.

        Returns
        -------
        res : AnnealResult object.
            A boolean version of ``self``. If ``self.spin == False``, then
            ``res`` will be the same as ``self``.

        """
        if not self.spin:
            return self.copy()
        return AnnealResult(spin_to_boolean(self.state), self.value, False)
Esempio n. 6
0
    def convert_solution(self, solution, spin=False):
        """convert_solution.

        Convert the solution to the QUBO or QUSO to the solution to the Job
        Sequencing problem.

        Parameters
        ----------
        solution : iterable or dict.
            The QUBO or QUSO solution output. The QUBO solution output
            is either a list or tuple where indices specify the label of the
            variable and the element specifies whether it's 0 or 1 for QUBO
            (or 1 or -1 for QUSO), or it can be a dictionary that maps the
            label of the variable to is value.
        spin : bool (optional, defaults to False).
            `spin` indicates whether ``solution`` is the solution to the
            boolean {0, 1} formulation of the problem or the spin {1, -1}
            formulation of the problem. This parameter usually does not matter,
            and it will be ignored if possible. The only time it is used is if
            ``solution`` contains all 1's. In this case, it is unclear whether
            ``solution`` came from a spin or boolean formulation of the
            problem, and we will figure it out based on the ``spin`` parameter.

        Returns
        -------
        res : tuple of sets.
            Each element of the tuple corresponds to a worker. Each element
            of the tuple is a set of jobs that are assigned to that worker.

        """
        sol_type = solution_type(solution, 'spin' if spin else 'bool')
        if sol_type == 'spin':
            solution = spin_to_boolean(solution)
        res = tuple(set() for _ in range(self._m))
        for worker in range(self._m):
            for job in self._lengths:
                if solution[self._x(job, worker)] == 1:
                    res[worker].add(job)
        return res
Esempio n. 7
0
    def convert_solution(self, solution, spin=False):
        """convert_solution.

        Convert the solution to the QUBO or QUSO to the solution to the Vertex
        Cover problem.

        Parameters
        ----------
        solution : iterable or dict.
            The QUBO or QUSO solution output. The QUBO solution output
            is either a list or tuple where indices specify the label of the
            variable and the element specifies whether it's 0 or 1 for QUBO
            (or 1 or -1 for QUSO), or it can be a dictionary that maps the
            label of the variable to is value.
        spin : bool (optional, defaults to False).
            `spin` indicates whether ``solution`` is the solution to the
            boolean {0, 1} formulation of the problem or the spin {1, -1}
            formulation of the problem. This parameter usually does not matter,
            and it will be ignored if possible. The only time it is used is if
            ``solution`` contains all 1's. In this case, it is unclear whether
            ``solution`` came from a spin or boolean formulation of the
            problem, and we will figure it out based on the ``spin`` parameter.

        Return
        ------
        res : set.
            A set of which verticies need to be colored. Thus, if this
            function returns {0, 2}, then this means that vertex 0 and 2
            should be colored.

        """
        if not isinstance(solution, dict):
            solution = dict(enumerate(solution))
        sol_type = solution_type(solution, 'spin' if spin else 'bool')
        if sol_type == 'spin':
            solution = spin_to_boolean(solution)
        return set(self._index_to_vertex[i] for i, x in solution.items() if x)
Esempio n. 8
0
def test_annealresults():

    states = [
        ({
            0: -1,
            1: 1,
            'a': -1
        }, 1),
        ({
            0: 1,
            1: 1,
            'a': -1
        }, 9),
        ({
            0: -1,
            1: -1,
            'a': -1
        }, -3),
    ]
    sorted_states = sorted(states, key=lambda x: x[1])
    anneal_states = [AnnealResult(*s, True) for s in states]
    anneal_sorted_states = [AnnealResult(*s, True) for s in sorted_states]

    res, boolean_res = AnnealResults(), AnnealResults()
    for s in states:
        res.add_state(*s, True)
        boolean_res.add_state(spin_to_boolean(s[0]), s[1], False)

    for s in states:
        assert AnnealResult(*s, True) in res
        assert AnnealResult(s[0], s[1] + 1, True) not in res

    assert len(res) == 3
    assert len(boolean_res) == 3
    assert res.best.state == states[2][0]
    assert res.best.value == states[2][1]
    assert res.copy() == res
    assert res.to_spin() == res
    assert res.to_boolean() == boolean_res
    assert boolean_res.to_spin() == res
    assert res == anneal_states
    assert boolean_res == [x.to_boolean() for x in anneal_states]
    str(res)
    str(boolean_res)
    repr(res)
    repr(boolean_res)

    count = 0
    for s in res:
        assert s == AnnealResult(*states[count], True)
        count += 1

    for i in range(3):
        assert res[i] == anneal_states[i]

    assert res[:2] == anneal_states[:2]
    assert isinstance(res[1:3], AnnealResults)

    res.sort()
    count = 0
    for s in res:
        assert s == AnnealResult(*sorted_states[count], True)
        count += 1

    for i in range(3):
        assert res[i] == anneal_sorted_states[i]

    assert res[:2] == anneal_sorted_states[:2]

    boolean_res.sort()
    assert res == anneal_sorted_states
    assert boolean_res == [x.to_boolean() for x in anneal_sorted_states]

    assert type(res * 2) == AnnealResults
    assert type(res + res) == AnnealResults
    res *= 2
    assert type(res) == AnnealResults
    res += res
    assert type(res) == AnnealResults
    res += anneal_states
    assert type(res) == AnnealResults
    res.extend(anneal_sorted_states)
    assert type(res) == AnnealResults

    res.clear()
    assert res.best is None
    assert not res