Exemplo n.º 1
0
    def _sample(self,
                type_,
                linear,
                quadratic,
                params,
                undirected_biases=False):
        """Internal method for `sample_ising`, `sample_qubo` and `sample_bqm`.

        Args:
            linear (list/dict):
                Linear terms of the model.

            quadratic (dict[(int, int), float]):
                Quadratic terms of the model.

            params (dict):
                Parameters for the sampling method, solver-specific.

            undirected_biases (boolean, default=False):
                Are (quadratic) biases specified on undirected edges? For
                triangular or symmetric matrix of quadratic biases set it to
                ``True``.

        Returns:
            :class:`Future`
        """

        args = dict(type_=type_,
                    linear=linear,
                    quadratic=quadratic,
                    params=params)
        dispatch_event('before_sample', obj=self, args=args)

        # Check the problem
        if not self.check_problem(linear, quadratic):
            raise InvalidProblemError(
                "Problem graph incompatible with solver.")

        # Mix the new parameters with the default parameters
        combined_params = dict(self._params)
        combined_params.update(params)

        # Check the parameters before submitting
        for key in combined_params:
            if key not in self.parameters and not key.startswith('x_'):
                raise KeyError(
                    "{} is not a parameter of this solver.".format(key))

        # transform some of the parameters in-place
        self._format_params(type_, combined_params)

        body_data = json.dumps({
            'solver':
            self.id,
            'data':
            encode_problem_as_qp(self,
                                 linear,
                                 quadratic,
                                 undirected_biases=undirected_biases),
            'type':
            type_,
            'params':
            combined_params
        })
        logger.trace("Encoded sample request: %s", body_data)

        body = Present(result=body_data)
        computation = Future(solver=self,
                             id_=None,
                             return_matrix=self.return_matrix)

        logger.debug("Submitting new problem to: %s", self.id)
        self.client._submit(body, computation)

        dispatch_event('after_sample',
                       obj=self,
                       args=args,
                       return_value=computation)

        return computation
Exemplo n.º 2
0
    def _sample(self, type_, linear, quadratic, params):
        """Internal method for `sample_ising` and `sample_qubo`.

        Args:
            linear (list/dict):
                Linear terms of the model.

            quadratic (dict[(int, int), float]):
                Quadratic terms of the model.

            **params:
                Parameters for the sampling method, solver-specific.

        Returns:
            :class:`Future`
        """

        args = dict(type_=type_,
                    linear=linear,
                    quadratic=quadratic,
                    params=params)
        dispatch_event('before_sample', obj=self, args=args)

        # Check the problem
        if not self.check_problem(linear, quadratic):
            raise InvalidProblemError(
                "Problem graph incompatible with solver.")

        # Mix the new parameters with the default parameters
        combined_params = dict(self._params)
        combined_params.update(params)

        # Check the parameters before submitting
        for key in combined_params:
            if key not in self.parameters and not key.startswith('x_'):
                raise KeyError(
                    "{} is not a parameter of this solver.".format(key))

        # transform some of the parameters in-place
        self._format_params(type_, combined_params)

        body = json.dumps({
            'solver': self.id,
            'data': encode_problem_as_qp(self, linear, quadratic),
            'type': type_,
            'params': combined_params
        })
        logger.trace("Encoded sample request: %s", body)

        future = Future(solver=self,
                        id_=None,
                        return_matrix=self.return_matrix)

        logger.debug("Submitting new problem to: %s", self.id)
        self.client._submit(body, future)

        dispatch_event('after_sample',
                       obj=self,
                       args=args,
                       return_value=future)

        return future