Skip to content

Commit

Permalink
Allow server-side warnings from IonQ (quantumlib#5222)
Browse files Browse the repository at this point in the history
This will be used to notify users about deprecations and changes to the API without needing to update Cirq independently.
  • Loading branch information
Cynocracy authored and rht committed May 1, 2023
1 parent 4b8ee52 commit 3ad739f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 5 additions & 0 deletions cirq-ionq/cirq_ionq/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"""Represents a job created via the IonQ API."""

import time
import warnings
from typing import Dict, Sequence, Union, TYPE_CHECKING

from cirq_ionq import ionq_exceptions, results
Expand Down Expand Up @@ -196,6 +197,10 @@ def results(
break
time.sleep(polling_seconds)
time_waited_seconds += polling_seconds
if 'warning' in self._job and 'messages' in self._job['warning']:
for warning in self._job['warning']['messages']:
warnings.warn(warning)

if self.status() != 'completed':
if 'failure' in self._job and 'error' in self._job['failure']:
error = self._job['failure']['error']
Expand Down
10 changes: 9 additions & 1 deletion cirq-ionq/cirq_ionq/job_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from unittest import mock

import warnings
import pytest

import cirq_ionq as ionq
Expand Down Expand Up @@ -64,9 +65,16 @@ def test_job_results_qpu():
'target': 'qpu',
'metadata': {'shots': 1000, 'measurement0': f'a{chr(31)}0,1'},
'data': {'histogram': {'0': '0.6', '2': '0.4'}},
'warning': {
'messages': ['foo', 'bar'],
},
}
job = ionq.Job(None, job_dict)
results = job.results()
with warnings.catch_warnings(record=True) as w:
results = job.results()
assert len(w) == 2
assert "foo" in str(w[0].message)
assert "bar" in str(w[1].message)
expected = ionq.QPUResult({0: 600, 1: 400}, 2, {'a': [0, 1]})
assert results == expected

Expand Down

0 comments on commit 3ad739f

Please sign in to comment.