From 1e2575e5628411d62fed7f5a626ea36c45a8d418 Mon Sep 17 00:00:00 2001 From: Hao-Xin Wang Date: Fri, 9 Jul 2021 14:57:54 +0800 Subject: [PATCH 01/11] add annotation for two site vmps; change API for single site update sweep params for single site update are separated from that of two site update, renaming as SingleVMPSSweepParams. We support more interfaces for parameters that regulate noises of single site update . --- .../vmps/single_site_update_finite_vmps.h | 86 +++++++++++++++++++ .../single_site_update_finite_vmps_impl.h | 36 ++++---- .../vmps/two_site_update_finite_vmps.h | 5 -- .../vmps/two_site_update_finite_vmps_impl.h | 32 +++++-- include/gqmps2/gqmps2.h | 2 +- .../test_single_site_update_finite_vmps.cc | 36 ++++---- 6 files changed, 148 insertions(+), 49 deletions(-) create mode 100644 include/gqmps2/algorithm/vmps/single_site_update_finite_vmps.h diff --git a/include/gqmps2/algorithm/vmps/single_site_update_finite_vmps.h b/include/gqmps2/algorithm/vmps/single_site_update_finite_vmps.h new file mode 100644 index 0000000..65a2090 --- /dev/null +++ b/include/gqmps2/algorithm/vmps/single_site_update_finite_vmps.h @@ -0,0 +1,86 @@ +// SPDX-License-Identifier: LGPL-3.0-only + +/* +* Author: Hao-Xin Wang +* Rongyang Sun +* Creation Date: 2021-7-9 +* +* Description: GraceQ/MPS2 project. Single-site vMPS algorithm header. +*/ + +/** +@file single_site_update_finite_vmps.h +@brief Single-site update finite size vMPS. +*/ +#ifndef GQMPS2_ALGORITHM_VMPS_SINGLE_SITE_UPDATE_FINITE_VMPS_H +#define GQMPS2_ALGORITHM_VMPS_SINGLE_SITE_UPDATE_FINITE_VMPS_H + + +#include "gqmps2/consts.h" // kMpsPath, kRuntimeTempPath +#include "gqmps2/algorithm/lanczos_solver.h" // LanczParams + +#include // string + + +namespace gqmps2 { +const double kSingleVMPSMaxNoise = 1.0; //maximal noise +const double kSingleVMPSNoiseIncrease = 1.02; +const double kSingleVMPSNoiseDecrease = 0.95; +const double kSingleVMPSAlpha = 0.3; + +struct SingleVMPSSweepParams { + SingleVMPSSweepParams( + const size_t sweeps, + const size_t dmin, const size_t dmax, const double trunc_err, + const LanczosParams &lancz_params, + const std::vector noises = std::vector(1, 0.0), + const double max_noise = kSingleVMPSMaxNoise, + const double noise_increase = kSingleVMPSNoiseIncrease, + const double noise_decrease = kSingleVMPSNoiseDecrease, + const double alpha = kSingleVMPSAlpha, + const std::string mps_path = kMpsPath, + const std::string temp_path = kRuntimeTempPath + ) : + sweeps(sweeps), + Dmin(dmin), Dmax(dmax), trunc_err(trunc_err), + lancz_params(lancz_params), + noises(noises), + max_noise(max_noise), + noise_increase(noise_increase), + noise_decrease(noise_decrease), + alpha(alpha), + mps_path(mps_path), + temp_path(temp_path) {} + + size_t sweeps; + + size_t Dmin; + size_t Dmax; + double trunc_err; + + LanczosParams lancz_params; + + + /// Noise magnitude each sweep + std::vector noises; + double max_noise; + double noise_increase; + double noise_decrease; + double alpha; + + // Advanced parameters + /// MPS directory path + std::string mps_path; + + /// Runtime temporary files directory path + std::string temp_path; + +}; +} /* gqmps2 */ + + +// Implementation details +#include "gqmps2/algorithm/vmps/single_site_update_finite_vmps_impl.h" + + +#endif /* ifndef GQMPS2_ALGORITHM_VMPS_SINGLE_SITE_UPDATE_FINITE_VMPS_H */ diff --git a/include/gqmps2/algorithm/vmps/single_site_update_finite_vmps_impl.h b/include/gqmps2/algorithm/vmps/single_site_update_finite_vmps_impl.h index fd2d138..f9be9f9 100644 --- a/include/gqmps2/algorithm/vmps/single_site_update_finite_vmps_impl.h +++ b/include/gqmps2/algorithm/vmps/single_site_update_finite_vmps_impl.h @@ -15,7 +15,7 @@ #ifndef GQMPS2_ALGORITM_VMPS_ONE_SITE_UPDATE_FINITE_VMPS_IMPL_H #define GQMPS2_ALGORITM_VMPS_ONE_SITE_UPDATE_FINITE_VMPS_IMPL_H -#include "gqmps2/algorithm/vmps/two_site_update_finite_vmps.h" // SweepParams +#include "gqmps2/algorithm/vmps/two_site_update_finite_vmps.h" // helper functions #include "gqmps2/one_dim_tn/mpo/mpo.h" // MPO #include "gqmps2/one_dim_tn/mps/finite_mps/finite_mps.h" // FiniteMPS #include "gqmps2/utilities.h" // IsPathExist, CreatPath @@ -41,11 +41,6 @@ namespace gqmps2 { using std::endl; using std::vector; - const double kMaxNoise = 1.0; //maximal noise - const double kNoiseIncrease = 1.02; - const double kNoiseDecrease = 0.95; - const double kAlpha = 0.3; - // Helpers template inline double MeasureEE(const DTenT &s, const size_t sdim); @@ -98,7 +93,7 @@ template GQTEN_Double SingleSiteFiniteVMPS( FiniteMPS &mps, const MPO> &mpo, - SweepParams &sweep_params + SingleVMPSSweepParams &sweep_params ){ assert(mps.size() == mpo.size()); @@ -125,7 +120,7 @@ GQTEN_Double SingleSiteFiniteVMPS( // the left/right environments if (!IsPathExist(sweep_params.temp_path)) { CreatPath(sweep_params.temp_path); - InitEnvs(mps, mpo, sweep_params, 1); + InitEnvs(mps, mpo, sweep_params.mps_path, sweep_params.temp_path, 1); std::cout << "no exsiting path " < double SingleSiteFiniteVMPSSweep( FiniteMPS &mps, const MPO> &mpo, - const SweepParams &sweep_params, + const SingleVMPSSweepParams &sweep_params, double& noise_start ) { auto N = mps.size(); @@ -170,6 +165,11 @@ double SingleSiteFiniteVMPSSweep( TenVec lenvs(N), renvs(N); double e0(0.0), actual_e0(0.0), actual_laststep_e0(0.0); + const double& alpha = sweep_params.alpha; + const double& noise_decrease = sweep_params.noise_decrease; + const double& noise_increase = sweep_params.noise_increase; + const double& max_noise = sweep_params.max_noise; + double& noise_running = noise_start; for (size_t i = 0; i < N - 1; ++i) { LoadRelatedTensSingleSiteAlg(mps, lenvs, renvs, i, 'r', sweep_params); // note: here we need mps[i](do not need load), @@ -180,11 +180,11 @@ double SingleSiteFiniteVMPSSweep( // expand and truncate let the energy lower or not change // this case is very rare, but include the boundary mps tensor case // so we do nothing now - } else if ((actual_e0 - e0) >= kAlpha*fabs(actual_laststep_e0-e0)) { + } else if ((actual_e0 - e0) >= alpha*fabs(actual_laststep_e0-e0)) { // below two case suppose actual_laststep_e0-laststep_e0>0, usually it is right - noise_running = noise_running*kNoiseDecrease; + noise_running = noise_running*noise_decrease; } else { - noise_running = std::min(noise_running*kNoiseIncrease, kMaxNoise); + noise_running = std::min(noise_running*noise_increase, max_noise); } e0 = SingleSiteFiniteVMPSUpdate( mps, @@ -202,10 +202,10 @@ double SingleSiteFiniteVMPSSweep( LoadRelatedTensSingleSiteAlg(mps, lenvs, renvs, i, 'l', sweep_params); actual_e0 = CalEnergyEptSingleSite(mps, mpo,lenvs, renvs, i); if ((actual_e0 - e0) <= 0.0) { - } else if ((actual_e0 - e0) >= kAlpha*fabs(actual_laststep_e0 - e0)) { - noise_running = noise_running * kNoiseDecrease; + } else if ((actual_e0 - e0) >= alpha*fabs(actual_laststep_e0 - e0)) { + noise_running = noise_running * noise_decrease; } else { - noise_running = std::min(noise_running * kNoiseIncrease, kMaxNoise); + noise_running = std::min(noise_running * noise_increase, max_noise); } e0 = SingleSiteFiniteVMPSUpdate( mps, @@ -237,7 +237,7 @@ double SingleSiteFiniteVMPSUpdate( TenVec> &lenvs, TenVec> &renvs, const MPO> &mpo, - const SweepParams &sweep_params, + const SingleVMPSSweepParams &sweep_params, const char dir, const size_t target_site, const double preset_noise @@ -477,7 +477,7 @@ void LoadRelatedTensSingleSiteAlg( TenVec> &renvs, const size_t target_site, const char dir, - const SweepParams &sweep_params + const SingleVMPSSweepParams &sweep_params ) { auto N = mps.size(); switch (dir) { @@ -542,7 +542,7 @@ void DumpRelatedTensSingleSiteAlg( TenVec> &renvs, const size_t target_site, const char dir, - const SweepParams &sweep_params + const SingleVMPSSweepParams &sweep_params ) { auto N = mps.size(); lenvs.dealloc(target_site); diff --git a/include/gqmps2/algorithm/vmps/two_site_update_finite_vmps.h b/include/gqmps2/algorithm/vmps/two_site_update_finite_vmps.h index b8230fa..aab72f2 100644 --- a/include/gqmps2/algorithm/vmps/two_site_update_finite_vmps.h +++ b/include/gqmps2/algorithm/vmps/two_site_update_finite_vmps.h @@ -29,14 +29,12 @@ struct SweepParams { const size_t sweeps, const size_t dmin, const size_t dmax, const double trunc_err, const LanczosParams &lancz_params, - const std::vector noises = std::vector(1, 0.0), const std::string mps_path = kMpsPath, const std::string temp_path = kRuntimeTempPath ) : sweeps(sweeps), Dmin(dmin), Dmax(dmax), trunc_err(trunc_err), lancz_params(lancz_params), - noises(noises), mps_path(mps_path), temp_path(temp_path) {} @@ -54,9 +52,6 @@ struct SweepParams { /// Runtime temporary files directory path std::string temp_path; - - /// Noise magnitude each sweep - std::vector noises; }; } /* gqmps2 */ diff --git a/include/gqmps2/algorithm/vmps/two_site_update_finite_vmps_impl.h b/include/gqmps2/algorithm/vmps/two_site_update_finite_vmps_impl.h index 66ee5a7..7ca031e 100644 --- a/include/gqmps2/algorithm/vmps/two_site_update_finite_vmps_impl.h +++ b/include/gqmps2/algorithm/vmps/two_site_update_finite_vmps_impl.h @@ -75,7 +75,8 @@ inline void RemoveFile(const std::string &file) { Function to perform two-site update finite vMPS algorithm. @note The input MPS will be considered an empty one. -@note The canonical center of MPS should be set at site 0 or 1. +@note The canonical center of input MPS should be set at site 0 or 1. +@note The canonical center of output MPS is set at site 1. */ template GQTEN_Double TwoSiteFiniteVMPS( @@ -109,32 +110,45 @@ GQTEN_Double TwoSiteFiniteVMPS( true); return e0; } - - template void InitEnvs( FiniteMPS &mps, const MPO> &mpo, const SweepParams &sweep_params, + const unsigned int update_site_num =2){ + InitEnvs( + mps, + mpo, + sweep_params.mps_path, + sweep_params.temp_path, + update_site_num); +} + +template +void InitEnvs( + FiniteMPS &mps, + const MPO> &mpo, + const std::string mps_path, + const std::string temp_path, const unsigned int update_site_num = 2) { using TenT = GQTensor; auto N = mps.size(); TenT renv; //Write a trivial right environment tensor to disk - mps.LoadTen(N-1, GenMPSTenName(sweep_params.mps_path, N-1)); + mps.LoadTen(N-1, GenMPSTenName(mps_path, N-1)); auto mps_trivial_index = mps.back().GetIndexes()[2]; auto mpo_trivial_index_inv = InverseIndex(mpo.back().GetIndexes()[3]); auto mps_trivial_index_inv = InverseIndex(mps_trivial_index); renv = TenT({mps_trivial_index_inv, mpo_trivial_index_inv, mps_trivial_index}); renv({0,0,0}) = 1; - auto file = GenEnvTenName("r", 0, sweep_params.temp_path); + auto file = GenEnvTenName("r", 0, temp_path); WriteGQTensorTOFile(renv, file); //bulk right environment tensors for (size_t i = 1; i <= N - update_site_num; ++i) { - if(i>1){mps.LoadTen(N-i, GenMPSTenName(sweep_params.mps_path, N-i));} - auto file = GenEnvTenName("r", i, sweep_params.temp_path); + if(i>1){mps.LoadTen(N-i, GenMPSTenName(mps_path, N-i));} + auto file = GenEnvTenName("r", i, temp_path); TenT temp1; Contract(&mps[N-i], &renv, {{2}, {0}}, &temp1); renv = TenT(); @@ -148,13 +162,13 @@ void InitEnvs( //Write a trivial left environment tensor to disk TenT lenv; - mps.LoadTen(0, GenMPSTenName(sweep_params.mps_path, 0)); + mps.LoadTen(0, GenMPSTenName(mps_path, 0)); mps_trivial_index = mps.front().GetIndexes()[0]; mpo_trivial_index_inv = InverseIndex(mpo.front().GetIndexes()[0]); mps_trivial_index_inv = InverseIndex(mps_trivial_index); lenv = TenT({mps_trivial_index_inv, mpo_trivial_index_inv, mps_trivial_index}); lenv({0,0,0}) = 1; - file = GenEnvTenName("l", 0, sweep_params.temp_path); + file = GenEnvTenName("l", 0, temp_path); WriteGQTensorTOFile(lenv, file); mps.dealloc(0); diff --git a/include/gqmps2/gqmps2.h b/include/gqmps2/gqmps2.h index 881de4d..a775c73 100644 --- a/include/gqmps2/gqmps2.h +++ b/include/gqmps2/gqmps2.h @@ -32,7 +32,7 @@ // Algorithms #include "gqmps2/algorithm/lanczos_solver.h" // LanczosParams #include "gqmps2/algorithm/vmps/two_site_update_finite_vmps.h" // TwoSiteFiniteVMPS, SweepParams -#include "gqmps2/algorithm/vmps/single_site_update_finite_vmps_impl.h" // SingleSiteFiniteVMPS +#include "gqmps2/algorithm/vmps/single_site_update_finite_vmps.h" // SingleSiteFiniteVMPS #endif /* ifndef GQMPS2_GQMPS2_H */ diff --git a/tests/test_algorithm/test_single_site_update_finite_vmps.cc b/tests/test_algorithm/test_single_site_update_finite_vmps.cc index 6b9f9d4..080e9de 100644 --- a/tests/test_algorithm/test_single_site_update_finite_vmps.cc +++ b/tests/test_algorithm/test_single_site_update_finite_vmps.cc @@ -80,7 +80,7 @@ template void RunTestSingleSiteAlgorithmCase( FiniteMPS &mps, const MPO> &mpo, - SweepParams sweep_params, + SingleVMPSSweepParams sweep_params, const double benmrk_e0, const double precision ) { auto e0 = SingleSiteFiniteVMPS(mps, mpo, sweep_params); @@ -141,7 +141,7 @@ TEST_F(TestSingleSiteAlgorithmSpinSystem, 1DIsing) { } auto dmpo = dmpo_gen.Gen(); - auto sweep_params = SweepParams( + auto sweep_params = SingleVMPSSweepParams( 4, 1, 10, 1.0E-5, LanczosParams(1.0E-7) @@ -172,7 +172,7 @@ TEST_F(TestSingleSiteAlgorithmSpinSystem, 1DIsing) { zmpo_gen.AddTerm(1, {zsz, zsz}, {i, i+1}); } auto zmpo = zmpo_gen.Gen(); - sweep_params = SweepParams( + sweep_params = SingleVMPSSweepParams( 4, 1, 10, 1.0E-5, LanczosParams(1.0E-7) @@ -200,7 +200,7 @@ TEST_F(TestSingleSiteAlgorithmSpinSystem, 1DHeisenberg) { } auto dmpo = dmpo_gen.Gen(); - auto sweep_params = SweepParams( + auto sweep_params = SingleVMPSSweepParams( 6, 8, 8, 1.0E-9, LanczosParams(1.0E-7), @@ -233,7 +233,7 @@ TEST_F(TestSingleSiteAlgorithmSpinSystem, 1DHeisenberg) { } auto zmpo = zmpo_gen.Gen(); - sweep_params = SweepParams( + sweep_params = SingleVMPSSweepParams( 6, 8, 8, 1.0E-9, LanczosParams(1.0E-7), @@ -268,11 +268,15 @@ TEST_F(TestSingleSiteAlgorithmSpinSystem, 2DHeisenberg) { } auto dmpo = dmpo_gen.Gen(); - auto sweep_params = SweepParams( + auto sweep_params = SingleVMPSSweepParams( 6, 8, 8, 1.0E-9, LanczosParams(1.0E-7), - {0.01, 0.001, 0.0001, 0.00001} + {0.01, 0.001, 0.0001, 0.00001}, + 0.3, + 1.01, + 0.8, + 0.1 ); // Test direct product state initialization. @@ -296,7 +300,7 @@ TEST_F(TestSingleSiteAlgorithmSpinSystem, 2DHeisenberg) { } auto zmpo = zmpo_gen.Gen(); - sweep_params = SweepParams( + sweep_params = SingleVMPSSweepParams( 6, 8, 8, 1.0E-9, LanczosParams(1.0E-7), @@ -331,7 +335,7 @@ TEST_F(TestSingleSiteAlgorithmSpinSystem, 2DKitaevSimpleCase) { } auto dmpo = dmpo_gen.Gen(); - auto sweep_params = SweepParams( + auto sweep_params = SingleVMPSSweepParams( 4, 8, 8, 1.0E-4, LanczosParams(1.0E-10), @@ -482,7 +486,7 @@ TEST(TestSingleSiteAlgorithmNoSymmetrySpinSystem, 2DKitaevComplexCase) { was_up = true; } } - auto sweep_params = SweepParams( + auto sweep_params = SingleVMPSSweepParams( 4, 60, 60, 1.0E-4, LanczosParams(1.0E-10), @@ -576,7 +580,7 @@ TEST_F(TestSingleSiteAlgorithmTjSystem2U1Symm, 1DCase) { } auto dmpo = dmpo_gen.Gen(); - auto sweep_params = SweepParams( + auto sweep_params = SingleVMPSSweepParams( 11, 8, 8, 1.0E-9, LanczosParams(1.0E-8, 20), @@ -632,7 +636,7 @@ TEST_F(TestSingleSiteAlgorithmTjSystem2U1Symm, 2DCase) { } auto dmpo = dmpo_gen.Gen(); - auto sweep_params = SweepParams( + auto sweep_params = SingleVMPSSweepParams( 10, 8, 8, 1.0E-9, LanczosParams(1.0E-8, 20), @@ -786,7 +790,7 @@ TEST_F(TestSingleSiteAlgorithmTjSystem1U1Symm, RashbaTermCase) { } auto mpo = mpo_gen.Gen(); - auto sweep_params = SweepParams( + auto sweep_params = SingleVMPSSweepParams( 8, 30, 30, 1.0E-4, LanczosParams(1.0E-14, 100), @@ -950,7 +954,7 @@ TEST_F(TestSingleSiteAlgorithmHubbardSystem, 2Dcase) { } auto dmpo = dmpo_gen.Gen(); - auto sweep_params = SweepParams( + auto sweep_params = SingleVMPSSweepParams( 10, 16, 16, 1.0E-9, LanczosParams(1.0E-8, 20), @@ -1111,7 +1115,7 @@ TEST_F(TestKondoInsulatorSystem, doublechain) { } auto dmpo = dmpo_gen.Gen(); - auto sweep_params = SweepParams( + auto sweep_params = SingleVMPSSweepParams( 5, 64, 64, 1.0E-9, LanczosParams(1.0E-8, 20), @@ -1282,7 +1286,7 @@ TEST_F(TestSingleSiteAlgorithmElectronPhononSystem, holsteinchain) { qn_label=3-qn_label; } DirectStateInitMps(dmps, stat_labs); - auto sweep_params = SweepParams( + auto sweep_params = SingleVMPSSweepParams( 5, 256, 256, 1.0E-10, LanczosParams(1.0E-7), From 666db77fc8a6b49824e4ad7674fb1f8f196f9ce8 Mon Sep 17 00:00:00 2001 From: Rong-Yang Sun Date: Sat, 10 Jul 2021 11:03:30 +0800 Subject: [PATCH 02/11] style(VMPS): Slightly change the coding style --- .../single_site_update_finite_vmps_impl.h | 11 +++----- .../vmps/two_site_update_finite_vmps_impl.h | 27 ++++++++++--------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/include/gqmps2/algorithm/vmps/single_site_update_finite_vmps_impl.h b/include/gqmps2/algorithm/vmps/single_site_update_finite_vmps_impl.h index f9be9f9..d1e6606 100644 --- a/include/gqmps2/algorithm/vmps/single_site_update_finite_vmps_impl.h +++ b/include/gqmps2/algorithm/vmps/single_site_update_finite_vmps_impl.h @@ -37,9 +37,6 @@ namespace gqmps2 { using namespace gqten; - using std::cout; - using std::endl; - using std::vector; // Helpers template @@ -165,10 +162,10 @@ double SingleSiteFiniteVMPSSweep( TenVec lenvs(N), renvs(N); double e0(0.0), actual_e0(0.0), actual_laststep_e0(0.0); - const double& alpha = sweep_params.alpha; - const double& noise_decrease = sweep_params.noise_decrease; - const double& noise_increase = sweep_params.noise_increase; - const double& max_noise = sweep_params.max_noise; + const double alpha = sweep_params.alpha; + const double noise_decrease = sweep_params.noise_decrease; + const double noise_increase = sweep_params.noise_increase; + const double max_noise = sweep_params.max_noise; double& noise_running = noise_start; for (size_t i = 0; i < N - 1; ++i) { diff --git a/include/gqmps2/algorithm/vmps/two_site_update_finite_vmps_impl.h b/include/gqmps2/algorithm/vmps/two_site_update_finite_vmps_impl.h index 7ca031e..1019d41 100644 --- a/include/gqmps2/algorithm/vmps/two_site_update_finite_vmps_impl.h +++ b/include/gqmps2/algorithm/vmps/two_site_update_finite_vmps_impl.h @@ -94,9 +94,7 @@ GQTEN_Double TwoSiteFiniteVMPS( std::cout << "\n"; GQTEN_Double e0; - mps.LoadTen( - 1, - GenMPSTenName(sweep_params.mps_path, 1)); + mps.LoadTen(1, GenMPSTenName(sweep_params.mps_path, 1)); for (size_t sweep = 1; sweep <= sweep_params.sweeps; ++sweep) { std::cout << "sweep " << sweep << std::endl; Timer sweep_timer("sweep"); @@ -104,33 +102,36 @@ GQTEN_Double TwoSiteFiniteVMPS( sweep_timer.PrintElapsed(); std::cout << "\n"; } - mps.DumpTen( - 1, - GenMPSTenName(sweep_params.mps_path, 1), - true); + mps.DumpTen(1, GenMPSTenName(sweep_params.mps_path, 1), true); return e0; } + + template void InitEnvs( FiniteMPS &mps, const MPO> &mpo, const SweepParams &sweep_params, - const unsigned int update_site_num =2){ + const unsigned int update_site_num = 2 +){ InitEnvs( mps, mpo, sweep_params.mps_path, sweep_params.temp_path, - update_site_num); + update_site_num + ); } + template void InitEnvs( FiniteMPS &mps, const MPO> &mpo, const std::string mps_path, const std::string temp_path, - const unsigned int update_site_num = 2) { + const unsigned int update_site_num = 2 +) { using TenT = GQTensor; auto N = mps.size(); @@ -141,13 +142,13 @@ void InitEnvs( auto mpo_trivial_index_inv = InverseIndex(mpo.back().GetIndexes()[3]); auto mps_trivial_index_inv = InverseIndex(mps_trivial_index); renv = TenT({mps_trivial_index_inv, mpo_trivial_index_inv, mps_trivial_index}); - renv({0,0,0}) = 1; + renv({0, 0, 0}) = 1; auto file = GenEnvTenName("r", 0, temp_path); WriteGQTensorTOFile(renv, file); //bulk right environment tensors for (size_t i = 1; i <= N - update_site_num; ++i) { - if(i>1){mps.LoadTen(N-i, GenMPSTenName(mps_path, N-i));} + if (i>1) { mps.LoadTen(N-i, GenMPSTenName(mps_path, N-i)); } auto file = GenEnvTenName("r", i, temp_path); TenT temp1; Contract(&mps[N-i], &renv, {{2}, {0}}, &temp1); @@ -167,7 +168,7 @@ void InitEnvs( mpo_trivial_index_inv = InverseIndex(mpo.front().GetIndexes()[0]); mps_trivial_index_inv = InverseIndex(mps_trivial_index); lenv = TenT({mps_trivial_index_inv, mpo_trivial_index_inv, mps_trivial_index}); - lenv({0,0,0}) = 1; + lenv({0, 0, 0}) = 1; file = GenEnvTenName("l", 0, temp_path); WriteGQTensorTOFile(lenv, file); mps.dealloc(0); From 830ba65574bb6ed175d2bf8bb30ae8b8386a8263 Mon Sep 17 00:00:00 2001 From: Rong-Yang Sun Date: Sat, 10 Jul 2021 11:17:04 +0800 Subject: [PATCH 03/11] fix(SingleSiteFiniteVMPS): Fix missing include file --- .../vmps/single_site_update_finite_vmps_impl.h | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/include/gqmps2/algorithm/vmps/single_site_update_finite_vmps_impl.h b/include/gqmps2/algorithm/vmps/single_site_update_finite_vmps_impl.h index d1e6606..c636b09 100644 --- a/include/gqmps2/algorithm/vmps/single_site_update_finite_vmps_impl.h +++ b/include/gqmps2/algorithm/vmps/single_site_update_finite_vmps_impl.h @@ -15,14 +15,15 @@ #ifndef GQMPS2_ALGORITM_VMPS_ONE_SITE_UPDATE_FINITE_VMPS_IMPL_H #define GQMPS2_ALGORITM_VMPS_ONE_SITE_UPDATE_FINITE_VMPS_IMPL_H -#include "gqmps2/algorithm/vmps/two_site_update_finite_vmps.h" // helper functions -#include "gqmps2/one_dim_tn/mpo/mpo.h" // MPO -#include "gqmps2/one_dim_tn/mps/finite_mps/finite_mps.h" // FiniteMPS -#include "gqmps2/utilities.h" // IsPathExist, CreatPath -#include "gqmps2/one_dim_tn/framework/ten_vec.h" // TenVec +#include "gqmps2/algorithm/vmps/single_site_update_finite_vmps.h" // SingleVMPSSweepParams +#include "gqmps2/algorithm/vmps/two_site_update_finite_vmps.h" // helper functions +#include "gqmps2/one_dim_tn/mpo/mpo.h" // MPO +#include "gqmps2/one_dim_tn/mps/finite_mps/finite_mps.h" // FiniteMPS +#include "gqmps2/utilities.h" // IsPathExist, CreatPath +#include "gqmps2/one_dim_tn/framework/ten_vec.h" // TenVec #include "gqmps2/consts.h" #include "gqten/gqten.h" -#include "gqten/utility/timer.h" // Timer +#include "gqten/utility/timer.h" // Timer #include #include From e194506e1bac874e51dad7390c9425ccbe1f1c72 Mon Sep 17 00:00:00 2001 From: Rong-Yang Sun Date: Sat, 10 Jul 2021 11:18:51 +0800 Subject: [PATCH 04/11] refactor(TwoSiteFiniteVMPS): replace unsigned int to size_t --- .../vmps/two_site_update_finite_vmps_impl.h | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/include/gqmps2/algorithm/vmps/two_site_update_finite_vmps_impl.h b/include/gqmps2/algorithm/vmps/two_site_update_finite_vmps_impl.h index 1019d41..caa46b0 100644 --- a/include/gqmps2/algorithm/vmps/two_site_update_finite_vmps_impl.h +++ b/include/gqmps2/algorithm/vmps/two_site_update_finite_vmps_impl.h @@ -112,7 +112,7 @@ void InitEnvs( FiniteMPS &mps, const MPO> &mpo, const SweepParams &sweep_params, - const unsigned int update_site_num = 2 + const size_t update_site_num = 2 ){ InitEnvs( mps, @@ -130,7 +130,7 @@ void InitEnvs( const MPO> &mpo, const std::string mps_path, const std::string temp_path, - const unsigned int update_site_num = 2 + const size_t update_site_num = 2 ) { using TenT = GQTensor; auto N = mps.size(); @@ -464,10 +464,12 @@ void DumpRelatedTensTwoSiteAlg( mps.DumpTen( target_site, GenMPSTenName(sweep_params.mps_path, target_site), - true); + true + ); lenvs.DumpTen( target_site + 1, - GenEnvTenName("l", target_site + 1, sweep_params.temp_path)); + GenEnvTenName("l", target_site + 1, sweep_params.temp_path) + ); }break; case 'l':{ lenvs.dealloc((target_site+1) - 2); @@ -475,11 +477,13 @@ void DumpRelatedTensTwoSiteAlg( mps.DumpTen( target_site, GenMPSTenName(sweep_params.mps_path, target_site), - true); + true + ); auto next_renv_len = N - target_site; renvs.DumpTen( next_renv_len, - GenEnvTenName("r", next_renv_len, sweep_params.temp_path)); + GenEnvTenName("r", next_renv_len, sweep_params.temp_path) + ); }break; default: assert(false); From 23fd927ef71b92ce2cdc8e905ad9f6bdca54849a Mon Sep 17 00:00:00 2001 From: Hao-Xin Wang Date: Sun, 25 Jul 2021 12:20:27 +0800 Subject: [PATCH 05/11] Update lanczos_solver_impl.h For consistent with update of Timer --- include/gqmps2/algorithm/lanczos_solver_impl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/gqmps2/algorithm/lanczos_solver_impl.h b/include/gqmps2/algorithm/lanczos_solver_impl.h index fa79904..ead6049 100644 --- a/include/gqmps2/algorithm/lanczos_solver_impl.h +++ b/include/gqmps2/algorithm/lanczos_solver_impl.h @@ -178,7 +178,7 @@ LanczosRes LanczosSolver( bases[m] = gamma; #ifdef GQMPS2_TIMING_MODE - mat_vec_timer.Restart(); + mat_vec_timer.ClearAndRestart(); #endif last_mat_mul_vec_res = (*eff_ham_mul_state)(rpeff_ham, bases[m]); From cddeda5f27113619aeca8a5a6a9c37f63ebccd16 Mon Sep 17 00:00:00 2001 From: Hao-Xin Wang Date: Fri, 30 Jul 2021 18:40:00 +0800 Subject: [PATCH 06/11] Update .gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index dfd9b2d..d6c3bdd 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,5 @@ external/gqten/ *swo .DS_Store .ycm_extra_conf.py +.vscode/c_cpp_properties.json +.vscode/settings.json From 51331a4397b2c3a8fa14b3a3e788df6ac685350e Mon Sep 17 00:00:00 2001 From: Hao-Xin Wang Date: Sun, 1 Aug 2021 19:27:45 +0800 Subject: [PATCH 07/11] Update a better CMakeList; refinement single site update --- CMakeLists.txt | 23 +++++++++++----- .../single_site_update_finite_vmps_impl.h | 26 +++++++++++-------- tests/CMakeLists.txt | 4 --- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8446473..6e942d7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,7 +33,9 @@ endif() option(GQMPS2_BUILD_UNITTEST "Build unittests for GraceQ/mps2." OFF) -option(GQMPS2_BUILD_GQTEN_USE_EXTERNAL_HPTT_LIB "Use external hptt library when building dependency external/gqten." OFF) +option(TEST_BUILD_USE_EXTERNAL_GQTEN "Use external gqten library when building unittests for GraceQ/mps2." OFF) + +option(TEST_BUILD_USE_EXTERNAL_HPTT_LIB "Use external hptt library when building unittests for GraceQ/mps2." OFF) # Compilation and linking control. set(CMAKE_CXX_STANDARD 14) @@ -59,13 +61,20 @@ add_subdirectory(script) # Build unittests. if(GQMPS2_BUILD_UNITTEST) - # Build dependencies (GraceQ/tensor). - if(GQMPS2_BUILD_GQTEN_USE_EXTERNAL_HPTT_LIB) - option(GQTEN_USE_EXTERNAL_HPTT_LIB "Set related option in external/gqten" ON) - set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/external/gqten/cmake/Modules/") + if(NOT TEST_BUILD_USE_EXTERNAL_GQTEN) + add_subdirectory(external) + set(GQMPS2_TENSOR_LIB_HEADER_PATH "${PROJECT_SOURCE_DIR}/external/gqten/include") + else() + find_path(GQMPS2_TENSOR_LIB_HEADER_PATH "gqten") + endif() + + if(NOT TEST_BUILD_USE_EXTERNAL_HPTT_LIB) + set(hptt_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/external/gqten/external/hptt/include") + set(hptt_LIBRARY "${CMAKE_BINARY_DIR}/external/gqten/external/hptt/libhptt.a") + else() + find_path(hptt_INCLUDE_DIR "hptt.h") + find_library(hptt_LIBRARY "libhptt.a") endif() - add_subdirectory(external) - set(GQMPS2_TENSOR_LIB_HEADER_PATH "${PROJECT_SOURCE_DIR}/external/gqten/include") enable_testing() find_package(GTest REQUIRED) diff --git a/include/gqmps2/algorithm/vmps/single_site_update_finite_vmps_impl.h b/include/gqmps2/algorithm/vmps/single_site_update_finite_vmps_impl.h index c636b09..63ecf1e 100644 --- a/include/gqmps2/algorithm/vmps/single_site_update_finite_vmps_impl.h +++ b/include/gqmps2/algorithm/vmps/single_site_update_finite_vmps_impl.h @@ -414,11 +414,14 @@ double SingleSiteFiniteVMPSUpdate( return lancz_res.gs_eng; } - +/** + SingleSiteFiniteVMPSExpand Function + @note gs_vec will be changed when dir=='r' +*/ template void SingleSiteFiniteVMPSExpand( FiniteMPS &mps, - const GQTensor *gs_vec, + GQTensor *gs_vec, const std::vector< GQTensor *> &eff_ham, const char dir, const size_t target_site, @@ -432,12 +435,13 @@ void SingleSiteFiniteVMPSExpand( size_t next_site = target_site + 1; Contract(eff_ham[0], gs_vec, {{0}, {0}}, ten_tmp); InplaceContract(ten_tmp, eff_ham[1], {{0, 2}, {0, 1}}); - ten_tmp->Transpose({0, 2, 1, 3}); - TenT expanded_ten = FuseIndex(*ten_tmp, 2, 3); - expanded_ten = noise * expanded_ten;; - Expand(gs_vec, &expanded_ten, {2}, mps(target_site)); + ten_tmp->FuseIndex(1,3); + (*ten_tmp) *= noise; + gs_vec->Transpose({2,0,1}); + Expand(gs_vec, ten_tmp, {0}, mps(target_site)); + mps(target_site)->Transpose({1,2,0}); - auto expanded_index = InverseIndex(expanded_ten.GetIndexes()[2]); + auto expanded_index = InverseIndex(ten_tmp->GetIndexes()[0]); TenT expanded_zero_ten = TenT({ expanded_index, mps[next_site].GetIndexes()[1], @@ -451,11 +455,11 @@ void SingleSiteFiniteVMPSExpand( Contract(gs_vec, eff_ham[2], {{2}, {0}}, ten_tmp); InplaceContract(ten_tmp, eff_ham[1], {{1, 2}, {1, 3}}); ten_tmp->Transpose({0, 2, 3, 1}); - TenT expanded_ten = FuseIndex(*ten_tmp, 0, 1); - expanded_ten = noise*expanded_ten; - Expand(gs_vec, &expanded_ten, {0}, mps(target_site)); + ten_tmp->FuseIndex(0,1); + (*ten_tmp) *= noise; + Expand(gs_vec, ten_tmp, {0}, mps(target_site)); - auto expanded_index = InverseIndex(expanded_ten.GetIndexes()[0]); + auto expanded_index = InverseIndex(ten_tmp->GetIndexes()[0]); TenT expanded_zero_ten = TenT({ mps[next_site].GetIndexes()[0], mps[next_site].GetIndexes()[1], diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3e1ba31..b5941f7 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -5,10 +5,6 @@ # # Description: GraceQ/MPS2 project. CMake file to control unittest. # -if(NOT GQMPS2_BUILD_GQTEN_USE_EXTERNAL_HPTT_LIB) - set(hptt_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/external/gqten/external/hptt/include") - set(hptt_LIBRARY "${CMAKE_BINARY_DIR}/external/gqten/external/hptt/libhptt.a") -endif() # Set MKL compile flags and link flags. if(CMAKE_CXX_COMPILER_ID MATCHES "Intel") set(MATH_LIB_COMPILE_FLAGS "-I$ENV{MKLROOT}/include") From bbf2cebbfdd1839ac9691a3d622873dc6371e6f1 Mon Sep 17 00:00:00 2001 From: Hao-Xin Wang Date: Sun, 1 Aug 2021 20:52:12 +0800 Subject: [PATCH 08/11] update CMakeList --- tests/CMakeLists.txt | 44 ++++++++++++++----- .../test_single_site_update_finite_vmps.cc | 2 +- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b5941f7..12ab3e8 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -6,18 +6,38 @@ # Description: GraceQ/MPS2 project. CMake file to control unittest. # # Set MKL compile flags and link flags. -if(CMAKE_CXX_COMPILER_ID MATCHES "Intel") - set(MATH_LIB_COMPILE_FLAGS "-I$ENV{MKLROOT}/include") - set(MATH_LIB_LINK_FLAGS -Wl,--start-group $ENV{MKLROOT}/lib/intel64/libmkl_intel_lp64.a $ENV{MKLROOT}/lib/intel64/libmkl_intel_thread.a $ENV{MKLROOT}/lib/intel64/libmkl_core.a -Wl,--end-group -liomp5 -lpthread -lm -ldl) -endif() -if(CMAKE_CXX_COMPILER_ID MATCHES "GNU") - set(MATH_LIB_COMPILE_FLAGS -m64 -I$ENV{MKLROOT}/include) - # Link the Intel's OpenMP library to avoid performance issue when the library calls the MKL's gesdd function. - set(MATH_LIB_LINK_FLAGS -Wl,--start-group $ENV{MKLROOT}/lib/intel64/libmkl_intel_lp64.a $ENV{MKLROOT}/lib/intel64/libmkl_intel_thread.a $ENV{MKLROOT}/lib/intel64/libmkl_core.a -Wl,--end-group -L$ENV{MKLROOT}/lib/intel64 -liomp5 -lpthread -lm -ldl) -endif() -if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") - set(MATH_LIB_COMPILE_FLAGS -m64 -I$ENV{MKLROOT}/include) - set(MATH_LIB_LINK_FLAGS -Wl,--start-group $ENV{MKLROOT}/lib/intel64/libmkl_intel_lp64.a $ENV{MKLROOT}/lib/intel64/libmkl_intel_thread.a $ENV{MKLROOT}/lib/intel64/libmkl_core.a -Wl,--end-group -L$ENV{MKLROOT}/lib/intel64 -liomp5 -lpthread -lm -ldl) + +if(APPLE) + if(CMAKE_CXX_COMPILER_ID MATCHES "Intel") + set(MATH_LIB_COMPILE_FLAGS "-I$ENV{MKLROOT}/include") + #Need test + set(MATH_LIB_LINK_FLAGS $ENV{MKLROOT}/lib/libmkl_intel_lp64.a $ENV{MKLROOT}/lib/libmkl_intel_thread.a $ENV{MKLROOT}/lib/libmkl_core.a -liomp5 -lpthread -lm -ldl) + endif() + if(CMAKE_CXX_COMPILER_ID MATCHES "GNU") + set(MATH_LIB_COMPILE_FLAGS -m64 -I$ENV{MKLROOT}/include) + # May not work + set(MATH_LIB_LINK_FLAGS $ENV{MKLROOT}/lib/libmkl_intel_lp64.a $ENV{MKLROOT}/lib/libmkl_intel_thread.a $ENV{MKLROOT}/lib/libmkl_core.a -L$ENV{MKLROOT}/lib -L$ENV{CMPLR_ROOT}/mac/compiler/lib/ -liomp5 -lpthread -lm -ldl) + endif() + if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") + set(MATH_LIB_COMPILE_FLAGS -m64 -I$ENV{MKLROOT}/include) + # Note as of Intel oneAPI 2021.2, "source /opt/intel/oneapi/compiler/latest/env/vars.sh" + set(MATH_LIB_LINK_FLAGS $ENV{MKLROOT}/lib/libmkl_intel_lp64.a $ENV{MKLROOT}/lib/libmkl_intel_thread.a $ENV{MKLROOT}/lib/libmkl_core.a -L$ENV{MKLROOT}/lib -L$ENV{CMPLR_ROOT}/mac/compiler/lib/ -Wl, -rpath $ENV{CMPLR_ROOT}/mac/compiler/lib/libiomp5.dylib -liomp5 -lpthread -lm -ldl) + endif() +elseif(UNIX) + #UNIX include APPLE, but we except it here + if(CMAKE_CXX_COMPILER_ID MATCHES "Intel") + set(MATH_LIB_COMPILE_FLAGS "-I$ENV{MKLROOT}/include") + set(MATH_LIB_LINK_FLAGS -Wl,--start-group $ENV{MKLROOT}/lib/intel64/libmkl_intel_lp64.a $ENV{MKLROOT}/lib/intel64/libmkl_intel_thread.a $ENV{MKLROOT}/lib/intel64/libmkl_core.a -Wl,--end-group -liomp5 -lpthread -lm -ldl) + endif() + if(CMAKE_CXX_COMPILER_ID MATCHES "GNU") + set(MATH_LIB_COMPILE_FLAGS -m64 -I$ENV{MKLROOT}/include) + # Link the Intel's OpenMP library to avoid performance issue when the library calls the MKL's gesdd function. + set(MATH_LIB_LINK_FLAGS -Wl,--start-group $ENV{MKLROOT}/lib/intel64/libmkl_intel_lp64.a $ENV{MKLROOT}/lib/intel64/libmkl_intel_thread.a $ENV{MKLROOT}/lib/intel64/libmkl_core.a -Wl,--end-group -L$ENV{MKLROOT}/lib/intel64 -liomp5 -lpthread -lm -ldl) + endif() + if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") + set(MATH_LIB_COMPILE_FLAGS -m64 -I$ENV{MKLROOT}/include) + set(MATH_LIB_LINK_FLAGS -Wl,--start-group $ENV{MKLROOT}/lib/intel64/libmkl_intel_lp64.a $ENV{MKLROOT}/lib/intel64/libmkl_intel_thread.a $ENV{MKLROOT}/lib/intel64/libmkl_core.a -Wl,--end-group -L$ENV{MKLROOT}/lib/intel64 -liomp5 -lpthread -lm -ldl) + endif() endif() diff --git a/tests/test_algorithm/test_single_site_update_finite_vmps.cc b/tests/test_algorithm/test_single_site_update_finite_vmps.cc index 080e9de..ea8c188 100644 --- a/tests/test_algorithm/test_single_site_update_finite_vmps.cc +++ b/tests/test_algorithm/test_single_site_update_finite_vmps.cc @@ -1289,7 +1289,7 @@ TEST_F(TestSingleSiteAlgorithmElectronPhononSystem, holsteinchain) { auto sweep_params = SingleVMPSSweepParams( 5, 256, 256, 1.0E-10, - LanczosParams(1.0E-7), + LanczosParams(1.0E-9), {0.1, 0.1, 0.01, 0.001, 0.0001} ); dmps.Dump(sweep_params.mps_path, true); From 845e9782e7835e675901c748318789e90280f656 Mon Sep 17 00:00:00 2001 From: Hao-Xin Wang Date: Mon, 2 Aug 2021 16:02:23 +0800 Subject: [PATCH 09/11] add "inline" to regular function --- include/gqmps2/one_dim_tn/mpo/mpogen/fsm.h | 18 ++++++------- .../mpo/mpogen/symb_alg/coef_op_alg.h | 26 +++++++++---------- include/gqmps2/one_dim_tn/mps/mps.h | 2 +- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/include/gqmps2/one_dim_tn/mpo/mpogen/fsm.h b/include/gqmps2/one_dim_tn/mpo/mpogen/fsm.h index 1093be0..845cdaf 100644 --- a/include/gqmps2/one_dim_tn/mpo/mpogen/fsm.h +++ b/include/gqmps2/one_dim_tn/mpo/mpogen/fsm.h @@ -28,13 +28,13 @@ struct FSMNode { using FSMNodeVec = std::vector; -bool operator==(const FSMNode &lhs, const FSMNode &rhs) { +inline bool operator==(const FSMNode &lhs, const FSMNode &rhs) { return (lhs.fsm_site_idx == rhs.fsm_site_idx) && (lhs.fsm_stat_idx == rhs.fsm_stat_idx); } -bool operator!=(const FSMNode &lhs, const FSMNode &rhs) { +inline bool operator!=(const FSMNode &lhs, const FSMNode &rhs) { return !(lhs == rhs); } @@ -106,7 +106,7 @@ const long kFSMReadyStatIdx = 0; const long kFSMFinalStatIdx = -1; -void FSM::AddPath( +inline void FSM::AddPath( const size_t head_ntrvl_site_idx, const size_t tail_ntrvl_site_idx, const OpReprVec &ntrvl_ops) { assert( @@ -148,7 +148,7 @@ void FSM::AddPath( } -SparOpReprMatVec FSM::GenMatRepr(void) const { +inline SparOpReprMatVec FSM::GenMatRepr(void) const { auto fsm_site_dims = CalcFSMSiteDims_(); auto final_stat_dim_idxs = CalcFinalStatDimIdxs_(fsm_site_dims); SparOpReprMatVec fsm_mat_repr; @@ -164,7 +164,7 @@ SparOpReprMatVec FSM::GenMatRepr(void) const { } -std::vector FSM::CalcFSMSiteDims_(void) const { +inline std::vector FSM::CalcFSMSiteDims_(void) const { std::vector fsm_site_dims(fsm_site_num_, 0); for (size_t i = 0; i < fsm_site_num_; ++i) { auto fsm_site_dim = mid_stat_nums_[i]; @@ -176,7 +176,7 @@ std::vector FSM::CalcFSMSiteDims_(void) const { } -std::vector FSM::CalcFinalStatDimIdxs_( +inline std::vector FSM::CalcFinalStatDimIdxs_( const std::vector &fsm_site_dims) const { std::vector final_stat_dim_idxs(fsm_site_num_, -1); for (size_t i = 0; i < fsm_site_num_; ++i) { @@ -192,7 +192,7 @@ std::vector FSM::CalcFinalStatDimIdxs_( } -void FSM::CastFSMPathToMatRepr_( +inline void FSM::CastFSMPathToMatRepr_( const FSMPath &fsm_path, const std::vector &final_stat_dim_idxs, SparOpReprMatVec &fsm_mat_repr) const { @@ -229,7 +229,7 @@ void FSM::CastFSMPathToMatRepr_( } -SparOpReprMatVec FSM::GenCompressedMatRepr(void) const { +inline SparOpReprMatVec FSM::GenCompressedMatRepr(void) const { auto comp_mat_repr = GenMatRepr(); for (size_t i = 0; i < phys_site_num_-1; ++i) { SparOpReprMatColCompresser(comp_mat_repr[i], comp_mat_repr[i+1]); @@ -241,7 +241,7 @@ SparOpReprMatVec FSM::GenCompressedMatRepr(void) const { } -void FSM::ReplaceIdOpLabels(std::vector &new_id_op_labels) { +inline void FSM::ReplaceIdOpLabels(std::vector &new_id_op_labels) { assert(new_id_op_labels.size() == id_op_labels_.size()); id_op_labels_ = new_id_op_labels; } diff --git a/include/gqmps2/one_dim_tn/mpo/mpogen/symb_alg/coef_op_alg.h b/include/gqmps2/one_dim_tn/mpo/mpogen/symb_alg/coef_op_alg.h index b5eaba2..1d5f3a8 100644 --- a/include/gqmps2/one_dim_tn/mpo/mpogen/symb_alg/coef_op_alg.h +++ b/include/gqmps2/one_dim_tn/mpo/mpogen/symb_alg/coef_op_alg.h @@ -244,7 +244,7 @@ const OpRepr kIdOpRepr = OpRepr(kIdOpLabel); // Operator representation for ide using OpReprVec = std::vector; -std::pair SeparateCoefAndBase(const OpRepr &op_repr) { +inline std::pair SeparateCoefAndBase(const OpRepr &op_repr) { auto term_num = op_repr.coef_repr_list_.size(); if (term_num == 0) { return std::make_pair(kNullCoefRepr, kNullOpRepr); @@ -264,7 +264,7 @@ std::pair SeparateCoefAndBase(const OpRepr &op_repr) { } -CoefRepr GetOpReprCoef(const OpRepr &op_repr) { +inline CoefRepr GetOpReprCoef(const OpRepr &op_repr) { return SeparateCoefAndBase(op_repr).first; } @@ -480,7 +480,7 @@ using SparOpReprMatVec = std::vector; // Incomplete multiplication for SparMat. -OpRepr CoefReprOpReprIncompleteMulti(const CoefRepr &coef, const OpRepr &op) { +inline OpRepr CoefReprOpReprIncompleteMulti(const CoefRepr &coef, const OpRepr &op) { if (op == kNullOpRepr) { return kNullOpRepr; } if (coef == kIdCoefRepr) { return op; } for (auto &c : op.coef_repr_list_) { @@ -494,7 +494,7 @@ OpRepr CoefReprOpReprIncompleteMulti(const CoefRepr &coef, const OpRepr &op) { } -void SparCoefReprMatSparOpReprMatIncompleteMultiKernel( +inline void SparCoefReprMatSparOpReprMatIncompleteMultiKernel( const SparCoefReprMat &coef_mat, const SparOpReprMat &op_mat, const size_t coef_mat_row_idx, const size_t op_mat_col_idx, SparOpReprMat &res) { @@ -513,7 +513,7 @@ void SparCoefReprMatSparOpReprMatIncompleteMultiKernel( } -void SparOpReprMatSparCoefReprMatIncompleteMultiKernel( +inline void SparOpReprMatSparCoefReprMatIncompleteMultiKernel( const SparOpReprMat &op_mat, const SparCoefReprMat &coef_mat, const size_t op_mat_row_idx, const size_t coef_mat_col_idx, SparOpReprMat &res) { @@ -532,7 +532,7 @@ void SparOpReprMatSparCoefReprMatIncompleteMultiKernel( } -SparOpReprMat SparCoefReprMatSparOpReprMatIncompleteMulti( +inline SparOpReprMat SparCoefReprMatSparOpReprMatIncompleteMulti( const SparCoefReprMat &coef_mat, const SparOpReprMat &op_mat) { assert(coef_mat.cols == op_mat.rows); SparOpReprMat res(coef_mat.rows, op_mat.cols); @@ -546,7 +546,7 @@ SparOpReprMat SparCoefReprMatSparOpReprMatIncompleteMulti( } -SparOpReprMat SparOpReprMatSparCoefReprMatIncompleteMulti( +inline SparOpReprMat SparOpReprMatSparCoefReprMatIncompleteMulti( const SparOpReprMat &op_mat, const SparCoefReprMat &coef_mat) { assert(op_mat.cols == coef_mat.rows); SparOpReprMat res(op_mat.rows, coef_mat.cols); @@ -562,7 +562,7 @@ SparOpReprMat SparOpReprMatSparCoefReprMatIncompleteMulti( // Row and column delinearization. /* TODO: So bad implementation, need refactor. */ -OpReprVec CalcSparOpReprMatRowLinCmb( +inline OpReprVec CalcSparOpReprMatRowLinCmb( const SparOpReprMat &m, const CoefReprVec &cmb) { auto work_row_num = cmb.size(); assert(work_row_num > 0); @@ -595,7 +595,7 @@ OpReprVec CalcSparOpReprMatRowLinCmb( } -OpReprVec CalcSparOpReprMatColLinCmb( +inline OpReprVec CalcSparOpReprMatColLinCmb( const SparOpReprMat &m, const CoefReprVec &cmb) { auto work_col_num = cmb.size(); assert(work_col_num > 0); @@ -628,7 +628,7 @@ OpReprVec CalcSparOpReprMatColLinCmb( } -void SparOpReprMatRowDelinearize( +inline void SparOpReprMatRowDelinearize( SparOpReprMat &target, SparOpReprMat &follower) { auto row_num = target.rows; size_t i; @@ -660,7 +660,7 @@ void SparOpReprMatRowDelinearize( } -void SparOpReprMatColDelinearize( +inline void SparOpReprMatColDelinearize( SparOpReprMat &target, SparOpReprMat &follower) { auto col_num = target.cols; size_t i; @@ -693,7 +693,7 @@ void SparOpReprMatColDelinearize( // Row and column compresser. -void SparOpReprMatRowCompresser( +inline void SparOpReprMatRowCompresser( SparOpReprMat &target, SparOpReprMat &follower) { assert(target.rows == follower.cols); auto row_num = target.rows; @@ -725,7 +725,7 @@ void SparOpReprMatRowCompresser( } -void SparOpReprMatColCompresser( +inline void SparOpReprMatColCompresser( SparOpReprMat &target, SparOpReprMat &follower) { assert(target.cols == follower.rows); auto col_num = target.cols; diff --git a/include/gqmps2/one_dim_tn/mps/mps.h b/include/gqmps2/one_dim_tn/mps/mps.h index a21c968..96da694 100644 --- a/include/gqmps2/one_dim_tn/mps/mps.h +++ b/include/gqmps2/one_dim_tn/mps/mps.h @@ -28,7 +28,7 @@ using namespace gqten; // Helpers -std::string GenMPSTenName(const std::string &mps_path, const size_t idx) { +inline std::string GenMPSTenName(const std::string &mps_path, const size_t idx) { return mps_path + "/" + kMpsTenBaseName + std::to_string(idx) + "." + kGQTenFileSuffix; } From 7ba0f39a78a10f9acf98b560d588ccf2bd36bee1 Mon Sep 17 00:00:00 2001 From: Hao-Xin Wang Date: Wed, 4 Aug 2021 13:24:57 +0800 Subject: [PATCH 10/11] Update single_site_update_finite_vmps_impl.h remove fuse index function, add more timing_mode --- .../single_site_update_finite_vmps_impl.h | 97 +++++++++++-------- 1 file changed, 55 insertions(+), 42 deletions(-) diff --git a/include/gqmps2/algorithm/vmps/single_site_update_finite_vmps_impl.h b/include/gqmps2/algorithm/vmps/single_site_update_finite_vmps_impl.h index 63ecf1e..839194b 100644 --- a/include/gqmps2/algorithm/vmps/single_site_update_finite_vmps_impl.h +++ b/include/gqmps2/algorithm/vmps/single_site_update_finite_vmps_impl.h @@ -43,44 +43,6 @@ namespace gqmps2 { template inline double MeasureEE(const DTenT &s, const size_t sdim); - // TODO: Need improve! - /** Fuse two indices of a tensor t. we suppose i - GQTensor FuseIndex( - const GQTensor& t, - size_t i, size_t j - ) { - assert(i index1 = t.GetIndexes()[i]; - Index index2 = t.GetIndexes()[j]; - - GQTensor combiner = IndexCombine( - InverseIndex(index1), - InverseIndex(index2), - index1.GetDir() - ); - GQTensor fused_tensor; - Contract(&t, &combiner, {{i, j}, {0, 1}}, &fused_tensor); - size_t fused_ten_rank = fused_tensor.Rank(); - std::vector axs(fused_ten_rank); - for (size_t k = 0; k < fused_ten_rank; k++) { - if (k < i) { - axs[k] = k; - } else if (k == i) { - axs[k] = fused_ten_rank - 1; - } else { - axs[k] = k - 1; - } - } - fused_tensor.Transpose(axs); - return fused_tensor; - } - - /** Function to perform single-site update finite vMPS algorithm. @@ -242,9 +204,6 @@ double SingleSiteFiniteVMPSUpdate( ) { Timer update_timer("single_site_fvmps_update"); -#ifdef GQMPS2_TIMING_MODE - Timer preprocessing_timer("single_site_fvmps_preprocessing"); -#endif double noise = preset_noise; auto N = mps.size(); size_t lenv_len = target_site; @@ -286,7 +245,7 @@ double SingleSiteFiniteVMPSUpdate( #endif #ifdef GQMPS2_TIMING_MODE - Timer expand_timer("single_site_fvmps_expand"); + Timer expand_timer("single_site_fvmps_add_noise"); #endif bool need_expand(true); @@ -432,11 +391,26 @@ void SingleSiteFiniteVMPSExpand( TenT* ten_tmp = new TenT(); mps(target_site) = new TenT(); // we suppose mps only contain mps[next_site] if (dir=='r') { +#ifdef GQMPS2_TIMING_MODE + Timer contract_timer("single_site_fvmps_add_noise_contract"); +#endif size_t next_site = target_site + 1; Contract(eff_ham[0], gs_vec, {{0}, {0}}, ten_tmp); InplaceContract(ten_tmp, eff_ham[1], {{0, 2}, {0, 1}}); +#ifdef GQMPS2_TIMING_MODE + contract_timer.PrintElapsed(); + Timer fuse_index_timer("single_site_fvmps_add_noise_fuse_index"); +#endif ten_tmp->FuseIndex(1,3); +#ifdef GQMPS2_TIMING_MODE + fuse_index_timer.PrintElapsed(); + Timer scalar_multip_timer("single_site_fvmps_add_noise_scalar_multiplication"); +#endif (*ten_tmp) *= noise; +#ifdef GQMPS2_TIMING_MODE + scalar_multip_timer.PrintElapsed(); + Timer expansion_timer("single_site_fvmps_add_noise_expansion"); +#endif gs_vec->Transpose({2,0,1}); Expand(gs_vec, ten_tmp, {0}, mps(target_site)); mps(target_site)->Transpose({1,2,0}); @@ -450,13 +424,31 @@ void SingleSiteFiniteVMPSExpand( Expand(mps(next_site), &expanded_zero_ten, {0}, ten_tmp); delete mps(next_site); mps(next_site) = ten_tmp; +#ifdef GQMPS2_TIMING_MODE + expansion_timer.PrintElapsed(); +#endif } else if (dir=='l') { +#ifdef GQMPS2_TIMING_MODE + Timer contract_timer("single_site_fvmps_add_noise_contract"); +#endif size_t next_site = target_site - 1; Contract(gs_vec, eff_ham[2], {{2}, {0}}, ten_tmp); InplaceContract(ten_tmp, eff_ham[1], {{1, 2}, {1, 3}}); +#ifdef GQMPS2_TIMING_MODE + contract_timer.PrintElapsed(); + Timer fuse_index_timer("single_site_fvmps_add_noise_fuse_index"); +#endif ten_tmp->Transpose({0, 2, 3, 1}); ten_tmp->FuseIndex(0,1); +#ifdef GQMPS2_TIMING_MODE + fuse_index_timer.PrintElapsed(); + Timer scalar_multip_timer("single_site_fvmps_add_noise_scalar_multiplication"); +#endif (*ten_tmp) *= noise; +#ifdef GQMPS2_TIMING_MODE + scalar_multip_timer.PrintElapsed(); + Timer expansion_timer("single_site_fvmps_add_noise_expansion"); +#endif Expand(gs_vec, ten_tmp, {0}, mps(target_site)); auto expanded_index = InverseIndex(ten_tmp->GetIndexes()[0]); @@ -468,6 +460,9 @@ void SingleSiteFiniteVMPSExpand( Expand(mps(next_site), &expanded_zero_ten, {2}, ten_tmp); delete mps(next_site); mps(next_site) = ten_tmp; +#ifdef GQMPS2_TIMING_MODE + expansion_timer.PrintElapsed(); +#endif } } @@ -481,6 +476,9 @@ void LoadRelatedTensSingleSiteAlg( const char dir, const SingleVMPSSweepParams &sweep_params ) { +#ifdef GQMPS2_TIMING_MODE + Timer preprocessing_timer("single_site_fvmps_preprocessing"); +#endif auto N = mps.size(); switch (dir) { case 'r': @@ -534,6 +532,9 @@ void LoadRelatedTensSingleSiteAlg( default: assert(false); } +#ifdef GQMPS2_TIMING_MODE + preprocessing_timer.PrintElapsed(); +#endif } @@ -546,6 +547,9 @@ void DumpRelatedTensSingleSiteAlg( const char dir, const SingleVMPSSweepParams &sweep_params ) { +#ifdef GQMPS2_TIMING_MODE + Timer postprocessing_timer("single_site_fvmps_postprocessing"); +#endif auto N = mps.size(); lenvs.dealloc(target_site); renvs.dealloc(N - target_site - 1); @@ -568,6 +572,9 @@ void DumpRelatedTensSingleSiteAlg( default: assert(false); } +#ifdef GQMPS2_TIMING_MODE + postprocessing_timer.PrintElapsed(); +#endif } @@ -579,6 +586,9 @@ double CalEnergyEptSingleSite( TenVec> &renvs, const size_t target_site ) { +#ifdef GQMPS2_TIMING_MODE + Timer cal_energy_timer("single_site_fvmps_pre_caluclate_energy"); +#endif using TenT = GQTensor; std::vector eff_ham(3); size_t lenv_len = target_site; @@ -593,6 +603,9 @@ double CalEnergyEptSingleSite( Contract(h_mul_state, &mps_ten_dag, {{0, 1, 2}, {0, 1, 2}}, &scalar_ten); delete h_mul_state; double energy = Real(scalar_ten()); +#ifdef GQMPS2_TIMING_MODE + cal_energy_timer.PrintElapsed(); +#endif return energy; } } /* gqmps2 */ From 64e8b181383af532f445dc83d0bf5228f208f968 Mon Sep 17 00:00:00 2001 From: Hao-Xin Wang Date: Fri, 20 May 2022 09:46:48 +0800 Subject: [PATCH 11/11] Fix operator=(&,&&) bug for derived class of DuoVector --- .../gqmps2/one_dim_tn/framework/duovector.h | 34 ++++++++++++++----- include/gqmps2/one_dim_tn/framework/ten_vec.h | 14 ++++++++ tests/test_one_dim_tn/test_duovector.cc | 6 ++-- .../test_finite_mps/test_finite_mps.cc | 6 ++-- 4 files changed, 47 insertions(+), 13 deletions(-) diff --git a/include/gqmps2/one_dim_tn/framework/duovector.h b/include/gqmps2/one_dim_tn/framework/duovector.h index e593b48..c621fff 100644 --- a/include/gqmps2/one_dim_tn/framework/duovector.h +++ b/include/gqmps2/one_dim_tn/framework/duovector.h @@ -50,7 +50,7 @@ class DuoVector { @param duovec A DuoVector instance. */ - DuoVector(const DuoVector &duovec) : raw_data_(duovec.size(), nullptr) { + DuoVector(const DuoVector &duovec) : raw_data_(duovec.size(), nullptr) { for (size_t i = 0; i < duovec.size(); ++i) { if (duovec(i) != nullptr) { raw_data_[i] = new ElemT(duovec[i]); @@ -63,10 +63,17 @@ class DuoVector { @param rhs A DuoVector instance. */ - DuoVector &operator=(const DuoVector &rhs) { - ~DuoVector(); - raw_data_ = std::vector(rhs.size(), nullptr); - for (size_t i = 0; i < rhs[i]; ++i) { + DuoVector &operator=(const DuoVector &rhs) { +// this->DuoVector::~DuoVector(); +// I'm no sure why above line do not work if used in the derived FiniteMPS class + for (auto &rpelem : raw_data_) { + if (rpelem != nullptr) { + delete rpelem; + } + } + const size_t N = rhs.size(); + raw_data_ = std::vector(N, nullptr); + for (size_t i = 0; i < N; ++i) { if (rhs(i) != nullptr) { raw_data_[i] = new ElemT(rhs[i]); } @@ -80,17 +87,26 @@ class DuoVector { @param duovec A DuoVector instance. */ DuoVector( - DuoVector &&duovec - ) noexcept : raw_data_(std::move(duovec.raw_data_)) {} + DuoVector &&duovec + ) noexcept : raw_data_(std::move(duovec.raw_data_)) { + duovec.raw_data_ = std::vector(duovec.size(), nullptr); + } /** Move a DuoVector. @param rhs A DuoVector instance. */ - DuoVector &operator=(DuoVector &&rhs) noexcept { - ~DuoVector(); + DuoVector &operator=(DuoVector &&rhs) noexcept { +// this->DuoVector::~DuoVector(); +// I have no idea why above line do not work if used in the derived FiniteMPS class + for (auto &rpelem : raw_data_) { + if (rpelem != nullptr) { + delete rpelem; + } + } raw_data_ = std::move(rhs.raw_data_); + rhs.raw_data_ = std::vector(rhs.size(), nullptr); return *this; } diff --git a/include/gqmps2/one_dim_tn/framework/ten_vec.h b/include/gqmps2/one_dim_tn/framework/ten_vec.h index 81c1a1a..854ce1f 100644 --- a/include/gqmps2/one_dim_tn/framework/ten_vec.h +++ b/include/gqmps2/one_dim_tn/framework/ten_vec.h @@ -34,6 +34,9 @@ A fix size tensor vector. template class TenVec : public DuoVector { public: + + //No default constructor + /** Create a TenVec using its size. @@ -41,6 +44,17 @@ class TenVec : public DuoVector { */ TenVec(const size_t size) : DuoVector(size) {} +// /** +// * Copy constructor +// */ +// TenVec(const TenVec &rhs) : DuoVector( (DuoVector&)rhs ) {} + +// TenVec &operator=(const TenVec &rhs) { +// (*this) = rhs; +// return *this; +// } +// + /** Load element tensor from a file. diff --git a/tests/test_one_dim_tn/test_duovector.cc b/tests/test_one_dim_tn/test_duovector.cc index 8701ffe..5d9c661 100644 --- a/tests/test_one_dim_tn/test_duovector.cc +++ b/tests/test_one_dim_tn/test_duovector.cc @@ -40,13 +40,15 @@ void RunTestDuoVectorConstructorsCase(const size_t size) { EXPECT_EQ(duovec_moved[i], duovec[i]); } - auto duovec_copy2 = duovec; + DuoVector duovec_copy2; + duovec_copy2 = duovec; for (size_t i = 0; i < size; ++i) { EXPECT_EQ(duovec_copy2[i], duovec[i]); EXPECT_NE(duovec_copy2(i), duovec(i)); } auto craw_data_copy2 = duovec_copy2.cdata(); - auto duovec_moved2 = std::move(duovec_copy2); + DuoVector duovec_moved2; + duovec_moved2 = std::move(duovec_copy2); for (size_t i = 0; i < size; ++i) { EXPECT_EQ(duovec_moved2(i), craw_data_copy2[i]); EXPECT_EQ(duovec_moved2[i], duovec[i]); diff --git a/tests/test_one_dim_tn/test_finite_mps/test_finite_mps.cc b/tests/test_one_dim_tn/test_finite_mps/test_finite_mps.cc index f0defc5..f226d81 100644 --- a/tests/test_one_dim_tn/test_finite_mps/test_finite_mps.cc +++ b/tests/test_one_dim_tn/test_finite_mps/test_finite_mps.cc @@ -175,7 +175,8 @@ TEST_F(TestMPS, TestCopyAndMove) { EXPECT_NE(crmps_copy(i), crmps(i)); } - MPST mps_copy2 = mps; + MPST mps_copy2(mps.GetSitesInfo()); + mps_copy2 = mps; const MPST &crmps_copy2 = mps_copy2; EXPECT_EQ(mps_copy2.GetCenter(), mps.GetCenter()); for (size_t i = 0; i < mps.size(); ++i) { @@ -195,7 +196,8 @@ TEST_F(TestMPS, TestCopyAndMove) { } auto craw_data_copy2 = mps_copy2.cdata(); - MPST mps_move2 = std::move(mps_copy2); + MPST mps_move2(mps_copy2.GetSitesInfo()); + mps_move2 = std::move(mps_copy2); const MPST &crmps_move2 = mps_move2; EXPECT_EQ(mps_move2.GetCenter(), mps.GetCenter()); for (size_t i = 0; i < mps.size(); ++i) {