Skip to content

Commit

Permalink
Add json serialization to SycamoreTargetGateset. (quantumlib#5314)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
verult authored and rht committed May 1, 2023
1 parent 6ec1f36 commit e6179b4
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 4 deletions.
1 change: 1 addition & 0 deletions cirq-google/cirq_google/json_resolver_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def _class_resolver_dictionary() -> Dict[str, ObjectFactory]:
'FloquetPhasedFSimCalibrationRequest': cirq_google.FloquetPhasedFSimCalibrationRequest,
'PhasedFSimCalibrationResult': cirq_google.PhasedFSimCalibrationResult,
'PhasedFSimCharacterization': cirq_google.PhasedFSimCharacterization,
'SycamoreTargetGateset': cirq_google.SycamoreTargetGateset,
'XEBPhasedFSimCalibrationOptions': cirq_google.XEBPhasedFSimCalibrationOptions,
'XEBPhasedFSimCalibrationRequest': cirq_google.XEBPhasedFSimCalibrationRequest,
'LocalXEBPhasedFSimCalibrationOptions': cirq_google.LocalXEBPhasedFSimCalibrationOptions,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cirq_type": "SycamoreTargetGateset",
"atol": 1e-08,
"tabulation": null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cirq_google.SycamoreTargetGateset(atol=1e-08, tabulation=None)
1 change: 0 additions & 1 deletion cirq-google/cirq_google/json_test_data/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
'WITHOUT_CHI_FLOQUET_PHASED_FSIM_CHARACTERIZATION',
'XmonDevice',
'XMON',
'SycamoreTargetGateset',
],
should_not_be_serialized=[
'AnnealSequenceSearchStrategy',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"""Target gateset used for compiling circuits to Sycamore + 1-q rotations + measurement gates."""

import itertools
from typing import cast, List, Optional, Sequence
from typing import cast, Any, Dict, List, Optional, Sequence

import cirq
from cirq.protocols.decompose_protocol import DecomposeResult
Expand Down Expand Up @@ -165,6 +165,12 @@ def __repr__(self) -> str:
return (
f'cirq_google.SycamoreTargetGateset('
f'atol={self.atol}, '
f'tabulation={self.tabulation}, '
f')'
f'tabulation={self.tabulation})'
)

def _json_dict_(self) -> Dict[str, Any]:
return {'atol': self.atol, 'tabulation': self.tabulation}

@classmethod
def _from_json_dict_(cls, atol, tabulation, **kwargs):
return cls(atol=atol, tabulation=tabulation)
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,19 @@ def test_supported_operation(op):
)
multi_qubit_ops = [e for e in converted_circuit.all_operations() if len(e.qubits) > 1]
assert all(isinstance(e.gate, cirq_google.SycamoreGate) for e in multi_qubit_ops)


@pytest.mark.parametrize(
'gateset',
[
cirq_google.SycamoreTargetGateset(),
cirq_google.SycamoreTargetGateset(
tabulation=cirq.two_qubit_gate_product_tabulation(
cirq.unitary(cirq_google.SYC), 0.1, random_state=cirq.value.parse_random_state(11)
)
),
],
)
def test_repr_json(gateset):
assert eval(repr(gateset)) == gateset
assert cirq.read_json(json_text=cirq.to_json(gateset)) == gateset

0 comments on commit e6179b4

Please sign in to comment.