def test_json_serializable_dataclass_parenthesis(): @cirq.json_serializable_dataclass() class MyDC: q: cirq.LineQubit desc: str def custom_resolver(name): if name == 'MyDC': return MyDC my_dc = MyDC(cirq.LineQubit(4), 'hi mom') assert_json_roundtrip_works(my_dc, resolvers=[custom_resolver] + cirq.DEFAULT_RESOLVERS)
def test_json_serializable_dataclass_namespace(): @cirq.json_serializable_dataclass(namespace='cirq.experiments') class QuantumVolumeParams: width: int depth: int circuit_i: int qvp = QuantumVolumeParams(width=5, depth=5, circuit_i=0) def custom_resolver(name): if name == 'cirq.experiments.QuantumVolumeParams': return QuantumVolumeParams assert_json_roundtrip_works(qvp, resolvers=[custom_resolver] + cirq.DEFAULT_RESOLVERS)
def test_dataclass_json_dict(): @dataclasses.dataclass(frozen=True) class MyDC: q: cirq.LineQubit desc: str def _json_dict_(self): return cirq.dataclass_json_dict(self) def custom_resolver(name): if name == 'MyDC': return MyDC my_dc = MyDC(cirq.LineQubit(4), 'hi mom') assert_json_roundtrip_works( my_dc, resolvers=[custom_resolver, *cirq.DEFAULT_RESOLVERS])
def test_builtins(): assert_json_roundtrip_works(True) assert_json_roundtrip_works(1) assert_json_roundtrip_works(1 + 2j) assert_json_roundtrip_works({ 'test': [123, 5.5], 'key2': 'asdf', '3': None, '0.0': [], })
def test_op_roundtrip(): q = cirq.LineQubit(5) op1 = cirq.rx(.123).on(q) assert_json_roundtrip_works(op1, text_should_be="""{ "cirq_type": "GateOperation", "gate": { "cirq_type": "XPowGate", "exponent": 0.03915211600060625, "global_shift": -0.5 }, "qubits": [ { "cirq_type": "LineQubit", "x": 5 } ] }""")
def test_pandas(): assert_json_roundtrip_works( pd.DataFrame(data=[[1, 2, 3], [4, 5, 6]], columns=['x', 'y', 'z'], index=[2, 5])) assert_json_roundtrip_works(pd.Index([1, 2, 3], name='test')) assert_json_roundtrip_works( pd.MultiIndex.from_tuples([(1, 2), (3, 4), (5, 6)], names=['alice', 'bob'])) assert_json_roundtrip_works( pd.DataFrame(index=pd.Index([1, 2, 3], name='test'), data=[[11, 21.0], [12, 22.0], [13, 23.0]], columns=['a', 'b'])) assert_json_roundtrip_works( pd.DataFrame(index=pd.MultiIndex.from_tuples([(1, 2), (2, 3), (3, 4)], names=['x', 'y']), data=[[11, 21.0], [12, 22.0], [13, 23.0]], columns=pd.Index(['a', 'b'], name='c')))
def test_op_roundtrip(): q = cirq.LineQubit(5) op1 = cirq.rx(0.123).on(q) assert_json_roundtrip_works( op1, text_should_be="""{ "cirq_type": "GateOperation", "gate": { "cirq_type": "Rx", "rads": 0.123 }, "qubits": [ { "cirq_type": "LineQubit", "x": 5 } ] }""", )
def test_sympy(): # Raw values. assert_json_roundtrip_works(sympy.Symbol('theta')) assert_json_roundtrip_works(sympy.Integer(5)) assert_json_roundtrip_works(sympy.Rational(2, 3)) assert_json_roundtrip_works(sympy.Float(1.1)) # Basic operations. s = sympy.Symbol('s') t = sympy.Symbol('t') assert_json_roundtrip_works(t + s) assert_json_roundtrip_works(t * s) assert_json_roundtrip_works(t / s) assert_json_roundtrip_works(t - s) assert_json_roundtrip_works(t**s) # Linear combinations. assert_json_roundtrip_works(t * 2) assert_json_roundtrip_works(4 * t + 3 * s + 2)
def test_numpy(): x = np.ones(1)[0] assert_json_roundtrip_works(x.astype(np.bool)) assert_json_roundtrip_works(x.astype(np.int8)) assert_json_roundtrip_works(x.astype(np.int16)) assert_json_roundtrip_works(x.astype(np.int32)) assert_json_roundtrip_works(x.astype(np.int64)) assert_json_roundtrip_works(x.astype(np.uint8)) assert_json_roundtrip_works(x.astype(np.uint16)) assert_json_roundtrip_works(x.astype(np.uint32)) assert_json_roundtrip_works(x.astype(np.uint64)) assert_json_roundtrip_works(x.astype(np.float32)) assert_json_roundtrip_works(x.astype(np.float64)) assert_json_roundtrip_works(x.astype(np.complex64)) assert_json_roundtrip_works(x.astype(np.complex128)) assert_json_roundtrip_works(np.ones((11, 5))) assert_json_roundtrip_works(np.arange(3))
def test_context_serialization(): def custom_resolver(name): if name == 'SBKImpl': return SBKImpl test_resolvers = [custom_resolver] + cirq.DEFAULT_RESOLVERS sbki_empty = SBKImpl('sbki_empty') assert_json_roundtrip_works(sbki_empty, resolvers=test_resolvers) sbki_list = SBKImpl('sbki_list', data_list=[sbki_empty, sbki_empty]) assert_json_roundtrip_works(sbki_list, resolvers=test_resolvers) sbki_tuple = SBKImpl('sbki_tuple', data_tuple=(sbki_list, sbki_list)) assert_json_roundtrip_works(sbki_tuple, resolvers=test_resolvers) sbki_dict = SBKImpl('sbki_dict', data_dict={ 'a': sbki_tuple, 'b': sbki_tuple }) assert_json_roundtrip_works(sbki_dict, resolvers=test_resolvers) sbki_json = str(cirq.to_json(sbki_dict)) # There should be exactly one context item for each previous SBKImpl. assert sbki_json.count('"cirq_type": "_SerializedContext"') == 4 # There should be exactly two key items for each of sbki_(empty|list|tuple), # plus one for the top-level sbki_dict. assert sbki_json.count('"cirq_type": "_SerializedKey"') == 7 # The final object should be a _SerializedKey for sbki_dict. final_obj_idx = sbki_json.rfind('{') final_obj = sbki_json[final_obj_idx:sbki_json.find('}', final_obj_idx) + 1] assert (final_obj == """{ "cirq_type": "_SerializedKey", "key": 4 }""") list_sbki = [sbki_dict] assert_json_roundtrip_works(list_sbki, resolvers=test_resolvers) dict_sbki = {'a': sbki_dict} assert_json_roundtrip_works(dict_sbki, resolvers=test_resolvers) assert sbki_list != json_serialization._SerializedKey(sbki_list) # Serialization keys have unique suffixes. sbki_other_list = SBKImpl('sbki_list', data_list=[sbki_list]) assert_json_roundtrip_works(sbki_other_list, resolvers=test_resolvers)
def test_swap_permutation_gate(): gate = SwapPermutationGate(swap_gate=cirq.SWAP) assert_json_roundtrip_works(gate, resolvers=DEFAULT_CONTRIB_RESOLVERS)