def _validate_num_qubits(density_matrix: np.ndarray) -> int:
    """Validates that matrix's shape is a valid shape for qubits.

    This method only works on a qubit-only density matrix.  Use
    `_validate_density_matrix_qid_shape` otherwise.
    """
    shape = density_matrix.shape
    half_index = len(shape) // 2
    row_size = np.prod(shape[:half_index]).item() if shape else 0
    col_size = np.prod(shape[half_index:]).item() if shape else 0
    if row_size != col_size:
        raise ValueError(f'Matrix was not square. Shape was {shape}')
    if row_size & (row_size - 1):
        raise ValueError(
            'Matrix could not be shaped into a square matrix with dimensions '
            f'that are a power of two. Shape was {shape}'
        )
    if len(shape) > 2 and not np.allclose(shape, 2):
        raise ValueError(
            'Matrix is a tensor of rank greater than 2, but had dimensions '
            f'that are not powers of two. Shape was {shape}'
        )
    return int(row_size).bit_length() - 1
Validates that matrix's shape is a valid shape for qubits.

    This method only works on a qubit-only density matrix.  Use
    `_validate_density_matrix_qid_shape` otherwise.
    
cirq-core/cirq/sim/density_matrix_utils.py