def assert_consistent_mixture(gate: Any, rtol: float = 1e-5, atol: float = 1e-8):
    """Asserts that a given gate is a mixture and the mixture probabilities sum to one."""
    assert cirq.has_mixture(gate), f"Give gate {gate!r} does not return for cirq.has_mixture."
    mixture = cirq.mixture(gate)
    total = np.sum(np.fromiter((k for k, v in mixture), dtype=float))
    assert np.abs(1 - total) <= atol + rtol * np.abs(total), (
        f"The mixture for gate {gate!r} did not return coefficients that sum to 1. Summed to "
        f"{total}."
    )
Asserts that a given gate is a mixture and the mixture probabilities sum to one.
cirq-core/cirq/testing/consistent_channels.py