Skip to content

Commit

Permalink
Fix more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rathishcholarajan committed Nov 22, 2021
1 parent 470e643 commit efd80af
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 336 deletions.
50 changes: 3 additions & 47 deletions test/ibm/test_account_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,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 +74,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 +90,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 Down Expand Up @@ -168,16 +127,13 @@ 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

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):
Expand Down
40 changes: 2 additions & 38 deletions test/ibm/test_ibm_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

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


class TestIBMBackend(IBMTestCase):
Expand Down Expand Up @@ -107,42 +107,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 +156,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
21 changes: 2 additions & 19 deletions test/ibm/test_ibm_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,26 +383,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')

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 @@ -440,5 +423,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)
172 changes: 0 additions & 172 deletions test/ibm/test_ibm_qasm_simulator.py

This file was deleted.

Loading

0 comments on commit efd80af

Please sign in to comment.