Skip to content

Commit f66d46b

Browse files
committed
replace chr(30) and chr(31) delimiters with k and t
1 parent e8e79bb commit f66d46b

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

cirq-ionq/cirq_ionq/job.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,22 @@ def measurement_dict(self, circuit_index=0) -> dict[str, Sequence[int]]:
178178
measurement_matadata = self._job['metadata']
179179

180180
if measurement_matadata is not None:
181-
full_str = ''.join(
182-
value
183-
for key, value in measurement_matadata.items()
184-
if key.startswith('measurement')
185-
)
186-
if full_str == '':
181+
pieces = [
182+
(int(k[len("measurement") :]), v)
183+
for k, v in measurement_matadata.items()
184+
if k.startswith("measurement")
185+
]
186+
if not pieces:
187+
return measurement_dict
188+
pieces.sort(key=lambda kv: kv[0])
189+
full_str = "".join(v for _, v in pieces)
190+
# JSON encoding: [{"k": str, "t": [int, ...]}, ...]
191+
arr = json.loads(full_str)
192+
if isinstance(arr, list) and all(isinstance(x, dict) for x in arr):
193+
for item in arr:
194+
if 'k' in item and 't' in item:
195+
measurement_dict[str(item['k'])] = [int(t) for t in item['t']]
187196
return measurement_dict
188-
for key_value in full_str.split(chr(30)):
189-
key, value = key_value.split(chr(31))
190-
measurement_dict[key] = [int(t) for t in value.split(',')]
191197

192198
return measurement_dict
193199

cirq-ionq/cirq_ionq/serializer.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -407,12 +407,11 @@ def _serialize_measurements(self, meas_ops: Iterator) -> dict[str, str]:
407407
and targets into a form that is suitable for passing through IonQ's metadata field
408408
for a job.
409409
410-
Each key and targets are serialized into a string of the form `key` + the ASCII unit
411-
separator (chr(31)) + targets as a comma separated value. These are then combined
412-
into a string with a separator character of the ASCII record separator (chr(30)).
413-
Finally this full string is serialized as the values in the metadata dict with keys
414-
given by `measurementX` for X = 0,1, .. 9 and X large enough to contain the entire
415-
string.
410+
Measurements are encoded as a compact JSON array of objects with keys:
411+
`{"k": <measurement_key:str>, "t": <list[int] of target qubit indices>}`.
412+
The resulting JSON string is split into chunks and stored under
413+
`measurement0`, `measurement1`, ... to respect the IonQ metadata
414+
value-size limit.
416415
417416
Args:
418417
A list of the result of serializing the measurement (not supported by the API).
@@ -423,8 +422,13 @@ def _serialize_measurements(self, meas_ops: Iterator) -> dict[str, str]:
423422
Raises:
424423
ValueError: if the
425424
"""
426-
key_values = [f'{op["key"]}{chr(31)}{op["targets"]}' for op in meas_ops]
427-
full_str = chr(30).join(key_values)
425+
# Encode as JSON (no control chars) and keep measurement0..N chunking.
426+
json_array = [
427+
{"k": op["key"], "t": [int(t) for t in op["targets"].split(",")]} for op in meas_ops
428+
]
429+
if not json_array:
430+
return {}
431+
full_str = json.dumps(json_array, separators=(",", ":"))
428432
# IonQ maximum value size for metadata.
429433
max_value_size = 40
430434
split_strs = [

0 commit comments

Comments
 (0)