Skip to content

Commit 8bf636f

Browse files
committed
Add SampleSet.wait_id()
1 parent e965bb7 commit 8bf636f

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

dimod/sampleset.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,9 +1489,28 @@ def resolve(self):
14891489
if hasattr(self, '_future'):
14901490
samples = self._result_hook(self._future)
14911491
self.__init__(samples.record, samples.variables, samples.info, samples.vartype)
1492+
1493+
# cache problem id if we just resolved from a cloud-client's future
1494+
if hasattr(self._future, 'wait_id'):
1495+
self._future_wait_id_result = self._future.wait_id(timeout=0)
1496+
14921497
del self._future
14931498
del self._result_hook
14941499

1500+
def wait_id(self, timeout: Optional[float] = None) -> Optional[str]:
1501+
"""Return a QPU problem ID when the sample set is constructed from a QPU
1502+
computation answer (future) using :meth:`.from_future`.
1503+
1504+
For details, see :meth:`~dwave.cloud.client.computation.Future.wait_id`.
1505+
"""
1506+
# note: after the sample set is resolved and the underlying future deleted,
1507+
# we cache the ``wait_id`` result so that the future is not referenced and
1508+
# it can be gc'ed.
1509+
if hasattr(self, '_future_wait_id_result'):
1510+
return self._future_wait_id_result
1511+
if hasattr(self, '_future') and hasattr(self._future, 'wait_id'):
1512+
return self._future.wait_id(timeout=timeout)
1513+
14951514
def aggregate(self):
14961515
"""Create a new SampleSet with repeated samples aggregated.
14971516
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
features:
3+
- |
4+
Add ``SampleSet.wait_id()`` as a proxy to the underlying QPU result future,
5+
when sample set is constructed from such a future.
6+
See `#1392 <https://github.com/dwavesystems/dimod/issues/1392>`_.

tests/test_sampleset.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,29 @@ def result_to_response(future):
786786

787787
np.testing.assert_equal(matrix, result['samples'])
788788

789+
def test_cloud_client_future(self):
790+
answer = dimod.SampleSet.from_samples(samples_like=[[1]], vartype='SPIN', energy=[1])
791+
pid = 'problem-id'
792+
793+
future = concurrent.futures.Future()
794+
future.wait_id = lambda timeout=None: pid
795+
796+
ss = dimod.SampleSet.from_future(future)
797+
798+
with self.subTest('unresolved sampleset from qpu computation has a functional wait_id'):
799+
self.assertFalse(ss.done())
800+
self.assertTrue(hasattr(ss, 'wait_id'))
801+
self.assertEqual(ss.wait_id(), pid)
802+
803+
future.set_result(answer)
804+
ss.resolve()
805+
806+
with self.subTest('resolved sampleset caches wait_id result'):
807+
self.assertTrue(ss.done())
808+
self.assertTrue(hasattr(ss, 'wait_id'))
809+
self.assertEqual(ss.wait_id(), pid)
810+
self.assertFalse(hasattr(ss, '_future'))
811+
789812

790813
class TestKeepVariables(unittest.TestCase):
791814
def test_empty(self):

0 commit comments

Comments
 (0)