-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Client: Runtime service wrapper to track runtime jobs (#1358)
- Loading branch information
Showing
8 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
client/qiskit_serverless/utils/runtime_service_client.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
# This code is a Qiskit project. | ||
# | ||
# (C) Copyright IBM 2024. | ||
# | ||
# This code is licensed under the Apache License, Version 2.0. You may | ||
# obtain a copy of this license in the LICENSE.txt file in the root directory | ||
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
# | ||
# Any modifications or derivative works of this code must retain this | ||
# copyright notice, and modified files need to carry a notice indicating | ||
# that they have been altered from the originals. | ||
|
||
""" | ||
====================================================================== | ||
Json utilities (:mod:`qiskit_serverless.utils.runtime_service_client`) | ||
====================================================================== | ||
.. currentmodule:: qiskit_serverless.utils.runtime_service_client | ||
Qiskit Serverless runtime client wrapper | ||
======================================== | ||
.. autosummary:: | ||
:toctree: ../stubs/ | ||
ServerlessRuntimeService | ||
""" | ||
import os | ||
import logging | ||
from typing import Callable, Dict, Sequence, Type, Union, Optional | ||
|
||
import requests | ||
from qiskit_ibm_runtime import QiskitRuntimeService | ||
from qiskit_ibm_runtime.runtime_job import RuntimeJob | ||
from qiskit_ibm_runtime.runtime_job_v2 import RuntimeJobV2 | ||
from qiskit_ibm_runtime.runtime_options import RuntimeOptions | ||
from qiskit_ibm_runtime.utils.result_decoder import ResultDecoder | ||
|
||
from qiskit_serverless.core.constants import ( | ||
REQUESTS_TIMEOUT, | ||
ENV_JOB_GATEWAY_TOKEN, | ||
ENV_JOB_GATEWAY_HOST, | ||
ENV_JOB_ID_GATEWAY, | ||
ENV_GATEWAY_PROVIDER_VERSION, | ||
GATEWAY_PROVIDER_VERSION_DEFAULT, | ||
) | ||
|
||
|
||
def associate_runtime_job_with_serverless_job( | ||
runtime_job_id: str, session_id: Optional[str] = None | ||
) -> bool: | ||
"""Make a request to gateway to associate runtime job id with serverless job id. | ||
Args: | ||
runtime_job_id (str): job id for runtime primitive | ||
session_id (str): session/batch id | ||
Returns: | ||
bool: if request was ok | ||
""" | ||
version = os.environ.get(ENV_GATEWAY_PROVIDER_VERSION) | ||
if version is None: | ||
version = GATEWAY_PROVIDER_VERSION_DEFAULT | ||
|
||
token = os.environ.get(ENV_JOB_GATEWAY_TOKEN) | ||
if token is None: | ||
logging.warning("Runtime job will not be associated with serverless job.") | ||
return False | ||
|
||
url = ( | ||
f"{os.environ.get(ENV_JOB_GATEWAY_HOST)}/" | ||
f"api/{version}/jobs/{os.environ.get(ENV_JOB_ID_GATEWAY)}/add_runtimejob/" | ||
) | ||
response = requests.post( | ||
url, | ||
json={"runtime_job": runtime_job_id, "runtime_session": session_id}, | ||
headers={"Authorization": f"Bearer {token}"}, | ||
timeout=REQUESTS_TIMEOUT, | ||
) | ||
if not response.ok: | ||
logging.warning("Something went wrong: %s", response.text) | ||
|
||
return response.ok | ||
|
||
|
||
class ServerlessRuntimeService(QiskitRuntimeService): | ||
"""Serverless wrapper for QiskitRuntimeService. | ||
Used for associating runtime jobs with serverless jobs. | ||
Args: | ||
QiskitRuntimeService (QiskitRuntimeService): Qiskit runtime service object. | ||
""" | ||
|
||
def run( | ||
self, | ||
program_id: str, | ||
inputs: Dict, | ||
options: Optional[Union[RuntimeOptions, Dict]] = None, | ||
callback: Optional[Callable] = None, | ||
result_decoder: Optional[ | ||
Union[Type[ResultDecoder], Sequence[Type[ResultDecoder]]] | ||
] = None, | ||
session_id: Optional[str] = None, | ||
start_session: Optional[bool] = False, | ||
) -> Union[RuntimeJob, RuntimeJobV2]: | ||
runtime_job = super().run( | ||
program_id, | ||
inputs, | ||
options, | ||
callback, | ||
result_decoder, | ||
session_id, | ||
start_session, | ||
) | ||
associate_runtime_job_with_serverless_job( | ||
runtime_job.job_id(), runtime_job.session_id | ||
) | ||
return runtime_job |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 5.0.6 on 2024-06-06 14:19 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("api", "0023_provider_program_provider"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="runtimejob", | ||
name="session_id", | ||
field=models.CharField(blank=True, default=None, max_length=100, null=True), | ||
), | ||
] |
18 changes: 18 additions & 0 deletions
18
gateway/api/migrations/0025_rename_session_id_runtimejob_runtime_session.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 5.0.6 on 2024-06-11 18:49 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("api", "0024_runtimejob_session_id"), | ||
] | ||
|
||
operations = [ | ||
migrations.RenameField( | ||
model_name="runtimejob", | ||
old_name="session_id", | ||
new_name="runtime_session", | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Generated by Django 5.0.6 on 2024-06-13 18:48 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("api", "0024_program_description"), | ||
("api", "0025_rename_session_id_runtimejob_runtime_session"), | ||
] | ||
|
||
operations = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters