-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Return a single results object instead of always a list - IonQ #7285
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
Open
splch
wants to merge
14
commits into
quantumlib:main
Choose a base branch
from
splch:scalar-or-list-results
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3e9cb5c
return a single results object instead of always a list
splch 382be84
Clarify comment regarding single-circuit job results
splch 80e830f
Clarify return type in Sampler.run_sweep documentation
splch af66606
Clarify handling of single-circuit job results in Service.run method
splch 0625bbc
Refactor type annotations in job and sampler modules for consistency
splch 6364c30
Add assertion to ensure job results are iterable in Service.run method
splch 84b10f1
Refactor import statements for improved organization in service.py
splch ad95fc5
Merge branch 'main' into scalar-or-list-results
splch 809e2b6
Add test to verify Service.run unwraps single result list
splch 899cb05
format service
splch 96797b6
Merge branch 'main' into scalar-or-list-results
splch 7624722
Add test for Service.run_batch to preserve input order of circuits
splch e76ec62
Reorder import statements in service_test.py to follow conventions
splch 5474b73
Merge branch 'main' into scalar-or-list-results
splch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
# limitations under the License. | ||
|
||
import datetime | ||
import json | ||
import os | ||
from unittest import mock | ||
|
||
|
@@ -296,3 +297,77 @@ def test_service_remote_host_default(): | |
def test_service_remote_host_from_env_var_cirq_ionq_precedence(): | ||
service = ionq.Service(api_key='tomyheart') | ||
assert service.remote_host == 'http://example.com' | ||
|
||
|
||
def test_service_run_unwraps_single_result_list(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add a test for the correctness of the order of multiple results being returned? |
||
"""`Service.run` should unwrap `[result]` to `result`.""" | ||
# set up a real Service object (we'll monkey‑patch its create_job) | ||
service = ionq.Service(remote_host="http://example.com", api_key="key") | ||
|
||
# simple 1‑qubit circuit | ||
q = cirq.LineQubit(0) | ||
circuit = cirq.Circuit(cirq.X(q), cirq.measure(q, key="m")) | ||
|
||
# fabricate a QPUResult and wrap it in a list to mimic an erroneous behavior | ||
qpu_result = ionq.QPUResult(counts={1: 1}, num_qubits=1, measurement_dict={"m": [0]}) | ||
mock_job = mock.MagicMock() | ||
mock_job.results.return_value = [qpu_result] # <- list of length‑1 | ||
|
||
# monkey‑patch create_job so Service.run sees our mock_job | ||
with mock.patch.object(service, "create_job", return_value=mock_job): | ||
out = service.run(circuit=circuit, repetitions=1, target="qpu") | ||
|
||
# expected Cirq result after unwrapping and conversion | ||
expected = qpu_result.to_cirq_result(params=cirq.ParamResolver({})) | ||
|
||
assert out == expected | ||
mock_job.results.assert_called_once() | ||
|
||
|
||
@pytest.mark.parametrize("target", ["qpu", "simulator"]) | ||
def test_run_batch_preserves_order(target): | ||
"""``Service.run_batch`` must return results in the same order as the | ||
input ``circuits`` list, regardless of how the IonQ API happens to order | ||
its per‑circuit results. | ||
""" | ||
|
||
# Service with a fully mocked HTTP client. | ||
service = ionq.Service(remote_host="http://example.com", api_key="key") | ||
client = mock.MagicMock() | ||
service._client = client | ||
|
||
# Three trivial 1‑qubit circuits, each measuring under a unique key. | ||
keys = ["a", "b", "c"] | ||
q = cirq.LineQubit(0) | ||
circuits = [cirq.Circuit(cirq.measure(q, key=k)) for k in keys] | ||
|
||
client.create_job.return_value = {"id": "job_id", "status": "ready"} | ||
|
||
client.get_job.return_value = { | ||
"id": "job_id", | ||
"status": "completed", | ||
"target": target, | ||
"qubits": "1", | ||
"metadata": { | ||
"shots": "1", | ||
"measurements": json.dumps([{"measurement0": f"{k}\u001f0"} for k in keys]), | ||
"qubit_numbers": json.dumps([1, 1, 1]), | ||
}, | ||
} | ||
|
||
# Intentionally scramble the order returned by the API: b, a, c. | ||
client.get_results.return_value = { | ||
"res_b": {"0": "1"}, | ||
"res_a": {"0": "1"}, | ||
"res_c": {"0": "1"}, | ||
} | ||
|
||
results = service.run_batch(circuits, repetitions=1, target=target) | ||
|
||
# The order of measurement keys in the results should match the input | ||
# circuit order exactly (a, b, c). | ||
assert [next(iter(r.measurements)) for r in results] == keys | ||
|
||
# Smoke‑test on the mocked client usage. | ||
client.create_job.assert_called_once() | ||
client.get_results.assert_called_once() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.