Retrieving Results from Old Jobs

In this tutorial, we will run two patterns and then retrieve the results of each pattern 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"),
)
client
[1]:
<gateway-client>

Run two patterns in parallel.

[2]:
from qiskit_serverless import QiskitFunction

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

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

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

job1, job2
[2]:
(<Job | 45493849-444d-4fd2-9221-5e30e6061b60>,
 <Job | c124c352-93af-42ae-ab2a-1bc8b192d894>)

Retrieve the job IDs for each of the running patterns.

[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]:
[{'0': 0.4999999999999999, '3': 0.4999999999999999}]

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

[5]:
retrieved_job1 = client.get_job_by_id(job_id1)
retrieved_job2 = client.get_job_by_id(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: [{'0': 0.4999999999999999, '3': 0.4999999999999999}]
Job 2 results: [{'0': 0.4999999999999999, '3': 0.4999999999999999}]

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

[7]:
print(f"Job 1 logs: {retrieved_job1.logs()}")
Job 1 logs: OpenBLAS WARNING - could not determine the L2 cache size on this system, assuming 256k
OpenBLAS WARNING - could not determine the L2 cache size on this system, assuming 256k
Running pattern...
Completed running pattern.

To get a list of all previously executed patterns, 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 | 07b840ca-d09d-4e12-9a24-b7ce4e5e4c21>,
 <Job | 721339ce-1409-4326-8263-e0fa6b0bb6a8>]