code: def _validate_num_qubits(density_matrix: np.ndarray) -> int:
      
      shape = density_matrix.shape
      half_index = len(shape) // 2
      row_size = np.prod(shape[:half_index]) if len(shape) != 0 else 0
      col_size = np.prod(shape[half_index:]) if len(shape) != 0 else 0
      if row_size != col_size:
          raise ValueError(
              'Matrix was not square. Shape was {}'.format(shape))
      if row_size & (row_size - 1):
          raise ValueError(
              'Matrix could not be shaped into a square matrix with dimensions '
              'not a power of two. Shape was {}'.format(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 '
              'that are not powers of two. Shape was {}'.format(shape)
          )
      return int(row_size).bit_length() - 1
docs: Validates that matrix's shape is a valid shape for qubits.