Skip to content

Commit

Permalink
Make LeapHybridSampler prefer hybrid solvers by default
Browse files Browse the repository at this point in the history
Addresses #288.
  • Loading branch information
randomir committed Aug 27, 2020
1 parent 31bbd10 commit bbe5e23
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
8 changes: 5 additions & 3 deletions dwave/system/samplers/leap_hybrid_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

__all__ = ['LeapHybridSampler']


class LeapHybridSampler(dimod.Sampler):
"""A class for using Leap's cloud-based hybrid solvers.
Expand All @@ -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.
Expand Down Expand Up @@ -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':
Expand Down
26 changes: 14 additions & 12 deletions tests/test_leaphybridsolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import dimod
from dwave.cloud import Client
from dwave.cloud.exceptions import SolverNotFoundError

try:
# py3
Expand Down Expand Up @@ -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()

Expand All @@ -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'})

Expand All @@ -85,27 +87,27 @@ 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'})

# Named solver: hybrid
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):
Expand All @@ -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'})

Expand All @@ -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'})

Expand Down

0 comments on commit bbe5e23

Please sign in to comment.