Skip to content

Commit

Permalink
Extended the Julia wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Wietek committed Feb 5, 2025
1 parent 53a29f6 commit 0f2af21
Show file tree
Hide file tree
Showing 17 changed files with 629 additions and 407 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ project(
LANGUAGES CXX
)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_EXTENSIONS OFF)

option(BUILD_TESTING "Build the tests" Off)
Expand Down Expand Up @@ -88,6 +87,8 @@ else()
add_subdirectory(xdiag)
endif()

target_compile_features(${XDIAG_LIBRARY} PRIVATE cxx_std_17)

# Generate the export header for SomeLib and attach it to the target
include(GenerateExportHeader)
generate_export_header(${XDIAG_LIBRARY} EXPORT_FILE_NAME include/xdiag/export.hpp)
Expand Down
16 changes: 15 additions & 1 deletion docs/documentation/operators/hc.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ title: hc

Returns the hermitian conjugate $\mathcal{O}^\dagger$ of an operator $\mathcal{O}$ represented by an [Op](op.md) or [OpSum](opsum.md) object. Please note the details when conjugating complex couplings, outlined in [OpSum # Complex couplings](opsum.md#complex-couplings).

**Sources** [hc.hpp](https://github.com/awietek/xdiag/blob/main/xdiag/operators/logic/hc.hpp), [hc.cpp](https://github.com/awietek/xdiag/blob/main/xdiag/operators/logic/hc.cpp)
**Sources**<br>
[hc.hpp](https://github.com/awietek/xdiag/blob/main/xdiag/operators/logic/hc.hpp)<br>
[hc.cpp](https://github.com/awietek/xdiag/blob/main/xdiag/operators/logic/hc.cpp)<br>
[hc.jl](https://github.com/awietek/XDiag.jl/blob/main/xdiag/operators/logic/hc.jl)

---

Expand All @@ -15,6 +18,12 @@ Returns the hermitian conjugate $\mathcal{O}^\dagger$ of an operator $\mathcal{O
Op hc(Op const &op)
OpSum hc(OpSum const &ops)
```
=== "Julia"
```julia
hc(op::OpSum)
hc(ops::OpSum)
```

---

## Usage Example
Expand All @@ -23,3 +32,8 @@ Returns the hermitian conjugate $\mathcal{O}^\dagger$ of an operator $\mathcal{O
```c++
--8<-- "examples/usage_examples/main.cpp:hc"
```

=== "Julia"
```julia
--8<-- "examples/usage_examples/main.jl:hc"
```
36 changes: 35 additions & 1 deletion docs/documentation/operators/op.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ title: Op

Object describing a single linear operator acting on a Hilbert space.

**Sources** [op.hpp](https://github.com/awietek/xdiag/blob/main/xdiag/operators/op.hpp), [op.cpp](https://github.com/awietek/xdiag/blob/main/xdiag/operators/op.cpp)
**Sources**<br>
[op.hpp](https://github.com/awietek/xdiag/blob/main/xdiag/operators/op.hpp)<br>
[op.cpp](https://github.com/awietek/xdiag/blob/main/xdiag/operators/op.cpp)<br>
[op.jl](https://github.com/awietek/XDiag.jl/blob/main/src/operators/op.jl)

---

Expand Down Expand Up @@ -70,6 +73,37 @@ Returns whether an [Op](op.md) is a real operator.
```
---

#### isapprox

Returns whether two Ops are approximately equal.

=== "C++"
```c++
bool isapprox(Op const &op1, OpSum const &op2, double rtol = 1e-12,
double atol = 1e-12);
```

=== "Julia"
```julia
isapprox(op1::Op, op2::Op, rtol::Float64=1e-12, atol::Float64=1e-12)
```
---

#### to_string (operator<<)

Converts the Op to a readable string representation.

=== "C++"
```c++
std::string to_string(Op const &op);
std::ostream &operator<<(std::ostream &out, Op const &op);
```

=== "Julia"
```julia
to_string(op::Op)
```
---
## Usage Example

=== "C++"
Expand Down
48 changes: 24 additions & 24 deletions docs/documentation/operators/operator_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,30 @@ title: Operator types

Generic operators in XDiag are represented as [OpSum](opsum.md) objects made up of a coupling, which can be a real/complex number or a string, and [Op](op.md) objects. Every [Op](op.md) is defined by a `type`. Here we list all the available types implemented in XDiag, their required number of sites, and the blocks for which they are available.

| Type | Description | No. of sites | Blocks |
|:------------------|:--------------------------------------------------------------------------------------------------------------------------------------------------------|--------------|------------------------------------------------------------|
| `Hop` | A hopping term for $\uparrow$ and $\downarrow$ spins of the form $$ \textcolor{red}{-}\sum_{\sigma=\uparrow\downarrow} (c^\dagger_{i\sigma}c_{j\sigma} + \textrm{h.c.})$$ | 2 | tJ, Electron, tJDistributed |
| `Hopup` | A hopping term for $\uparrow$ spins of the form $$ \textcolor{red}{-}(c^\dagger_{i\uparrow}c_{j\uparrow} + \textrm{h.c.})$$ | 2 | tJ, Electron, tJDistributed |
| `Hopdn` | A hopping term for $\downarrow$ spins of the form $$ \textcolor{red}{-}(c^\dagger_{i\downarrow}c_{j\downarrow} + \textrm{h.c.})$$ | 2 | tJ, Electron, tJDistributed |
| `HubbardU` | A uniform Hubbard interaction across the full lattice of the form $$ \sum_i n_{i\uparrow}n_{i\downarrow}$$ | 0 | Electron |
| `Cdagup` | A fermionic creation operator for an $\uparrow$ spin $c^\dagger_{i\uparrow}$ | 1 | tJ, Electron, tJDistributed |
| `Cdagdn` | A fermionic creation operator for an $\downarrow$ spin $c^\dagger_{i\downarrow}$ | 1 | tJ, Electron, tJDistributed |
| `Cup` | A fermionic annihilation operator for an $\uparrow$ spin $c_{i\uparrow}$ | 1 | tJ, Electron, tJDistributed |
| `Cdn` | A fermionic annihilation operator for an $\downarrow$ spin $c_{i\downarrow}$ | 1 | tJ, Electron, tJDistributed |
| `Nup` | A number operator for an $\uparrow$ spin $n_{i\uparrow}$ | 1 | tJ, Electron, tJDistributed |
| `Ndn` | A number operator for an $\downarrow$ spin $n_{i\downarrow}$ | 1 | tJ, Electron, tJDistributed |
| `Ntot` | A number operator $n_i = n_{i\uparrow} + n_{i\downarrow}$ | 1 | tJ, Electron, tJDistributed |
| `NtotNtot` | A density-density interaction $n_i n_j$ | 2 | tJ, Electron, tJDistributed |
| `SdotS` | A Heisenberg interaction of the form $$ \mathbf{S}_i \cdot \mathbf{S}_j = S^x_iS^x_j + S^y_iS^y_j + S^z_iS^z_j$$ | 2 | Spinhalf, tJ, Electron, SpinhalfDistributed, tJDistributed |
| `SzSz` | An Ising interaction of the form $ S^z_i S^z_j $ | 2 | Spinhalf, tJ, Electron, SpinhalfDistributed, tJDistributed |
| `Exchange` | A spin exchange interaction of the form $$ \frac{1}{2}(S^+_i S^-_j + S^-_iS^+_j) = S^x_iS^x_j + S^y_iS^y_j j$$ | 2 | Spinhalf, tJ, Electron, SpinhalfDistributed, tJDistributed |
| `Sz` | A local magnetic moment in the $z$-direction $ S^z_i$ | 1 | Spinhalf, tJ, Electron, SpinhalfDistributed, tJDistributed |
| `S+` | A local spin raising operator $S^+_i$ | 1 | Spinhalf, SpinhalfDistributed |
| `S-` | A local spin lowering operator $S^-_i$ | 1 | Spinhalf, SpinhalfDistributed |
| `ScalarChirality` | A scalar chirality interaction of the form $$ \mathbf{S}_i \cdot ( \mathbf{S}_j \times \mathbf{S}_k)$$ | 3 | Spinhalf |
| `tJSzSz` | An Ising interaction as encountered in the $t-J$ model of the form $$ S^z_i S^z_j - \frac{n_i n_j}{4}$$ | 2 | tJ, tJDistributed |
| `tJSdotS` | An Heisenberg interaction as encountered in the $t-J$ model of the form $$ \mathbf{S}_i \cdot \mathbf{S}_j - \frac{n_i n_j}{4}$$ | 2 | tJ, tJDistributed |
| `Matrix` | A generic spin interaction no an arbitrary number of sites defined via a coupling matrix | arbitrary | Spinhalf |
| Type | Description | No. of sites | Blocks |
|:------------------|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------|------------------------------------------------------------|
| `Hop` | A hopping term for $\uparrow$ and $\downarrow$ spins of the form $$ \textcolor{red}{-}\sum_{\sigma=\uparrow\downarrow} (tc^\dagger_{i\sigma}c_{j\sigma} + \textrm{h.c.})$$ | 2 | tJ, Electron, tJDistributed |
| `Hopup` | A hopping term for $\uparrow$ spins of the form $$ \textcolor{red}{-}(tc^\dagger_{i\uparrow}c_{j\uparrow} + \textrm{h.c.})$$ | 2 | tJ, Electron, tJDistributed |
| `Hopdn` | A hopping term for $\downarrow$ spins of the form $$ \textcolor{red}{-}(tc^\dagger_{i\downarrow}c_{j\downarrow} + \textrm{h.c.})$$ | 2 | tJ, Electron, tJDistributed |
| `HubbardU` | A uniform Hubbard interaction across the full lattice of the form $$ \sum_i n_{i\uparrow}n_{i\downarrow}$$ | 0 | Electron |
| `Cdagup` | A fermionic creation operator for an $\uparrow$ spin $c^\dagger_{i\uparrow}$ | 1 | tJ, Electron, tJDistributed |
| `Cdagdn` | A fermionic creation operator for an $\downarrow$ spin $c^\dagger_{i\downarrow}$ | 1 | tJ, Electron, tJDistributed |
| `Cup` | A fermionic annihilation operator for an $\uparrow$ spin $c_{i\uparrow}$ | 1 | tJ, Electron, tJDistributed |
| `Cdn` | A fermionic annihilation operator for an $\downarrow$ spin $c_{i\downarrow}$ | 1 | tJ, Electron, tJDistributed |
| `Nup` | A number operator for an $\uparrow$ spin $n_{i\uparrow}$ | 1 | tJ, Electron, tJDistributed |
| `Ndn` | A number operator for an $\downarrow$ spin $n_{i\downarrow}$ | 1 | tJ, Electron, tJDistributed |
| `Ntot` | A number operator $n_i = n_{i\uparrow} + n_{i\downarrow}$ | 1 | tJ, Electron, tJDistributed |
| `NtotNtot` | A density-density interaction $n_i n_j$ | 2 | tJ, Electron, tJDistributed |
| `SdotS` | A Heisenberg interaction of the form $$ \mathbf{S}_i \cdot \mathbf{S}_j = S^x_iS^x_j + S^y_iS^y_j + S^z_iS^z_j$$ | 2 | Spinhalf, tJ, Electron, SpinhalfDistributed, tJDistributed |
| `SzSz` | An Ising interaction of the form $ S^z_i S^z_j $ | 2 | Spinhalf, tJ, Electron, SpinhalfDistributed, tJDistributed |
| `Exchange` | A spin exchange interaction of the form $$ \frac{1}{2}(JS^+_i S^-_j + J^*S^-_iS^+_j)$$ | 2 | Spinhalf, tJ, Electron, SpinhalfDistributed, tJDistributed |
| `Sz` | A local magnetic moment in the $z$-direction $ S^z_i$ | 1 | Spinhalf, tJ, Electron, SpinhalfDistributed, tJDistributed |
| `S+` | A local spin raising operator $S^+_i$ | 1 | Spinhalf, SpinhalfDistributed |
| `S-` | A local spin lowering operator $S^-_i$ | 1 | Spinhalf, SpinhalfDistributed |
| `ScalarChirality` | A scalar chirality interaction of the form $$ \mathbf{S}_i \cdot ( \mathbf{S}_j \times \mathbf{S}_k)$$ | 3 | Spinhalf |
| `tJSzSz` | An Ising interaction as encountered in the $t-J$ model of the form $$ S^z_i S^z_j - \frac{n_i n_j}{4}$$ | 2 | tJ, tJDistributed |
| `tJSdotS` | An Heisenberg interaction as encountered in the $t-J$ model of the form $$ \mathbf{S}_i \cdot \mathbf{S}_j - \frac{n_i n_j}{4}$$ | 2 | tJ, tJDistributed |
| `Matrix` | A generic spin interaction no an arbitrary number of sites defined via a coupling matrix | arbitrary | Spinhalf |


## Matrix type
Expand Down
99 changes: 91 additions & 8 deletions docs/documentation/operators/opsum.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ Object representing a generic many-body operator by a sum of operators of the fo

$$ \mathcal{O} = \sum_i c_i \mathcal{O}_i. $$

**Sources** [opsum.hpp](https://github.com/awietek/xdiag/blob/main/xdiag/operators/opsum.hpp), [opsum.cpp](https://github.com/awietek/xdiag/blob/main/xdiag/operators/opsum.cpp)
**Sources**<br>
[opsum.hpp](https://github.com/awietek/xdiag/blob/main/xdiag/operators/opsum.hpp)<br>
[opsum.cpp](https://github.com/awietek/xdiag/blob/main/xdiag/operators/opsum.cpp)<br>
[opsum.jl](https://github.com/awietek/XDiag.jl/blob/main/src/operators/opsum.jl)

---

Expand Down Expand Up @@ -56,9 +59,16 @@ Alternatively, an OpSum can also be constructed via the `* operator`, for exampl
```c++
auto ops = OpSum();
for (int i = 0; i<N; ++i) {
ops += "J" * Op("SzSz", {i, (i + 1) % N});
ops += "J" * Op("SzSz", {i, (i + 1) % N});
}
```
=== "Julia"
```julia
ops = OpSum();
for i in 1:n
ops += "J" * Op("SzSz", [i, mod1(i+1, N)]);
```

---

## Complex couplings
Expand Down Expand Up @@ -107,9 +117,15 @@ Creates an OpSum with a single pair of coupling constant and an [Op](op.md) obje

=== "C++"
```c++
OpSum operator*(std::string cpl, Op const &op);
OpSum operator*(double cpl, Op const &op);
OpSum operator*(complex cpl, Op const &op);
OpSum operator*(double coupling, Op const &op);
OpSum operator*(complex coupling, Op const &op);
OpSum operator*(std::string coupling, Op const &op);
```
=== "Julia"
```julia
Base.:*(coupling::Float64, op::Op)
Base.:*(coupling::ComplexF64, op::Op)
Base.:*(coupling::String, op::Op)
```
---

Expand All @@ -126,7 +142,7 @@ Adds two OpSum objects $\mathcal{A} = \sum_i a_i \mathcal{A}_i$ and $\mathcal{B}

=== "Julia"
```julia
Base.:+(ops::OpSum, ops2::OpSum)
Base.:+(ops1::OpSum, ops2::OpSum)
```

---
Expand All @@ -146,7 +162,7 @@ Subtracts to OpSum objects.
```
---

#### operator* , operator/ (Scalar muliplication/division)
#### operator* , operator/ (scalar muliplication/division)

Multiplies an OpSum $\mathcal{A} = \sum_i a_i \mathcal{A}_i$ with a scalar $b$ to form

Expand All @@ -166,6 +182,17 @@ $$\mathcal{B} = b \sum_i a_i \mathcal{A}_i$$
OpSum operator/(OpSum const &op, double scalar);
OpSum operator/(OpSum const &op, complex scalar);
```

=== "Julia"
```julia
Base.:*(coupling::Float64, ops::OpSum)
Base.:*(coupling::ComplexF64, ops::OpSum)
Base.:*(ops::OpSum, coupling::Float64)
Base.:*(ops::OpSum, coupling::ComplexF64)
Base.:/(ops::OpSum, coupling::Float64)
Base.:/(ops::OpSum, coupling::ComplexF64)
```

---

#### operator[]
Expand All @@ -175,8 +202,12 @@ Sets a coupling constant defined as a string to a numerical value.
=== "C++"
```c++
Scalar &operator[](std::string name);
Scalar const &operator[](std::string name) const;
```
=== "Julia"
```julia
Base.setindex!(ops::OpSum, cpl::Float64, name::String)
Base.setindex!(ops::OpSum, cpl::ComplexF64, name::String)
```
---

#### constants
Expand All @@ -187,8 +218,60 @@ Returns a vector of strings with the coupling constants defined, i.e. the string
```c++
std::vector<std::string> constants(OpSum const &ops) const;
```
=== "Julia"
```c++
constants(ops::OpSum)
```

---

#### isreal

Returns whether an [OpSum](opsum.md) is a real operator.

=== "C++"
```c++
bool isreal(OpSum const &ops);
```

=== "Julia"
```julia
isreal(ops::OpSum)
```
---

#### isapprox

Returns whether two OpSums are approximately equal.

=== "C++"
```c++
bool isapprox(OpSum const &ops1, OpSum const &ops2, double rtol = 1e-12,
double atol = 1e-12);
```

=== "Julia"
```julia
isapprox(ops1::OpSum, ops2::OpSum, rtol::Float64=1e-12, atol::Float64=1e-12)
```
---

#### to_string (operator<<)

Converts the OpSum to a readable string representation.

=== "C++"
```c++
std::string to_string(OpSum const &ops);
std::ostream &operator<<(std::ostream &out, OpSum const &ops);
```

=== "Julia"
```julia
to_string(ops::OpSum)
```
---

## Usage Example

=== "C++"
Expand Down
Loading

0 comments on commit 0f2af21

Please sign in to comment.