Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Commit

Permalink
Accept None as Value for Limit Parameter for jobs() (#591)
Browse files Browse the repository at this point in the history
* Accept None as limit value for jobs()

* Added release note

* Remove limit=None from integration test

* Adjusted default limits in jobs()

* Added unit tests for ibm_backend_service

* Added decorator for mocking a function with patch

* Unnecessary variable remove and logic improved

* Fixed typo

* repairs skipping to skip all jobs

* explains setting environmental variables for running integration tests locally

* makes page size not be used as magic numbers

* tests limit in integration test

* removes limit unit tests

* removes unnecessary decorator

* implements simpler suggested fix

* applies linting fixes

* force ci

* force GitHub action

---------

Co-authored-by: merav-aharoni <merav@il.ibm.com>
  • Loading branch information
andre-a-alves and merav-aharoni authored Jul 31, 2023
1 parent 5475289 commit e53a67b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
6 changes: 5 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,15 @@ QISKIT_IBM_URL=https://auth.quantum-computing.ibm.com/api # IBM Quantum AP
QISKIT_IBM_INSTANCE=ibm-q/open/main # IBM Quantum provider to use (hub/group/project)
```

To enable test cases against external system in your private fork, make sure to set above values as
To enable test cases against external system in your private fork's CI workflow, make sure to set above values as
[encrypted environment secrets](https://docs.github.com/en/actions/security-guides/encrypted-secrets#creating-encrypted-secrets-for-an-environment).
The names of the environments must match the ones that the [CI workflow](.github/workflows/ci.yml) relies
upon.

To run integration and E2E tests locally, set environment variables in your terminal.
In Linux or MacOS, this is accomplished using the `export` command.
In Windows, the method will depend on which console you are using.

### Style guide

Please submit clean code and please make effort to follow existing
Expand Down
12 changes: 7 additions & 5 deletions qiskit_ibm_provider/ibm_backend_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
from .utils.hgp import to_instance_format

logger = logging.getLogger(__name__)
PAGE_SIZE = 50


class IBMBackendService:
Expand Down Expand Up @@ -322,7 +323,6 @@ def jobs(
)
)
job_list = []
original_limit = limit
while True:
job_responses = self._get_jobs(
api_filter=api_filter,
Expand Down Expand Up @@ -362,9 +362,9 @@ def jobs(
)
continue
job_list.append(job)
if len(job_list) == original_limit:
if limit and len(job_list) == limit:
return job_list
skip += limit
skip += len(job_responses)
return job_list

def _get_jobs(
Expand All @@ -391,7 +391,9 @@ def _get_jobs(
# Retrieve the requested number of jobs, using pagination. The server
# might limit the number of jobs per request.
job_responses: List[Dict[str, Any]] = []
current_page_limit = limit if (limit is not None and limit <= 50) else 50
current_page_limit = (
limit if (limit is not None and limit <= PAGE_SIZE) else PAGE_SIZE
)
while True:
if legacy:
job_page = self._default_hgp._api_client.list_jobs(
Expand Down Expand Up @@ -420,7 +422,7 @@ def _get_jobs(
break
current_page_limit = limit - len(job_responses)
else:
current_page_limit = 50
current_page_limit = PAGE_SIZE
skip = len(job_responses)
return job_responses

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
Fixes a bug in the function ``jobs()``. Refer to
`#586 <https://github.com/Qiskit/qiskit-ibm-provider/issues/586>`_ for
more details.
39 changes: 38 additions & 1 deletion test/integration/test_ibm_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

from qiskit_ibm_provider.job.ibm_job import IBMJob
from qiskit_ibm_provider.ibm_backend import IBMBackend
from qiskit_ibm_provider.ibm_backend_service import IBMBackendService
from qiskit_ibm_provider.ibm_backend_service import IBMBackendService, PAGE_SIZE
from qiskit_ibm_provider.ibm_provider import IBMProvider
from ..account import temporary_account_config_file
from ..decorators import (
Expand Down Expand Up @@ -200,6 +200,43 @@ def test_get_backend(self):
backend = self.dependencies.provider.get_backend(name=self.backend_name)
self.assertEqual(backend.name, self.backend_name)

def test_jobs_filter(self):
"""Test limit filters when accessing jobs from the provider."""
num_jobs = PAGE_SIZE + 1
small_limit = PAGE_SIZE // 2
large_limit = PAGE_SIZE * 2
backend_name = self.backend_name
backend = self.dependencies.provider.get_backend(name=backend_name)
circuit = QuantumCircuit(1)
circuit.h(0)

start_time = datetime.now()
for _ in range(num_jobs):
backend.run(circuit, job_tags=["ibm-provider-test"], shots=1)
end_time = datetime.now()

recent_jobs_small_limit = self.dependencies.provider.jobs(
backend_name=backend_name,
limit=small_limit,
start_datetime=start_time,
end_datetime=end_time,
)
recent_jobs_large_limit = self.dependencies.provider.jobs(
backend_name=backend_name,
limit=large_limit,
start_datetime=start_time,
end_datetime=end_time,
)
recent_jobs_no_limit = self.dependencies.provider.jobs(
backend_name=backend_name,
limit=None,
start_datetime=start_time,
end_datetime=end_time,
)
self.assertEqual(len(recent_jobs_small_limit), small_limit)
self.assertEqual(len(recent_jobs_large_limit), num_jobs)
self.assertEqual(len(recent_jobs_no_limit), num_jobs)

def test_backend_instance(self):
"""Test that the instance is saved correctly."""
backend = self.dependencies.provider.get_backend(
Expand Down

0 comments on commit e53a67b

Please sign in to comment.