Repository: Qiskit/qiskit

Issue:
Handling old-format jobs (test_get_jobs_filter_job_status)
<!-- ⚠️ If you do not respect this template, your issue will be closed -->
<!-- ⚠️ Make sure to browse the opened and closed issues -->

### Information

We are experiencing some intermitent failures at `test.python.ibmq.test_ibmqjob.TestIBMQJob.test_get_jobs_filter_job_status` (specifically, seems to happen at premium):

https://travis-ci.org/Qiskit/qiskit-terra/jobs/461104008

```
1:     b'Traceback (most recent call last):'
1:     b'  File "/home/travis/build/Qiskit/qiskit-terra/test/python/ibmq/test_ibmqjob.py", line 283, in test_get_jobs_filter_job_status'
1:     b"    self.log.info('match #%d: %s', i, job.result().status)"
1:     b'  File "/home/travis/build/Qiskit/qiskit-terra/qiskit/backends/ibmq/ibmqjob.py", line 210, in result'
1:     b'    return self._result_from_job_response(job_response)'
1:     b'  File "/home/travis/build/Qiskit/qiskit-terra/qiskit/backends/ibmq/ibmqjob.py", line 506, in _result_from_job_response'
1:     b'    _reorder_bits(job_response)'
1:     b'  File "/home/travis/build/Qiskit/qiskit-terra/qiskit/backends/ibmq/ibmqjob.py", line 546, in _reorder_bits'
1:     b"    for circuit_result in job_data['qasms']:"
1:     b"KeyError: 'qasms'"
1:     b''
```

This seems to be because of the co-existence of different formats currently, and sort of accounted for - by 0.7, all jobs created and stored should have the same format. However, we should provide a cleaner way of dealing with pre-0.7 style jobs, revising the functions that retrieve them from the API (and emitting a warning if an old-style job cannot be parsed or similar).


### What is the expected behavior?



### Suggested solutions





Discussion:
lets just only work for the new jobs format
Per conversations with Ali, the root cause of #1372 seems to be a bug introduced by #1404. Despite Jay's comment of considering new jobs only, all the machinery for supporting the old format is in place. Fixing the root cause should be considered.

Resolving patch:
diff --git a/qiskit/backends/ibmq/ibmqbackend.py b/qiskit/backends/ibmq/ibmqbackend.py
--- a/qiskit/backends/ibmq/ibmqbackend.py
+++ b/qiskit/backends/ibmq/ibmqbackend.py
@@ -15,7 +15,7 @@
 from marshmallow import ValidationError
 
 from qiskit import QiskitError
-from qiskit.backends import BaseBackend, JobStatus, JobError
+from qiskit.backends import BaseBackend, JobStatus
 from qiskit.backends.models import BackendStatus, BackendProperties
 
 from .api import ApiError
@@ -162,10 +162,10 @@ def jobs(self, limit=50, skip=0, status=None, db_filter=None):
         for job_info in job_info_list:
             job_class = _job_class_from_job_response(job_info)
             if job_class is IBMQJobPreQobj:
