def _binary_quadratic_model_sampler(f, *args, **kw): # convert into a sequence if necessary if isinstance(which_args, int): iter_args = (which_args,) else: iter_args = iter(which_args) # check each sampler for the correct methods new_args = [arg for arg in args] for idx in iter_args: sampler = args[idx] # if no sampler is provided, get the default sampler if it has # been set if sampler is None: # this sampler has already been vetted default_sampler = dnx.get_default_sampler() if default_sampler is None: raise dnx.DWaveNetworkXMissingSampler('no default sampler set') new_args[idx] = default_sampler continue if not hasattr(sampler, "sample_qubo") or not callable(sampler.sample_qubo): raise TypeError("expected sampler to have a 'sample_qubo' method") if not hasattr(sampler, "sample_ising") or not callable(sampler.sample_ising): raise TypeError("expected sampler to have a 'sample_ising' method") # now run the function and return the results return f(*new_args, **kw)
def func(*args, **kwargs): bound_arguments = inspect.signature(f).bind(*args, **kwargs) bound_arguments.apply_defaults() args = bound_arguments.args kw = bound_arguments.kwargs if isinstance(which_args, int): iter_args = (which_args, ) else: iter_args = iter(which_args) # check each sampler for the correct methods new_args = [arg for arg in args] for idx in iter_args: sampler = args[idx] # if no sampler is provided, get the default sampler if it has # been set if sampler is None: # this sampler has already been vetted default_sampler = dnx.get_default_sampler() if default_sampler is None: raise dnx.DWaveNetworkXMissingSampler( 'no default sampler set') new_args[idx] = default_sampler continue if not hasattr(sampler, "sample_qubo") or not callable( sampler.sample_qubo): raise TypeError( "expected sampler to have a 'sample_qubo' method") if not hasattr(sampler, "sample_ising") or not callable( sampler.sample_ising): raise TypeError( "expected sampler to have a 'sample_ising' method") # now run the function and return the results return f(*new_args, **kw)