Skip to content

Commit

Permalink
Merge 1c251dc into 7af287c
Browse files Browse the repository at this point in the history
  • Loading branch information
QFer authored Apr 12, 2019
2 parents 7af287c + 1c251dc commit f414a4a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
11 changes: 8 additions & 3 deletions src/quantuminspire/projectq/backend_qx.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
Measure, Ph, Rx, Ry, Rz, S, Sdag, Swap, T, Tdag, X,
Y, Z, Command)
from projectq.types import Qubit
from quantuminspire.exceptions import ApiError
from quantuminspire.api import QuantumInspireAPI
from quantuminspire.exceptions import ProjectQBackendError

Expand All @@ -47,7 +48,7 @@ def __init__(self, num_runs: int = 1024, verbose: int = 0, quantum_inspire_api:
Args:
num_runs: Number of runs to collect statistics (default is 1024).
verbose: Verbosity level, defaults to 0, which produces no extra output.
quantum_inspire_api: Connection to QI platform, required parameter.
quantum_inspire_api: Connection to QI platform, optional parameter.
backend_type: Backend to use for execution. When no backend_type is provided, the default backend will be
used.
"""
Expand All @@ -63,8 +64,12 @@ def __init__(self, num_runs: int = 1024, verbose: int = 0, quantum_inspire_api:
self._measured_ids: List[int] = []
self._allocated_qubits: Set[int] = set()
self._max_qubit_id: int = -1
if not quantum_inspire_api:
raise RuntimeError("Api is required")
if quantum_inspire_api is None:
try:
quantum_inspire_api = QuantumInspireAPI()
except ApiError as ex:
raise ApiError('Make sure you have saved your token credentials on disk (see README.md) or '
'provide a QuantumInspireAPI instance as parameter to QIBackend') from ex
self.quantum_inspire_api: QuantumInspireAPI = quantum_inspire_api
self.backend_type: Optional[Union[Dict[str, Any], int, str]] = backend_type

Expand Down
25 changes: 21 additions & 4 deletions src/tests/quantuminspire/projectq/test_backend_qx.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import io
import unittest
import warnings
import json
import os
import coreapi
from collections import OrderedDict
from unittest.mock import MagicMock, patch

Expand All @@ -27,7 +30,7 @@
Ph, Rx, Ry, Rz, S, Sdag, Swap, T, Tdag, Toffoli, X,
Y, Z)

from quantuminspire.exceptions import ProjectQBackendError
from quantuminspire.exceptions import ProjectQBackendError, ApiError
from quantuminspire.projectq.backend_qx import QIBackend


Expand All @@ -51,9 +54,23 @@ def test_init_has_correct_values(self):
self.assertEqual(backend.quantum_inspire_api, api)
self.assertIsNone(backend.backend_type)

def test_init_raises_runtime_error(self):
api = None
self.assertRaises(RuntimeError, QIBackend, quantum_inspire_api=api)
def test_init_without_api_has_correct_values(self):
os.environ.get = MagicMock()
os.environ.get.return_value = 'token'
coreapi.Client.get = MagicMock()
backend = QIBackend()
self.assertIsInstance(backend.qasm, str)
self.assertNotEqual(backend.quantum_inspire_api, None)
self.assertIsNone(backend.backend_type)

def test_init_raises_no_account_api_error(self):
json.load = MagicMock()
json.load.return_value = {'faulty_key': 'faulty_token'}
os.environ.get = MagicMock()
os.environ.get.return_value = None
self.assertRaisesRegex(ApiError, 'Make sure you have saved your token credentials on disk \(see README\.md\) '
'or provide a QuantumInspireAPI instance as parameter to QIBackend',
QIBackend)

def test_cqasm_returns_correct_cqasm_data(self):
api = MockApiClient()
Expand Down

0 comments on commit f414a4a

Please sign in to comment.