Skip to content

Commit e6179b4

Browse files
verultrht
authored andcommitted
Add json serialization to SycamoreTargetGateset. (quantumlib#5314)
This makes it possible to fully serialize `GridDeviceMetadata`. A repr/json with tabulation set is omitted because the repr of cirq.two_qubit_gate_product_tabulation() is very long. @tanujkhattar
1 parent 6ec1f36 commit e6179b4

File tree

6 files changed

+32
-4
lines changed

6 files changed

+32
-4
lines changed

cirq-google/cirq_google/json_resolver_cache.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def _class_resolver_dictionary() -> Dict[str, ObjectFactory]:
3939
'FloquetPhasedFSimCalibrationRequest': cirq_google.FloquetPhasedFSimCalibrationRequest,
4040
'PhasedFSimCalibrationResult': cirq_google.PhasedFSimCalibrationResult,
4141
'PhasedFSimCharacterization': cirq_google.PhasedFSimCharacterization,
42+
'SycamoreTargetGateset': cirq_google.SycamoreTargetGateset,
4243
'XEBPhasedFSimCalibrationOptions': cirq_google.XEBPhasedFSimCalibrationOptions,
4344
'XEBPhasedFSimCalibrationRequest': cirq_google.XEBPhasedFSimCalibrationRequest,
4445
'LocalXEBPhasedFSimCalibrationOptions': cirq_google.LocalXEBPhasedFSimCalibrationOptions,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"cirq_type": "SycamoreTargetGateset",
3+
"atol": 1e-08,
4+
"tabulation": null
5+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cirq_google.SycamoreTargetGateset(atol=1e-08, tabulation=None)

cirq-google/cirq_google/json_test_data/spec.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
'WITHOUT_CHI_FLOQUET_PHASED_FSIM_CHARACTERIZATION',
2323
'XmonDevice',
2424
'XMON',
25-
'SycamoreTargetGateset',
2625
],
2726
should_not_be_serialized=[
2827
'AnnealSequenceSearchStrategy',

cirq-google/cirq_google/transformers/target_gatesets/sycamore_gateset.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"""Target gateset used for compiling circuits to Sycamore + 1-q rotations + measurement gates."""
1616

1717
import itertools
18-
from typing import cast, List, Optional, Sequence
18+
from typing import cast, Any, Dict, List, Optional, Sequence
1919

2020
import cirq
2121
from cirq.protocols.decompose_protocol import DecomposeResult
@@ -165,6 +165,12 @@ def __repr__(self) -> str:
165165
return (
166166
f'cirq_google.SycamoreTargetGateset('
167167
f'atol={self.atol}, '
168-
f'tabulation={self.tabulation}, '
169-
f')'
168+
f'tabulation={self.tabulation})'
170169
)
170+
171+
def _json_dict_(self) -> Dict[str, Any]:
172+
return {'atol': self.atol, 'tabulation': self.tabulation}
173+
174+
@classmethod
175+
def _from_json_dict_(cls, atol, tabulation, **kwargs):
176+
return cls(atol=atol, tabulation=tabulation)

cirq-google/cirq_google/transformers/target_gatesets/sycamore_gateset_test.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,3 +387,19 @@ def test_supported_operation(op):
387387
)
388388
multi_qubit_ops = [e for e in converted_circuit.all_operations() if len(e.qubits) > 1]
389389
assert all(isinstance(e.gate, cirq_google.SycamoreGate) for e in multi_qubit_ops)
390+
391+
392+
@pytest.mark.parametrize(
393+
'gateset',
394+
[
395+
cirq_google.SycamoreTargetGateset(),
396+
cirq_google.SycamoreTargetGateset(
397+
tabulation=cirq.two_qubit_gate_product_tabulation(
398+
cirq.unitary(cirq_google.SYC), 0.1, random_state=cirq.value.parse_random_state(11)
399+
)
400+
),
401+
],
402+
)
403+
def test_repr_json(gateset):
404+
assert eval(repr(gateset)) == gateset
405+
assert cirq.read_json(json_text=cirq.to_json(gateset)) == gateset

0 commit comments

Comments
 (0)