subroutine calc_perturbation_overlap(ivec)
use hash, only: hash_table_lookup
use SystemData, only: nel
integer, intent(in) :: ivec
integer :: idet, sign_ind, hash_val, det_ind
integer :: nI(nel)
integer(n_int) :: int_sign(lenof_sign_kp)
real(dp) :: real_sign_1(lenof_sign_kp), real_sign_2(lenof_sign_kp)
real(dp) :: overlap
logical :: tDetFound
overlap = 0.0_dp
sign_ind = nifd + lenof_sign_kp * (ivec - 1) + 1
! Loop over all determinants in perturbed_ground
do idet = 1, size(perturbed_ground, dim=2)
call extract_sign(perturbed_ground(:, idet), real_sign_1)
if (IsUnoccDet(real_sign_1)) cycle
call decode_bit_det(nI, perturbed_ground(:, idet))
! Search to see if this determinant is in any Krylov vector.
call hash_table_lookup(nI, perturbed_ground(:, idet), nifd, krylov_vecs_ht, krylov_vecs, &
det_ind, hash_val, tDetFound)
if (tDetFound) then
! If here then this determinant was found in the hash table.
! Add in the contributions to the overlap.
int_sign = krylov_vecs(sign_ind:sign_ind + lenof_sign_kp - 1, det_ind)
real_sign_2 = transfer(int_sign, real_sign_1)
overlap = overlap + (real_sign_1(kp_ind_1(1)) * real_sign_2(kp_ind_2(1)) + &
real_sign_1(kp_ind_2(1)) * real_sign_2(kp_ind_1(1))) / 2.0_dp
end if
end do
pert_overlaps(ivec) = pert_overlaps(ivec) + overlap
end subroutine calc_perturbation_overlap