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

add job status methods. #494

Merged
merged 10 commits into from
Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ The format is based on [Keep a Changelog].

## [UNRELEASED]

### Added

- `IBMQJob` now has three new methods: `done()`, `running()`, and
`cancelled()`. The methods are used to indicate the job status. (\#494)

### Changed

- The Exception hierarchy has been refined with more specialized classes.
Expand Down
35 changes: 35 additions & 0 deletions qiskit/providers/ibmq/job/ibmqjob.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,41 @@ def status(self) -> JobStatus:

return self._status

def done(self) -> bool:
"""Return whether the job has successfully run.

Returns:
True if job status is done, else false.
"""
return self._is_job_status(JobStatus.DONE)

def running(self) -> bool:
"""Return whether the job is actively running.

Returns:
True if job status is running, else false.
"""
return self._is_job_status(JobStatus.RUNNING)

def cancelled(self) -> bool:
"""Return whether the job has been cancelled.

Returns:
True if job status is cancelled, else false.
"""
return self._is_job_status(JobStatus.CANCELLED)

def _is_job_status(self, job_status: JobStatus) -> bool:
"""Return whether the current job status matches the desired one.

Args:
job_status: the job status to check against.

Returns:
True if the current job status matches the desired one, else false.
"""
return self.status() == job_status

def _update_status_position(
self,
status: ApiJobStatus,
Expand Down
33 changes: 33 additions & 0 deletions test/ibmq/test_ibmq_job_states.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,39 @@ def test_unrecognized_status(self):
with self.assertRaises(IBMQJobApiError):
self.wait_for_initialization(job)

def test_done_status(self):
"""Test checking whether the job status is done while progressing job states."""
job = self.run_with_api(QueuedAPI())

self.assertFalse(job.done())
self.wait_for_initialization(job)

self._current_api.progress()
self.assertFalse(job.done())

self._current_api.progress()
self.assertTrue(job.done())

def test_running_status(self):
"""Test checking whether the job status is running while progressing job states."""
job = self.run_with_api(ValidatingAPI())

self.assertFalse(job.running())
self.wait_for_initialization(job)

self._current_api.progress()
self.assertTrue(job.running())

def test_cancelled_status(self):
"""Test checking whether the job status is cancelled while progressing job states."""
job = self.run_with_api(CancellableAPI())

self.assertFalse(job.cancelled())
self.wait_for_initialization(job)

self._current_api.progress()
self.assertTrue(job.cancelled())

def test_validating_job(self):
job = self.run_with_api(ValidatingAPI())

Expand Down