Skip to content

Commit

Permalink
fixed code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
korgan00 committed Oct 11, 2024
1 parent f289a7e commit 8dc49bd
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 42 deletions.
2 changes: 1 addition & 1 deletion client/arguments.serverless
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"circuit": {"__type__": "QuantumCircuit", "__value__": "eJwL9Az29gzhZmRiYYAAxtRCBu40Bg4gEyYEAkwgKSibJTmzKLk0s0TX0MwAJl9dW8gI1sLIWMiAChiRjAABZrgMB5oKdBAckZLunliSWoiimhmH6iBn5wiYamYQwVQINpsdagMTLn2RkTBtYB0MaTDnnVzG37bY9Ic9AyvUWlwODYXbC9frPTeU5VUpowNcgN3Jd+++JYIIgWNBqqWnbRgdgOz/SABmJgCi1zDN"}, "array": {"__type__": "ndarray", "__value__": "eJyb7BfqGxDJyFDGUK2eklqcXKRupaBuk2ahrqOgnpZfVFKUmBefX5SSChJ3S8wpTgWKF2ckFqQC+RpGOgqGmjoKtQpkAy4GMHB1YIACABOFG/M="}}
{"circuit": {"__type__": "QuantumCircuit", "__value__": "eJwL9Az29gzhZmRiYYAAxtRCBu40Bg4gEyYEAkwgKSibOTmzKLk0s0TX0MwEJl9dW8gI1sLIWMiAChiRjABrh8twQGlki5AUsTsbB0e4J5akgk0EEUwgAmwBMwM71GgmTM1gEBQZCdPLDNaWBrPVrEn1gruzuAPcfkbsJjAER6Skw8wAWfMfCcDUAACxLyM+"}, "array": {"__type__": "ndarray", "__value__": "eJyb7BfqGxDJyFDGUK2eklqcXKRupaBuk2ahrqOgnpZfVFKUmBefX5SSChJ3S8wpTgWKF2ckFqQC+RpGOgqGmjoKtQpkAy4GMHB1YIACABOFG/M="}}
6 changes: 3 additions & 3 deletions client/qiskit_serverless/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@

from .client import BaseClient

from .clients.LocalClient import LocalClient
from .clients.RayClient import RayClient
from .clients.ServerlessClient import ServerlessClient, IBMServerlessClient
from .clients.local_client import LocalClient
from .clients.ray_client import RayClient
from .clients.serverless_client import ServerlessClient, IBMServerlessClient

