Esempio n. 1
0
def test_solution_type():

    assert solution_type((0, 1, 1, 0)) == 'bool'
    assert solution_type((1, -1, -1, 1)) == 'spin'
    assert solution_type((1, 1, 1, 1), 'testing') == 'testing'
    assert solution_type((1, 1, 1, 1)) == 'bool'
    assert solution_type((1, 1, 1, 1), 'spin') == 'spin'

    assert solution_type(dict(enumerate((0, 1, 1, 0)))) == 'bool'
    assert solution_type(dict(enumerate((1, -1, -1, 1)))) == 'spin'
    assert solution_type(dict(enumerate((1, 1, 1, 1))), 'testing') == 'testing'
    assert solution_type(dict(enumerate((1, 1, 1, 1)))) == 'bool'
    assert solution_type(dict(enumerate((1, 1, 1, 1))), 'spin') == 'spin'
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]``.

        """
        sol_type = solution_type(solution, 'spin' if spin else 'bool')
        if sol_type == '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.

        """
        sol_type = solution_type(solution, 'spin' if spin else 'bool')
        if sol_type == 'spin':
            solution = spin_to_boolean(solution)
        return np.array([int(bool(solution[i])) for i in range(self._N)])
Esempio n. 4
0
    def convert_solution(self, solution, spin=False):
        """convert_solution.

        Convert the solution to the QUBO or QUSO to the solution to the
        Alternating Sectors Chain 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 : tuple.
            Value of each spin, -1 or 1.

        Examples
        --------
        >>> problem = AlternatingSectorsChain(5)
        >>> problem.convert_solution([0, 0, 1, 0, 1])
        (1, 1, -1, 1, -1)
        >>> problem.convert_solution([-1, -1, 1, -1, 1])
        (-1, -1, 1, -1, 1)

        """
        if isinstance(solution, dict):
            solution = tuple(v for _, v in sorted(solution.items()))
        sol_type = solution_type(solution, 'spin' if spin else 'bool')
        if sol_type == 'bool':
            return boolean_to_spin(solution)
        return solution
Esempio n. 5
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. 6
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)