diff --git a/dwave/system/samplers/leap_hybrid_sampler.py b/dwave/system/samplers/leap_hybrid_sampler.py index 98067159..b14b33d1 100644 --- a/dwave/system/samplers/leap_hybrid_sampler.py +++ b/dwave/system/samplers/leap_hybrid_sampler.py @@ -29,6 +29,7 @@ __all__ = ['LeapHybridSampler'] + class LeapHybridSampler(dimod.Sampler): """A class for using Leap's cloud-based hybrid solvers. @@ -55,7 +56,7 @@ class LeapHybridSampler(dimod.Sampler): solver (dict/str, optional): Solver (a hybrid solver on which to run submitted problems) to select, - formatted as a string. + formatted as a string, or as a dict of solver features required. proxy (str, optional): Proxy URL to be used for accessing the D-Wave API. @@ -87,13 +88,14 @@ class LeapHybridSampler(dimod.Sampler): def __init__(self, solver=None, connection_close=True, **config): - # always use the base class (QPU client filters-out the hybrid solvers) - config['client'] = 'base' + # we want a Hybrid solver by default, but allow override + config.setdefault('client', 'hybrid') if solver is None: solver = {} if isinstance(solver, abc.Mapping): + # TODO: instead of solver selection, try with user's default first if solver.setdefault('category', 'hybrid') != 'hybrid': raise ValueError("the only 'category' this sampler supports is 'hybrid'") if solver.setdefault('supported_problem_types__contains', 'bqm') != 'bqm': diff --git a/tests/test_leaphybridsolver.py b/tests/test_leaphybridsolver.py index 73031e1d..235c89e9 100644 --- a/tests/test_leaphybridsolver.py +++ b/tests/test_leaphybridsolver.py @@ -18,6 +18,7 @@ import dimod from dwave.cloud import Client +from dwave.cloud.exceptions import SolverNotFoundError try: # py3 @@ -46,7 +47,7 @@ def get_solver(self): return MockBadLeapHybridSolver() if self.args.get('client', 'base') not in ['base', 'hybrid']: - return MockBadLeapHybridSolver() + raise SolverNotFoundError return MockLeapHybridSolver() @@ -61,19 +62,20 @@ def test_solver_init(self, mock_client): mock_client.reset_mock() LeapHybridSampler() mock_client.from_config.assert_called_once_with( - client='base', connection_close=True, + client='hybrid', connection_close=True, solver={'category': 'hybrid', 'supported_problem_types__contains': 'bqm'}) - # Non-hybrid client setting is ignored? + # Non-hybrid client setting mock_client.reset_mock() - LeapHybridSampler(client='qpu') + with self.assertRaises(SolverNotFoundError): + LeapHybridSampler(client='qpu') # Explicitly set category to hybrid mock_client.reset_mock() LeapHybridSampler(solver={'category': 'hybrid', 'supported_problem_types__contains': 'bqm'}) mock_client.from_config.assert_called_once_with( - client='base', connection_close=True, + client='hybrid', connection_close=True, solver={'category': 'hybrid', 'supported_problem_types__contains': 'bqm'}) @@ -85,14 +87,14 @@ def test_solver_init(self, mock_client): mock_client.reset_mock() LeapHybridSampler(solver={'qpu': True}) mock_client.from_config.assert_called_once_with( - client='base', connection_close=True, + client='hybrid', connection_close=True, solver={'qpu': True, 'category': 'hybrid', 'supported_problem_types__contains': 'bqm'}) mock_client.reset_mock() - LeapHybridSampler(solver={'qpu': True, 'anneal_schedule' :False}) + LeapHybridSampler(solver={'qpu': True, 'anneal_schedule': False}) mock_client.from_config.assert_called_once_with( - client='base', connection_close=True, + client='hybrid', connection_close=True, solver={'anneal_schedule': False, 'qpu': True, 'category': 'hybrid', 'supported_problem_types__contains': 'bqm'}) @@ -100,12 +102,12 @@ def test_solver_init(self, mock_client): mock_client.reset_mock() LeapHybridSampler(solver="hybrid_solver") mock_client.from_config.assert_called_once_with( - client='base', connection_close=True, solver="hybrid_solver") + client='hybrid', connection_close=True, solver="hybrid_solver") mock_client.reset_mock() LeapHybridSampler(connection_close=False, solver="hybrid_solver") mock_client.from_config.assert_called_once_with( - client='base', connection_close=False, solver="hybrid_solver") + client='hybrid', connection_close=False, solver="hybrid_solver") # Named solver: non-hybrid with self.assertRaises(ValueError): @@ -115,7 +117,7 @@ def test_solver_init(self, mock_client): mock_client.reset_mock() LeapHybridSampler(connection_close=False) mock_client.from_config.assert_called_once_with( - client='base', connection_close=False, + client='hybrid', connection_close=False, solver={'category': 'hybrid', 'supported_problem_types__contains': 'bqm'}) @@ -124,7 +126,7 @@ def test_solver_init(self, mock_client): solver={'category': 'hybrid', 'supported_problem_types__contains': 'bqm'}) mock_client.from_config.assert_called_once_with( - client='base', connection_close=False, + client='hybrid', connection_close=False, solver={'category': 'hybrid', 'supported_problem_types__contains': 'bqm'})