def _json_dict_(self): accept_gates_json = [ gate if not isinstance(gate, type) else cirq.json_cirq_type(gate) for gate in self.gates_to_accept ] check_gates_json = [ cirq.json_cirq_type(gate) for gate in self.gate_types_to_check ] return { 'gates_to_accept': accept_gates_json, 'gate_types_to_check': check_gates_json, 'allow_symbols': self.allow_symbols, 'atol': self.atol, }
def test_deprecated_cirq_type_in_json_dict(): class HasOldJsonDict: # Required for testing serialization of non-cirq objects. __module__ = 'test.noncirq.namespace' # type: ignore def __eq__(self, other): return isinstance(other, HasOldJsonDict) def _json_dict_(self): return {'cirq_type': 'test.noncirq.namespace.HasOldJsonDict'} @classmethod def _from_json_dict_(cls, **kwargs): return cls() with pytest.raises(ValueError, match='not a Cirq type'): _ = cirq.json_cirq_type(HasOldJsonDict) def custom_resolver(name): if name == 'test.noncirq.namespace.HasOldJsonDict': return HasOldJsonDict test_resolvers = [custom_resolver] + cirq.DEFAULT_RESOLVERS with cirq.testing.assert_deprecated("Found 'cirq_type'", deadline='v0.15'): assert_json_roundtrip_works(HasOldJsonDict(), resolvers=test_resolvers)
def test_type_serialization(mod_spec: ModuleJsonTestSpec, cirq_obj_name: str, cls): if cirq_obj_name in mod_spec.tested_elsewhere: pytest.skip("Tested elsewhere.") if cirq_obj_name in mod_spec.not_yet_serializable: return pytest.xfail(reason="Not serializable (yet)") if cls is None: pytest.skip(f'No serialization for None-mapped type: {cirq_obj_name}') try: typename = cirq.json_cirq_type(cls) except ValueError as e: pytest.skip(f'No serialization for non-Cirq type: {str(e)}') def custom_resolver(name): if name == 'SerializableTypeObject': return SerializableTypeObject sto = SerializableTypeObject(cls) test_resolvers = [custom_resolver] + cirq.DEFAULT_RESOLVERS expected_json = (f'{{\n "cirq_type": "SerializableTypeObject",\n' f' "test_type": "{typename}"\n}}') assert cirq.to_json(sto) == expected_json assert cirq.read_json(json_text=expected_json, resolvers=test_resolvers) == sto assert_json_roundtrip_works(sto, resolvers=test_resolvers)
def test_deprecated_dataclass_json_dict_namespace(): @dataclasses.dataclass class HasOldJsonDict: # Required for testing serialization of non-cirq objects. __module__: ClassVar = 'test.noncirq.namespace' # type: ignore x: int def _json_dict_(self): return json_serialization.dataclass_json_dict( self, namespace='test.noncirq.namespace') @classmethod def _from_json_dict_(cls, x, **kwargs): return cls(x) with pytest.raises(ValueError, match='not a Cirq type'): _ = cirq.json_cirq_type(HasOldJsonDict) def custom_resolver(name): if name == 'test.noncirq.namespace.HasOldJsonDict': return HasOldJsonDict test_resolvers = [custom_resolver] + cirq.DEFAULT_RESOLVERS with cirq.testing.assert_deprecated("Found 'cirq_type'", 'Define obj._json_namespace_', deadline='v0.15', count=5): assert_json_roundtrip_works(HasOldJsonDict(1), resolvers=test_resolvers)
def test_deprecated_obj_to_dict_helper_namespace(): class HasOldJsonDict: # Required for testing serialization of non-cirq objects. __module__ = 'test.noncirq.namespace' # type: ignore def __init__(self, x): self.x = x def __eq__(self, other): return isinstance(other, HasOldJsonDict) and other.x == self.x def _json_dict_(self): return json_serialization.obj_to_dict_helper( self, ['x'], namespace='test.noncirq.namespace') @classmethod def _from_json_dict_(cls, x, **kwargs): return cls(x) with pytest.raises(ValueError, match='not a Cirq type'): _ = cirq.json_cirq_type(HasOldJsonDict) def custom_resolver(name): if name == 'test.noncirq.namespace.HasOldJsonDict': return HasOldJsonDict test_resolvers = [custom_resolver] + cirq.DEFAULT_RESOLVERS with cirq.testing.assert_deprecated("Found 'cirq_type'", 'Define obj._json_namespace_', deadline='v0.15', count=3): assert_json_roundtrip_works(HasOldJsonDict(1), resolvers=test_resolvers)
def _json_dict_(self): storage_gate_times = { cirq.json_cirq_type(key): val for key, val in self.gate_times_ns.items() } return { # JSON requires mappings to have keys of basic types. # Pairs must be sorted to ensure consistent serialization. 'gate_times_ns': tuple(storage_gate_times.items()), 't1_ns': tuple(self.t1_ns.items()), 'tphi_ns': tuple(self.tphi_ns.items()), 'readout_errors': tuple((k, v.tolist()) for k, v in self.readout_errors.items()), 'gate_pauli_errors': tuple(self.gate_pauli_errors.items()), 'fsim_errors': tuple(self.fsim_errors.items()), 'validate': self.validate, }