Skip to content

Commit

Permalink
Fix broken tests (#23)
Browse files Browse the repository at this point in the history
* Fix some tests

* Fix more tests

* Fix more tests

* Fix more tests

* Fix lint
  • Loading branch information
rathishcholarajan authored Nov 23, 2021
1 parent a679eff commit 42b5391
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 548 deletions.
128 changes: 68 additions & 60 deletions test/ibm/runtime/test_runtime.py

Large diffs are not rendered by default.

119 changes: 3 additions & 116 deletions test/ibm/test_account_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,15 @@
"""Tests for the AccountClient class."""

import re
import traceback
from unittest import mock
from urllib3.connectionpool import HTTPConnectionPool
from urllib3.exceptions import MaxRetryError

from qiskit.circuit import ClassicalRegister, QuantumCircuit, QuantumRegister
from qiskit.compiler import assemble, transpile
from qiskit_ibm_runtime.apiconstants import ApiJobStatus
from qiskit_ibm_runtime.api.clients import AccountClient, AuthClient
from qiskit_ibm_runtime.api.exceptions import ApiError, RequestsApiError
from qiskit_ibm_runtime.utils.utils import RefreshQueue

from ..ibm_test_case import IBMTestCase
from ..decorators import requires_qe_access, requires_provider
from ..contextmanagers import custom_envs, no_envs
from ..http_server import SimpleServer, ServerErrorOnceHandler, ClientErrorHandler
from ..http_server import SimpleServer, ClientErrorHandler


class TestAccountClient(IBMTestCase):
Expand All @@ -44,7 +37,7 @@ def setUpClass(cls, service, hub, group, project):
cls.hub = hub
cls.group = group
cls.project = project
default_hgp = cls.service.backend._default_hgp
default_hgp = cls.service._default_hgp
cls.access_token = default_hgp._api_client.account_api.session._access_token

def setUp(self):
Expand Down Expand Up @@ -74,7 +67,7 @@ def tearDown(self) -> None:
def _get_client(self):
"""Helper for instantiating an AccountClient."""
# pylint: disable=no-value-for-parameter
return AccountClient(self.service.backend._default_hgp.credentials)
return AccountClient(self.service._default_hgp.credentials)

def test_custom_client_app_header(self):
"""Check custom client application header."""
Expand All @@ -90,47 +83,6 @@ def test_custom_client_app_header(self):
self.assertNotIn(custom_header,
client._session.headers['X-Qx-Client-Application'])

def test_access_token_not_in_exception_traceback(self):
"""Check that access token is replaced within chained request exceptions."""
backend_name = 'ibmq_qasm_simulator'
backend = self.service.get_backend(backend_name, hub=self.hub,
group=self.group, project=self.project)
circuit = transpile(self.qc1, backend, seed_transpiler=self.seed)
qobj = assemble(circuit, backend, shots=1)
client = backend._api_client

exception_message = 'The access token in this exception ' \
'message should be replaced: {}'.format(self.access_token)
exception_traceback_str = ''
try:
with mock.patch.object(
HTTPConnectionPool,
'urlopen',
side_effect=MaxRetryError(
HTTPConnectionPool('host'), 'url', reason=exception_message)):
_ = client.job_submit(backend.name(), qobj.to_dict())
except RequestsApiError:
exception_traceback_str = traceback.format_exc()

self.assertTrue(exception_traceback_str)
if self.access_token in exception_traceback_str:
self.fail('Access token not replaced in request exception traceback.')

def test_job_submit_retry(self):
"""Test job submit requests get retried."""
client = self._get_client()

# Send request to local server.
valid_data = {'id': 'fake_id',
'objectStorageInfo': {'uploadUrl': SimpleServer.URL},
'job': {'id': 'fake_id'}}
self.fake_server = SimpleServer(handler_class=ServerErrorOnceHandler)
self.fake_server.set_good_response(valid_data)
self.fake_server.start()
client.account_api.session.base_url = SimpleServer.URL

client.job_submit('ibmq_qasm_simulator', {})

def test_client_error(self):
"""Test client error."""
client = self._get_client()
Expand All @@ -152,71 +104,6 @@ def test_client_error(self):
self.assertIn('Bad client input', str(err_cm.exception))


class TestAccountClientJobs(IBMTestCase):
"""Tests for AccountClient methods related to jobs.
This TestCase submits a Job during class invocation, available at
``cls.job``. Tests should inspect that job according to their needs.
"""

@classmethod
@requires_provider
def setUpClass(cls, service, hub, group, project):
# pylint: disable=arguments-differ
super().setUpClass()
cls.service = service
cls.hub = hub
cls.group = group
cls.project = project
default_hgp = cls.service.backend._default_hgp
cls.access_token = default_hgp._api_client.account_api.session._access_token

backend_name = 'ibmq_qasm_simulator'
backend = cls.service.get_backend(backend_name, hub=cls.hub,
group=cls.group, project=cls.project)
cls.client = backend._api_client
cls.job = cls.client.job_submit(
backend_name, cls._get_qobj(backend).to_dict())
cls.job_id = cls.job['job_id']

@staticmethod
def _get_qobj(backend):
"""Return a Qobj."""
# Create a circuit.
qr = QuantumRegister(2)
cr = ClassicalRegister(2)
qc1 = QuantumCircuit(qr, cr, name='qc1')
seed = 73846087

# Assemble the Qobj.
qobj = assemble(transpile([qc1], backend=backend,
seed_transpiler=seed),
backend=backend, shots=1)

return qobj

def test_job_get(self):
"""Test job_get."""
response = self.client.job_get(self.job_id)
self.assertIn('status', response)

def test_job_final_status_polling(self):
"""Test getting a job's final status via polling."""
status_queue = RefreshQueue(maxsize=1)
response = self.client._job_final_status_polling(self.job_id, status_queue=status_queue)
self.assertEqual(response.pop('status', None), ApiJobStatus.COMPLETED.value)
self.assertNotEqual(status_queue.qsize(), 0)

def test_list_jobs_statuses_skip(self):
"""Test listing job statuses with an offset."""
jobs_raw = self.client.list_jobs_statuses(limit=1, skip=1, extra_filter={
'creationDate': {'lte': self.job['creation_date']}})

# Ensure our job is skipped
for job in jobs_raw:
self.assertNotEqual(job['job_id'], self.job_id)


class TestAuthClient(IBMTestCase):
"""Tests for the AuthClient."""

Expand Down
41 changes: 1 addition & 40 deletions test/ibm/test_ibm_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,13 @@
"""IBMBackend Test."""

from datetime import timedelta, datetime
from unittest import SkipTest
from unittest.mock import patch

from qiskit import QuantumCircuit
from qiskit.providers.models import QasmBackendConfiguration
from qiskit.test.reference_circuits import ReferenceCircuits

from ..ibm_test_case import IBMTestCase
from ..decorators import requires_device, requires_provider
from ..utils import get_pulse_schedule, cancel_job


class TestIBMBackend(IBMTestCase):
Expand Down Expand Up @@ -107,42 +104,6 @@ def test_backend_reservations(self):
"Reservation {} found={}, used start datetime {}, end datetime {}".format(
reserv, found, start_dt, end_dt))

