Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ProcessTomography analysis gives UnboundLocalError: local variable 'num_cond' referenced before assignment #1182

Closed
kevinsung opened this issue May 19, 2023 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@kevinsung
Copy link
Contributor

kevinsung commented May 19, 2023

Informations

  • Qiskit Experiments version: 0ce3f85
  • Python version: 3.10.11
  • Operating system: Arch Linux

What is the current behavior?

Steps to reproduce the problem

  1. Run a ProcessTomography experiment and save the data to the database service
from qiskit import QuantumCircuit, QuantumRegister
from qiskit_experiments.library import ProcessTomography
from qiskit_ibm_provider import IBMProvider

hub = 'ibm-q-internal'
group = 'deployed'
project ='default'

provider = IBMProvider(instance=f"{hub}/{group}/{project}")
backend = provider.get_backend("ibm_sherbrooke")

register = QuantumRegister(2, name="q")
target_circuit = QuantumCircuit(register)
a, b = register
target_circuit.h(a)
target_circuit.cnot(a, b)

experiment = ProcessTomography(target_circuit)
device_data = experiment.run(backend, shots=10000)
device_data.save()
  1. Load the data from the service and attempt to produce analysis results
from qiskit_experiments.framework.experiment_data import ExperimentData

service = ExperimentData.get_service_from_backend(backend)
data = ExperimentData.load("4f939335-6fe0-4711-b47e-e7c6a14afb0c", service)
experiment = ProcessTomography(target_circuit)
data = experiment.analysis.run(data)
data.block_for_results()
  1. Observe error
Analysis callback failed [Experiment ID: 1ccd621b-cdb8-4c16-b575-db350cbf99d2][Analysis Callback ID: d313440656894aecb8c2338ac6f23ecd]:
Traceback (most recent call last):
  File "/home/kjs/projects/qiskit-experiments/qiskit_experiments/framework/experiment_data.py", line 992, in _run_analysis_callback
    callback(self, **kwargs)
  File "/home/kjs/projects/qiskit-experiments/qiskit_experiments/framework/base_analysis.py", line 169, in run_analysis
    results, figures = analysis._run_analysis(expdata)
  File "/home/kjs/projects/qiskit-experiments/qiskit_experiments/library/tomography/tomography_analysis.py", line 192, in _run_analysis
    outcome_data, shot_data, meas_data, prep_data = tomography_fitter_data(
  File "/home/kjs/projects/qiskit-experiments/qiskit_experiments/library/tomography/fitters/fitter_data.py", line 122, in tomography_fitter_data
    if num_cond:
UnboundLocalError: local variable 'num_cond' referenced before assignment

What is the expected behavior?

Should work.

Suggested solutions

Weird thing I noticed is that if I add the following line, which should be unnecessary, then there is no error, but also no analysis results are produced (data.analysis_results() is an empty list)

data.add_jobs([provider.retrieve_job(job_id) for job_id in data.job_ids])
@kevinsung kevinsung added the bug Something isn't working label May 19, 2023
@kevinsung
Copy link
Contributor Author

Ok specifying the provider fixes the issue. The error message should be made more helpful though.

from qiskit_experiments.framework.experiment_data import ExperimentData

service = ExperimentData.get_service_from_backend(backend)
data = ExperimentData.load("4f939335-6fe0-4711-b47e-e7c6a14afb0c", service, provider=provider)
experiment = ProcessTomography(target_circuit)
data = experiment.analysis.run(data)
data.block_for_results()

@ItamarGoldman ItamarGoldman self-assigned this Oct 30, 2023
@ItamarGoldman
Copy link
Contributor

ItamarGoldman commented Oct 30, 2023

the data is retrieved using _retrieve_data() in ExperimentData class. If provider is not passed when loading the data, the method _retrieve_data() return nothing.
I will add a meaningful message so the user will know.

Edit:
From what I can see there is no check to see if the ExperimentData object contain any data before running an analysis. I think we should check if there is data before calling the analysis.

github-merge-queue bot pushed a commit that referenced this issue Nov 13, 2023
…bject (#1313)

### Summary

Added a warning for the user if the `ExperimentData` data is empty
before running an analysis and if the data couldn't be retrieved.

### Details and comments

Some details that should be in this section include:

- The user will get an un-inductive error regarding the analysis while
the reason it was raised is because the analysis doesn't have data to
analyze from the experiment. We can see an example in #1182 .
- A solution that was suggested was to raise an error that says the user
is trying to run analysis on empty `ExperimentData` object but the team
didn't want to constraint the user to run analysis only if the
experiment has data so we decided that raising a warning is enough.

Changes:
- [x] Added warning before analysis if `ExperimentData.data()` is empty.

### PR checklist (delete when all criteria are met)

- [x] I have read the contributing guide `CONTRIBUTING.md`.
- [x] I have added a release note file using `reno` if this change needs
to be documented in the release notes.

---------

Co-authored-by: Yael Ben-Haim <yaelbh@il.ibm.com>
nkanazawa1989 pushed a commit to nkanazawa1989/qiskit-experiments that referenced this issue Jan 17, 2024
…bject (qiskit-community#1313)

### Summary

Added a warning for the user if the `ExperimentData` data is empty
before running an analysis and if the data couldn't be retrieved.

### Details and comments

Some details that should be in this section include:

- The user will get an un-inductive error regarding the analysis while
the reason it was raised is because the analysis doesn't have data to
analyze from the experiment. We can see an example in qiskit-community#1182 .
- A solution that was suggested was to raise an error that says the user
is trying to run analysis on empty `ExperimentData` object but the team
didn't want to constraint the user to run analysis only if the
experiment has data so we decided that raising a warning is enough.

Changes:
- [x] Added warning before analysis if `ExperimentData.data()` is empty.

### PR checklist (delete when all criteria are met)

- [x] I have read the contributing guide `CONTRIBUTING.md`.
- [x] I have added a release note file using `reno` if this change needs
to be documented in the release notes.

---------

Co-authored-by: Yael Ben-Haim <yaelbh@il.ibm.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants