def test_implicit_solver_topology(self): """All data adapters should work on Chimera-implied solvers.""" # sample with BrickedClient() as client: solver = client.get_solver(qpu=True) response = solver.sample_ising(*self.problem, **self.params) # simulate old solver, without explicit topology property del response.solver.properties['topology'] # convert and validate data = from_qmi_response(self.problem, response, params=self.params) self.verify_data_encoding(problem=self.problem, response=response, solver=solver, params=self.params, data=data) # in addition to `topology` missing, remove "structure", so Chimera # can't be implied delattr(solver, 'edges') # ensure `from_qmi_response` adapter fails on unstructured old solver with self.assertRaises(TypeError): from_qmi_response(self.problem, response, params=self.params)
def setUpClass(cls): with BrickedClient() as client: cls.solver = client.get_solver(qpu=True) cls.ising = ({}, {'ab': 1, 'bc': 1, 'ca': 1}) cls.bqm = dimod.BQM.from_ising(*cls.ising) cls.embedding = {'a': [0], 'b': [4], 'c': [1, 5]} cls.chain_strength = 1.0 cls.embedding_context = dict(embedding=cls.embedding, chain_strength=cls.chain_strength) target_edgelist = [[0, 4], [0, 5], [1, 4], [1, 5]] target_adjacency = edgelist_to_adjacency(target_edgelist) cls.bqm_embedded = embed_bqm(cls.bqm, cls.embedding, target_adjacency, chain_strength=cls.chain_strength) cls.ising_embedded = cls.bqm_embedded.to_ising() cls.problem = cls.ising_embedded[:2] cls.params = dict(num_reads=100) cls.label = "pretty-label" # get the expected response (from VCR) cls.response = cls.solver.sample_ising(*cls.problem, **cls.params)
def test_from_qmi_response__problem_encoding(self): """Problem data is serialized even when it uses non-standard types (like numpy.int64).""" # `self.problem` == ( # {0: 0.0, 4: 0.0, 1: 0.0, 5: 0.0}, # {(0, 4): 1.0, (0, 5): 1.0, (4, 1): 1.0, (1, 5): -1.0} # ) h = { 0: numpy.int64(0), 4: numpy.double(0), 1: numpy.int8(0), 5: Decimal('0'), } J = { (0, 4): numpy.float16(1), (0, 5): Decimal('1'), (4, 1): Fraction(2, 2), (1, 5): numpy.int32(-1), } problem = (h, J) # sample with BrickedClient() as client: solver = client.get_solver(qpu=True) response = solver.sample_ising(*problem, **self.params) # convert data = from_qmi_response(problem, response, params=self.params) # validate data encoding self.verify_data_encoding(problem=problem, response=response, solver=solver, params=self.params, data=data)
def _test_from_bqm_response(self, bqm): # sample with BrickedClient() as client: solver = client.get_solver(qpu=True) response = solver.sample_ising(*self.problem, **self.params) # induce sampleset production in response, to test serialization of # sampleset-provided data, like `num_occurrences` (an numpy.ndarray) # NOTE: `dwave.cloud.computation.Future.num_occurrences` et al. will # favorize returning data from a sampleset, if it's present, instead # of returning raw SAPI data _ = response.sampleset # convert data = from_bqm_response(bqm, self.embedding_context, response, params=self.params) # validate data encoding self.verify_data_encoding(problem=self.problem, response=response, solver=solver, params=self.params, data=data, embedding_context=self.embedding_context)
def test_from_qmi_response__qubo(self): """Inspector data is correctly encoded for a simple QUBO triangle problem.""" # vars = (0, 1, 4, 5) # h = {}, J = {(0, 4): 1, (0, 5): 1, (1, 5): -1, (4, 1): 1} problem = { (0, 4): 0.5, (0, 5): 0.5, (1, 4): 0.5, (1, 5): -0.5, (4, 0): 0.5, (4, 1): 0.5, (5, 0): 0.5, (5, 1): -0.5, } # sample with BrickedClient() as client: solver = client.get_solver(qpu=True) response = solver.sample_qubo(problem, **self.params) # convert data = from_qmi_response(problem, response, params=self.params) # validate data encoding self.verify_data_encoding(problem=problem, response=response, solver=solver, params=self.params, data=data)
def test_solver_modernization(self): """Solver's data is "modernized", i.e. missing props are added.""" # get real solver with BrickedClient() as client: solver = client.get_solver(qpu=True) # cripple it del solver.properties['topology'] # mock minimal data adapter response inspector_data = { 'rel': { 'solver': solver }, 'details': { 'id': 'mock-id' } } # store it push_inspector_data(inspector_data) # get solver data solver_data = get_solver_data(solver.id) # verify missing data is recreated self.assertIn('topology', solver_data['properties']) self.assertEqual(solver_data['properties']['topology']['type'], 'chimera') # verify the original solver data is intact self.assertNotIn('topology', solver.properties)
def test_from_qmi_response__ising(self): """Inspector data is correctly encoded for a simple Ising triangle problem.""" # sample with BrickedClient() as client: solver = client.get_solver(qpu=True) response = solver.sample_ising(*self.problem, **self.params) # convert data = from_qmi_response(self.problem, response, params=self.params) # validate data encoding self.verify_data_encoding(problem=self.problem, response=response, solver=solver, params=self.params, data=data)
def test_from_qmi_response__single_qubit(self): """Problem/solutions are correctly encoded for single-qubit problems.""" problem = ({0: 1}, {}) # sample with BrickedClient() as client: solver = client.get_solver(qpu=True) response = solver.sample_ising(*problem, **self.params) # convert data = from_qmi_response(problem, response, params=self.params) # validate data encoding self.verify_data_encoding(problem=problem, response=response, solver=solver, params=self.params, data=data)
def test_from_qmi_response__couplings_only(self): """Problem/solutions are correctly encoded when qubits are referenced via couplings only.""" problem = ({}, self.ising_embedded[1]) # sample with BrickedClient() as client: solver = client.get_solver(qpu=True) response = solver.sample_ising(*problem, **self.params) # convert data = from_qmi_response(problem, response, params=self.params) # validate data encoding self.verify_data_encoding(problem=problem, response=response, solver=solver, params=self.params, data=data)
def test_from_qmi_response__ising_bqm(self): """Inspector data is correctly encoded for a simple Ising triangle problem given as a BQM.""" problem = (self.bqm_embedded.linear, self.bqm_embedded.quadratic) # sample with BrickedClient() as client: solver = client.get_solver(qpu=True) response = solver.sample_ising(*problem, **self.params) # convert data = from_qmi_response(problem, response, params=self.params) # validate data encoding self.verify_data_encoding(problem=problem, response=response, solver=solver, params=self.params, data=data)
def test_problem_label_in_response(self): """All data adapters should propagate problem label.""" # sample ising -> response with BrickedClient() as client: solver = client.get_solver(qpu=True) response = solver.sample_ising(*self.problem, label=self.label, **self.params) # ensure `from_qmi_response` adapter propagates label data = from_qmi_response(self.problem, response, params=self.params) self.assertEqual(data['details']['label'], self.label) # ensure `from_bqm_response` adapter propagates label data = from_bqm_response(self.bqm, self.embedding_context, response, params=self.params) self.assertEqual(data['details']['label'], self.label)
def test_solver_graph_validation(self): """All data adapters should fail on non-Chimera/Pegasus solvers.""" # sample with BrickedClient() as client: solver = client.get_solver(qpu=True) response = solver.sample_ising(*self.problem, **self.params) # resolve it before we mangle with it response.result() # change solver topology to non-chimera/pegasus to test solver validation response.solver.properties['topology']['type'] = 'unknown' # ensure `from_qmi_response` adapter fails on unstructured solver with self.assertRaises(TypeError): from_qmi_response(self.problem, response, params=self.params) # ensure `from_bqm_response` adapter fails on unstructured solver with self.assertRaises(TypeError): from_bqm_response(self.bqm, self.embedding_context, response, params=self.params)
def test_solver_type_validation(self): """All data adapters should fail on non-StructuredSolvers.""" # sample with BrickedClient() as client: solver = client.get_solver(qpu=True) response = solver.sample_ising(*self.problem, **self.params) # resolve it before we mangle with it response.result() # change solver to unstructured to test solver validation response.solver = unstructured_solver_mock # ensure `from_qmi_response` adapter fails on unstructured solver with self.assertRaises(TypeError): from_qmi_response(self.problem, response, params=self.params) # ensure `from_bqm_response` adapter fails on unstructured solver with self.assertRaises(TypeError): from_bqm_response(self.bqm, self.embedding_context, response, params=self.params)
def setUpClass(cls): # sample with BrickedClient() as client: solver = client.get_solver(qpu=True) cls.response = solver.sample_ising({}, {(0, 4): 1, (0, 5): 1, (4, 1): 1, (1, 5): -1}) cls.problem_id = cls.response.wait_id()