-                warnings.warn('The result of job {} is in an old and no longer supported format, '
-                              'please resend the job with Qiskit 0.7'.format(job_info.get('id')),
+                warnings.warn('The result of job {} is in a no longer supported format. '
+                              'These jobs will stop working after Qiskit 0.7. Save the results '
+                              'or send the job with Qiskit 0.7+'.format(job_info.get('id')),
                               DeprecationWarning)
-                continue
 
             is_device = not bool(self.configuration().simulator)
             job = job_class(self, job_info.get('id'), self._api, is_device,
@@ -185,7 +185,6 @@ def retrieve_job(self, job_id):
 
         Raises:
             IBMQBackendError: if retrieval failed
-            DeprecatedFormatJobError: if the job retrieved has a format pre 0.7
         """
         try:
             job_info = self._api.get_status_job(job_id)
@@ -197,9 +196,10 @@ def retrieve_job(self, job_id):
                                    .format(job_id, str(ex)))
         job_class = _job_class_from_job_response(job_info)
         if job_class is IBMQJobPreQobj:
-            raise DeprecatedFormatJobError('The result of job {} is in an old and no '
-                                           'longer supported format, please resend the '
-                                           'job with Qiskit 0.7'.format(job_id))
+            warnings.warn('The result of job {} is in a no longer supported format. '
+                          'These jobs will stop working after Qiskit 0.7. Save the results '
+                          'or send the job with Qiskit 0.7+'.format(job_id),
+                          DeprecationWarning)
 
         is_device = not bool(self.configuration().simulator)
         job = job_class(self, job_info.get('id'), self._api, is_device,
@@ -226,11 +226,6 @@ class IBMQBackendValueError(IBMQBackendError, ValueError):
     pass
 
 
-class DeprecatedFormatJobError(JobError):
-    """Retrieving responses pre-qobj is no longer supported."""
-    pass
-
-
 def _job_class_from_job_response(job_response):
     is_qobj = job_response.get('kind', None) == 'q-object'
     return IBMQJob if is_qobj else IBMQJobPreQobj
diff --git a/qiskit/backends/ibmq/ibmqjob.py b/qiskit/backends/ibmq/ibmqjob.py
--- a/qiskit/backends/ibmq/ibmqjob.py
+++ b/qiskit/backends/ibmq/ibmqjob.py
@@ -516,7 +516,7 @@ def _reorder_bits(job_data):
         job_data (dict): dict with the bare contents of the API.get_job request.
 
     Raises:
-            JobError: raised if the creg sizes don't add up in result header.
+        JobError: raised if the creg sizes don't add up in result header.
     """
     for circuit_result in job_data['qasms']:
         if 'metadata' in circuit_result:
@@ -526,7 +526,7 @@ def _reorder_bits(job_data):
                            ' bits: bits may be out of order')
             return
         # device_qubit -> device_clbit (how it should have been)
-        measure_dict = {op['qubits'][0]: op['memory'][0]
+        measure_dict = {op['qubits'][0]: op['clbits'][0]
                         for op in circ['operations']
                         if op['name'] == 'measure'}
         counts_dict_new = {}
@@ -543,13 +543,13 @@ def _reorder_bits(job_data):
             reordered_bits.reverse()
 
             # only keep the clbits specified by circuit, not everything on device
-            num_clbits = circ['header']['memory_slots']
+            num_clbits = circ['header']['number_of_clbits']
             compact_key = reordered_bits[-num_clbits:]
             compact_key = "".join([b if b != 'x' else '0'
                                    for b in compact_key])
 
             # insert spaces to signify different classical registers
-            cregs = circ['header']['creg_sizes']
+            cregs = circ['header']['clbit_labels']
             if sum([creg[1] for creg in cregs]) != num_clbits:
                 raise JobError("creg sizes don't add up in result header.")
             creg_begin_pos = []


Test patch:
diff --git a/test/python/ibmq/test_ibmqjob.py b/test/python/ibmq/test_ibmqjob.py
--- a/test/python/ibmq/test_ibmqjob.py
+++ b/test/python/ibmq/test_ibmqjob.py
@@ -23,10 +23,9 @@
 from qiskit import IBMQ
 from qiskit import compile
 from qiskit.backends import JobStatus, JobError
-from qiskit.backends.ibmq.ibmqbackend import IBMQBackendError, DeprecatedFormatJobError
-from qiskit.backends.ibmq.ibmqjob import IBMQJob
 from qiskit.backends.ibmq import least_busy
-
+from qiskit.backends.ibmq.ibmqbackend import IBMQBackendError
+from qiskit.backends.ibmq.ibmqjob import IBMQJob
 from ..common import requires_qe_access, JobTestCase, slow_test
 
 
@@ -259,11 +258,7 @@ def test_retrieve_job(self, qe_token, qe_url):
         qobj = compile(self._qc, backend)
         job = backend.run(qobj)
 
-        try:
-            rjob = backend.retrieve_job(job.job_id())
-        except DeprecatedFormatJobError as err:
-            self.skipTest(err.message)
-
+        rjob = backend.retrieve_job(job.job_id())
         self.assertTrue(job.job_id() == rjob.job_id())
         self.assertTrue(job.result().get_counts() == rjob.result().get_counts())