from .job import (
Job,
Expand Down
25 changes: 10 additions & 15 deletions client/qiskit_serverless/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ def get_jobs(self, **kwargs) -> List[Job]:
Returns:
list of jobs.
"""
pass

@abstractmethod
def get_job(self, job_id: str) -> Optional[Job]:
Expand All @@ -107,9 +106,16 @@ def get_job(self, job_id: str) -> Optional[Job]:
Returns:
Job instance
"""
pass

def get_job_by_id(self, job_id: str) -> Optional[Job]:
"""Returns job by job id.
Args:
job_id: job id
Returns:
Job instance
"""
warnings.warn(
"`get_job_by_id` method has been deprecated. "
"And will be removed in future releases. "
Expand Down Expand Up @@ -144,34 +150,28 @@ def run(
Returns:
Job
"""
pass

@abstractmethod
def status(self, job_id: str) -> str:
"""Check status."""
pass

@abstractmethod
def stop(
self, job_id: str, service: Optional[QiskitRuntimeService] = None
) -> Union[str, bool]:
"""Stops job/program."""
pass

@abstractmethod
def result(self, job_id: str) -> Any:
"""Return results."""
pass

@abstractmethod
def logs(self, job_id: str) -> str:
"""Return logs."""
pass

@abstractmethod
def filtered_logs(self, job_id: str, **kwargs) -> str:
"""Return filtered logs."""
pass

#########################
####### Functions #######
Expand All @@ -180,23 +180,21 @@ def filtered_logs(self, job_id: str, **kwargs) -> str:
@abstractmethod
def upload(self, program: QiskitFunction) -> Optional[QiskitFunction]:
"""Uploads program."""
pass

@abstractmethod
def get_functions(self, **kwargs) -> List[QiskitFunction]:
"""Returns list of available programs."""
pass

@abstractmethod
def get_function(
self, title: str, provider: Optional[str] = None
) -> Optional[QiskitFunction]:
"""Returns program based on parameters."""
pass

def get(
self, title: str, provider: Optional[str] = None
) -> Optional[QiskitFunction]:
"""Returns program based on parameters."""
warnings.warn(
"`get` method has been deprecated. "
"And will be removed in future releases. "
Expand All @@ -206,6 +204,7 @@ def get(
return self.get_function(title, provider=provider)

def list(self, **kwargs) -> List[QiskitFunction]:
"""Returns list of available programs."""
warnings.warn(
"`list` method has been deprecated. "
"And will be removed in future releases. "
Expand All @@ -221,7 +220,6 @@ def list(self, **kwargs) -> List[QiskitFunction]:
@abstractmethod
def files(self) -> List[str]:
"""Returns list of available files produced by programs to download."""
pass

@abstractmethod
def file_download(
Expand All @@ -231,17 +229,14 @@ def file_download(
download_location: str = "./",
):
"""Download file."""
pass

@abstractmethod
def file_delete(self, file: str):
"""Deletes file uploaded or produced by the programs,"""
pass

@abstractmethod
def file_upload(self, file: str):
"""Upload file."""
pass

######################
####### Widget #######
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ def run(
arguments: Optional[Dict[str, Any]] = None,
config: Optional[Configuration] = None,
) -> Job:
# pylint: disable=too-many-locals
title = ""
if isinstance(program, QiskitFunction):
title = program.title
else:
Expand Down Expand Up @@ -129,8 +131,12 @@ def run(
result = results.group(1)

job = Job(job_id=str(uuid4()), client=self)
entry = {"status": status, "logs": output, "result": result, "job": job}
self._jobs[job.job_id] = entry
self._jobs[job.job_id] = {
"status": status,
"logs": output,
"result": result,
"job": job,
}
return job

def status(self, job_id: str):
Expand Down Expand Up @@ -189,9 +195,7 @@ def get_functions(self, **kwargs) -> List[QiskitFunction]:
def get_function(
self, title: str, provider: Optional[str] = None
) -> Optional[QiskitFunction]:
functions = {
function.title: function for function in self.client.get_programs()
}
functions = {function.title: function for function in self.get_functions()}
return functions.get(title)

#####################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def __init__(self, host: str):
>>> ray_provider = RayClient("http://localhost:8265")
"""
super().__init__("ray-client", host)
self.jobSubmissionClient = JobSubmissionClient(host)
self.job_submission_client = JobSubmissionClient(host)

####################
####### JOBS #######
Expand All @@ -76,7 +76,8 @@ def get_jobs(self, **kwargs) -> List[Job]:
list of jobs.
"""
return [
Job(job.job_id, client=self) for job in self.jobSubmissionClient.list_jobs()
Job(job.job_id, client=self)
for job in self.job_submission_client.list_jobs()
]

def get_job(self, job_id: str) -> Optional[Job]:
Expand All @@ -89,7 +90,7 @@ def get_job(self, job_id: str) -> Optional[Job]:
Job instance
"""
return Job(
self.jobSubmissionClient.get_job_info(job_id).submission_id, client=self
self.job_submission_client.get_job_info(job_id).submission_id, client=self
)

def run(
Expand All @@ -115,7 +116,7 @@ def run(
**{ENV_JOB_ARGUMENTS: json.dumps(arguments, cls=QiskitObjectsEncoder)},
}

job_id = self.jobSubmissionClient.submit_job(
job_id = self.job_submission_client.submit_job(
entrypoint=entrypoint,
submission_id=f"qs_{uuid4()}",
runtime_env={
Expand All @@ -128,21 +129,21 @@ def run(

def status(self, job_id: str) -> str:
"""Check status."""
return self.jobSubmissionClient.get_job_status(job_id).value
return self.job_submission_client.get_job_status(job_id).value

def stop(
self, job_id: str, service: Optional[QiskitRuntimeService] = None
) -> Union[str, bool]:
"""Stops job/program."""
return self.jobSubmissionClient.stop_job(job_id)
return self.job_submission_client.stop_job(job_id)

def result(self, job_id: str) -> Any:
"""Return results."""
return self.logs(job_id)

def logs(self, job_id: str) -> str:
"""Return logs."""
return self.jobSubmissionClient.get_job_logs(job_id)
return self.job_submission_client.get_job_logs(job_id)

def filtered_logs(self, job_id: str, **kwargs) -> str:
"""Return filtered logs."""
Expand Down
5 changes: 1 addition & 4 deletions client/qiskit_serverless/core/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,13 @@
ENV_JOB_ID_GATEWAY,
ENV_GATEWAY_PROVIDER_VERSION,
GATEWAY_PROVIDER_VERSION_DEFAULT,
MAX_ARTIFACT_FILE_SIZE_MB,
)

from qiskit_serverless.core.function import QiskitFunction
from qiskit_serverless.exception import QiskitServerlessException
from qiskit_serverless.serializers.program_serializers import (
QiskitObjectsEncoder,
QiskitObjectsDecoder,
)
from qiskit_serverless.utils.json import is_jsonable, safe_json_request
from qiskit_serverless.utils.json import is_jsonable

RuntimeEnv = ray.runtime_env.RuntimeEnv

Expand Down
4 changes: 4 additions & 0 deletions client/tests/core/test_job.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Tests job."""

# pylint: disable=too-few-public-methods
import os
from unittest import TestCase
from unittest.mock import MagicMock, Mock, patch
Expand All @@ -18,6 +20,8 @@


class ResponseMock:
"""Utility class to mock request.get response with a json"""

ok = True
text = "{}"

Expand Down
4 changes: 0 additions & 4 deletions client/tests/core/test_pattern.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
"""Tests jobs."""
import os

import ray

from testcontainers.compose import DockerCompose

from qiskit_serverless import RayClient, QiskitFunction
from qiskit_serverless.core.job import Job
from tests.utils import wait_for_ray_ready, wait_for_job_completion

import time

resources_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "../resources"
)
Expand Down
7 changes: 4 additions & 3 deletions client/tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
"""Test utils."""
import time

from qiskit_serverless import RayClient
from qiskit_serverless.core.job import Job
from ray.dashboard.modules.job.sdk import JobSubmissionClient

from qiskit_serverless.core.job import Job


def wait_for_ray_ready(connection_url: str, timeout: int = 60):
"""Utility function that waits for ray to be up."""
client = None
must_finish = time.time() + timeout
while time.time() < must_finish and not client:
try:
client = JobSubmissionClient(connection_url)
except:
except ConnectionError:
time.sleep(1)


Expand Down

0 comments on commit 8dc49bd

Please sign in to comment.