Skip to content
This repository was archived by the owner on Jul 28, 2023. It is now read-only.

Commit af4f413

Browse files
Luis Garciajyu00
Luis Garcia
authored andcommitted
add refresh to ibmqjob.result (#469)
* add refresh to ibmqjob.result * Update changelog. * add param to docstring. * update changelog * remove unused import * Add default value to docstring.
1 parent 15a786d commit af4f413

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ The format is based on [Keep a Changelog].
1414
> - **Security**: in case of vulnerabilities.
1515
1616

17+
## [UNRELEASED]
18+
19+
### Added
20+
21+
- `IBMQJob.result()` now accepts an optional `refresh` parameter. If
22+
`refresh=True` is specified, the function re-queries the api for the
23+
results, rather than returning those cached. (\#469)
24+
1725
## [0.4.3] - 2019-11-21
1826

1927
### Fixed

qiskit/providers/ibmq/job/ibmqjob.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ def result(
186186
self,
187187
timeout: Optional[float] = None,
188188
wait: float = 5,
189-
partial: bool = False
189+
partial: bool = False,
190+
refresh: bool = False
190191
) -> Result:
191192
"""Return the result of the job.
192193
@@ -222,6 +223,8 @@ def result(
222223
timeout: number of seconds to wait for job
223224
wait: time between queries to IBM Q server
224225
partial: if true attempts to return partial results for the job.
226+
refresh: if true, query the API for the result again.
227+
Otherwise return the cached value. Default: False.
225228
226229
Returns:
227230
Result object.
@@ -243,7 +246,7 @@ def result(
243246
raise IBMQJobFailureError('Unable to retrieve job result. Job has failed. '
244247
'Use job.error_message() to get more details.')
245248

246-
return self._retrieve_result()
249+
return self._retrieve_result(refresh=refresh)
247250

248251
def cancel(self) -> bool:
249252
"""Attempt to cancel a job.
@@ -349,7 +352,7 @@ def queue_position(self, refresh: bool = False) -> Optional[int]:
349352
"""Return the position in the server queue.
350353
351354
Args:
352-
refresh (bool): if True, query the API and return the latest value.
355+
refresh: if True, query the API and return the latest value.
353356
Otherwise return the cached value.
354357
355358
Returns:
@@ -483,9 +486,13 @@ def _wait_for_completion(
483486

484487
return self._status in required_status
485488

486-
def _retrieve_result(self) -> Result:
489+
def _retrieve_result(self, refresh: bool = False) -> Result:
487490
"""Retrieve the job result response.
488491
492+
Args:
493+
refresh: if true, query the API for the result again.
494+
Otherwise return the cached value. Default: False.
495+
489496
Returns:
490497
The job result.
491498
@@ -496,7 +503,7 @@ def _retrieve_result(self) -> Result:
496503
"""
497504
# pylint: disable=access-member-before-definition,attribute-defined-outside-init
498505
result_response = None
499-
if not self._result: # type: ignore[has-type]
506+
if not self._result or refresh: # type: ignore[has-type]
500507
try:
501508
result_response = self._api.job_result(self.job_id(), self._use_object_storage)
502509
self._result = Result.from_dict(result_response)

test/ibmq/test_ibmq_job.py

+23
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"""IBMQJob Test."""
1616

1717
import time
18+
import copy
1819
from concurrent import futures
1920
from datetime import datetime, timedelta
2021
from unittest import SkipTest
@@ -484,6 +485,28 @@ def test_retrieve_from_retired_backend(self, provider):
484485
self.assertTrue(isinstance(new_job2.backend(), IBMQRetiredBackend))
485486
self.assertNotEqual(new_job2.backend().name(), 'unknown')
486487

488+
@requires_provider
489+
def test_refresh_job_result(self, provider):
490+
"""Test re-retrieving job result via refresh."""
491+
backend = provider.get_backend('ibmq_qasm_simulator')
492+
qobj = assemble(transpile(self._qc, backend=backend), backend=backend)
493+
job = backend.run(qobj)
494+
result = job.result()
495+
496+
# Save original cached results.
497+
cached_result = copy.deepcopy(result)
498+
self.assertTrue(cached_result)
499+
500+
# Modify cached results.
501+
result.results[0].header.name = 'modified_result'
502+
self.assertNotEqual(cached_result, result)
503+
self.assertEqual(result.results[0].header.name, 'modified_result')
504+
505+
# Re-retrieve result via refresh.
506+
result = job.result(refresh=True)
507+
self.assertEqual(cached_result, result)
508+
self.assertNotEqual(result.results[0].header.name, 'modified_result')
509+
487510

488511
def _bell_circuit():
489512
qr = QuantumRegister(2, 'q')

0 commit comments

Comments
 (0)