89 "╔╦╗╦═╗╦╔═╗ ╔═╗ ┌─┐┌┬┐┬ ┬┬ ┬┌┐ \n"
90 " ║ ╠╦╝║║═╬╗╚═╗ │ │ ├─┤└┬┘├┴┐\n"
91 " ╩ ╩╚═╩╚═╝╚╚═╝ └─┘ ┴ ┴ ┴ ┴ └─┘\n\n";
94 fundamental_operator_set fops;
95 for (
auto const &[bl, bl_size] : gf_struct) {
96 for (
auto idx : range(bl_size)) { fops.insert(bl, idx); }
100 std::map<std::pair<int, int>,
int> linindex;
102 for (
auto const &[bl, bl_size] : gf_struct) {
104 for (
auto idx : range(bl_size)) {
105 linindex[std::make_pair(block_index, inner_index)] = fops[{bl, idx}];
112 std::vector<int> n_inner;
113 for (
auto const &[bl, bl_size] : gf_struct) { n_inner.push_back(bl_size); }
115 if (not delta_interface) {
119 if (not is_gf_hermitian(_G0_iw.value())) {
121 std::cout <<
"!-------------------------------------------------------------------------------------------!\n"
122 "! WARNING: S.G0_iw violates fundamental Green Function property G0(iw)[i,j] = G0(-iw)*[j,i] !\n"
123 "! Symmetrizing S.G0_iw ... !\n"
124 "!-------------------------------------------------------------------------------------------!\n\n";
125 _G0_iw = make_hermitian(_G0_iw.value());
131 auto Delta_iw = inverse(_G0_iw.value());
132 for (
auto bl : range(gf_struct.size()))
133 for (
auto iw : Delta_iw[bl].mesh()) Delta_iw[bl][iw] = iw - Delta_iw[bl][iw];
136 Delta_infty_vec = map(
138 [imag_threshold = params.
imag_threshold](gf_const_view<imfreq> d) {
139 auto [tail, err] = fit_hermitian_tail(d);
140 if (err > 1e-5) std::cerr <<
"WARNING: Tail fit in extraction of delta(infty) has large error of: " << err << std::endl;
141 auto Delta_infty = matrix<dcomplex>{tail(0, ellipsis())};
142#ifndef HYBRIDISATION_IS_COMPLEX
143 double max_imag = max_element(abs(imag(
Delta_infty)));
144 if (max_imag > imag_threshold)
145 TRIQS_RUNTIME_ERROR <<
"Largest imaginary element of delta(infty) e.g. of the local part of G0: " << max_imag
146 <<
", is larger than the set parameter imag_threshold " << imag_threshold;
153 for (
auto bl : range(gf_struct.size())) {
154 for (
auto iw : Delta_iw[bl].mesh()) Delta_iw[bl][iw] = Delta_iw[bl][iw] - Delta_infty_vec.value()[bl];
155 auto [Delta_tail_b, tail_err] = fit_hermitian_tail(Delta_iw[bl]);
156 _Delta_tau[bl]() = fourier(Delta_iw[bl], Delta_tail_b);
164 for (
auto bl : range(gf_struct.size())) {
165 for (
auto [n1, n2] : Delta_iw[bl].target_indices()) {
166#ifdef LOCAL_HAMILTONIAN_IS_COMPLEX
168 double max_imag = max_element(abs(imag(Delta_infty_vec.value()[bl])));
170 e_ij = Delta_infty_vec.value()[bl](n1, n2);
172 e_ij = Delta_infty_vec.value()[bl](n1, n2).real();
174 double e_ij = Delta_infty_vec.value()[bl](n1, n2).real();
177 if (n1 != n2 && abs(Delta_infty_vec.value()[bl](n1, n2)) < params.
off_diag_threshold) e_ij = 0.0;
179 auto bl_name = gf_struct[bl].first;
180 _h_loc0 = _h_loc0 + e_ij * c_dag<h_scalar_t>(bl_name, n1) * c<h_scalar_t>(bl_name, n2);
185 if (not is_gf_hermitian(_Delta_tau)) {
186 if (params.verbosity >= 2)
187 std::cout <<
"!---------------------------------------------------------------------------------------!\n"
188 "! WARNING: S.Delta_tau violates fundamental symmetry Delta(tau)[i,j] = Delta(tau)*[j,i] !\n"
189 "! Symmetrizing S.Delta_tau ... !\n"
190 "!---------------------------------------------------------------------------------------!\n\n";
191 _Delta_tau = make_hermitian(_Delta_tau);
193 if (not params.h_loc0.has_value()) TRIQS_RUNTIME_ERROR <<
"h_loc0 must be provided when using the Delta interface";
194 _h_loc0 = params.h_loc0.value();
197 _h_loc = params.h_int + _h_loc0;
199#ifndef HYBRIDISATION_IS_COMPLEX
201 for (
auto bl : range(gf_struct.size())) {
202 long bl_size = gf_struct[bl].second;
205 for (
auto [i, j] : product_range(bl_size, bl_size)) {
206 auto Delta_tau_bl_ij = _Delta_tau[bl].data()(_, i, j);
207 double max_imag = max_element(abs(imag(Delta_tau_bl_ij)));
208 if (i == j && max_imag > 1e-10) {
209 std::cout <<
"WARNING: max(abs(imag(S.Delta_tau[" << bl <<
"][" << i <<
", " << j <<
"]))) = "
210 << max_imag <<
" setting to zero.\n";
211 Delta_tau_bl_ij = real(Delta_tau_bl_ij);
212 }
else if (max_imag < params.imag_threshold) {
213 Delta_tau_bl_ij = real(Delta_tau_bl_ij);
220 if (params.verbosity >= 2)
221 std::cout <<
"The local Hamiltonian of the problem:" << std::endl << _h_loc << std::endl;
223 _performance_analysis.clear();
224 histo_map_t *histo_map = params.performance_analysis ? &_performance_analysis :
nullptr;
227 if (params.partition_method ==
"autopartition") {
228 if (params.verbosity >= 2)
229 std::cout <<
"Using autopartition algorithm to partition the local Hilbert space"
231 if (params.loc_n_min == 0 && params.loc_n_max == INT_MAX)
232 h_diag = {_h_loc, fops};
234 if (params.verbosity >= 2)
235 std::cout <<
"Restricting the local Hilbert space to states with [" << params.loc_n_min
236 <<
";" << params.loc_n_max <<
"] particles" << std::endl;
237 h_diag = {_h_loc, fops, params.loc_n_min, params.loc_n_max};
239 }
else if (params.partition_method ==
"quantum_numbers") {
240 if (params.quantum_numbers.empty()) TRIQS_RUNTIME_ERROR <<
"No quantum numbers provided.";
241 if (params.verbosity >= 2)
242 std::cout <<
"Using quantum numbers to partition the local Hilbert space" << std::endl;
243 h_diag = {_h_loc, fops, params.quantum_numbers};
244 }
else if (params.partition_method ==
"none") {
245 std::cout <<
"Not partitioning the local Hilbert space" << std::endl;
246 h_diag = {_h_loc, fops, std::vector<many_body_op_t>{}};
248 TRIQS_RUNTIME_ERROR <<
"Partition method " << params.partition_method <<
" not recognised.";
253 if (params.verbosity >= 2)
254 std::cout <<
"Found " << h_diag.n_subspaces() <<
" subspaces." << std::endl;
256 if (params.performance_analysis) std::ofstream(
"impurity_blocks.dat") << h_diag;
259 if (params.n_warmup_cycles == 0 && params.n_cycles == 0) {
260 if (params.measure_density_matrix) _density_matrix = atomic_density_matrix(h_diag, beta);
265 qmc_data data(beta, params, h_diag, linindex, _Delta_tau, n_inner, histo_map);
267 mc_tools::mc_generic<mc_weight_t>(params.random_name, params.random_seed, params.verbosity);
273 using move_set_type = mc_tools::move_set<mc_weight_t>;
274 move_set_type inserts(qmc.get_rng());
275 move_set_type removes(qmc.get_rng());
276 move_set_type double_inserts(qmc.get_rng());
277 move_set_type double_removes(qmc.get_rng());
279 auto &delta_names = _Delta_tau.block_names();
280 auto get_prob_prop = [¶ms](std::string
const &block_name) {
281 auto f = params.proposal_prob.find(block_name);
282 return (f != params.proposal_prob.end() ? f->second : 1.0);
285 for (
size_t block = 0; block < _Delta_tau.size(); ++block) {
286 int block_size = _Delta_tau[block].data().shape()[1];
287 auto const &block_name = delta_names[block];
288 double prop_prob = get_prob_prop(block_name);
289 inserts.add(move_insert_c_cdag(block, block_size, block_name, data, qmc.get_rng(), histo_map),
290 "Insert Delta_" + block_name, prop_prob);
291 removes.add(move_remove_c_cdag(block, block_size, block_name, data, qmc.get_rng(), histo_map),
292 "Remove Delta_" + block_name, prop_prob);
293 if (params.move_double) {
294 for (
size_t block2 = 0; block2 < _Delta_tau.size(); ++block2) {
295 int block_size2 = _Delta_tau[block2].data().shape()[1];
296 auto const &block_name2 = delta_names[block2];
297 double prop_prob2 = get_prob_prop(block_name2);
299 move_insert_c_c_cdag_cdag(block, block2, block_size, block_size2, block_name,
300 block_name2, data, qmc.get_rng(), histo_map),
301 "Insert Delta_" + block_name +
"_" + block_name2, prop_prob * prop_prob2);
303 move_remove_c_c_cdag_cdag(block, block2, block_size, block_size2, block_name,
304 block_name2, data, qmc.get_rng(), histo_map),
305 "Remove Delta_" + block_name +
"_" + block_name2, prop_prob * prop_prob2);
310 qmc.add_move(std::move(inserts),
"Insert two operators", 1.0);
311 qmc.add_move(std::move(removes),
"Remove two operators", 1.0);
312 if (params.move_double) {
313 qmc.add_move(std::move(double_inserts),
"Insert four operators", 1.0);
314 qmc.add_move(std::move(double_removes),
"Remove four operators", 1.0);
317 if (params.move_shift)
318 qmc.add_move(move_shift_operator(data, qmc.get_rng(), histo_map),
"Shift one operator", 1.0);
320 if (params.move_global.size()) {
321 move_set_type global(qmc.get_rng());
322 for (
auto const &mv : params.move_global) {
323 auto const &name = mv.first;
324 auto const &substitutions = mv.second;
325 global.add(move_global(name, substitutions, data, qmc.get_rng()), name, 1.0);
327 qmc.add_move(std::move(global),
"Global moves", params.move_global_prob);
341 if (params.measure_G2_tau)
342 qmc.add_measure(measure_G2_tau{G2_tau, data, G2_measures},
343 "G2_tau imaginary-time measurement");
347 if (params.measure_G2_iw_nfft)
348 qmc.add_measure(measure_G2_iw_nfft<G2_channel::AllFermionic>{G2_iw_nfft, data, G2_measures},
349 "G2_iw nfft fermionic measurement");
350 if (params.measure_G2_iw_pp_nfft)
351 qmc.add_measure(measure_G2_iw_nfft<G2_channel::PP>{G2_iw_pp_nfft, data, G2_measures},
352 "G2_iw_pp nfft particle-particle measurement");
353 if (params.measure_G2_iw_ph_nfft)
354 qmc.add_measure(measure_G2_iw_nfft<G2_channel::PH>{G2_iw_ph_nfft, data, G2_measures},
355 "G2_iw_ph nfft particle-hole measurement");
359 if (params.measure_G2_iw)
360 qmc.add_measure(measure_G2_iw<G2_channel::AllFermionic>{G2_iw, data, G2_measures},
361 "G2_iw fermionic measurement");
362 if (params.measure_G2_iw_pp)
363 qmc.add_measure(measure_G2_iw<G2_channel::PP>{G2_iw_pp, data, G2_measures},
364 "G2_iw_pp particle-particle measurement");
365 if (params.measure_G2_iw_ph)
366 qmc.add_measure(measure_G2_iw<G2_channel::PH>{G2_iw_ph, data, G2_measures},
367 "G2_iw_ph particle-hole measurement");
370 if (params.measure_G2_iwll_pp)
371 qmc.add_measure(measure_G2_iwll<G2_channel::PP>{G2_iwll_pp, data, G2_measures},
372 "G2_iwll_pp Legendre particle-particle measurement");
373 if (params.measure_G2_iwll_ph)
374 qmc.add_measure(measure_G2_iwll<G2_channel::PH>{G2_iwll_ph, data, G2_measures},
375 "G2_iwll_ph Legendre particle-hole measurement");
381 if (params.measure_O_tau) {
383 const auto &[O1, O2] = *params.measure_O_tau;
384 auto comm_0 = O1 * O2 - O2 * O1;
385 auto comm_1 = O1 * _h_loc - _h_loc * O1;
386 auto comm_2 = O2 * _h_loc - _h_loc * O2;
388 if (!comm_0.is_zero() || !comm_1.is_zero() || !comm_2.is_zero()) {
389 if (params.verbosity >= 2) {
390 TRIQS_RUNTIME_ERROR <<
"Error: measure_O_tau, supplied operators does not commute with "
391 "the local Hamiltonian.\n"
392 <<
"[O1, O2] = " << comm_0 <<
"\n"
393 <<
"[O1, H_loc] = " << comm_1 <<
"\n"
394 <<
"[O2, H_loc] = " << comm_2 <<
"\n";
398 measure_O_tau_ins{O_tau, data, n_tau, O1, O2, params.measure_O_tau_min_ins, qmc.get_rng()},
399 "O_tau insertion measure");
402 if (params.measure_G_tau) {
403 G_tau = block_gf<imtime>{{beta, Fermion, n_tau}, gf_struct};
404 qmc.add_measure(measure_G_tau{data, n_tau, gf_struct, container_set()},
"G_tau measure");
407 if (params.measure_G_l) qmc.add_measure(measure_G_l{G_l, data, n_l, gf_struct},
"G_l measure");
410 if (params.measure_pert_order) {
411 auto &g_names = _Delta_tau.block_names();
412 perturbation_order = histo_map_t{};
413 perturbation_order_total = histogram{};
414 for (
size_t block = 0; block < _Delta_tau.size(); ++block) {
415 auto const &block_name = g_names[block];
416 qmc.add_measure(measure_perturbation_hist(block, data, (*perturbation_order)[block_name]),
"Perturbation order (" + block_name +
")");
418 qmc.add_measure(measure_perturbation_hist_total(data, *perturbation_order_total),
"Perturbation order");
420 if (params.measure_density_matrix) {
421 if (!params.use_norm_as_weight)
422 TRIQS_RUNTIME_ERROR <<
"To measure the density_matrix of atomic states, you need to set "
423 "use_norm_as_weight to True, i.e. to reweight the QMC";
424 qmc.add_measure(measure_density_matrix{data, _density_matrix},
425 "Density Matrix for local static observable");
428 qmc.add_measure(measure_average_sign{data, _average_sign},
"Average sign");
430 qmc.add_measure(
measure_auto_corr_time{data, _auto_corr_time, _auto_corr_time_converged},
"Auto-correlation time");
435 if (std::abs(data.atomic_weight) == 0) TRIQS_RUNTIME_ERROR <<
"Error: Atomic weight of initial configuration is zero";
436 mc_weight_t sign = data.current_sign * data.atomic_weight / std::abs(data.atomic_weight);
438 for (
size_t block = 0; block < _Delta_tau.size(); ++block) {
439 auto det = data.dets[block].determinant();
440 if (std::abs(det) == 0) TRIQS_RUNTIME_ERROR <<
"Error: Determinant of block " << block <<
" is zero";
441 sign *= det / std::abs(det);
448 qmc.warmup_and_accumulate(params.n_warmup_cycles, params.n_cycles, params.length_cycle,
449 triqs::utility::clock_callback(params.max_time), sign);
450 qmc.collect_results(_comm);
453 _last_configuration = data.config;
455 if (params.verbosity >= 2) {
456 std::cout <<
"Average sign: " << _average_sign << std::endl;
457 std::cout <<
"Average order: " << _average_order << std::endl;
458 if (_auto_corr_time_converged)
459 std::cout <<
"Auto-correlation time: " << _auto_corr_time <<
" cycles" << std::endl;
461 std::cout <<
"Auto-correlation time: >~ " << _auto_corr_time <<
" cycles (not converged, run longer)" << std::endl;
465 if (G_tau && G_tau_accum) *G_tau = *G_tau_accum;