def test_pathlib_paths(tmpdir): path = pathlib.Path(tmpdir) / 'op.json' cirq.to_json(cirq.X, path) assert cirq.read_json(path) == cirq.X
def test_metadata_json_load_logic(): qubits = cirq.LineQubit.range(4) graph = nx.star_graph(3) metadata = cirq.DeviceMetadata(qubits, graph) str_rep = cirq.to_json(metadata) assert metadata == cirq.read_json(json_text=str_rep)
def assert_repr_and_json_test_data_agree( mod_spec: ModuleJsonTestSpec, repr_path: pathlib.Path, json_path: pathlib.Path, inward_only: bool, deprecation_deadline: Optional[str], ): if not repr_path.exists() and not json_path.exists(): return rel_repr_path = f'{repr_path.relative_to(REPO_ROOT)}' rel_json_path = f'{json_path.relative_to(REPO_ROOT)}' try: json_from_file = json_path.read_text() ctx_manager = (cirq.testing.assert_deprecated( deadline=deprecation_deadline, count=None) if deprecation_deadline else contextlib.suppress()) with ctx_manager: json_obj = cirq.read_json(json_text=json_from_file) except ValueError as ex: # coverage: ignore # coverage: ignore if "Could not resolve type" in str(ex): mod_path = mod_spec.name.replace(".", "/") rel_resolver_cache_path = f"{mod_path}/json_resolver_cache.py" # coverage: ignore pytest.fail( f"{rel_json_path} can't be parsed to JSON.\n" f"Maybe an entry is missing from the " f" `_class_resolver_dictionary` method in {rel_resolver_cache_path}?" ) else: raise ValueError( f"deprecation: {deprecation_deadline} - got error: {ex}") except AssertionError as ex: # coverage: ignore # coverage: ignore raise ex except Exception as ex: # coverage: ignore # coverage: ignore raise IOError( f'Failed to parse test json data from {rel_json_path}.') from ex try: repr_obj = _eval_repr_data_file(repr_path, deprecation_deadline) except Exception as ex: # coverage: ignore # coverage: ignore raise IOError( f'Failed to parse test repr data from {rel_repr_path}.') from ex assert proper_eq(json_obj, repr_obj), ( f'The json data from {rel_json_path} did not parse ' f'into an object equivalent to the repr data from {rel_repr_path}.\n' f'\n' f'json object: {json_obj!r}\n' f'repr object: {repr_obj!r}\n') if not inward_only: json_from_cirq = cirq.to_json(repr_obj) json_from_cirq_obj = json.loads(json_from_cirq) json_from_file_obj = json.loads(json_from_file) assert json_from_cirq_obj == json_from_file_obj, ( f'The json produced by cirq no longer agrees with the json in the ' f'{rel_json_path} test data file.\n' f'\n' f'You must either fix the cirq code to continue to produce the ' f'same output, or you must move the old test data to ' f'{rel_json_path}_inward and create a fresh {rel_json_path} file.\n' f'\n' f'test data json:\n' f'{json_from_file}\n' f'\n' f'cirq produced json:\n' f'{json_from_cirq}\n')
def test_parses_single_qubit_gate(gate): assert gate == cirq.read_json(json_text=(cirq.to_json(gate)))