Retrieving Results from Old Jobs

In this tutorial, we will run two Qiskit Functions and then retrieve the results of each Qiskit Function using the job IDs and the serverless client.

First, create ServerlessProvider instance.

⚠ This provider is set up with default credentials to a test cluster intended to run on your machine. For information on setting up infrastructure on your local machine, check out the guide on local infrastructure setup.

[1]:
from qiskit_serverless import ServerlessClient
import os

client = ServerlessClient(
    token=os.environ.get("GATEWAY_TOKEN", "awesome_token"),
    host=os.environ.get("GATEWAY_HOST", "http://localhost:8000"),
    # If you are using the kubernetes approach the URL must be http://localhost
)
client
[1]:
<gateway-client>

Run two functions in parallel.

[2]:
from qiskit_serverless import QiskitFunction

function = QiskitFunction(
    title="function-to-fetch-results", entrypoint="function.py", working_dir="./source_files/"
)
client.upload(function)

my_function = client.get("function-to-fetch-results")

job1 = my_function.run()
job2 = my_function.run()

job1, job2
[2]:
(<Job | 4c0b828f-03eb-487d-afa4-6cb4b2d38b18>,
 <Job | 95e6312c-d246-48ac-b86b-70825ab30f26>)

Retrieve the job IDs for each of the running functions.

[3]:
job_id1 = job1.job_id
job_id2 = job2.job_id

Call the blocking comand, Job.result(), to ensure the results are ready in the following cells.

[4]:
job1.result()
job2.result()
[4]:
{'11': 531, '00': 493}

Retrieve the completed jobs through the serverless client, using the job IDs.

[5]:
retrieved_job1 = client.job(job_id1)
retrieved_job2 = client.job(job_id2)

Inspect the results of the retrieved jobs.

[6]:
print(f"Job 1 results: {retrieved_job1.result()}")
print(f"Job 2 results: {retrieved_job2.result()}")
Job 1 results: {'00': 519, '11': 505}
Job 2 results: {'11': 531, '00': 493}

To inspect the logs from a function, access them from the Job instance.

[7]:
print(f"Job 1 logs: {retrieved_job1.logs()}")
Job 1 logs: 2024-12-05 17:33:36,813     INFO job_manager.py:531 -- Runtime env is setting up.
Running function...
Completed running function.

To get a list of all previously executed functions, use the .get_jobs() method of the QiskitServerless object. The get_jobs method accepts 2 optional parameters, limit and offset, which control the size of returned results.

[8]:
client.get_jobs(limit=2, offset=1)
[8]:
[<Job | 4c0b828f-03eb-487d-afa4-6cb4b2d38b18>,
 <Job | 8eedfd5f-a903-49f1-9147-ee2140f67e55>]