-
Notifications
You must be signed in to change notification settings - Fork 216
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 19 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,35 +853,29 @@ 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. | ||
| """ | ||
| already_retrieved = False | ||
| if skip is None: | ||
| skip = 0 | ||
| if not self._programs or refresh: | ||
| self._programs = {} | ||
| current_page_limit = 20 | ||
| offset = 0 | ||
| while True: | ||
| response = self._api_client.list_programs( | ||
| limit=current_page_limit, skip=offset | ||
| ) | ||
| program_page = response.get("programs", []) | ||
| # count is the total number of programs that would be returned if | ||
| # there was no limit or skip | ||
| count = response.get("count", 0) | ||
| for prog_dict in program_page: | ||
| program = self._to_program(prog_dict) | ||
| self._programs[program.program_id] = program | ||
| if len(self._programs) == count: | ||
| # Stop if there are no more programs returned by the server. | ||
| break | ||
| offset += len(program_page) | ||
| if not self._programs or (refresh and not name): | ||
| self._retrieve_programs() | ||
| already_retrieved = True | ||
| if limit is None: | ||
| limit = len(self._programs) | ||
| if name: | ||
| if refresh and not already_retrieved: | ||
| self._retrieve_programs(name) | ||
| 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: | ||
|
|
@@ -908,6 +909,31 @@ def program(self, program_id: str, refresh: bool = False) -> RuntimeProgram: | |
|
|
||
| return self._programs[program_id] | ||
|
|
||
| def _retrieve_programs(self, name: str = "") -> None: | ||
| """Make an API call to fetch programs. | ||
|
|
||
| Args: | ||
| name: Name of program. | ||
| """ | ||
| self._programs = {} | ||
|
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. This function should not modify self._programs right? If we call this function with name parameter then we will lose all programs already in cache that don’t match the name. This function should return programs and they should be stored in self._programs in the calling function above when called without name and stored in temp variable when called with name. |
||
| current_page_limit = 20 | ||
| offset = 0 | ||
| while True: | ||
| response = self._api_client.list_programs( | ||
| name=name, limit=current_page_limit, skip=offset | ||
| ) | ||
| program_page = response.get("programs", []) | ||
| # count is the total number of programs that would be returned if | ||
| # there was no limit or skip | ||
| count = response.get("count", 0) | ||
| for prog_dict in program_page: | ||
| program = self._to_program(prog_dict) | ||
| self._programs[program.program_id] = program | ||
| if len(self._programs) == count: | ||
| # Stop if there are no more programs returned by the server. | ||
| break | ||
| offset += len(program_page) | ||
|
|
||
| def _to_program(self, response: Dict) -> RuntimeProgram: | ||
| """Convert server response to ``RuntimeProgram`` instances. | ||
|
|
||
|
|
||
| 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.