Skip to content

Commit

Permalink
Merge b2efa87 into de217f6
Browse files Browse the repository at this point in the history
  • Loading branch information
QFer committed Sep 2, 2021
2 parents de217f6 + b2efa87 commit f826d66
Show file tree
Hide file tree
Showing 6 changed files with 412 additions and 418 deletions.
41 changes: 20 additions & 21 deletions src/quantuminspire/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import time
import uuid
from typing import Type, List, Dict, Union, Optional, Any, Tuple
from collections import OrderedDict
from urllib.parse import urljoin
import coreapi
from coreapi.auth import TokenAuthentication
Expand Down Expand Up @@ -183,7 +182,7 @@ def get_default_backend_type(self) -> Dict[str, Any]:
to use this backend.
``number_of_qubits`` int Maximum number of qubits the backend supports.
``description`` str Short description of the backend.
``topology`` OrderedDict Dictionary with property `edges` that contains a list of
``topology`` dict Dictionary with property `edges` that contains a list of
tuples that define qubit connectivity for 2-qubit gates.
``is_allowed`` bool Indicates whether the user is allowed to use this backend.
``status`` str Status of the backend.
Expand All @@ -201,7 +200,7 @@ def get_default_backend_type(self) -> Dict[str, Any]:
=================================== =========== ============================================================
"""
return OrderedDict(self._action(['backendtypes', 'default', 'list']))
return dict(self._action(['backendtypes', 'default', 'list']))

def get_backend_types(self) -> List[Dict[str, Any]]:
""" Gets a list of backend types with properties.
Expand Down Expand Up @@ -230,7 +229,7 @@ def get_backend_type_by_id(self, backend_type_id: int) -> Dict[str, Any]:
backend_type = self._action(['backendtypes', 'read'], params={'id': backend_type_id})
except ErrorMessage as err_msg:
raise ApiError(f'Backend type with id {backend_type_id} does not exist!') from err_msg
return OrderedDict(backend_type)
return dict(backend_type)

def get_backend_type_by_name(self, backend_name: str) -> Dict[str, Any]:
""" Gets the properties of a backend type, given the backend name (case insensitive).
Expand All @@ -248,7 +247,7 @@ def get_backend_type_by_name(self, backend_name: str) -> Dict[str, Any]:
if backend['name'].lower() == backend_name.lower()), None)
if backend_type is None:
raise ApiError(f'Backend type with name {backend_name} does not exist!')
return OrderedDict(backend_type)
return dict(backend_type)

def get_backend_type(self, identifier: Optional[Union[int, str]] = None) -> Dict[str, Any]:
"""Gets the properties of the backend type indicated by `identifier`.
Expand Down Expand Up @@ -318,7 +317,7 @@ def get_project(self, project_id: int) -> Dict[str, Any]:
project = self._action(['projects', 'read'], params={'id': project_id})
except ErrorMessage as err_msg:
raise ApiError(f'Project with id {project_id} does not exist!') from err_msg
return OrderedDict(project)
return dict(project)

def get_projects(self) -> List[Dict[str, Any]]:
""" Gets all the projects registered to the user the API is currently authenticated for.
Expand Down Expand Up @@ -347,7 +346,7 @@ def create_project(self, name: str, default_number_of_shots: int, backend_type:
'default_number_of_shots': default_number_of_shots,
'backend_type': backend_type['url'],
}
return OrderedDict((self._action(['projects', 'create'], params=payload)))
return dict((self._action(['projects', 'create'], params=payload)))

def delete_project(self, project_id: int) -> None:
""" Delete a project.
Expand Down Expand Up @@ -416,7 +415,7 @@ def get_job(self, job_id: int) -> Dict[str, Any]:
job = self._action(['jobs', 'read'], params={'id': job_id})
except ErrorMessage as err_msg:
raise ApiError(f'Job with id {job_id} does not exist!') from err_msg
return OrderedDict(job)
return dict(job)

def get_jobs(self) -> List[Dict[str, Any]]:
""" Gets all the jobs registered to projects for the user the API is currently authenticated for.
Expand Down Expand Up @@ -478,7 +477,7 @@ def delete_job(self, job_id: int) -> Dict[str, Any]:
:raises ApiError: If the job identified by `job_id` does not exist.
"""
try:
return OrderedDict(self._action(['jobs', 'delete'], params={'id': job_id}))
return dict(self._action(['jobs', 'delete'], params={'id': job_id}))
except ErrorMessage as err_msg:
raise ApiError(f'Job with id {job_id} does not exist!') from err_msg

