コード例 #1
0
def test_retrieve_and_device_offline_exception(monkeypatch):
    qasms = {'qasms': [{'qasm': 'my qasm'}]}
    json_qasm = json.dumps(qasms)
    request_num = [0]

    def mocked_requests_get(*args, **kwargs):
        class MockResponse:
            def __init__(self, json_data, status_code):
                self.json_data = json_data
                self.status_code = status_code

            def json(self):
                return self.json_data

            def raise_for_status(self):
                pass

        # Accessing status of device. Return online.
        status_url = 'Backends/ibmqx4/queue/status'
        if args[0] == urljoin(_api_url, status_url) and request_num[0] < 2:
            return MockResponse({"state": True, "lengthQueue": 10}, 200)
        elif args[0] == urljoin(_api_url, status_url):
            return MockResponse({"state": False}, 200)
        job_url = 'Jobs/{}'.format("123e")
        if args[0] == urljoin(_api_url, job_url):
            request_num[0] += 1
            return MockResponse({"noqasms": "not done"}, 200)

    def mocked_requests_post(*args, **kwargs):
        class MockRequest:
            def __init__(self, url=""):
                self.url = url

        class MockPostResponse:
            def __init__(self, json_data, text=" "):
                self.json_data = json_data
                self.text = text
                self.request = MockRequest()

            def json(self):
                return self.json_data

            def raise_for_status(self):
                pass

        login_url = 'users/login'
        if args[0] == urljoin(_api_url, login_url):
            return MockPostResponse({"userId": "1", "id": "12"})

    monkeypatch.setattr("requests.get", mocked_requests_get)
    monkeypatch.setattr("requests.post", mocked_requests_post)
    _ibm_http_client.time.sleep = lambda x: x
    with pytest.raises(_ibm_http_client.DeviceOfflineError):
        _ibm_http_client.retrieve(device="ibmqx4",
                                  user="******",
                                  password="******",
                                  jobid="123e")
コード例 #2
0
def test_retrieve(monkeypatch):
    request_num = [0]
    execution_id = '3'

    def mocked_requests_get(*args, **kwargs):
        class MockResponse:
            def __init__(self, json_data, status_code):
                self.json_data = json_data
                self.status_code = status_code

            def json(self):
                return self.json_data

            def raise_for_status(self):
                pass

        # Accessing status of device. Return online.
        status_url = 'Network/ibm-q/Groups/open/Projects/main/devices/v/1'
        if args[1] == urljoin(_API_URL, status_url):
            return MockResponse([{
                'backend_name': 'ibmqx4',
                'coupling_map': None,
                'backend_version': '0.1.547',
                'n_qubits': 32
            }], 200)

        #STEP5
        elif (args[1] == urljoin(
                _API_URL,
                "Network/ibm-q/Groups/open/Projects/main/Jobs/{execution_id}".
                format(execution_id=execution_id)) and request_num[0] < 1):
            request_num[0] += 1
            return MockResponse({"status": "RUNNING"}, 200)
        elif (args[1] == urljoin(
                _API_URL,
                "Network/ibm-q/Groups/open/Projects/main/Jobs/{execution_id}".
                format(execution_id=execution_id))):
            return MockResponse({"status": "COMPLETED"}, 200)
        #STEP6
        elif (args[1] == urljoin(
                _API_URL,
                "Network/ibm-q/Groups/open/Projects/main/Jobs/{execution_id}/resultDownloadUrl"
                .format(execution_id=execution_id))):
            return MockResponse({"url": "result_download_url"}, 200)
        #STEP7
        elif (args[1] == "result_download_url"):
            return MockResponse({"results": ['correct']}, 200)

    def mocked_requests_post(*args, **kwargs):
        class MockRequest:
            def __init__(self, url=""):
                self.url = url

        class MockPostResponse:
            def __init__(self, json_data, text=" "):
                self.json_data = json_data
                self.text = text
                self.request = MockRequest()

            def json(self):
                return self.json_data

            def raise_for_status(self):
                pass

        if args[1] == _AUTH_API_URL:
            return MockPostResponse({"userId": "1", "id": "12"})

        #STEP8
        elif (args[1] == urljoin(
                _API_URL,
                "Network/ibm-q/Groups/open/Projects/main/Jobs/{execution_id}/resultDownloaded"
                .format(execution_id=execution_id))):
            return MockPostResponse({}, 200)

    monkeypatch.setattr("requests.sessions.Session.get", mocked_requests_get)
    monkeypatch.setattr("requests.sessions.Session.post", mocked_requests_post)

    _ibm_http_client.time.sleep = lambda x: x
    res = _ibm_http_client.retrieve(device="ibmqx4",
                                    token="test",
                                    jobid=execution_id)
    assert res == 'correct'
コード例 #3
0
def test_retrieve_and_device_offline_exception(monkeypatch):
    request_num = [0]

    def mocked_requests_get(*args, **kwargs):
        class MockResponse:
            def __init__(self, json_data, status_code):
                self.json_data = json_data
                self.status_code = status_code

            def json(self):
                return self.json_data

            def raise_for_status(self):
                pass

        # Accessing status of device. Return online.
        status_url = 'Network/ibm-q/Groups/open/Projects/main/devices/v/1'
        if args[1] == urljoin(_API_URL, status_url) and request_num[0] < 2:
            return MockResponse([{
                'backend_name': 'ibmqx4',
                'coupling_map': None,
                'backend_version': '0.1.547',
                'n_qubits': 32
            }], 200)
        elif args[1] == urljoin(
                _API_URL,
                status_url):  # ibmqx4 gets disconnected, replaced by ibmqx5
            return MockResponse([{
                'backend_name': 'ibmqx5',
                'coupling_map': None,
                'backend_version': '0.1.547',
                'n_qubits': 32
            }], 200)
        job_url = "Network/ibm-q/Groups/open/Projects/main/Jobs/{}".format(
            "123e")
        err_url = "Network/ibm-q/Groups/open/Projects/main/Jobs/{}".format(
            "123ee")
        if args[1] == urljoin(_API_URL, job_url):
            request_num[0] += 1
            return MockResponse(
                {
                    "status": "RUNNING",
                    'iteration': request_num[0]
                }, 200)
        if args[1] == urljoin(_API_URL, err_url):
            request_num[0] += 1
            return MockResponse(
                {
                    "status": "TERMINATED",
                    'iteration': request_num[0]
                }, 400)

    def mocked_requests_post(*args, **kwargs):
        class MockRequest:
            def __init__(self, url=""):
                self.url = url

        class MockPostResponse:
            def __init__(self, json_data, text=" "):
                self.json_data = json_data
                self.text = text
                self.request = MockRequest()

            def json(self):
                return self.json_data

            def raise_for_status(self):
                pass

        if args[1] == _AUTH_API_URL:
            return MockPostResponse({"userId": "1", "id": "12"})

    monkeypatch.setattr("requests.sessions.Session.get", mocked_requests_get)
    monkeypatch.setattr("requests.sessions.Session.post", mocked_requests_post)

    _ibm_http_client.time.sleep = lambda x: x
    with pytest.raises(_ibm_http_client.DeviceOfflineError):
        _ibm_http_client.retrieve(device="ibmqx4",
                                  token="test",
                                  jobid="123e",
                                  num_retries=200)
    with pytest.raises(Exception):
        _ibm_http_client.retrieve(device="ibmqx4",
                                  token="test",
                                  jobid="123ee",
                                  num_retries=200)