예제 #1
0
    def test_bq_ref_input_validation(self):
        problem_id = '123'
        with self.assertRaises(TypeError):
            encode_problem_as_bq(problem_id)

        bqm = dimod.BQM.from_qubo({})
        with self.assertRaises(TypeError):
            encode_problem_as_ref(bqm)
예제 #2
0
    def test_ref_encoder(self):
        problem_id = '123'

        req = encode_problem_as_ref(problem_id)

        self.assertEqual(req.get('format'), 'ref')
        self.assertEqual(req.get('data'), problem_id)
예제 #3
0
    def _encode_problem_as_ref(self, problem, params):
        """Encode `problem` for submitting in `ref` format. Upload the
        problem if it's not already uploaded.

        Args:
            problem (:class:`~dimod.BinaryQuadraticModel`/str):
                A binary quadratic model, or a reference to one (Problem ID).

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

        Returns:
            str:
                JSON-encoded problem submit body
        """

        if isinstance(problem, str):
            problem_id = problem
        else:
            logger.debug(
                "To encode the problem for submit in the 'ref' format, "
                "we need to upload it first.")
            problem_id = self.upload_bqm(problem).result()

        body = json.dumps({
            'solver': self.id,
            'data': encode_problem_as_ref(problem_id),
            'type': 'bqm',
            'params': params
        })
        logger.trace("Sampling request encoded as: %s", body)

        return body
예제 #4
0
    def _encode_problem_for_submission(self,
                                       problem,
                                       problem_type,
                                       params,
                                       label=None):
        """Encode `problem` for submitting in `ref` format. Upload the
        problem if it's not already uploaded.

        Args:
            problem (dimod-model-like/str):
                A quadratic model, or a reference to one (Problem ID).

            problem_type (str):
                Problem type, one of the handled problem types by the solver.

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

            label (str, optional):
                Problem label.

        Returns:
            str:
                JSON-encoded problem submit body
        """

        if isinstance(problem, str):
            problem_id = problem
        else:
            logger.debug(
                "To encode the problem for submit in the 'ref' format, "
                "we need to upload it first.")
            problem_id = self.upload_problem(problem).result()

        body = {
            'solver': self.id,
            'data': encode_problem_as_ref(problem_id),
            'type': problem_type,
            'params': params
        }
        if label is not None:
            body['label'] = label
        body_data = json.dumps(body)
        logger.trace("Sampling request encoded as: %s", body_data)

        return body_data
예제 #5
0
    def setUpClass(cls):
        """Configure attributes required (used) by ProblemResourcesBaseTests."""

        with Client(**config) as client:
            cls.token = config['token']
            cls.api = Problems.from_client_config(client)

            # submit and solve an Ising problem as a fixture
            solver = client.get_solver(hybrid=True)
            cls.solver_id = solver.id
            cls.bqm = dimod.BQM.from_ising({}, {'ab': 1.0})
            problem_data_id = solver.upload_problem(cls.bqm).result()
            problem_data_ref = encode_problem_as_ref(problem_data_id)
            cls.problem_data = models.ProblemData.parse_obj(problem_data_ref)
            cls.problem_type = constants.ProblemType.BQM
            cls.params = dict(time_limit=3)
            future = solver.sample_bqm(problem_data_id, **cls.params)
            resolved = future.result()
            cls.problem_id = future.id
            cls.problem_label = future.label

            # double-check
            assert future.remote_status == constants.ProblemStatus.COMPLETED.value
예제 #6
0
 def problem_data(self,
                  solver: UnstructuredSolver = None,
                  problem: 'dimod.BQM' = None) -> dict:
     return encode_problem_as_ref(self.problem_data_id)