Skip to content

Commit 05418b1

Browse files
authored
Allow server-side warnings from IonQ (#5222)
This will be used to notify users about deprecations and changes to the API without needing to update Cirq independently.
1 parent 94e4ee6 commit 05418b1

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

cirq-ionq/cirq_ionq/job.py

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"""Represents a job created via the IonQ API."""
1515

1616
import time
17+
import warnings
1718
from typing import Dict, Sequence, Union, TYPE_CHECKING
1819

1920
from cirq_ionq import ionq_exceptions, results
@@ -196,6 +197,10 @@ def results(
196197
break
197198
time.sleep(polling_seconds)
198199
time_waited_seconds += polling_seconds
200+
if 'warning' in self._job and 'messages' in self._job['warning']:
201+
for warning in self._job['warning']['messages']:
202+
warnings.warn(warning)
203+
199204
if self.status() != 'completed':
200205
if 'failure' in self._job and 'error' in self._job['failure']:
201206
error = self._job['failure']['error']

cirq-ionq/cirq_ionq/job_test.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from unittest import mock
1616

17+
import warnings
1718
import pytest
1819

1920
import cirq_ionq as ionq
@@ -64,9 +65,16 @@ def test_job_results_qpu():
6465
'target': 'qpu',
6566
'metadata': {'shots': 1000, 'measurement0': f'a{chr(31)}0,1'},
6667
'data': {'histogram': {'0': '0.6', '2': '0.4'}},
68+
'warning': {
69+
'messages': ['foo', 'bar'],
70+
},
6771
}
6872
job = ionq.Job(None, job_dict)
69-
results = job.results()
73+
with warnings.catch_warnings(record=True) as w:
74+
results = job.results()
75+
assert len(w) == 2
76+
assert "foo" in str(w[0].message)
77+
assert "bar" in str(w[1].message)
7078
expected = ionq.QPUResult({0: 600, 1: 400}, 2, {'a': [0, 1]})
7179
assert results == expected
7280

0 commit comments

Comments
 (0)