From f5aa30431287ff0aa30cb2550f4bc68382e157c2 Mon Sep 17 00:00:00 2001 From: Sebastian Wagner Date: Fri, 26 Apr 2024 17:32:50 +0200 Subject: [PATCH] feat: Add `jobs` function to PlanqkQuantumProvider to retrieve user jobs Implemented a new `jobs` function within the PlanqkQuantumProvider class. This function enables users and orgs. to fetch a list of all their jobs. --- planqk/qiskit/client/client.py | 5 +++++ planqk/qiskit/job.py | 7 +++++++ planqk/qiskit/provider.py | 12 ++++++++++++ 3 files changed, 24 insertions(+) diff --git a/planqk/qiskit/client/client.py b/planqk/qiskit/client/client.py index cc06424..8686f30 100644 --- a/planqk/qiskit/client/client.py +++ b/planqk/qiskit/client/client.py @@ -115,6 +115,11 @@ def get_job(cls, job_id: str, provider: Optional[PROVIDER] = None) -> JobDto: response = cls.perform_request(requests.get, f"{base_url()}/jobs/{job_id}", params=params) return JobDto(**response) + @classmethod + def get_jobs(cls) -> List[JobDto]: + response = cls.perform_request(requests.get, f"{base_url()}/jobs") + return [JobDto(**job_info) for job_info in response] + @classmethod def get_job_result(cls, job_id: str, provider: Optional[PROVIDER] = None) -> Dict[str, Any]: params = {} diff --git a/planqk/qiskit/job.py b/planqk/qiskit/job.py index ff56efd..73cc443 100644 --- a/planqk/qiskit/job.py +++ b/planqk/qiskit/job.py @@ -134,6 +134,13 @@ def to_dict(self) -> dict: """ return {key: value for key, value in vars(self).items() if not key.startswith('_')} + def backend(self) -> Backend: + """Return the backend where this job was executed.""" + if self._backend is None: + from planqk.qiskit import PlanqkBackend + self._backend = PlanqkBackend(self._job_details.backend_id) + return self._backend + def queue_position(self): """ Return the position of the job in the server queue. diff --git a/planqk/qiskit/provider.py b/planqk/qiskit/provider.py index fc0b294..1e69f5d 100644 --- a/planqk/qiskit/provider.py +++ b/planqk/qiskit/provider.py @@ -1,4 +1,5 @@ import json +from typing import List from qiskit.providers import ProviderV1 as Provider, QiskitBackendNotFoundError @@ -124,3 +125,14 @@ def retrieve_job(self, backend: PlanqkBackend, job_id: str): Job: the job from the backend with the given id. """ return PlanqkJob(backend=backend, job_id=job_id) + + def jobs(self) -> List[PlanqkJob]: + """ + Returns all jobs of the user or organization. + + Returns: + List[PlanqkJob]: a list of active jobs. + """ + print("Getting your jobs from PlanQK, this may take a few seconds...") + job_dtos = _PlanqkClient.get_jobs() + return [PlanqkJob(backend=None, job_id=job_dto.id, job_details=job_dto) for job_dto in job_dtos]