Skip to content

Commit

Permalink
Fixed norm_estimation bug with transpose
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Wietek committed Mar 29, 2024
1 parent b74ef96 commit 9afc195
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 31 deletions.
9 changes: 7 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,13 @@ add_subdirectory(hydra)
# target_include_directories(
# ${HYDRA_LIBRARY} PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>")



# 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)
Expand Down
3 changes: 2 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
11 changes: 11 additions & 0 deletions docs/javascripts/katex.js
Original file line number Diff line number Diff line change
@@ -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 }
],
})
})

53 changes: 53 additions & 0 deletions docs/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 <hydra/all.h>

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);
}
```
13 changes: 3 additions & 10 deletions examples/spectrum/spinhalf_chain_e0/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
14 changes: 8 additions & 6 deletions examples/spectrum/spinhalf_chain_e0/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include <hydra/all.h>

int main() {
using namespace hydra;
using namespace hydra;

int main() try {

int n_sites = 16;
int nup = n_sites / 2;
Expand All @@ -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);
}
2 changes: 0 additions & 2 deletions examples/spectrum/spinhalf_chain_e0/run.sh

This file was deleted.

8 changes: 5 additions & 3 deletions hydra/algorithms/norm_estimate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<coeff_t>;

arma::arma_rng::set_seed(seed);
Expand All @@ -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) {
Expand Down Expand Up @@ -159,13 +159,15 @@ template <typename coeff_t>
double norm_estimate(arma::Mat<coeff_t> const &A, int64_t n_max_attempts,
uint64_t seed) try {
auto apply_A = [&A](arma::Col<coeff_t> const &v) { return A * v; };
auto apply_A_T = [&A](arma::Col<coeff_t> const &v) { return A.t() * v; };
auto apply_A_T = [&A](arma::Col<coeff_t> const &v) {
arma::Col<coeff_t> vv = A.t() * v;
return vv;
};
auto norm_f = [](arma::Col<coeff_t> const &v) { return arma::norm(v); };
auto norm1_f = [](arma::Col<coeff_t> const &v) { return arma::norm(v, 1); };
auto norminf_f = [](arma::Col<coeff_t> const &v) {
return arma::norm(v, "inf");
};

return norm_estimate<coeff_t>(apply_A, apply_A_T, norm_f, norm1_f, norminf_f,
A.n_cols, A.n_cols, n_max_attempts, seed);

Expand Down
8 changes: 4 additions & 4 deletions misc/data/toml/write.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ] ]
]
14 changes: 13 additions & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
site_name: Hydra Documentation
theme:
name: material
palette:
scheme: slate

markdown_extensions:
- attr_list
- admonition
- pymdownx.highlight:
anchor_linenums: true
auto_title: false
- pymdownx.superfences
- 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
2 changes: 1 addition & 1 deletion tests/algorithms/test_norm_estimate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 9afc195

Please sign in to comment.