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

[BUG] IonQJob.result() for ideal simulator fails bc shots=None #786

Open
ryanhill1 opened this issue Oct 10, 2024 · 0 comments
Open

[BUG] IonQJob.result() for ideal simulator fails bc shots=None #786

ryanhill1 opened this issue Oct 10, 2024 · 0 comments
Labels
bug 🐛 Something isn't working jobs 👷‍♂️ Creating, retrieving, or managing quantum jobs results 📋 Post-processing & handling of results data

Comments

@ryanhill1
Copy link
Member

Jobs run on the IonQ "ideal" simulator provide deterministic results, so for these jobs, the IonQ API returns results as histogram probabilities instead of measurement counts. However, currently, there is no way to pass just raw probability data to GateModelResultData (#785). To patch this, we've added an arbitrary default shots value to IonQ.submit:

def submit(self, run_input: list[dict], shots: int = 100, **kwargs) -> IonQJob:

so that the IonQJob.result() method can construct measurement counts data based on the returned probabilities together with that shots value:

results_url: str = job_data["results_url"]
results_endpoint = results_url.split("v0.3")[-1]
job_data["probabilities"] = self.session.get(results_endpoint).json()
job_data["shots"] = job_data.get("shots", self._cache_metadata.get("shots"))
measurement_counts = self._get_counts(job_data)
data = GateModelResultData(measurement_counts=measurement_counts)

For sequential job submission and results retrieval this works fine:

from qbraid.runtime import IonQJob, IonQProvider

provider = IonQProvider(api_key="...")

device = provider.get_device("simulator")

program = """
OPENQASM 2.0;
qreg q[2];
h q[0];
cx q[0],q[1];
cx q[1],q[0];
"""

job = device.run(program)

# Getting the result directly from the job object created from device.run
# works fine because we carry over arbitrary placeholder value for shots
print(job._cache_metadata.get("shots")) # 100

result = job.result()

However, this arbitrary shots value is not used by the IonQ "ideal" simulator, and therefore is not stored anywhere other than locally within that initial IonQJob instance. So if you then create a new job instance from the job ID, the result() method fails with a ValueError because there is no stored value for "shots."

job = IonQJob(job.id, session=provider.session)

print(job._cache_metadata.get("shots")) # None

result = job.result()
# File ~/.../qBraid/qbraid/runtime/ionq/job.py:86, in IonQJob._get_counts(result)
#      84 probs_dec_str: Optional[dict[str, float]] = result.get("probabilities")
#      85 if shots is None or probs_dec_str is None:
# ---> 86     raise ValueError("Missing shots or probabilities in result data.")
#      87 probs_dec = {int(key): value for key, value in probs_dec_str.items()}
#      88 probs_normal = normalize_counts(probs_dec)

# ValueError: Missing shots or probabilities in result data.

TODO

@ryanhill1 ryanhill1 added bug 🐛 Something isn't working jobs 👷‍♂️ Creating, retrieving, or managing quantum jobs results 📋 Post-processing & handling of results data labels Oct 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something isn't working jobs 👷‍♂️ Creating, retrieving, or managing quantum jobs results 📋 Post-processing & handling of results data
Projects
None yet
Development

No branches or pull requests

1 participant