Skip to content
This repository was archived by the owner on Jul 28, 2023. It is now read-only.

Add input_allowed backend filter #957

Merged
merged 3 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions qiskit/providers/ibmq/ibmqbackendservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def backends(
filters: Optional[Callable[[List[IBMQBackend]], bool]] = None,
timeout: Optional[float] = None,
min_num_qubits: Optional[int] = None,
input_allowed: Optional[Union[str, List[str]]] = None,
**kwargs: Any
) -> List[IBMQBackend]:
"""Return all backends accessible via this provider, subject to optional filtering.
Expand All @@ -103,6 +104,11 @@ def backends(
timeout: Maximum number of seconds to wait for the discovery of
remote backends.
min_num_qubits: Minimum number of qubits the backend has to have.
input_allowed: Filter by the types of input the backend supports.
Valid input types are ``job`` (circuit job) and ``runtime`` (Qiskit Runtime).
For example, ``inputs_allowed='runtime'`` will return all backends
that support Qiskit Runtime. If a list is given, the backend must
support all types specified in the list.
kwargs: Simple filters that specify a ``True``/``False`` criteria in the
backend configuration, backends status, or provider credentials.
An example to get the operational backends with 5 qubits::
Expand Down Expand Up @@ -131,6 +137,12 @@ def backends(
backends = list(filter(
lambda b: b.configuration().n_qubits >= min_num_qubits, backends))

if input_allowed:
if not isinstance(input_allowed, list):
input_allowed = [input_allowed]
backends = list(filter(
lambda b: set(input_allowed) <= set(b.configuration().input_allowed), backends))

return filter_backends(backends, filters=filters, **kwargs)

def jobs(
Expand Down
14 changes: 14 additions & 0 deletions releasenotes/notes/runtime_backend-fe2dc3d6054eee41.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
features:
- |
A new backend configuration attribute ``input_allowed`` now tells you the
types of input supported by the backend. Valid input types are ``job``, which
means circuit jobs, and ``runtime``, which means Qiskit Runtime.

You can also use ``input_allowed`` in backend filtering. For example::

from qiskit import IBMQ

provider = IBMQ.load_account()
# Get a list of all backends that support runtime.
runtime_backends = provider.backends(input_allowed='runtime')
17 changes: 15 additions & 2 deletions test/ibmq/test_filter_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,24 @@ def test_filter_least_busy_paused(self):
def test_filter_min_num_qubits(self):
"""Test filtering by minimum number of qubits."""
filtered_backends = self.provider.backends(
min_num_qubits=10, simulator=False,
min_num_qubits=5, simulator=False,
filters=lambda b: b.configuration().quantum_volume >= 10)

self.assertTrue(filtered_backends)
for backend in filtered_backends[:5]:
with self.subTest(backend=backend):
self.assertGreaterEqual(backend.configuration().n_qubits, 10)
self.assertGreaterEqual(backend.configuration().n_qubits, 5)
self.assertTrue(backend.configuration().quantum_volume, 10)

def test_filter_input_allowed(self):
"""Test filtering by input allowed"""
subtests = ('job', ['job'], ['job', 'runtime'])

for input_type in subtests:
with self.subTest(input_type=input_type):
filtered = self.provider.backends(input_allowed=input_type)
self.assertTrue(filtered)
if not isinstance(input_type, list):
input_type = [input_type]
for backend in filtered[:5]:
self.assertTrue(set(input_type) <= set(backend.configuration().input_allowed))