Skip to content

Quick Start

Julia

To install XDiag, enter the package mode in the Julia REPL using ] and type

add XDiag

That's it! Now we are ready to perform our first exact diagonalization. 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\).

using XDiag

let
    say_hello()
    N = 16
    nup = N รท 2
    block = Spinhalf(N, nup)

    # Define the nearest-neighbor Heisenberg model
    ops = OpSum()
    for i in 1:N
        ops += "J" * Op("SdotS", [i, mod1(i+1, N)])
    end
    ops["J"] = 1.0

    set_verbosity(2)            # set verbosity for monitoring progress
    e0 = eigval0(ops, block)    # compute ground state energy

    println("Ground state energy: $e0")
end

For more examples, see our Example Collection.

C++

Using XDiag with C++, we first need to compile the XDiag library, see the Library compilation instructions. Then, our application code looks like this:

#include <xdiag/all.hpp>

using namespace xdiag;

int main() try {
  say_hello();
  int N = 16;
  int nup = N / 2;
  Spinhalf block(N, nup);

  // Define the nearest-neighbor Heisenberg model
  OpSum ops;
  for (int i = 0; i < N; ++i) {
    ops += "J" * Op("SdotS", {i, (i + 1) % N});
  }
  ops["J"] = 1.0;

  set_verbosity(2);                  // set verbosity for monitoring progress
  double e0 = eigval0(ops, block); // compute ground state energy

  Log("Ground state energy: {:.12f}", e0);
} catch (Error e) {
  error_trace(e);
}

The try / catch clause implements an error trace mechanism, which we recommend for every XDiag application. The application is then compiled as well, see Application compilation instructions, resulting in our final executable.

The C++ versions offers the opportunity to optimize compilation the code for the target architecture, see C++ optimization