Expand Down Expand Up @@ -515,7 +514,7 @@ def _create_job(self, name: str, asset: Dict[str, Any], number_of_shots: int,
logger.warning("Your experiment can not be optimized and may take longer to execute, "
"see https://www.quantum-inspire.com/kbase/optimization-of-simulations/ for details.")
try:
return OrderedDict(self._action(['jobs', 'create'], params=payload))
return dict(self._action(['jobs', 'create'], params=payload))
except (CoreAPIException, TypeError, ValueError) as err_msg:
raise ApiError(f'Job with name {name} not created: {err_msg}') from err_msg

Expand Down Expand Up @@ -554,10 +553,10 @@ def get_result(self, result_id: int) -> Dict[str, Any]:
``raw_text`` str Text string filled when an error occurred, else empty.
``raw_data_url`` str Url to get the raw data of the result. The raw data exists of a
list of integer values depicting the state for each shot.
``histogram`` OrderedDict The histogram as a list of tuples with state (str) and
``histogram`` dict The histogram as a list of tuples with state (str) and
its probability (float).
``histogram_url`` str Url to get the histogram with probabilities. This results in the
OrderedDict as found in property ``histogram`` of result.
dict as found in property ``histogram`` of result.
``measurement_mask`` int (deprecated, unused) The measurement mask.
``quantum_states_url`` str Url to get a list of quantum states.
``measurement_register_url`` str Url to get a list of measurement register.
Expand All @@ -568,7 +567,7 @@ def get_result(self, result_id: int) -> Dict[str, Any]:
result = self._action(['results', 'read'], params={'id': result_id})
except ErrorMessage as err_msg:
raise ApiError(f'Result with id {result_id} does not exist!') from err_msg
return OrderedDict(result)
return dict(result)

def get_results(self) -> List[Dict[str, Any]]:
""" Gets all the results registered for the user the API is currently authenticated for.
Expand All @@ -595,7 +594,7 @@ def get_result_from_job(self, job_id: int) -> Dict[str, Any]:
result = self._action(['jobs', 'result', 'list'], params={'id': job_id})
except ErrorMessage as err_msg:
raise ApiError(f'Job with id {job_id} does not exist!') from err_msg
return OrderedDict(result)
return dict(result)

def get_raw_data_from_result(self, result_id: int) -> List[int]:
""" Gets the raw data from the result.
Expand Down Expand Up @@ -743,7 +742,7 @@ def get_asset(self, asset_id: int) -> Dict[str, Any]:
asset = self._action(['assets', 'read'], params={'id': asset_id})
except ErrorMessage as err_msg:
raise ApiError(f'Asset with id {asset_id} does not exist!') from err_msg
return OrderedDict(asset)
return dict(asset)

def get_assets(self) -> List[Dict[str, Any]]:
""" Gets all the assets registered for the user the API is currently authenticated for.
Expand Down Expand Up @@ -793,7 +792,7 @@ def get_asset_from_job(self, job_id: int) -> Dict[str, Any]:
asset = self._action(['assets', 'read'], params={'id': asset_id})
except ErrorMessage as err_msg:
raise ApiError(f'Asset with id {asset_id} does not exist!') from err_msg
return OrderedDict(asset)
return dict(asset)

def _create_asset(self, name: str, project: Dict[str, Any], content: str) -> Dict[str, Any]:
""" Create an asset.
Expand All @@ -816,7 +815,7 @@ def _create_asset(self, name: str, project: Dict[str, Any], content: str) -> Dic
'project': project['url'],
'content': content,
}
return OrderedDict(self._action(['assets', 'create'], params=payload))
return dict(self._action(['assets', 'create'], params=payload))

# other #

