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 quantum_serverless import ServerlessClient
import os
serverless = ServerlessClient(
token=os.environ.get("GATEWAY_TOKEN", "awesome_token"),
host=os.environ.get("GATEWAY_HOST", "http://localhost:8000"),
)
serverless
[1]:
<ServerlessProvider: gateway-provider>
Run two patterns in parallel.
[2]:
from quantum_serverless import QiskitFunction
function = QiskitFunction(
title="pattern-to-fetch-results", entrypoint="pattern.py", working_dir="./source_files/"
)
serverless.upload(function)
job1 = serverless.run("pattern-to-fetch-results")
job2 = serverless.run("pattern-to-fetch-results")
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 = serverless.get_job_by_id(job_id1)
retrieved_job2 = serverless.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 QuantumServerless
object. The get_jobs
method accepts 2 optional parameters, limit
and offset
, which control the size of returned results.
[8]:
serverless.get_jobs(limit=2, offset=1)
[8]:
[<Job | 07b840ca-d09d-4e12-9a24-b7ce4e5e4c21>,
<Job | 721339ce-1409-4326-8263-e0fa6b0bb6a8>]