From 9afc1954ae1f2a86d5a05842645798f02a8450d0 Mon Sep 17 00:00:00 2001 From: Alexander Wietek Date: Fri, 29 Mar 2024 16:30:21 +0100 Subject: [PATCH] Fixed norm_estimation bug with transpose --- CMakeLists.txt | 9 +++- docs/index.md | 3 +- docs/installation.md | 2 +- docs/javascripts/katex.js | 11 ++++ docs/quickstart.md | 53 +++++++++++++++++++ .../spectrum/spinhalf_chain_e0/CMakeLists.txt | 13 ++--- examples/spectrum/spinhalf_chain_e0/main.cpp | 14 ++--- examples/spectrum/spinhalf_chain_e0/run.sh | 2 - hydra/algorithms/norm_estimate.cpp | 8 +-- misc/data/toml/write.toml | 8 +-- mkdocs.yml | 14 ++++- tests/algorithms/test_norm_estimate.cpp | 2 +- 12 files changed, 108 insertions(+), 31 deletions(-) create mode 100644 docs/javascripts/katex.js delete mode 100755 examples/spectrum/spinhalf_chain_e0/run.sh diff --git a/CMakeLists.txt b/CMakeLists.txt index f1e51336..f2e908f3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -92,8 +92,13 @@ add_subdirectory(hydra) # target_include_directories( # ${HYDRA_LIBRARY} PUBLIC "$") - - +# Add sanitizers to debug mode +if (CMAKE_BUILD_TYPE STREQUAL "Debug") + message(STATUS "Adding Sanitizers to debug build") + target_compile_options(${HYDRA_LIBRARY} PUBLIC -fsanitize=address) + target_link_options(${HYDRA_LIBRARY} PUBLIC -fsanitize=address) +endif() + ######################################################### # Testing if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) diff --git a/docs/index.md b/docs/index.md index e2948c02..1d581b19 100644 --- a/docs/index.md +++ b/docs/index.md @@ -2,7 +2,8 @@ title: Home --- -[Code on GitHub](https://github.com/awietek/hydra){ .md-button .md-button--primary } +[Quick Start](quickstart.md){ .md-button .md-button--primary } +[Code on GitHub](https://github.com/awietek/hydra){ .md-button .md-button--secondary } ## Overview Hydra is a C++ library for performing Exact Diagonalizations of quantum many-body systems. Key features include optimized combinatorical algorithms for navigating Hilbert spaces, iterative algorithms for solving large-scale eigenvalue problems, and efficient parallelization. \ No newline at end of file diff --git a/docs/installation.md b/docs/installation.md index d066b25f..3c694781 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -21,7 +21,7 @@ in a second step. Here we explain how to compile the library. - **Download** the source code using [git](https://git-scm.com/) ```bash - cd /path/where/hydra/should/be + cd /path/to/where/hydra/should/be git clone https://github.com/awietek/hydra.git ``` diff --git a/docs/javascripts/katex.js b/docs/javascripts/katex.js new file mode 100644 index 00000000..7254740c --- /dev/null +++ b/docs/javascripts/katex.js @@ -0,0 +1,11 @@ +document$.subscribe(({ body }) => { + renderMathInElement(body, { + delimiters: [ + { left: "$$", right: "$$", display: true }, + { left: "$", right: "$", display: false }, + { left: "\\(", right: "\\)", display: false }, + { left: "\\[", right: "\\]", display: true } + ], + }) +}) + diff --git a/docs/quickstart.md b/docs/quickstart.md index 9f195a59..3b3ee9a3 100644 --- a/docs/quickstart.md +++ b/docs/quickstart.md @@ -37,6 +37,15 @@ target_link_libraries(main PUBLIC hydra::hydra) You should replace `"/path/to/hydra/install"` with the appropriate directory where your hydra library is installed after compilation. This exact `CMakeLists.txt` file can be used to compile any hydra application. +!!! info + + For using the distributed hydra library the last line of the above + `CMakeLists.txt` should be changed to + + ```cmake + target_link_libraries(main PUBLIC hydra::hydra_distributed) + ``` + We then compile the application code, ```bash @@ -48,4 +57,48 @@ and finally run our first `hydra` application. ```bash ./build/main +``` + +## Computing the ground state energy of a spin chain + +We compute the ground state energy of the $S=1/2$ Heisenberg chain on +a periodic chain lattice in one dimension. The Hamiltonian is given by + +$$ H = J\sum_{\langle i,j \rangle} \mathbf{S}_i \cdot \mathbf{S}_j$$ + +where $\mathbf{S}_i = (S_i^x, S_i^y, S_i^z)$ are the spin $S=1/2$ operators +and $\langle i,j \rangle$ denotes summation over nearest-meighbor sites +$i$ and $j$. + +The following code, sets up the Hilbert space, defines the Hamiltonian and finally calls an iterative eigenvalue solver to compute the ground state energy. + +```C++ +#include + +using namespace hydra; + +int main() try { + + int n_sites = 16; + int nup = n_sites / 2; + + // Define the Hilbert space block + auto block = Spinhalf(n_sites, nup); + + // Define the nearest-neighbor Heisenberg Hamiltonian + BondList bonds; + for (int i = 0; i < n_sites; ++i) { + bonds << Bond("HB", "J", {i, (i + 1) % n_sites}); + } + + // Set the coupling constant "J" to one + bonds["J"] = 1.0; + + // Compute and print the ground state energy + double e0 = eigval0(bonds, block); + Log("Ground state energy: {:.12f}", e0); + +} catch (std::exception const &e) { + traceback(e); +} ``` \ No newline at end of file diff --git a/examples/spectrum/spinhalf_chain_e0/CMakeLists.txt b/examples/spectrum/spinhalf_chain_e0/CMakeLists.txt index e63095ff..622716df 100644 --- a/examples/spectrum/spinhalf_chain_e0/CMakeLists.txt +++ b/examples/spectrum/spinhalf_chain_e0/CMakeLists.txt @@ -1,17 +1,10 @@ -cmake_minimum_required(VERSION 3.15) +cmake_minimum_required(VERSION 3.19) project( spinhalf_chain_e0 - VERSION 1.0 LANGUAGES CXX ) +find_package(hydra REQUIRED HINTS "../../../install") add_executable(main main.cpp) - -# set hydra compile options -find_package(hydra REQUIRED HINTS "../../..") -target_compile_features(main PUBLIC cxx_std_17) -target_compile_definitions(main PUBLIC ${HYDRA_DEFINITIONS}) -target_link_libraries(main PUBLIC ${HYDRA_LIBRARIES}) -target_include_directories(main PUBLIC ${HYDRA_INCLUDE_DIRS}) -set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE) +target_link_libraries(main PUBLIC hydra::hydra) diff --git a/examples/spectrum/spinhalf_chain_e0/main.cpp b/examples/spectrum/spinhalf_chain_e0/main.cpp index e3bdbf99..2ac0a26b 100644 --- a/examples/spectrum/spinhalf_chain_e0/main.cpp +++ b/examples/spectrum/spinhalf_chain_e0/main.cpp @@ -1,7 +1,8 @@ #include -int main() { - using namespace hydra; +using namespace hydra; + +int main() try { int n_sites = 16; int nup = n_sites / 2; @@ -19,8 +20,9 @@ int main() { bonds["J"] = 1.0; // Compute and print the ground state energy - double e0 = eig0(bonds, block); - HydraPrint(e0); - - return EXIT_SUCCESS; + double e0 = eigval0(bonds, block); + Log("Ground state energy: {:.12f}", e0); + +} catch (std::exception const &e) { + traceback(e); } diff --git a/examples/spectrum/spinhalf_chain_e0/run.sh b/examples/spectrum/spinhalf_chain_e0/run.sh deleted file mode 100755 index 0d1b1e4e..00000000 --- a/examples/spectrum/spinhalf_chain_e0/run.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -./build/main diff --git a/hydra/algorithms/norm_estimate.cpp b/hydra/algorithms/norm_estimate.cpp index a024546b..e09cadd8 100644 --- a/hydra/algorithms/norm_estimate.cpp +++ b/hydra/algorithms/norm_estimate.cpp @@ -34,7 +34,6 @@ double norm_estimate(apply_A_f &&apply_A, apply_A_T_f &&apply_A_T, // size: size of the local vector (only different from dim when distributed) // n_max_attempts: number of attempts to compute norm // seed: random seed - using vec_t = arma::Col; arma::arma_rng::set_seed(seed); @@ -49,6 +48,7 @@ double norm_estimate(apply_A_f &&apply_A, apply_A_T_f &&apply_A_T, double gamma = norm1(v); vec_t xsi = arma::sign(v); + vec_t x = apply_A_T(xsi); for (int k = 2; k <= n_max_attempts; ++k) { @@ -159,13 +159,15 @@ template double norm_estimate(arma::Mat const &A, int64_t n_max_attempts, uint64_t seed) try { auto apply_A = [&A](arma::Col const &v) { return A * v; }; - auto apply_A_T = [&A](arma::Col const &v) { return A.t() * v; }; + auto apply_A_T = [&A](arma::Col const &v) { + arma::Col vv = A.t() * v; + return vv; + }; auto norm_f = [](arma::Col const &v) { return arma::norm(v); }; auto norm1_f = [](arma::Col const &v) { return arma::norm(v, 1); }; auto norminf_f = [](arma::Col const &v) { return arma::norm(v, "inf"); }; - return norm_estimate(apply_A, apply_A_T, norm_f, norm1_f, norminf_f, A.n_cols, A.n_cols, n_max_attempts, seed); diff --git a/misc/data/toml/write.toml b/misc/data/toml/write.toml index 5b57d193..9cde6334 100644 --- a/misc/data/toml/write.toml +++ b/misc/data/toml/write.toml @@ -85,9 +85,9 @@ Interactions = [ J2 = [ 0.20000000000000001, -0.10000000000000001 ] [val.Matrices] - M1 = [ [ 0.79732180985488055, 0.1355332846831904, 0.73516601319588104, 0.41860636804976031 ], [ 0.80312197559847109, 0.68939140322247461, 0.39540759771917738, 0.43170335089266304 ], [ 0.34269518352060446, 0.12595650321969371, 0.44317490854419939, 0.50066787600139129 ] ] + M1 = [ [ 0.30367459066893016, 0.71838874534410724, 0.66539208003128136, 0.36382487673163311 ], [ 0.67976836228170079, 0.62725303943722832, 0.81333864473551687, 0.5106046794697916 ], [ 0.99462789056497347, 0.24398141209374263, 0.94431020354008677, 0.082190567952135973 ] ] M2 = [ - [ [ 0.1791206186337736, 0.88975387448583809 ], [ 0.21512726152070999, 0.67979684725473355 ], [ 0.77522421524070462, 0.82908801774997587 ], [ 0.20683337468004126, 0.02885882262782662 ] ], - [ [ 0.39735236715110961, 0.21977955504770136 ], [ 0.71615667873532263, 0.47979250968982179 ], [ 0.1497534840587052, 0.96482587591276703 ], [ 0.33491113797069327, 0.10597685615575372 ] ], - [ [ 0.27977271791393138, 0.28821155395318343 ], [ 0.16378389323660844, 0.35137542158147639 ], [ 0.61187045316641075, 0.51652887118831337 ], [ 0.98176760414055741, 0.41448834555982389 ] ] + [ [ 0.56912712589617065, 0.57492789455975146 ], [ 0.40884385499371151, 0.0015935368544022022 ], [ 0.041313364914633999, 0.99491524637039819 ], [ 0.98363794553077266, 0.77195016620740142 ] ], + [ [ 0.4582741277664284, 0.72367184734967172 ], [ 0.51083114899608828, 0.16464190187830452 ], [ 0.62492418744842304, 0.12792224099774505 ], [ 0.086113530496655433, 0.34116840324223446 ] ], + [ [ 0.56584819814236309, 0.10707551053179018 ], [ 0.35359294493942683, 0.38079734518128167 ], [ 0.57998380222325274, 0.28915184239432967 ], [ 0.049945850362084016, 0.51456964055908372 ] ] ] \ No newline at end of file diff --git a/mkdocs.yml b/mkdocs.yml index e6320b13..5a396ac6 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -1,6 +1,8 @@ site_name: Hydra Documentation theme: name: material + palette: + scheme: slate markdown_extensions: - attr_list @@ -8,4 +10,14 @@ markdown_extensions: - pymdownx.highlight: anchor_linenums: true auto_title: false - - pymdownx.superfences \ No newline at end of file + - pymdownx.superfences + - pymdownx.arithmatex: + generic: true + +extra_javascript: + - javascripts/katex.js + - https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.7/katex.min.js + - https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.7/contrib/auto-render.min.js + +extra_css: + - https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.7/katex.min.css \ No newline at end of file diff --git a/tests/algorithms/test_norm_estimate.cpp b/tests/algorithms/test_norm_estimate.cpp index d2d26aca..e9f17fbf 100644 --- a/tests/algorithms/test_norm_estimate.cpp +++ b/tests/algorithms/test_norm_estimate.cpp @@ -57,7 +57,7 @@ TEST_CASE("norm_estimate", "[algorithms]") { using namespace hydra; using namespace arma; - Log.set_verbosity(2); + Log.set_verbosity(1); for (int n = 50; n <= 350; n += 50) { Log("norm_estimate for random real non-hermitian matrices, D={}", n);