Skip to content

Commit

Permalink
Add job usage method (#1827)
Browse files Browse the repository at this point in the history
* Add job usage method

* add session/batch usage method

* Add release note

---------

Co-authored-by: ptristan3 <[email protected]>
  • Loading branch information
kt474 and ptristan3 authored Sep 16, 2024
1 parent f3b6abe commit 3eb9661
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
14 changes: 13 additions & 1 deletion qiskit_ibm_runtime/base_runtime_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,23 @@ def cancel_result_streaming(self) -> None:
return
self._ws_client.disconnect(WebsocketClientCloseCode.CANCEL)

def usage(self) -> float:
"""Return job usage in seconds."""
try:
metrics = self._api_client.job_metadata(self.job_id())
return metrics.get("usage", {}).get("quantum_seconds")
except RequestsApiError as err:
raise IBMRuntimeError(f"Failed to get job metadata: {err}") from None

def metrics(self) -> Dict[str, Any]:
"""Return job metrics.
Returns:
Job metrics, which includes timestamp information.
A dictionary with job metrics including but not limited to the following:
* ``timestamps``: Timestamps of when the job was created, started running, and finished.
* ``usage``: Details regarding job usage, the measurement of the amount of
time the QPU is locked for your workload.
Raises:
IBMRuntimeError: If a network error occurred.
Expand Down
14 changes: 14 additions & 0 deletions qiskit_ibm_runtime/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,20 @@ def status(self) -> Optional[str]:

return None

def usage(self) -> Optional[float]:
"""Return session usage in seconds.
Session usage is the time from when the first job starts until the session goes inactive,
is closed, or when its last job completes, whichever happens last.
Batch usage is the amount of time all jobs spend on the QPU.
"""
if self._session_id and isinstance(self._service, QiskitRuntimeService):
response = self._service._api_client.session_details(self._session_id)
if response:
return response.get("elapsed_time")
return None

def details(self) -> Optional[Dict[str, Any]]:
"""Return session details.
Expand Down
3 changes: 3 additions & 0 deletions release-notes/unreleased/1827.feat.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Added new methods ``Session.usage()``, ``Batch.usage()``, and ``Job.usage()`` that
all return information regarding job and session usage.
Please find more information here https://docs.quantum.ibm.com/guides/execution-modes#sessions-versus-batch-usage.

0 comments on commit 3eb9661

Please sign in to comment.