Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add close() to all QPU and hybrid samplers #554

Conversation

randomir
Copy link
Member

@randomir randomir commented Jan 29, 2025

Close #77.

Note: tests will fail because we need to release dwave-cloud-client==0.13.3 first. We skip tests that require UseAfterCloseError from cloud client 0.13.3.

Follow-up: add support for context manager protocol to all samplers.

@randomir
Copy link
Member Author

@arcondello, @JoelPasvolsky: cloud-client release might be a few days away, but you might want to take a preliminary look.

@JoelPasvolsky
Copy link
Contributor

JoelPasvolsky commented Jan 29, 2025

Can you give some more context: the description in #77 leaves a bit to be desired. Is it a problem of users repeatedly instantiating new samplers? If so, couldn't instantiation of a sampler (e.g., >>> sampler = LeapHybridSampler()) by default close or reuse an existing identical instance unless explicitly configured not to with something like an instantiate_new=True parameter? I mean, do we need to put this burden on users?
Doing it through a context manager works well

@randomir
Copy link
Member Author

randomir commented Jan 29, 2025

@JoelPasvolsky, each instance of a sampler should be closed after use. Similarly to Client.

An immediate follow-up to this PR will be #91, a context manager support for samplers. Similarly to Client. That'll be the preferred pattern. Of course, users will be able to not close (or not use ctx mgr), but that will cause resource exhaustion.

Typical abuse pattern is:

def solve(problem):
    bqm = create_bqm_from_problem(problem)
    sampler = DWaveSampler()
    return sampler.sample(bqm)

for p in problems:
    t = threading.Thread(worker=solve, args=[p])
    t.start()

now (or after #91 rather) users will be able to do:

def solve(problem):
    bqm = create_bqm_from_problem(problem)
    with DWaveSampler() as sampler:
        return sampler.sample(bqm)

(if using a shared sampler is not an option)

@randomir randomir force-pushed the feature/add-close-to-dwavesampler/issue-77 branch from a4209e8 to ee047a0 Compare January 30, 2025 14:49
@randomir randomir force-pushed the feature/add-close-to-dwavesampler/issue-77 branch from 18c7681 to 43852ce Compare January 30, 2025 17:39
@randomir randomir force-pushed the feature/add-close-to-dwavesampler/issue-77 branch from 740574f to 312b89f Compare January 30, 2025 19:15
Copy link

codecov bot commented Jan 30, 2025

Codecov Report

Attention: Patch coverage is 93.33333% with 1 line in your changes missing coverage. Please review.

Project coverage is 85.14%. Comparing base (1bb84c6) to head (33c470f).
Report is 10 commits behind head on master.

Files with missing lines Patch % Lines
dwave/system/testing.py 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #554      +/-   ##
==========================================
- Coverage   88.75%   85.14%   -3.62%     
==========================================
  Files          21       21              
  Lines        1654     1669      +15     
==========================================
- Hits         1468     1421      -47     
- Misses        186      248      +62     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@somethingLC
Copy link

somethingLC commented Jan 30, 2025 via email

@randomir
Copy link
Member Author

Thanks for the feedback, @JoelPasvolsky. I believe I addressed all your comments.

Copy link
Member

@arcondello arcondello left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@randomir randomir merged commit 46efbe5 into dwavesystems:master Feb 3, 2025
20 checks passed
@randomir randomir deleted the feature/add-close-to-dwavesampler/issue-77 branch February 3, 2025 16:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add close method to DWaveSampler()
4 participants