def solve(self, problem_type, problem, params): if problem_type in ('ising', 'qubo'): problem = sapiremote.encode_qp_problem(self._solver, problem) answer = self._solver.solve(problem_type, problem, params) if problem_type in ('ising', 'qubo'): answer = sapiremote.decode_qp_answer(answer[0], answer[1]) return answer
def test_empty(self): answer = { 'format': 'qp', 'energies': '', 'num_variables': 0, 'active_variables': '', 'solutions': '' } expected = {'energies': [], 'solutions': []} self.assertEqual(sapiremote.decode_qp_answer('ising', answer), expected)
def test_raw_qubo(self): answer = { 'format': 'qp', 'energies': 'AAAAAAAAJMAAAAAAAAAUwA==', 'num_variables': 5, 'active_variables': 'AQAAAAIAAAAEAAAA', 'solutions': 'AOA=', 'extra': 'stuff' } expected = { 'energies': [-10.0, -5.0], 'solutions': [[3, 0, 0, 3, 0], [3, 1, 1, 3, 1]], 'extra': 'stuff' } self.assertEqual(sapiremote.decode_qp_answer('qubo', answer), expected)
def test_hist_ising(self): answer = { 'format': 'qp', 'energies': 'AAAAAAAAJMAAAAAAAAAUwA==', 'num_variables': 5, 'active_variables': 'AQAAAAIAAAAEAAAA', 'solutions': 'AOA=', 'num_occurrences': '6AMAANsDAAA=', 'extra': 'stuff' } expected = { 'energies': [-10.0, -5.0], 'solutions': [[3, -1, -1, 3, -1], [3, 1, 1, 3, 1]], 'num_occurrences': [1000, 987], 'extra': 'stuff' } self.assertEqual(sapiremote.decode_qp_answer('ising', answer), expected)
def result(self): answer = self.submitted_problem.answer() if answer[0] in ('ising', 'qubo'): answer = sapiremote.decode_qp_answer(answer[0], answer[1]) return answer
def test_decode_error(self): self.assertRaises( RuntimeError, lambda: sapiremote.decode_qp_answer('qubo', {'format': 'qp'}))