qiskit-documentation/docs/guides/monitor-job.mdx

130 lines
7.2 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: Monitor or cancel a job
description: How to monitor or cancel a job submitted to IBM Quantum Platform or IBM Quantum on IBM Cloud
---
# Monitor or cancel a job
Jobs are listed on the Workloads page for your quantum service channel:
* IBM Cloud® channel: From the IBM Cloud console quantum [Instances page](https://cloud.ibm.com/quantum/instances), click the name of your instance, then click the Jobs tab.
* IBM Quantum™ channel: In IBM Quantum Platform, open the [Workloads page](https://quantum.ibm.com/workloads).
## Monitor a job
Use the job instance to check the job status or retrieve the results by calling the appropriate command:
| | |
| ----------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| job.result() | Review job results immediately after the job completes. Job results are available after the job completes. Therefore, job.result() is a blocking call until the job completes. |
| job.job\_id() | Return the ID that uniquely identifies that job. Retrieving the job results at a later time requires the job ID. Therefore, it is recommended that you save the IDs of jobs you might want to retrieve later. |
| job.status() | Check the job status. |
| job = service.job(\<job\_id>) | Retrieve a job you previously submitted. This call requires the job ID. |
<span id="retrieve-later"></span>
## Retrieve job results at a later time
Call `service.job(\<job\_id>)` to retrieve a job you previously submitted. If you dont have the job ID, or if you want to retrieve multiple jobs at once; including jobs from retired QPUs (quantum processing units), call `service.jobs()` with optional filters instead. See [QiskitRuntimeService.jobs](../api/qiskit-ibm-runtime/qiskit_ibm_runtime.QiskitRuntimeService#jobs).
<Admonition type="note" title="Deprecated provider packages">
`service.jobs()` also returns jobs run from the deprecated `qiskit-ibm-provider` package. Jobs submitted by the older (also deprecated) `qiskit-ibmq-provider` package are no longer available.
</Admonition>
### Example
This example returns the 10 most recent runtime jobs that were run on `my_backend`:
```python
from qiskit_ibm_runtime import QiskitRuntimeService
# Initialize the account first.
service = QiskitRuntimeService()
service.jobs(backend_name=my_backend)
```
## Cancel a job
You can cancel a job from the IBM Quantum Platform dashboard either on the Jobs page or the details page for a specific job. Click the menu with three vertical dots (at the end of the row on the Jobs page, or the top of the page on the Job details page), and select Cancel.
In Qiskit, use `job.cancel()` to cancel a job.
<span id="execution-spans"></span>
## View Sampler execution spans
The results of [`SamplerV2`](/api/qiskit-ibm-runtime/qiskit_ibm_runtime.SamplerV2) jobs executed in Qiskit Runtime contain execution timing information in their metadata.
This timing information can be used to place upper and lower timestamp bounds on when particular shots were executed on the QPU.
Shots are grouped into [`ExecutionSpan`](/api/qiskit-ibm-runtime/qiskit_ibm_runtime.execution_span.ExecutionSpan) objects, each of which indicates a start time, a stop time, and a specification of which shots were collected in the span.
An execution span specifies which data was executed during its window by providing an [`ExecutionSpan.mask`](/api/qiskit-ibm-runtime/qiskit_ibm_runtime.execution_span.ExecutionSpan#mask) method. This method, given any [Primitive Unified Block (PUB)](/guides/primitive-input-output) index, returns a boolean mask that is `True` for all shots executed during its window. PUBs are indexed by the order in which they were given to the Sampler run call. If, for example, a PUB has shape `(2, 3)` and was run with four shots, then the mask's shape is `(2, 3, 4)`. See the [execution_span](/api/qiskit-ibm-runtime/execution_span) API page for full details.
Example:
```python
# Get the mask of the 1st PUB for the 0th span.
mask = spans[0].mask(1)
# Decide whether the 0th shot of parameter set (1, 2) occurred in this span.
in_this_span = mask[1, 2, 0]
# Create a new bit array containing only the PUB-1 data collected during this span.
bits = pub_results[1].data.meas
filtered_data = BitArray(bits.array[mask], bits.num_bits)
```
To view execution span information, review the metadata of the result returned by `SamplerV2`, which comes in the form of an `ExecutionSpans` object. This object is a list-like container containing subclass instances of `ExecutionSpan`s such as `SliceSpan`:
```python
pub_results = job.result()
spans = pub_results.metadata["execution"]["execution_spans"]
for span in spans:
print(span)
```
Output:
```text
SliceSpan(<start='2024-09-09 11:48:21', stop='2024-09-09 11:48:22', size=1280>)
SliceSpan(<start='2024-09-09 11:48:21', stop='2024-09-09 11:48:25', size=1280>)
SliceSpan(<start='2024-09-09 11:48:21', stop='2024-09-09 11:48:24', size=1280>)
SliceSpan(<start='2024-09-09 11:48:21', stop='2024-09-09 11:48:27', size=1280>)
SliceSpan(<start='2024-09-09 11:48:21', stop='2024-09-09 11:48:26', size=1280>)
...
```
Execution spans can be filtered to include information pertaining to specific PUBs, selected by their indices:
```python
# take the subset of spans that reference data in PUBs 0 or 2
spans.filter_by_pub([0, 2])
```
View global information about the collection of execution spans:
```python
print("Number of execution spans:", len(spans))
print(" Start of the first span:", spans.start)
print(" End of the last span:", spans.stop)
print(" Total duration (s):", spans.duration)
```
Extract and inspect a particular span:
```python
spans.sort()
print(" Start of first span:", spans[0].start)
print(" End of first span:", spans[0].stop)
print("#shots in first span:", spans[0].size)
```
<Admonition type="note">
It is possible for time windows specified by distinct execution spans to overlap. This is not because a QPU was performing multiple executions at once, but is instead an artifact of certain classical processing that might happen concurrently with quantum execution. The guarantee being made is that the referenced data definitely occurred in the reported execution span, but not necessarily that the limits of the time window are as tight as possible.
</Admonition>
## Next steps
<Admonition type="tip" title="Recommendations">
- Try the [Grover's algorithm](https://learning.quantum.ibm.com/tutorial/grovers-algorithm) tutorial.
- [Migrate from qiskit_ibm_provider](/migration-guides/qiskit-runtime-from-ibm-provider)
</Admonition>