Skip to content

Commit 4e42767

Browse files
committed
give message as warning when getting backend
1 parent 510893f commit 4e42767

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed

qiskit_quantuminspire/qi_backend.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ def status(self) -> BackendStatus:
129129
backend_type: BackendType = run_async(self._get_backend_type())
130130
return backend_type.status
131131

132+
@property
133+
def message(self) -> str:
134+
backend_type: BackendType = run_async(self._get_backend_type())
135+
messages = ""
136+
for backend_name in backend_type.messages.keys():
137+
messages += f"{backend_name}: {backend_type.messages[backend_name].content}\n"
138+
return messages.rstrip("\n")
139+
132140
async def _get_backend_type(self) -> BackendType:
133141
async with ApiClient(config()) as client:
134142
backend_types_api = BackendTypesApi(client)

qiskit_quantuminspire/qi_provider.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import logging
12
from typing import Any, List, Optional, Sequence
23

4+
from colorama import Fore, Style
35
from compute_api_client import ApiClient, BackendType, BackendTypesApi, PageBackendType
46
from qi2_shared.client import config
57
from qi2_shared.pagination import PageReader
@@ -48,6 +50,9 @@ def get_backend(self, name: Optional[str] = None, id: Optional[int] = None) -> Q
4850

4951
for backend in self._qiskit_backends:
5052
if all(getattr(backend, key) == value for key, value in filter_arguments.items()):
53+
if backend.message:
54+
logging.basicConfig(format="%(levelname)s: %(message)s")
55+
logging.warning(f"{Fore.BLUE}{backend.message}{Style.RESET_ALL}")
5156
return backend
5257

5358
raise ValueError(f"Backend {name} not found")

tests/helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def create_backend_type(
3333
native_gateset="",
3434
supports_raw_data=supports_raw_data,
3535
enabled=True,
36-
messages={},
36+
messages={"backend": {"content": "message for backend"}},
3737
job_execution_time_limit=3600,
3838
identifier="dummy",
3939
)

tests/test_qi_backend.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,19 @@ def test_qi_backend_repr() -> None:
142142
assert qi_backend.name in repr(qi_backend)
143143

144144

145+
def test_qi_backend_construction_messages(mocker: MockerFixture, qi_backend_factory: Callable[..., QIBackend]) -> None:
146+
# Arrange
147+
backend_type = create_backend_type()
148+
mock_run_async = mocker.patch("qiskit_quantuminspire.qi_backend.run_async")
149+
mock_run_async.return_value = backend_type
150+
151+
# Act
152+
qi_backend = QIBackend(backend_type=backend_type)
153+
154+
# Assert
155+
assert qi_backend.message == "backend: message for backend"
156+
157+
145158
@pytest.mark.parametrize("backend_online", [True, False]) # Test cases for backend being available and offline
146159
def test_qi_backend_run_backend_status(
147160
qi_job_mock: MagicMock, qi_backend_factory: Callable[..., QIBackend], backend_online: bool

tests/test_qi_provider.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
from typing import Any
23
from unittest.mock import AsyncMock
34

@@ -13,6 +14,8 @@
1314
def backend_repository(mocker: MockerFixture, mock_job_api: Any, page_reader_mock: AsyncMock) -> None:
1415
mocker.patch("qiskit_quantuminspire.qi_provider.config")
1516
mocker.patch("qiskit_quantuminspire.qi_provider.ApiClient")
17+
mock_run_async = mocker.patch("qiskit_quantuminspire.qi_backend.run_async")
18+
mock_run_async.return_value = create_backend_type()
1619
page_reader_mock.get_all.return_value = [
1720
create_backend_type(name="qi_backend_1", id=10),
1821
create_backend_type(name="spin", id=20),
@@ -61,6 +64,19 @@ def test_get_backend_no_arguments_gets_first(backend_repository: None) -> None:
6164
assert backend.id == 10
6265

6366

67+
def test_get_backend_print_message(backend_repository: None, caplog: Any) -> None:
68+
# Arrange
69+
provider = QIProvider()
70+
71+
# Act
72+
with caplog.at_level(logging.WARNING):
73+
provider.get_backend(name="qi_backend_1")
74+
75+
# Assert
76+
assert len(caplog.records) == 1
77+
assert "backend: message for backend" in caplog.text
78+
79+
6480
@pytest.mark.parametrize("name,id", [("not_existing", None), (None, 6), ("not_existing", 6)])
6581
def test_get_backend_raises_value_error_if_not_found(name: str, id: int, backend_repository: None) -> None:
6682
# Arrange

0 commit comments

Comments
 (0)