def test_backend_options(self):
"""Test backend options."""
service = self.backend.provider()
backends = service.backends(open_pulse=True, operational=True, hub=self.backend.hub,
group=self.backend.group, project=self.backend.project)
if not backends:
raise SkipTest('Skipping pulse test since no pulse backend found.')

backend = backends[0]
backend.options.shots = 2048
backend.set_options(qubit_lo_freq=[4.9e9, 5.0e9],
meas_lo_freq=[6.5e9, 6.6e9],
meas_level=2)
job = backend.run(get_pulse_schedule(backend), meas_level=1, foo='foo')
backend_options = service.backend.job(job.job_id()).backend_options()
self.assertEqual(backend_options['shots'], 2048)
# Qobj config freq is in GHz.
self.assertAlmostEqual(backend_options['qubit_lo_freq'], [4.9e9, 5.0e9])
self.assertEqual(backend_options['meas_lo_freq'], [6.5e9, 6.6e9])
self.assertEqual(backend_options['meas_level'], 1)
self.assertEqual(backend_options['foo'], 'foo')
cancel_job(job)

def test_sim_backend_options(self):
"""Test simulator backend options."""
service = self.backend.provider()
backend = service.get_backend('ibmq_qasm_simulator', hub=self.backend.hub,
group=self.backend.group, project=self.backend.project)
backend.options.shots = 2048
backend.set_options(memory=True)
job = backend.run(ReferenceCircuits.bell(), shots=1024, foo='foo')
backend_options = service.backend.job(job.job_id()).backend_options()
self.assertEqual(backend_options['shots'], 1024)
self.assertTrue(backend_options['memory'])
self.assertEqual(backend_options['foo'], 'foo')

