22# Copyright (c) Microsoft Corporation.
33# Licensed under the MIT License.
44##
5- from typing import TYPE_CHECKING , Any , Dict , Union , Optional
5+ from __future__ import annotations
6+
7+ from typing import TYPE_CHECKING , Any , Dict , Optional , Sequence , Union
68
79try :
810 import cirq
@@ -117,7 +119,13 @@ def _translate_cirq_circuit(circuit) -> Dict[str, Any]:
117119 """Translate Cirq circuit to IonQ JSON. If dependencies \
118120 are not installed, throw error with installation instructions."""
119121 from cirq_ionq import Serializer
120- return Serializer ().serialize (circuit )
122+
123+ serializer = Serializer ()
124+ if hasattr (serializer , "serialize_single_circuit" ):
125+ return serializer .serialize_single_circuit (circuit )
126+
127+ # Backward-compat for older cirq_ionq.
128+ return serializer .serialize (circuit )
121129
122130 def _to_cirq_job (self , azure_job : "AzureJob" ) -> "CirqIonqJob" :
123131 """Convert Azure job to Cirq job"""
@@ -160,10 +168,18 @@ def submit(
160168
161169 @staticmethod
162170 def _to_cirq_result (
163- result : Union [ QPUResult , SimulatorResult ] ,
171+ result : "IonQResultLike" ,
164172 param_resolver : cirq .ParamResolverOrSimilarType = cirq .ParamResolver ({}),
165173 seed : cirq .RANDOM_STATE_OR_SEED_LIKE = None ,
166174 ) -> "cirq.Result" :
175+ # cirq_ionq>=1.6 returns a list of results even for a single circuit.
176+ if isinstance (result , (list , tuple )):
177+ if len (result ) != 1 :
178+ raise ValueError (
179+ f"Expected a single IonQ result for a single circuit, got { len (result )} results."
180+ )
181+ result = result [0 ]
182+
167183 if isinstance (result , QPUResult ):
168184 return result .to_cirq_result (params = cirq .ParamResolver (param_resolver ))
169185 elif isinstance (result , SimulatorResult ):
@@ -172,4 +188,8 @@ def _to_cirq_result(
172188 raise ValueError ("Result {result} not supported. \
173189 Expecting either a cirq_ionq.results.QPUResult \
174190 or cirq_ionq.results.SimulatorResult." )
191+
192+
193+ IonQSingleResult = Union [QPUResult , SimulatorResult ]
194+ IonQResultLike = Union [IonQSingleResult , Sequence [IonQSingleResult ]]
175195
0 commit comments