# Buggy code
def get_init_guess(self, cell=None, key="minao"):
    if cell is None:
        cell = self.cell
    dm_kpts = None
    if key.lower() == "1e":
        dm_kpts = self.init_guess_by_1e(cell)
    elif getattr(cell, "natm", 0) == 0:
        logger.info(self, "No atom found in cell. Use 1e initial guess")
        dm_kpts = self.init_guess_by_1e(cell)
    elif key.lower() == "atom":
        dm = self.init_guess_by_atom(cell)
    elif key.lower().startswith("chk"):
        try:
            dm_kpts = self.from_chk()
        except (IOError, KeyError):
            logger.warn(self, "Fail to read %s. Use MINAO initial guess", self.chkfile)
            dm = self.init_guess_by_minao(cell)
    else:
        dm = self.init_guess_by_minao(cell)

    if dm_kpts is None:
        nao = dm[0].shape[-1]
        nkpts = len(self.kpts)
        dm_kpts = lib.asarray([dm] * nkpts).reshape(nkpts, 2, nao, nao)
        dm_kpts = dm_kpts.transpose(1, 0, 2, 3)
        dm_kpts[0, :] *= 1.01
        dm_kpts[1, :] *= 0.99  # To break spin symmetry
        assert dm_kpts.shape[0] == 2

    if cell.dimension < 3:
        ne = np.einsum("xkij,kji->xk", dm_kpts, self.get_ovlp(cell)).real
        nelec = np.asarray(cell.nelec).reshape(2, 1)
        if np.any(abs(ne - nelec) > 1e-7):
            logger.warn(
                self,
                "Big error detected in the electron number "
                "of initial guess density matrix (Ne/cell = %g)!\n"
                "  This can cause huge error in Fock matrix and "
                "lead to instability in SCF for low-dimensional "
                "systems.\n  DM is normalized to correct number "
                "of electrons",
                ne.mean(),
            )
            dm_kpts *= (nelec / ne).reshape(2, -1, 1, 1)
    return dm_kpts


# Traceback (IndexError)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/gagliard/phamx494/pyscf/pyscf/scf/hf.py", line 1476, in kernel
return self.scf(dm0, **kwargs)
File "/home/gagliard/phamx494/pyscf/pyscf/scf/hf.py", line 1470, in scf
conv_check=self.conv_check, **kwargs)
File "/home/gagliard/phamx494/pyscf/pyscf/scf/hf.py", line 143, in kernel
vhf = mf.get_veff(mol, dm)
File "/home/gagliard/phamx494/pyscf/pyscf/pbc/scf/krohf.py", line 335, in get_veff
vj, vk = self.get_jk(cell, dm_kpts, hermi, kpts, kpts_band)
File "/home/gagliard/phamx494/pyscf/pyscf/pbc/scf/khf.py", line 493, in get_jk
exxdiv=self.exxdiv)
File "/home/gagliard/phamx494/pyscf/pyscf/pbc/df/fft.py", line 288, in get_jk
vj = fft_jk.get_j_kpts(self, dm, hermi, kpts, kpts_band)
File "/home/gagliard/phamx494/pyscf/pyscf/pbc/df/fft_jk.py", line 67, in get_j_kpts
rhoR[i,p0:p1] += make_rho(i, ao_ks, mask, 'LDA')
File "/home/gagliard/phamx494/pyscf/pyscf/pbc/dft/numint.py", line 1222, in make_rho
hermi=1)
File "/home/gagliard/phamx494/pyscf/pyscf/pbc/dft/numint.py", line 1086, in eval_rho
rhoR += eval_rho(cell, ao_kpts[k], dm_kpts[k], non0tab, xctype,
IndexError: index 2 is out of bounds for axis 0 with size 2

# Fixed code
def get_init_guess(self, cell=None, key="minao"):
    if cell is None:
        cell = self.cell
    dm_kpts = None
    key = key.lower()
    if key == "1e" or key == "hcore":
        dm_kpts = self.init_guess_by_1e(cell)
    elif getattr(cell, "natm", 0) == 0:
        logger.info(self, "No atom found in cell. Use 1e initial guess")
        dm_kpts = self.init_guess_by_1e(cell)
    elif key == "atom":
        dm = self.init_guess_by_atom(cell)
    elif key[:3] == "chk":
        try:
            dm_kpts = self.from_chk()
        except (IOError, KeyError):
            logger.warn(self, "Fail to read %s. Use MINAO initial guess", self.chkfile)
            dm = self.init_guess_by_minao(cell)
    else:
        dm = self.init_guess_by_minao(cell)

    if dm_kpts is None:
        nao = dm[0].shape[-1]
        nkpts = len(self.kpts)
        # dm[spin,nao,nao] at gamma point -> dm_kpts[spin,nkpts,nao,nao]
        dm_kpts = np.repeat(dm[:, None, :, :], nkpts, axis=1)
        dm_kpts[0, :] *= 1.01
        dm_kpts[1, :] *= 0.99  # To slightly break spin symmetry
        assert dm_kpts.shape[0] == 2

    if cell.dimension < 3:
        ne = np.einsum("xkij,kji->xk", dm_kpts, self.get_ovlp(cell)).real
        nelec = np.asarray(cell.nelec).reshape(2, 1)
        if np.any(abs(ne - nelec) > 1e-7):
            logger.warn(
                self,
                "Big error detected in the electron number "
                "of initial guess density matrix (Ne/cell = %g)!\n"
                "  This can cause huge error in Fock matrix and "
                "lead to instability in SCF for low-dimensional "
                "systems.\n  DM is normalized to correct number "
                "of electrons",
                ne.mean(),
            )
            dm_kpts *= (nelec / ne).reshape(2, -1, 1, 1)
    return dm_kpts