8

I have ran an experiment in one of the IBM processors and the work has finished. However, I can not obtain the data as histograms or counts in the qiskit notebook because the experiment was concluded while I was logged off and therefore now I would need to run the full script again. That is, I would need to run the experiment again. My question is if there is some way to access the results in the notebook without this requirement. I know that the histograms are stored in the section "results" but I specifically want to work with them in the notebook. Thank you.

Sanchayan Dutta
  • 17,497
  • 7
  • 48
  • 110
Henao
  • 113
  • 1
  • 4
  • If anyone is still wondering how to get the results of an old job, check out this video: https://www.youtube.com/watch?v=09-Qsge_qqo – Maha Metawei Mar 16 '22 at 05:44

3 Answers3

5

You can get a previous result by grabbing the corresponding job from the backend you sent it to:

from qiskit import *

provider = IBMQ.load_account()
backend = provider.get_backend('ibmq_BLAH')

#if you know the job ID
job = backend.retrieve_job('JOB_ID')

# if not, get the last 10 jobs on backend
jobs = backend.jobs()
met927
  • 3,251
  • 1
  • 9
  • 22
Paul Nation
  • 2,239
  • 8
  • 8
4

First, set up your provider

provider = IBMQ.load_account()

From this you can access a list of all your jobs with

provider.circuits.client.get_status_jobs()

Each job is recorded as a dictionary. One of the keys is 'id', which is the job id. To extract this for the first job on the list, for example, use

job_id = provider.circuits.client.get_status_jobs()[0]['id']

You can then use

job_info = provider.circuits.client.get_job(job_id)

To get a JSON which contains all the information about the job, including the results. The counts data for the first circuit in the job, for example, can be found at

job_info['qObjectResult']['results'][0]['data']['counts']

Though note that the strings will be hex strings rather than bit strings, and so will need conversion.

James Wootton
  • 11,272
  • 1
  • 31
  • 72
  • Hi James, thank you for the answer. I followed your instructions and at the end I get some information about the execution of the job. However, I still don't know how to access the results from the notebook. When I introduce something like job.get_counts("some circuit") it tells me that "job" is not defined, so it seems that anyway I would have to execute the job again because for what I know this is the only way I can define it. – Henao Aug 29 '19 at 13:38
  • @Henao I added a line on how to get counts info. – James Wootton Aug 29 '19 at 16:35
  • Thanks James, I added the line but then it tells me that "job_info" is not defined. I changed it by job_id, since this is how the circuit was previously identified, and then it tells me that "string indices must be integers". I really don't understand the meaning or function of job_info and 'qObjectResult' in the command, although the other part of it is more intuitive. – Henao Sep 01 '19 at 08:03
  • Looks like I forgot to define job_info. See my new snippets – James Wootton Sep 02 '19 at 11:05
2

When I tried getting data after using job = backend.retrieve_job('JOB_ID'), I got "QiskitError: 'Data for experiment "circuit0" could not be found.'"

When I tried using job_info = provider.circuits.client.get_job(job_id), I got "AttributeError: 'AccountProvider' object has no attribute 'circuits'."

What worked for me was job_info={the entire text of the json file downloaded from Results}, and then proceeding as James described above.

met927
  • 3,251
  • 1
  • 9
  • 22
Jed Brody
  • 95
  • 4
  • Hey! What did you used as the JOBID? It sound like maybe this was incorrect. You can get the JobIDs of previously completed jobs by looking in the IBMQ Experience website – met927 Apr 16 '20 at 22:10
  • I used JobID 5e98731a4ff153001919fef9, on backend ibmq_ourense. – Jed Brody Apr 18 '20 at 19:03
  • I think the API has changed, what Paul said in the second answer seemed to work for me, hope that helps! – met927 Apr 21 '20 at 12:05