8
$\begingroup$

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.

$\endgroup$
1

3 Answers 3

5
$\begingroup$

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()
$\endgroup$
3
  • 2
    $\begingroup$ I found backend.jobs()[0].job_id() even more useful $\endgroup$
    – Max
    May 16, 2020 at 3:47
  • $\begingroup$ Is it possible to retrieve results from backends that have been retired? (so can't create a backend object for them) $\endgroup$
    – RGWinston
    Jun 18, 2021 at 15:37
  • $\begingroup$ provider.backend.jobs(backend_name=XXX) $\endgroup$ Jun 18, 2021 at 15:41
4
$\begingroup$

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.

$\endgroup$
4
  • $\begingroup$ 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. $\endgroup$
    – Henao
    Aug 29, 2019 at 13:38
  • $\begingroup$ @Henao I added a line on how to get counts info. $\endgroup$ Aug 29, 2019 at 16:35
  • $\begingroup$ 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. $\endgroup$
    – Henao
    Sep 1, 2019 at 8:03
  • $\begingroup$ Looks like I forgot to define job_info. See my new snippets $\endgroup$ Sep 2, 2019 at 11:05
2
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ 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 $\endgroup$
    – met927
    Apr 16, 2020 at 22:10
  • $\begingroup$ I used JobID 5e98731a4ff153001919fef9, on backend ibmq_ourense. $\endgroup$
    – Jed Brody
    Apr 18, 2020 at 19:03
  • $\begingroup$ I think the API has changed, what Paul said in the second answer seemed to work for me, hope that helps! $\endgroup$
    – met927
    Apr 21, 2020 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.