dwave_qbsolv.QBSolv.sample¶
-
QBSolv.
sample
(bqm, num_repeats=50, seed=None, algorithm=None, verbosity=-1, timeout=2592000, solver_limit=None, solver=None, target=None, find_max=False, **sample_kwargs)[source]¶ Sample low-energy states defined by a QUBO using qbsolv.
Note
The qbsolv library being shared by all instances of this class is non-reentrant and not thread safe. The GIL should not be released by this method until that is resolved.
Note
The default build of this library doesn’t have the dw library. To use solver=’dw’ this module must be built from source with that library.
The parameter solver given to this method has several valid forms:
- String ‘tabu’ (default): sub problems are called via an internal call to tabu.
- String ‘dw’: sub problems are given to the dw library.
- Instance of a dimod sampler. The sample_qubo method is invoked.
- Callable that has the signature (qubo: dict, current_best: dict) and returns a result list/dictionary with the new solution.
Parameters: - Q (dict) – A dictionary defining the QUBO. Should be of the form {(u, v): bias} where u, v are variables and bias is numeric.
- num_repeats (int, optional) – Determines the number of times to repeat the main loop in qbsolv after determining a better sample. Default 50.
- seed (int, optional) – Random seed. Default generated by random module.
- algorithm (int, optional) – Algorithm to use. Default is ENERGY_IMPACT. Algorithm numbers can be imported from the module under the names ENERGY_IMPACT and SOLUTION_DIVERSITY.
- verbosity (int, optional) – Prints more detail about qbsolv’s internal process as this number increases.
- timeout (float, optional) – Number of seconds before routine halts. Default is 2592000.
- solver – Sampling method for qbsolv to use; see method description.
- solver_limit (int, optional) – Maximum number of variables in a sub problem.
- target (float, optional) – If given, qbsolv halts when a state with this energy value or better is discoverd. Default is None.
- find_max (bool, optional) – Switches from searching for minimization to maximization. Default is False (minimization).
Returns: Response
Examples
This example uses the tabu search algorithm to solve a small QUBO.
>>> Q = {(0, 0): 1, (1, 1): 1, (0, 1): 1} >>> response = QBSolv().sample_qubo(Q) >>> list(response.samples()) '[{0: 0, 1: 0}]' >>> list(response.energies()) '[0.0]'