def test_load_solver_missing_data(self): """Try to load a solver that has incomplete data.""" with requests_mock.mock() as m: m.get(solver1_url, text=solver_object(solver_name, True)) client = Client(url, token) with self.assertRaises(InvalidAPIResponseError): client.get_solver(solver_name)
def test_load_missing_solver(self): """Try to load a solver that does not exist.""" with requests_mock.mock() as m: m.get(requests_mock.ANY, status_code=404) client = Client(url, token) with self.assertRaises(KeyError): client.get_solver(solver_name)
def test_load_solver_broken_response(self): """Try to load a solver for which the server has returned a truncated response.""" with requests_mock.mock() as m: body = solver_object(solver_name) m.get(solver1_url, text=body[0:len(body) // 2]) client = Client(url, token) with self.assertRaises(ValueError): client.get_solver(solver_name)
def test_submit_batch(self): """Submit batch of problems.""" # Connect client = Client(config_url, config_token) solver = client.get_solver(config_solver) result_list = [] for _ in range(100): # Build a linear problem linear = [0] * (max(solver.nodes) + 1) for index in solver.nodes: linear[index] = random.choice([-1, 1]) # Build a quad = { key: random.choice([-1, 1]) for key in solver.undirected_edges } results = solver.sample_ising(linear, quad, num_reads=10) result_list.append([results, linear, quad]) for results, linear, quad in result_list: # Did we get the right number of samples? self.assertTrue(10 == sum(results.occurrences)) # Make sure the number of occurrences and energies are all correct for energy, state in zip(results.energies, results.samples): self.assertTrue(energy == evaluate_ising(linear, quad, state))
def test_submit_partial_problem(self): """Submit a problem with only some of the terms set.""" # Connect client = Client(config_url, config_token) solver = client.get_solver(config_solver) # Build a linear problem linear = [0] * (max(solver.nodes) + 1) for index in solver.nodes: linear[index] = random.choice([-1, 1]) # Build a quad = {key: random.choice([-1, 1]) for key in solver.undirected_edges} # Remove half the qubits nodes = list(solver.nodes) for index in nodes[0:len(nodes) // 2]: linear[index] = 0 quad = { key: value for key, value in quad.items() if index not in key } # Solve the problem self._submit_and_check(solver, linear, quad)
def test_submit_invalid_parameter(self): """Ensure that the parameters are populated.""" client = Client(config_url, config_token) solver = client.get_solver(config_solver) assert 'not_a_parameter' not in solver.parameters with self.assertRaises(KeyError): solver.sample_ising({}, {}, not_a_parameter=True)
def test_wait_many(self): """Submit a batch of problems then use `wait_multiple` to wait on all of them.""" # Connect client = Client(config_url, config_token) solver = client.get_solver(config_solver) # Build a linear problem linear = [0] * (max(solver.nodes) + 1) for index in solver.nodes: linear[index] = random.choice([-1, 1]) # Build a quad = {key: random.choice([-1, 1]) for key in solver.undirected_edges} result_list = [] for _ in range(100): results = solver.sample_ising(linear, quad, num_reads=40) result_list.append([results, linear, quad]) dwave.cloud.computation.Future.wait_multiple( [f[0] for f in result_list]) for results, _, _ in result_list: self.assertTrue(results.done()) for results, linear, quad in result_list: # Did we get the right number of samples? self.assertTrue(40 == sum(results.occurrences)) # Make sure the number of occurrences and energies are all correct for energy, state in zip(results.energies, results.samples): self.assertTrue(energy == evaluate_ising(linear, quad, state))
def test_as_completed(self): """Submit a batch of problems then use `as_completed` to iterate over all of them.""" # Connect client = Client(config_url, config_token) solver = client.get_solver(config_solver) # Build a problem linear = [0] * (max(solver.nodes) + 1) for index in solver.nodes: linear[index] = random.choice([-1, 1]) quad = {key: random.choice([-1, 1]) for key in solver.undirected_edges} # Sample the solution 100x40 times computations = [ solver.sample_ising(linear, quad, num_reads=40) for _ in range(100) ] # Go over computations, one by one, as they're done and check they're OK for computation in dwave.cloud.computation.Future.as_completed( computations): self.assertTrue(computation.done()) self.assertTrue(40 == sum(computation.occurrences)) for energy, state in zip(computation.energies, computation.samples): self.assertTrue(energy == evaluate_ising(linear, quad, state))
def test_result_structure(self): client = Client(config_url, config_token) solver = client.get_solver(config_solver) computation = solver.sample_ising({}, {}) result = computation.result() self.assertIn('samples', result) self.assertIn('energies', result) self.assertIn('occurrences', result) self.assertIn('timing', result)
def test_load_solver(self): """Load a single solver.""" with requests_mock.mock() as m: setup_server(m) # test default, cached solver get client = Client(url, token) solver = client.get_solver(solver_name) self.assertEqual(solver.id, solver_name) # fetch solver not present in cache client._solvers = {} self.assertEqual(client.get_solver(solver_name).id, solver_name) # re-fetch solver present in cache solver = client.get_solver(solver_name) solver.id = 'different-solver' self.assertEqual( client.get_solver(solver_name, refresh=True).id, solver_name)
def test_submit_dict_problem(self): """Submit a problem using a dict for the linear terms.""" # Connect client = Client(config_url, config_token) solver = client.get_solver(config_solver) # Build a problem linear = {index: random.choice([-1, 1]) for index in solver.nodes} quad = {key: random.choice([-1, 1]) for key in solver.undirected_edges} # Solve the problem self._submit_and_check(solver, linear, quad)
def test_submit_linear_problem(self): """Submit a problem with all the linear terms populated.""" # Connect client = Client(config_url, config_token) solver = client.get_solver(config_solver) # Build a linear problem linear = [0] * (max(solver.nodes) + 1) for index in solver.nodes: linear[index] = 1 quad = {} # Solve the problem self._submit_and_check(solver, linear, quad)
def test_request_raw_list_with_numpy(self): """Submit a problem using a dict for the linear terms.""" # Connect client = Client(config_url, config_token) assert dwave.cloud.computation._numpy solver = client.get_solver(config_solver) solver.return_matrix = False # Build a problem linear = {index: random.choice([-1, 1]) for index in solver.nodes} quad = {key: random.choice([-1, 1]) for key in solver.undirected_edges} # Solve the problem self._submit_and_check(solver, linear, quad)
def test_request_raw_matrix_with_no_numpy(self): """Submit a problem using a dict for the linear terms.""" # Connect client = Client(config_url, config_token) dwave.cloud.computation._numpy = False solver = client.get_solver(config_solver) solver.return_matrix = True # Build a problem linear = {index: random.choice([-1, 1]) for index in solver.nodes} quad = {key: random.choice([-1, 1]) for key in solver.undirected_edges} # Solve the problem with self.assertRaises(ValueError): self._submit_and_check(solver, linear, quad, answer_mode='raw')
def test_submit_full_problem(self): """Submit a problem with all supported coefficients set.""" # Connect client = Client(config_url, config_token) solver = client.get_solver(config_solver) # Build a linear problem linear = [0] * (max(solver.nodes) + 1) for index in solver.nodes: linear[index] = random.choice([-1, 1]) # Build a quad = {key: random.choice([-1, 1]) for key in solver.undirected_edges} # Solve the problem self._submit_and_check(solver, linear, quad)
def test_submit_extra_qubit(self): """Submit a defective problem with an unsupported variable.""" # Connect client = Client(config_url, config_token) solver = client.get_solver(config_solver) # Build a linear problem linear = [0] * (max(solver.nodes) + 1) for index in solver.nodes: linear[index] = 1 quad = {} # Add a variable that shouldn't exist linear.append(1) with self.assertRaises(ValueError): results = solver.sample_ising(linear, quad) results.samples
def test_request_matrix_with_numpy(self): """Submit a problem using a dict for the linear terms.""" # Connect import numpy client = Client(config_url, config_token) assert dwave.cloud.computation._numpy solver = client.get_solver(config_solver) solver.return_matrix = True # Build a problem linear = {index: random.choice([-1, 1]) for index in solver.nodes} quad = {key: random.choice([-1, 1]) for key in solver.undirected_edges} # Solve the problem result = self._submit_and_check(solver, linear, quad) self.assertIsInstance(result.samples, numpy.ndarray) self.assertIsInstance(result.energies, numpy.ndarray) self.assertIsInstance(result.occurrences, numpy.ndarray)
def test_load_all_solvers(self): """List and retrieve all the solvers.""" client = Client(config_url, config_token) for name in client.get_solvers(): client.get_solver(name)
def test_read_connectivity(self): """Ensure that the edge set is populated.""" client = Client(config_url, config_token) solver = client.get_solver(config_solver) self.assertTrue(len(solver.edges) > 0)
def test_load_parameters(self): """Make sure the parameters are populated.""" client = Client(config_url, config_token) solver = client.get_solver(config_solver) self.assertTrue(len(solver.parameters) > 0)
def test_load_bad_solvers(self): """Try to load a nonexistent solver.""" client = Client(config_url, config_token) with self.assertRaises(KeyError): client.get_solver("not-a-solver")
def test_load_any_solver(self): """Load a single solver without calling get_solvers (which caches data).""" client = Client(config_url, config_token) client.get_solver(config_solver)
def test_load_properties(self): """Ensure that the propreties are populated.""" client = Client(config_url, config_token) solver = client.get_solver(config_solver) self.assertTrue(len(solver.properties) > 0)