Clients¶
The solvers that provide sampling for solving Ising and QUBO problems, such
as a D-Wave 2000Q QPU or a software sampler such as the dimod
simulated annealing sampler, are typically remote resources. The D-Wave Cloud Client
Client class manages such remote solver resources.
Preferred use is with a context manager—a with Client.from_config(...) as
construct—to ensure proper closure of all resources. The following example snippet
creates a client based on an auto-detected configuration file and instantiates
a solver.
>>> with Client.from_config() as client: # doctest: +SKIP
... solver = client.get_solver(num_qubits__gt=2000)
Alternatively, the following example snippet creates a client for software resources that it later explicitly closes.
>>> client = Client.from_config(software=True) # doctest: +SKIP
>>> # code that uses client
>>> client.close() # doctest: +SKIP
Typically you use the Client class. By default, it instantiates
a QPU client. You can also use the specialized QPU and CPU/GPU clients directly.
Client (Base Client)¶
D-Wave API clients handle communications with solver resources: problem submittal, monitoring, samples retrieval, etc.
Examples
This example creates a client using the local system’s default D-Wave Cloud Client configuration file, which is configured to access a D-Wave 2000Q QPU, submits a QUBO problem (a Boolean NOT gate represented by a penalty model), and samples 5 times.
>>> from dwave.cloud import Client
>>> Q = {(0, 0): -1, (0, 4): 0, (4, 0): 2, (4, 4): -1}
>>> with Client.from_config() as client: # doctest: +SKIP
... solver = client.get_solver()
... computation = solver.sample_qubo(Q, num_reads=5)
...
>>> for i in range(5): # doctest: +SKIP
... print(computation.samples[i][0], computation.samples[i][4])
...
(1, 0)
(1, 0)
(0, 1)
(0, 1)
(0, 1)
Class¶
-
class
dwave.cloud.client.Client(endpoint=None, token=None, solver=None, proxy=None, permissive_ssl=False, request_timeout=60, polling_timeout=None, connection_close=False, headers=None, **kwargs)¶ Base client class for all D-Wave API clients. Used by QPU and software sampler classes.
Manages workers and handles thread pools for submitting problems, cancelling tasks, polling problem status, and retrieving results.
Parameters: - endpoint (str) – D-Wave API endpoint URL.
- token (str) – Authentication token for the D-Wave API.
- solver (dict/str) – Default solver features (or simply solver name).
- proxy (str) – Proxy URL to be used for accessing the D-Wave API.
- permissive_ssl (bool, default=False) – Disables SSL verification.
- request_timeout (float, default=60) – Connect and read timeout (in seconds) for all requests to the D-Wave API.
- polling_timeout (float, default=None) – Problem status polling timeout (in seconds), after which polling is aborted.
- connection_close (bool, default=False) – Force HTTP(S) connection close after each request.
- headers (dict/str) – Additional HTTP headers.
Other Parameters: Unrecognized keys (str) – All unrecognized keys are passed through to the appropriate client class constructor as string keyword arguments.
An explicit key value overrides an identical user-defined key value loaded from a configuration file.
Examples
This example directly initializes a
Client. Direct initialization uses class constructor arguments, the minimum being a value for token.>>> from dwave.cloud import Client >>> client = Client(token='secret') >>> # code that uses client >>> client.close()
Methods¶
client.Client.from_config([config_file, …]) |
Client factory method to instantiate a client instance from configuration. |
client.Client.solvers([refresh]) |
Deprecated in favor of get_solvers(). |
client.Client.get_solver([name, refresh]) |
Load the configuration for a single solver. |
client.Client.get_solvers([refresh, order_by]) |
Return a filtered list of solvers handled by this client. |
client.Client.is_solver_handled(solver) |
Determine if the specified solver should be handled by this client. |
client.Client.close() |
Perform a clean shutdown. |
Specialized Clients¶
Typically you use the Client class. By default, it instantiates
a QPU client. You can also instantiate a QPU or CPU/GPU client directly.
QPU Client¶
An implementation of the REST API for D-Wave Solver API (SAPI) servers.
SAPI servers provide authentication, queuing, and scheduling services, and provide a network interface to solvers. This API enables you submit a binary quadratic (Ising or QUBO) model and receive samples from a distribution over the model as defined by a selected solver.
SAPI server workflow is roughly as follows:
- Submitted problems enter an input queue. Each user has an input queue per solver.
- Drawing from all input queues for a solver, problems are scheduled.
- Results are cached for retrieval by the client.
Class¶
-
class
dwave.cloud.qpu.Client(endpoint=None, token=None, solver=None, proxy=None, permissive_ssl=False, request_timeout=60, polling_timeout=None, connection_close=False, headers=None, **kwargs)¶ D-Wave API client specialized to work with QPU solvers.
This class is instantiated by default, or explicitly when client=qpu, with the typical base client instantiation
with Client.from_config() as client:of a client. (You should not instantiate this class with client=sw or use it with solver feature constraint software=True.)Examples
This example explicitly instantiates a
dwave.cloud.qpu.clientbased on the local system`s default D-Wave Cloud Client configuration file to sample a random Ising problem tailored to fit the client`s default solver`s graph.import random from dwave.cloud.qpu import Client # Use context manager to ensure resources (thread pools used by Client) are released with Client.from_config() as client: solver = client.get_solver() # Build problem to exactly fit the solver graph linear = {index: random.choice([-1, 1]) for index in solver.nodes} quad = {key: random.choice([-1, 1]) for key in solver.undirected_edges} # Sample 100 times and print out the first sample computation = solver.sample_ising(linear, quad, num_reads=100) print(computation.samples[0])
Methods¶
qpu.Client.is_solver_handled(solver) |
Determine if the specified solver should be handled by this client. |
Software-Samplers Client¶
Class¶
-
class
dwave.cloud.sw.Client(endpoint=None, token=None, solver=None, proxy=None, permissive_ssl=False, request_timeout=60, polling_timeout=None, connection_close=False, headers=None, **kwargs)¶ D-Wave API client specialized to work with remote software solvers (samplers).
This class is instantiated by default, or explicitly when client=sw, with the typical base client instantiation
with Client.from_config() as client:of a client. (You should not instantiate this class with client=qpu or use it with solver feature constraint qpu=True.)Examples
This example indirectly instantiates a
dwave.cloud.sw.clientbased on the local system`s default D-Wave Cloud Client configuration file to sample a random Ising problem tailored to fit the client`s default solver`s graph.import random from dwave.cloud import Client # Use context manager to ensure resources (thread pools used by Client) are released with Client.from_config(solver={"software": True}) as client: solver = client.get_solver() # Build problem to exactly fit the solver graph linear = {index: random.choice([-1, 1]) for index in solver.nodes} quad = {key: random.choice([-1, 1]) for key in solver.undirected_edges} # Sample 100 times and print out the first sample computation = solver.sample_ising(linear, quad, num_reads=100) print(computation.samples[0])
Methods¶
sw.Client.is_solver_handled(solver) |
Determine if the specified solver should be handled by this client. |