Skip to content

Commit

Permalink
Add get_quantum_architecture method to IQMClient. (#51)
Browse files Browse the repository at this point in the history
* Add get_quantum_architecture method to IQMClient.
  • Loading branch information
q-mat-beu authored Oct 10, 2022
1 parent 8996e51 commit d403c18
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
Changelog
=========

Version 8.2
===========

* Add method ``IQMClient.get_quantum_architecture``. `#51 <https://github.com/iqm-finland/iqm-client/pull/51>`_

Version 8.1
===========

Expand Down
36 changes: 36 additions & 0 deletions src/iqm_client/iqm_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,24 @@ def from_dict(inp: dict[str, Union[str, dict]]) -> RunStatus:
input_copy = inp.copy()
return RunStatus(status=Status(input_copy.pop('status')), **input_copy)

class QuantumArchitectureSpecification(BaseModel):
"""Quantum architecture specification."""
name: str = Field(..., description='name of the quantum architecture')
'name of the quantum architecture'
operations: list[str] = Field(..., description='list of operations supported by this quantum architecture')
'list of operations supported by this quantum architecture'
qubits: list[str] = Field(..., description='list of qubits of this quantum architecture')
'list of qubits of this quantum architecture'
qubit_connectivity: list[list[str]] = Field(..., description='qubit connectivity of this quantum architecture')
'qubit connectivity of this quantum architecture'

class QuantumArchitecture(BaseModel):
"""Quantum architecture as returned by Cortex."""
quantum_architecture: QuantumArchitectureSpecification = Field(
...,
description='details about the quantum architecture'
)
'details about the quantum architecture'

class GrantType(str, Enum):
"""
Expand Down Expand Up @@ -651,6 +669,24 @@ def wait_for_results(self, job_id: UUID, timeout_secs: float = DEFAULT_TIMEOUT_S
time.sleep(SECONDS_BETWEEN_CALLS)
raise APITimeoutError(f"The task didn't finish in {timeout_secs} seconds.")

def get_quantum_architecture(self) -> QuantumArchitecture:
"""Retrieve quantum architecture from Cortex.
Returns:
quantum architecture
Raises:
APITimeoutError: time exceeded the set timeout
"""
bearer_token = self._get_bearer_token()
result = requests.get(
join(self._base_url, 'quantum-architecture'),
headers=None if not bearer_token else {'Authorization': bearer_token},
timeout=REQUESTS_TIMEOUT
)
result.raise_for_status()
return QuantumArchitecture(**result.json())

def close_auth_session(self) -> bool:
"""Terminate session with authentication server if there was one created.
Expand Down
8 changes: 8 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ def sample_circuit():
]
}

@pytest.fixture
def sample_quantum_architecture():
return {'quantum_architecture': {
'name': 'hercules',
'qubits': ['QB1', 'QB2'],
'qubit_connectivity': [['QB1', 'QB2']],
'operations': ['phased_rx', 'CZ']
}}

class MockJsonResponse:
def __init__(self, status_code: int, json_data: dict):
Expand Down
10 changes: 9 additions & 1 deletion tests/test_iqm_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
from requests import HTTPError

from iqm_client import (Circuit, ClientConfigurationError, IQMClient,
SingleQubitMapping, Status, serialize_qubit_mapping)
QuantumArchitecture, SingleQubitMapping, Status,
serialize_qubit_mapping)
from tests.conftest import MockJsonResponse, existing_run, missing_run

REQUESTS_TIMEOUT = 60
Expand Down Expand Up @@ -172,6 +173,13 @@ def test_waiting_for_results(mock_server, base_url):
client = IQMClient(base_url)
assert client.wait_for_results(existing_run).status == Status.READY

def test_get_quantum_architecture(sample_quantum_architecture, base_url):
"""Test retrieving the quantum architecture"""
client = IQMClient(base_url)
when(requests).get(f'{base_url}/quantum-architecture', ...).thenReturn(
MockJsonResponse(200, sample_quantum_architecture)
)
assert client.get_quantum_architecture() == QuantumArchitecture(**sample_quantum_architecture)

def test_user_warning_is_emitted_when_warnings_in_response(base_url):
client = IQMClient(base_url)
Expand Down

0 comments on commit d403c18

Please sign in to comment.