-
Notifications
You must be signed in to change notification settings - Fork 218
Query programs by name #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 17 commits
07460d7
85be645
7413496
3c282e1
d2cc513
f733ee0
b26f544
17d1f47
610e8a0
ad53714
7be7a77
bf87cae
5a1d7c2
b9933c9
830ddfc
ebacbee
d48e892
0ab06e6
c188a14
8d287d9
861cf00
af309c7
ea04aa8
fca5a98
0bc0bca
6671b7d
851caa2
d33595e
5e827fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -171,6 +171,7 @@ def __init__( | |
| ) | ||
| self._backends: Dict[str, "ibm_backend.IBMBackend"] = {} | ||
| self._api_client = None | ||
| self._programs_first_call = True | ||
|
kt474 marked this conversation as resolved.
Outdated
|
||
| hgps = self._get_hgps() | ||
| for hgp in hgps: | ||
| for name, backend in hgp.backends.items(): | ||
|
|
@@ -811,6 +812,7 @@ def pprint_programs( | |
| self, | ||
| refresh: bool = False, | ||
| detailed: bool = False, | ||
| name: Optional[str] = "", | ||
| limit: int = 20, | ||
| skip: int = 0, | ||
| ) -> None: | ||
|
|
@@ -820,11 +822,12 @@ def pprint_programs( | |
| refresh: If ``True``, re-query the server for the programs. Otherwise | ||
| return the cached value. | ||
| detailed: If ``True`` print all details about available runtime programs. | ||
| name: Only retrieve programs with the exact program name given. | ||
| limit: The number of programs returned at a time. Default and maximum | ||
| value of 20. | ||
| skip: The number of programs to skip. | ||
| """ | ||
| programs = self.programs(refresh, limit, skip) | ||
| programs = self.programs(refresh, name, limit, skip) | ||
| for prog in programs: | ||
| print("=" * 50) | ||
| if detailed: | ||
|
|
@@ -837,7 +840,11 @@ def pprint_programs( | |
| print(f" Description: {prog.description}") | ||
|
|
||
| def programs( | ||
| self, refresh: bool = False, limit: int = 20, skip: int = 0 | ||
| self, | ||
| refresh: bool = False, | ||
| name: Optional[str] = "", | ||
| limit: int = 20, | ||
| skip: int = 0, | ||
| ) -> List[RuntimeProgram]: | ||
| """Return available runtime programs. | ||
|
|
||
|
|
@@ -846,21 +853,24 @@ def programs( | |
| Args: | ||
| refresh: If ``True``, re-query the server for the programs. Otherwise | ||
| return the cached value. | ||
| name: Only retrieve programs with the exact program name given. | ||
| limit: The number of programs returned at a time. ``None`` means no limit. | ||
| skip: The number of programs to skip. | ||
|
|
||
| Returns: | ||
| A list of runtime programs. | ||
| """ | ||
| refreshed = False | ||
| if skip is None: | ||
| skip = 0 | ||
| if not self._programs or refresh: | ||
| self._programs = {} | ||
| current_page_limit = 20 | ||
| offset = 0 | ||
| program_name = "" if self._programs_first_call else name | ||
| while True: | ||
| response = self._api_client.list_programs( | ||
| limit=current_page_limit, skip=offset | ||
| name=program_name, limit=current_page_limit, skip=offset | ||
| ) | ||
| program_page = response.get("programs", []) | ||
| # count is the total number of programs that would be returned if | ||
|
|
@@ -870,11 +880,22 @@ def programs( | |
| program = self._to_program(prog_dict) | ||
| self._programs[program.program_id] = program | ||
| if len(self._programs) == count: | ||
| if not self._programs_first_call: | ||
| refreshed = True | ||
| self._programs_first_call = False | ||
| # Stop if there are no more programs returned by the server. | ||
| break | ||
| offset += len(program_page) | ||
| if limit is None: | ||
| limit = len(self._programs) | ||
| if name: | ||
| if refreshed: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this additional variable? If refresh is passed can’t we just make another call here to API to get programs by name? For second time onwards, if refresh is True and if no name is set then repopulate entire cache, So in the call above where you populate entire cache you don’t have to pass name.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If refresh is passed then we wouldn't need to make another api call here right? Just refresh on the first call and whenever refresh is True and then handle all name querying locally
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could do that but we would end up not using the API at all. If name and refresh are passed it’d be faster to make the API call to just update the cache partially.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we don't pass name in the first call and only in the second - if both name and refresh are passed wouldn't there be two api calls? Don't we have to use an additional variable/logic and pass in name in the first call?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the first time when cache is empty once we populate all programs using first call, set refresh to false, so when it goes to the latter call only local filtering will happen. From second time onwards first call won’t execute but the latter call will execute when name and refresh are set. So at any given time only one API call should be made. |
||
| return list(self._programs.values())[skip : limit + skip] | ||
| matched_programs = [] | ||
| for program in list(self._programs.values()): | ||
| if program.name == name: | ||
| matched_programs.append(program) | ||
| return matched_programs[skip : limit + skip] | ||
| return list(self._programs.values())[skip : limit + skip] | ||
|
|
||
| def program(self, program_id: str, refresh: bool = False) -> RuntimeProgram: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| upgrade: | ||
| - | | ||
| The ``name`` parameter has been added to | ||
| :meth:`qiskit_ibm_runtime.IBMRuntimeService.programs` and | ||
| :meth:`qiskit_ibm_runtime.IBMRuntimeService.pprint_programs` | ||
| which can be used to filter by a specific program name. The | ||
| ``name`` given must be an exact match with an actual program name. |
Uh oh!
There was an error while loading. Please reload this page.