Skip to content

Commit fabf8ac

Browse files
committed
Update docs to promote use of context manager for samplers
1 parent 4b0f162 commit fabf8ac

File tree

3 files changed

+54
-84
lines changed

3 files changed

+54
-84
lines changed

dwave/system/samplers/clique.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ class DWaveCliqueSampler(dimod.Sampler, AbstractContextManager):
167167
The preferred and recommended way to use :class:`DWaveCliqueSampler` is
168168
from a runtime context:
169169
170-
>>> with DWaveCliqueSampler() as sampler: # doctest: +SKIP
171-
>>> sampler.sample(...)
170+
>>> with DWaveCliqueSampler() as sampler:
171+
... sampler.sample(...) # doctest: +SKIP
172172
173173
If this is not feasible in your code, don't forget to shutdown sampler
174174
resources by calling :meth:`~DWaveCliqueSampler.close`:
@@ -190,11 +190,10 @@ class DWaveCliqueSampler(dimod.Sampler, AbstractContextManager):
190190
...
191191
>>> bqm = dimod.generators.ran_r(1, 6)
192192
...
193-
>>> sampler = DWaveCliqueSampler() # doctest: +SKIP
194-
>>> sampler.largest_clique_size > 5 # doctest: +SKIP
193+
>>> with DWaveCliqueSampler() as sampler: # doctest: +SKIP
194+
... print(sampler.largest_clique_size > 5)
195+
... sampleset = sampler.sample(bqm, num_reads=100)
195196
True
196-
>>> sampleset = sampler.sample(bqm, num_reads=100) # doctest: +SKIP
197-
>>> sampler.close() # doctest: +SKIP
198197
199198
"""
200199
def __init__(self, *,

dwave/system/samplers/dwave_sampler.py

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ class DWaveSampler(dimod.Sampler, dimod.Structured, AbstractContextManager):
141141
The preferred and recommended way to use :class:`DWaveSampler` is from a
142142
runtime context created by ``DWaveSampler()``:
143143
144-
>>> with DWaveSampler() as sampler: # doctest: +SKIP
145-
>>> sampler.sample_ising(...)
144+
>>> with DWaveSampler() as sampler:
145+
... sampler.sample_ising(...) # doctest: +SKIP
146146
147147
If this is not feasible in your code, don't forget to shutdown sampler
148148
resources by calling :meth:`~DWaveSampler.close` when done:
@@ -166,16 +166,14 @@ class DWaveSampler(dimod.Sampler, dimod.Structured, AbstractContextManager):
166166
167167
>>> from dwave.system import DWaveSampler
168168
...
169-
>>> sampler = DWaveSampler()
170-
...
171-
>>> qubit_a = sampler.nodelist[0]
172-
>>> qubit_b = next(iter(sampler.adjacency[qubit_a]))
173-
>>> sampleset = sampler.sample_ising({qubit_a: -1, qubit_b: 1},
174-
... {},
175-
... num_reads=100)
176-
>>> print(sampleset.first.sample[qubit_a] == 1 and sampleset.first.sample[qubit_b] == -1)
169+
>>> with DWaveSampler() as sampler:
170+
... qubit_a = sampler.nodelist[0]
171+
... qubit_b = next(iter(sampler.adjacency[qubit_a]))
172+
... sampleset = sampler.sample_ising({qubit_a: -1, qubit_b: 1},
173+
... {},
174+
... num_reads=100)
175+
... print(sampleset.first.sample[qubit_a] == 1 and sampleset.first.sample[qubit_b] == -1)
177176
True
178-
>>> sampler.close()
179177
180178
See `Ocean Glossary <https://docs.ocean.dwavesys.com/en/stable/concepts/index.html>`_
181179
for explanations of technical terms in descriptions of Ocean tools.
@@ -259,8 +257,8 @@ def properties(self):
259257
Examples:
260258
261259
>>> from dwave.system import DWaveSampler
262-
>>> sampler = DWaveSampler()
263-
>>> sampler.properties # doctest: +SKIP
260+
>>> with DWaveSampler() as sampler: # doctest: +SKIP
261+
... sampler.properties
264262
{'anneal_offset_ranges': [[-0.2197463755538704, 0.03821687759418928],
265263
[-0.2242514597680286, 0.01718456460967399],
266264
[-0.20860153999435985, 0.05511969218508182],
@@ -290,8 +288,8 @@ def parameters(self):
290288
Examples:
291289
292290
>>> from dwave.system import DWaveSampler
293-
>>> sampler = DWaveSampler()
294-
>>> sampler.parameters # doctest: +SKIP
291+
>>> with DWaveSampler() as sampler: # doctest: +SKIP
292+
... sampler.parameters
295293
{'anneal_offsets': ['parameters'],
296294
'anneal_schedule': ['parameters'],
297295
'annealing_time': ['parameters'],
@@ -321,8 +319,8 @@ def edgelist(self):
321319
First 5 entries of the coupler list for one Advantage system.
322320
323321
>>> from dwave.system import DWaveSampler
324-
>>> sampler = DWaveSampler()
325-
>>> sampler.edgelist[:5] # doctest: +SKIP
322+
>>> with DWaveSampler() as sampler: # doctest: +SKIP
323+
... sampler.edgelist[:5]
326324
[(30, 31), (30, 45), (30, 2940), (30, 2955), (30, 2970)]
327325
328326
See `Ocean Glossary <https://docs.ocean.dwavesys.com/en/stable/concepts/index.html>`_
@@ -345,8 +343,8 @@ def nodelist(self):
345343
First 5 entries of the node list for one Advantage system.
346344
347345
>>> from dwave.system import DWaveSampler
348-
>>> sampler = DWaveSampler()
349-
>>> sampler.nodelist[:5] # doctest: +SKIP
346+
>>> with DWaveSampler() as sampler: # doctest: +SKIP
347+
... sampler.nodelist[:5]
350348
[30, 31, 32, 33, 34]
351349
352350
See `Ocean Glossary <https://docs.ocean.dwavesys.com/en/stable/concepts/index.html>`_
@@ -428,14 +426,13 @@ def sample(self, bqm, warnings=None, **kwargs):
428426
429427
>>> from dwave.system import DWaveSampler
430428
...
431-
>>> sampler = DWaveSampler()
432-
...
433-
>>> qubit_a = sampler.nodelist[0]
434-
>>> qubit_b = next(iter(sampler.adjacency[qubit_a]))
435-
>>> sampleset = sampler.sample_ising({qubit_a: -1, qubit_b: 1},
436-
... {},
437-
... num_reads=100)
438-
>>> print(sampleset.first.sample[qubit_a] == 1 and sampleset.first.sample[qubit_b] == -1)
429+
>>> with DWaveSampler() as sampler:
430+
... qubit_a = sampler.nodelist[0]
431+
... qubit_b = next(iter(sampler.adjacency[qubit_a]))
432+
... sampleset = sampler.sample_ising({qubit_a: -1, qubit_b: 1},
433+
... {},
434+
... num_reads=100)
435+
... print(sampleset.first.sample[qubit_a] == 1 and sampleset.first.sample[qubit_b] == -1)
439436
True
440437
441438
See `Ocean Glossary <https://docs.ocean.dwavesys.com/en/stable/concepts/index.html>`_
@@ -548,10 +545,9 @@ def validate_anneal_schedule(self, anneal_schedule):
548545
This example sets a quench schedule on a D-Wave system.
549546
550547
>>> from dwave.system import DWaveSampler
551-
>>> sampler = DWaveSampler()
552-
>>> quench_schedule=[[0.0, 0.0], [12.0, 0.6], [12.8, 1.0]]
553-
>>> DWaveSampler().validate_anneal_schedule(quench_schedule) # doctest: +SKIP
554-
>>>
548+
>>> with DWaveSampler() as sampler: # doctest: +SKIP
549+
... quench_schedule=[[0.0, 0.0], [12.0, 0.6], [12.8, 1.0]]
550+
... DWaveSampler().validate_anneal_schedule(quench_schedule)
555551
556552
"""
557553
if 'anneal_schedule' not in self.parameters:
@@ -622,9 +618,9 @@ def to_networkx_graph(self):
622618
623619
>>> from dwave.system import DWaveSampler
624620
...
625-
>>> sampler = DWaveSampler()
626-
>>> g = sampler.to_networkx_graph() # doctest: +SKIP
627-
>>> len(g.nodes) > 5000 # doctest: +SKIP
621+
>>> with DWaveSampler() as sampler: # doctest: +SKIP
622+
... g = sampler.to_networkx_graph()
623+
... len(g.nodes) > 5000
628624
True
629625
"""
630626
return qpu_graph(self.properties['topology']['type'],

dwave/system/samplers/leap_hybrid_sampler.py

Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -110,23 +110,8 @@ class LeapHybridSampler(dimod.Sampler, ClosableClientBaseMixin):
110110
>>> bqm = dimod.BQM.from_qubo(qubo)
111111
...
112112
>>> # Find a good solution
113-
>>> sampler = LeapHybridSampler() # doctest: +SKIP
114-
>>> sampleset = sampler.sample(bqm) # doctest: +SKIP
115-
116-
This example specializes the default solver selection by filtering out
117-
bulk BQM solvers. (Bulk solvers are throughput-optimal for heavy/batch
118-
workloads, have a higher start-up latency, and are not well suited for
119-
live workloads. Not all Leap accounts have access to bulk solvers.)
120-
121-
>>> from dwave.system import LeapHybridSampler
122-
...
123-
>>> solver = LeapHybridSampler.default_solver
124-
>>> solver.update(name__regex=".*(?<!bulk)$") # name shouldn't end with "bulk"
125-
>>> sampler = LeapHybridSampler(solver=solver) # doctest: +SKIP
126-
>>> sampler.solver # doctest: +SKIP
127-
BQMSolver(id='hybrid_binary_quadratic_model_version2')
128-
...
129-
>>> sampler.close() # doctest: +SKIP
113+
>>> with LeapHybridSampler() as sampler: # doctest: +SKIP
114+
... sampleset = sampler.sample(bqm)
130115
"""
131116

