def test_proper_eq(): assert proper_eq(1, 1) assert not proper_eq(1, 2) assert proper_eq(np.array([1, 2, 3]), np.array([1, 2, 3])) assert not proper_eq(np.array([1, 2, 3]), np.array([1, 2, 3, 4])) assert not proper_eq(np.array([1, 2, 3]), np.array([[1, 2, 3]])) assert not proper_eq(np.array([1, 2, 3]), np.array([1, 4, 3])) assert proper_eq(pd.Index([1, 2, 3]), pd.Index([1, 2, 3])) assert not proper_eq(pd.Index([1, 2, 3]), pd.Index([1, 2, 3, 4])) assert not proper_eq(pd.Index([1, 2, 3]), pd.Index([1, 4, 3]))
def __eq__(self, other): if not isinstance(other, type(self)): return NotImplemented return (np.array_equal(self.base_gate, other.base_gate) and np.array_equal(self.kak_vecs, other.kak_vecs) and proper_eq(self.single_qubit_gates, other.single_qubit_gates) and self.max_expected_infidelity == other.max_expected_infidelity and self.summary == other.summary and np.array_equal(self.missed_points, other.missed_points))
def assert_repr_and_json_test_data_agree(repr_path: pathlib.Path, json_path: pathlib.Path, inward_only: bool): if not repr_path.exists() and not json_path.exists(): return rel_repr_path = f'{TEST_DATA_REL}/{repr_path.name}' rel_json_path = f'{TEST_DATA_REL}/{json_path.name}' try: json_from_file = json_path.read_text() json_obj = cirq.read_json(json_text=json_from_file) 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) 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 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')