def test_deprecate_id_instruction(self):
"""Test replacement of 'id' Instructions with 'Delay' instructions."""

Expand Down Expand Up @@ -192,7 +153,7 @@ def setUpClass(cls, service, hub, group, project):

def test_my_reservations(self):
"""Test my_reservations method"""
reservations = self.service.backend.my_reservations()
reservations = self.service.my_reservations()
for reserv in reservations:
for attr in reserv.__dict__:
self.assertIsNotNone(
Expand Down
42 changes: 4 additions & 38 deletions test/ibm/test_ibm_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
from unittest import skipIf, mock
from configparser import ConfigParser
from qiskit import ClassicalRegister, QuantumCircuit, QuantumRegister
from qiskit.test import providers, slow_test
from qiskit.compiler import transpile
from qiskit.test import providers
from qiskit.providers.exceptions import QiskitBackendNotFoundError
from qiskit.providers.models.backendproperties import BackendProperties

Expand All @@ -35,7 +34,7 @@
from qiskit_ibm_runtime.apiconstants import QISKIT_IBM_RUNTIME_API_URL

from ..ibm_test_case import IBMTestCase
from ..decorators import requires_device, requires_qe_access, requires_provider
from ..decorators import requires_qe_access, requires_provider
from ..contextmanagers import custom_qiskitrc, no_envs, CREDENTIAL_ENV_VARS
from ..utils import get_hgp

Expand Down Expand Up @@ -383,42 +382,9 @@ def test_remote_backend_properties(self):
if backend.configuration().simulator:
self.assertEqual(properties, None)

def test_headers_in_result_sims(self):
"""Test that the qobj headers are passed onto the results for sims."""
backend = self.service.get_backend('ibmq_qasm_simulator', hub=self.hub, group=self.group,
project=self.project)

custom_header = {'x': 1, 'y': [1, 2, 3], 'z': {'a': 4}}
circuits = transpile(self.qc1, backend=backend)

# TODO Use circuit metadata for individual header when terra PR-5270 is released.
# qobj.experiments[0].header.some_field = 'extra info'

job = backend.run(circuits, header=custom_header)
result = job.result()
self.assertTrue(custom_header.items() <= job.header().items())
self.assertTrue(custom_header.items() <= result.header.to_dict().items())
# self.assertEqual(result.results[0].header.some_field, 'extra info')

@slow_test
@requires_device
def test_headers_in_result_devices(self, backend):
"""Test that the qobj headers are passed onto the results for devices."""
custom_header = {'x': 1, 'y': [1, 2, 3], 'z': {'a': 4}}

# TODO Use circuit metadata for individual header when terra PR-5270 is released.
# qobj.experiments[0].header.some_field = 'extra info'

job = backend.run(transpile(self.qc1, backend=backend), header=custom_header)
job.wait_for_final_state(wait=300, callback=self.simple_job_callback)
result = job.result()
self.assertTrue(custom_header.items() <= job.header().items())
self.assertTrue(custom_header.items() <= result.header.to_dict().items())
# self.assertEqual(result.results[0].header.some_field, 'extra info')

def test_aliases(self):
"""Test that display names of devices map the regular names."""
aliased_names = self.service.backend._aliased_backend_names()
aliased_names = self.service._aliased_backend_names()

for display_name, backend_name in aliased_names.items():
with self.subTest(display_name=display_name,
Expand Down Expand Up @@ -456,5 +422,5 @@ def test_provider_backends(self):
"""Test provider_backends have correct attributes."""
provider_backends = {back for back in dir(self.service.backend)
if isinstance(getattr(self.service.backend, back), IBMBackend)}
backends = {back.name().lower() for back in self.service.backend._backends.values()}
backends = {back.name().lower() for back in self.service._backends.values()}
self.assertEqual(provider_backends, backends)
Loading

0 comments on commit 42b5391

Please sign in to comment.