132117
_INTEGER_BQM_SIZE_THRESHOLD = 10000
@@ -237,10 +222,8 @@ def sample(self, bqm, time_limit=None, **kwargs):
237222
>>> bqm = dimod.BQM.from_qubo(qubo)
238223
...
239224
>>> # Find a good solution
240-
>>> sampler = LeapHybridSampler() # doctest: +SKIP
241-
>>> sampleset = sampler.sample(bqm) # doctest: +SKIP
242-
...
243-
>>> sampler.close() # doctest: +SKIP
225+
>>> with LeapHybridSampler() as sampler: # doctest: +SKIP
226+
... sampleset = sampler.sample(bqm)
244227
"""
245228

246229
if not isinstance(bqm, dimod.BQM):
@@ -368,14 +351,11 @@ class LeapHybridDQMSampler(ClosableClientBaseMixin):
368351
... dqm.set_quadratic('my_hand', 'their_hand',
369352
... {(my_idx, their_idx): 1})
370353
...
371-
>>> dqm_sampler = LeapHybridDQMSampler() # doctest: +SKIP
372-
...
373-
>>> sampleset = dqm_sampler.sample_dqm(dqm) # doctest: +SKIP
374-
>>> print("{} beats {}".format(cases[sampleset.first.sample['my_hand']],
375-
... cases[sampleset.first.sample['their_hand']])) # doctest: +SKIP
354+
>>> with LeapHybridDQMSampler() as dqm_sampler: # doctest: +SKIP
355+
... sampleset = dqm_sampler.sample_dqm(dqm)
356+
... print(f"{} beats {}".format(cases[sampleset.first.sample['my_hand']],
357+
... cases[sampleset.first.sample['their_hand']]))
376358
rock beats scissors
377-
...
378-
>>> dqm_sampler.close() # doctest: +SKIP
379359
"""
380360