Expand Down Expand Up @@ -907,11 +906,11 @@ def execute_qasm(self, qasm: str, backend_type: Optional[Union[Dict[str, Any], i
user_data = user_data)

has_results, message = self._wait_for_completed_job(quantum_inspire_job, collect_tries)
return OrderedDict(quantum_inspire_job.retrieve_results()) if has_results else \
OrderedDict(self._generate_error_result(message))
return dict(quantum_inspire_job.retrieve_results()) if has_results else \
dict(self._generate_error_result(message))
except (CoreAPIException, TypeError, ValueError, ApiError) as err_msg:
message = f'Error raised while executing qasm: {err_msg}'
return OrderedDict(self._generate_error_result(message))
return dict(self._generate_error_result(message))
finally:
if delete_project_afterwards and quantum_inspire_job is not None:
project_identifier = quantum_inspire_job.get_project_identifier()
Expand Down Expand Up @@ -988,7 +987,7 @@ def execute_qasm_async(self, qasm: str, backend_type: Optional[Union[Dict[str, A
An encapsulated job object containing methods the get the status of the job and
retrieve the execution results.
"""
if not isinstance(backend_type, OrderedDict):
if not isinstance(backend_type, dict):
if backend_type is None:
backend_type = self.get_backend_type(None)
elif isinstance(backend_type, int):
Expand Down
5 changes: 2 additions & 3 deletions src/quantuminspire/qiskit/backend_qx.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import io
import json
import uuid
from collections import defaultdict, OrderedDict, Counter
from collections import defaultdict, Counter
from typing import Dict, List, Tuple, Optional, Any

import numpy as np
Expand Down Expand Up @@ -479,5 +479,4 @@ def __convert_result_data(self, result: Dict[str, Any], measurements: Dict[str,
break

sorted_histogram_data: List[Tuple[str, int]] = sorted(histogram_data.items(), key=lambda kv: int(kv[0], 16))
histogram_obj = OrderedDict(sorted_histogram_data)
return dict(histogram_obj), memory_data
return dict(sorted_histogram_data), memory_data
79 changes: 39 additions & 40 deletions src/tests/quantuminspire/projectq/test_backend_qx.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import warnings
import json
import coreapi
from collections import OrderedDict
from unittest.mock import MagicMock, patch

from projectq.meta import LogicalQubitIDTag
Expand All @@ -38,24 +37,24 @@ class MockApiClient:
def __init__(self):
result = {'histogram': {'00': 0.49, '11': 0.51}, 'results': 'dummy'}
self.execute_qasm = MagicMock(return_value=result)
self.get_backend_type = MagicMock(return_value=OrderedDict({'is_hardware_backend': False,
'is_allowed': True,
'allowed_operations': {
'display': ['display', 'display_binary'],
'measure': ['measure_x', 'measure_y',
'measure_z', 'measure'],
'measure_all': ['measure_all'],
'parameterized_single_gates': ['rx', 'ry',
'rz'],
'prep': ['prep_x', 'prep_y', 'prep_z', 'prep'],
'single_gates': ['mx90', 'my90', 'x90', 'y90',
't', 'tdag', 's', 'sdag',
'x', 'y', 'z', 'h', 'i'],
'dual_gates': ['cz', 'cnot', 'swap', 'cr'],
'triple_gates': ['toffoli']
},
'max_number_of_shots': 4096,
'number_of_qubits': 26}))
self.get_backend_type = MagicMock(return_value=dict({'is_hardware_backend': False,
'is_allowed': True,
'allowed_operations': {
'display': ['display', 'display_binary'],
'measure': ['measure_x', 'measure_y',
'measure_z', 'measure'],
'measure_all': ['measure_all'],
'parameterized_single_gates': ['rx', 'ry',
'rz'],
'prep': ['prep_x', 'prep_y', 'prep_z', 'prep'],
'single_gates': ['mx90', 'my90', 'x90', 'y90',
't', 'tdag', 's', 'sdag',
'x', 'y', 'z', 'h', 'i'],
'dual_gates': ['cz', 'cnot', 'swap', 'cr'],
'triple_gates': ['toffoli']
},
'max_number_of_shots': 4096,
'number_of_qubits': 26}))


class QIBackendNonProtected(QIBackend):
Expand Down Expand Up @@ -153,27 +152,27 @@ def logical_to_physical(self, qb_id):
class TestProjectQBackend(unittest.TestCase):

def setUp(self):
self.hardware_backend_type = OrderedDict({'is_hardware_backend': True,
'is_allowed': True,
'allowed_operations': {
'measure': ['measure_z', 'measure'],
'measure_all': ['measure_all'],
'parameterized_single_gates': ['rx', 'ry', 'rz'],
'single_gates': ['x', 'y', 'z', 'h', 'i'],
'dual_gates': ['cz', 'cnot', 'swap']
},
'max_number_of_shots': 4096,
'max_number_of_simultaneous_jobs': 1,
'topology': {'edges': []},
'number_of_qubits': 3})

self.simulator_backend_type = OrderedDict({'is_hardware_backend': False,
'is_allowed': True,
'allowed_operations': {},
'max_number_of_shots': 4096,
'max_number_of_simultaneous_jobs': 3,
'topology': {'edges': []},
'number_of_qubits': 5})
self.hardware_backend_type = dict({'is_hardware_backend': True,
'is_allowed': True,
'allowed_operations': {
'measure': ['measure_z', 'measure'],
'measure_all': ['measure_all'],
'parameterized_single_gates': ['rx', 'ry', 'rz'],
'single_gates': ['x', 'y', 'z', 'h', 'i'],
'dual_gates': ['cz', 'cnot', 'swap']
},
'max_number_of_shots': 4096,
'max_number_of_simultaneous_jobs': 1,
'topology': {'edges': []},
'number_of_qubits': 3})

self.simulator_backend_type = dict({'is_hardware_backend': False,
'is_allowed': True,
'allowed_operations': {},
'max_number_of_shots': 4096,
'max_number_of_simultaneous_jobs': 3,
'topology': {'edges': []},
'number_of_qubits': 5})

warnings.filterwarnings("ignore", category=PendingDeprecationWarning)

Expand Down
Loading

0 comments on commit f826d66

Please sign in to comment.