def assert_controlled_unitary_consistent(gate: ops.Gate):
    """Checks that unitary of ControlledGate(gate) is consistent with gate.controlled()."""

    u_orig = protocols.unitary(ops.ControlledGate(gate))
    u_controlled = protocols.unitary(gate.controlled())
    np.testing.assert_allclose(
        u_orig,
        u_controlled,
        atol=1e-6,
        err_msg=f"Unitary for gate.controlled() is inconsistent for {gate=}",
    )
Checks that unitary of ControlledGate(gate) is consistent with gate.controlled().
cirq-core/cirq/testing/consistent_controlled_gate_op.py