381361
@classproperty
@@ -618,17 +598,15 @@ class LeapHybridCQMSampler(ClosableClientBaseMixin):
618598
a remote solver provided by the Leap quantum cloud service:
619599
620600
>>> from dwave.system import LeapHybridCQMSampler # doctest: +SKIP
621-
>>> sampler = LeapHybridCQMSampler() # doctest: +SKIP
622-
>>> sampleset = sampler.sample_cqm(cqm) # doctest: +SKIP
623-
>>> print(sampleset.first) # doctest: +SKIP
601+
>>> with LeapHybridCQMSampler() as sampler: # doctest: +SKIP
602+
... sampleset = sampler.sample_cqm(cqm)
603+
... print(sampleset.first)
624604
Sample(sample={'i': 2.0, 'j': 2.0}, energy=-4.0, num_occurrences=1,
625605
... is_feasible=True, is_satisfied=array([ True]))
626606
627607
The best (lowest-energy) solution found has :math:`i=j=2` as expected,
628608
a solution that is feasible because all the constraints (one in this
629609
example) are satisfied.
630-
...
631-
>>> sampler.close() # doctest: +SKIP
632610
"""
633611

634612
def __init__(self, **config):
@@ -852,18 +830,15 @@ class LeapHybridNLSampler(ClosableClientBaseMixin):
852830
>>> from dwave.optimization.generators import flow_shop_scheduling
853831
>>> from dwave.system import LeapHybridNLSampler
854832
...
855-
>>> sampler = LeapHybridNLSampler() # doctest: +SKIP
856-
...
857-
>>> processing_times = [[10, 5, 7], [20, 10, 15]]
858-
>>> model = flow_shop_scheduling(processing_times=processing_times)
859-
>>> results = sampler.sample(model, label="Small FSS problem") # doctest: +SKIP
860-
>>> job_order = next(model.iter_decisions()) # doctest: +SKIP
861-
>>> print(f"State 0 of {model.objective.state_size()} has an "\ # doctest: +SKIP
862-
... f"objective value {model.objective.state(0)} for order " \ # doctest: +SKIP
863-
... f"{job_order.state(0)}.") # doctest: +SKIP
833+
>>> with LeapHybridNLSampler() as sampler: # doctest: +SKIP
834+
... processing_times = [[10, 5, 7], [20, 10, 15]]
835+
... model = flow_shop_scheduling(processing_times=processing_times)
836+
... results = sampler.sample(model, label="Small FSS problem")
837+
... job_order = next(model.iter_decisions())
838+
... print(f"State 0 of {model.objective.state_size()} has an "
839+
... f"objective value {model.objective.state(0)} for order "
840+
... f"{job_order.state(0)}.")
864841
State 0 of 8 has an objective value 50.0 for order [1. 2. 0.].
865-
...
866-
>>> sampler.close() # doctest: +SKIP
867842
"""
868843

869844
def __init__(self, **config):

0 commit comments

Comments
 (0)