Skip to content

Commit

Permalink
Added auxiliary functions for wrapping vectors of Op and Permutation …
Browse files Browse the repository at this point in the history
…in Julia
  • Loading branch information
Alexander Wietek committed Aug 27, 2024
1 parent 65e51f9 commit 68693ad
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 21 deletions.
12 changes: 9 additions & 3 deletions julia/src/operators/op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace xdiag::julia {

void define_op(jlcxx::Module &mod) {

mod.add_type<Op>("cxx_Op")
.constructor<>()
.constructor<std::string, Coupling const &,
Expand All @@ -24,8 +25,10 @@ void define_op(jlcxx::Module &mod) {
.method("coupling",
[](Op const &op) { JULIA_XDIAG_CALL_RETURN(op.coupling()) })
.method("size", [](Op const &op) { JULIA_XDIAG_CALL_RETURN(op.size()) })
.method("getindex", [](Op const &op,
int64_t idx) { JULIA_XDIAG_CALL_RETURN(op[idx-1]) })
.method("getindex",
[](Op const &op, int64_t idx) {
JULIA_XDIAG_CALL_RETURN(op[idx - 1])
})
.method("sites", [](Op const &op) { JULIA_XDIAG_CALL_RETURN(op.sites()) })
.method("isreal",
[](Op const &op) { JULIA_XDIAG_CALL_RETURN(op.isreal()) })
Expand All @@ -35,7 +38,10 @@ void define_op(jlcxx::Module &mod) {
[](Op const &op) { JULIA_XDIAG_CALL_RETURN(op.isexplicit()) });

mod.method("to_string", [](Op const &op) { return to_string(op); });


mod.add_type<VectorOp>("cxx_VectorOp")
.constructor<>()
.method("push_back", &VectorOp::push_back);
}

} // namespace xdiag::julia
4 changes: 1 addition & 3 deletions julia/src/operators/opsum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ namespace xdiag::julia {

void define_opsum(jlcxx::Module &mod) {

mod.add_type<std::vector<Op>>("StdVectorOpSum");

mod.add_type<OpSum>("cxx_OpSum")
.constructor<>()
.constructor<std::vector<Op> const &>()
.constructor<VectorOp const &>()
.method("size",
[](OpSum const &ops) { JULIA_XDIAG_CALL_RETURN(ops.size()) })
.method("defined",
Expand Down
4 changes: 4 additions & 0 deletions julia/src/symmetries/permutation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ void define_permutation(jlcxx::Module &mod) {

mod.method("to_string", [](Permutation const &p) { return to_string(p); });

mod.add_type<VectorPermutation>("cxx_VectorPermutation")
.constructor<>()
.method("push_back", &VectorPermutation::push_back);

}

} // namespace xdiag::julia
5 changes: 2 additions & 3 deletions julia/src/symmetries/permutation_group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ namespace xdiag::julia {

void define_permutation_group(jlcxx::Module &mod) {

mod.add_type<std::vector<Permutation>>("StdVectorPermutation");

mod.add_type<PermutationGroup>("cxx_PermutationGroup")
.constructor<>()
.constructor<std::vector<Permutation> const &>()
.constructor<VectorPermutation const &>()
.method("inverse", &PermutationGroup::inverse)
.method("n_sites", &PermutationGroup::n_sites)
.method("size", &PermutationGroup::size);

mod.method("to_string",
[](PermutationGroup const &p) { return to_string(p); });

}

} // namespace xdiag::julia
2 changes: 2 additions & 0 deletions misc/release_todo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
- open pull request to Yggdrasil (make sure using branch xdiag)
- after pull request to Yggdrasil is merged update dependency XDiag_jll in Project.toml of XDiag.jl
- git commit XDiag.jl again
- tag XDiag.jl
- open issue "@JuliaRegistrator register" on the XDiag.jl github page
- optionally, update documentation with mkdocs gh-deploy
3 changes: 3 additions & 0 deletions xdiag/operators/op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,7 @@ std::ostream &operator<<(std::ostream &out, Op const &op) {

std::string to_string(Op const &op) { return to_string_generic(op); }

void VectorOp::push_back(Op const &op) { v_.push_back(op); }
std::vector<Op> VectorOp::vector() const { return v_; }

} // namespace xdiag
10 changes: 10 additions & 0 deletions xdiag/operators/op.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,14 @@ std::vector<int64_t> common_sites(Op const &b1, Op const &b2);
std::ostream &operator<<(std::ostream &out, Op const &op);
std::string to_string(Op const &op);

// helper for julia wrapper
class VectorOp {
public:
VectorOp() = default;
void push_back(Op const &op);
std::vector<Op> vector() const;
private:
std::vector<Op> v_;
};

} // namespace xdiag
2 changes: 2 additions & 0 deletions xdiag/operators/opsum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
namespace xdiag {

OpSum::OpSum(std::vector<Op> const &ops) : ops_(ops) {}
OpSum::OpSum(VectorOp const &ops) : OpSum(ops.vector()) {}

int64_t OpSum::size() const { return ops_.size(); }
Coupling &OpSum::operator[](std::string name) { return couplings_[name]; }
Coupling const &OpSum::operator[](std::string name) const {
Expand Down
1 change: 1 addition & 0 deletions xdiag/operators/opsum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class OpSum {

OpSum() = default;
explicit OpSum(std::vector<Op> const &ops);
explicit OpSum(VectorOp const &ops); // auxiliary for julia

int64_t size() const;
bool defined(std::string name) const;
Expand Down
7 changes: 6 additions & 1 deletion xdiag/symmetries/permutation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ std::ostream &operator<<(std::ostream &out, Permutation const &p) {
out << "\n";
return out;
}
std::string to_string(Permutation const &perm) { return to_string_generic(perm); }
std::string to_string(Permutation const &perm) {
return to_string_generic(perm);
}

void VectorPermutation::push_back(Permutation const &p) { v_.push_back(p); }
std::vector<Permutation> VectorPermutation::vector() const { return v_; }

} // namespace xdiag
10 changes: 10 additions & 0 deletions xdiag/symmetries/permutation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,14 @@ Permutation shuffle(Permutation const &p);
std::ostream &operator<<(std::ostream &out, Permutation const &perm);
std::string to_string(Permutation const &perm);

// helper for julia wrapper
class VectorPermutation {
public:
VectorPermutation() = default;
void push_back(Permutation const &p);
std::vector<Permutation> vector() const;

private:
std::vector<Permutation> v_;
};
} // namespace xdiag
39 changes: 28 additions & 11 deletions xdiag/symmetries/permutation_group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@
#include <fstream>
#include <iostream>

#include <xdiag/common.hpp>
#include <xdiag/symmetries/operations/symmetry_operations.hpp>
#include <xdiag/utils/logger.hpp>

namespace xdiag {

PermutationGroup::PermutationGroup(std::vector<Permutation> const &permutations)
PermutationGroup::PermutationGroup(
std::vector<Permutation> const &permutations) try
: n_sites_(permutations.size() > 0 ? permutations[0].size() : 0),
n_symmetries_(permutations.size()), permutations_(permutations),
inverse_(n_symmetries_) {

// Check whether all permutations have same number of sites
for (auto p : permutations) {
if (p.size() != n_sites_) {
Log.err("Error constructing PermutationGroup: not all Permutations have "
"the same number of sites");
XDIAG_THROW(
"Error constructing PermutationGroup: not all Permutations have "
"the same number of sites");
}
}

Expand All @@ -27,7 +30,8 @@ PermutationGroup::PermutationGroup(std::vector<Permutation> const &permutations)
auto id = identity_permutation(n_sites_);
if (std::find(permutations.begin(), permutations.end(), id) ==
permutations.end()) {
Log.err("Error constructing PermutationGroup: no identity element found");
XDIAG_THROW(
"Error constructing PermutationGroup: no identity element found");
}
}

Expand All @@ -37,8 +41,9 @@ PermutationGroup::PermutationGroup(std::vector<Permutation> const &permutations)
auto p = p1 * p2;
if (std::find(permutations.begin(), permutations.end(), p) ==
permutations.end()) {
Log.err("Error constructing PermutationGroup: group multiplication not "
"closed");
XDIAG_THROW(
"Error constructing PermutationGroup: group multiplication not "
"closed");
}
}
}
Expand All @@ -49,13 +54,22 @@ PermutationGroup::PermutationGroup(std::vector<Permutation> const &permutations)
auto pinv = xdiag::inverse(p);
auto it = std::find(permutations.begin(), permutations.end(), pinv);
if (it == permutations.end()) {
Log.err("Error constructing PermutationGroup: inverse element not found");
XDIAG_THROW(
"Error constructing PermutationGroup: inverse element not found");
} else {
int64_t idx_inv = std::distance(permutations.begin(), it);
inverse_[idx] = idx_inv;
}
idx++;
}
} catch (Error const &e) {
XDIAG_RETHROW(e);
}

PermutationGroup::PermutationGroup(VectorPermutation const &permutations) try
: PermutationGroup(permutations.vector()) {
} catch (Error const &e) {
XDIAG_RETHROW(e);
}

int64_t PermutationGroup::n_sites() const { return n_sites_; }
Expand All @@ -67,19 +81,22 @@ Permutation const &PermutationGroup::operator[](int64_t sym) const {
int64_t PermutationGroup::inverse(int64_t sym) const { return inverse_[sym]; }

PermutationGroup
PermutationGroup::subgroup(std::vector<int64_t> const &symmetry_numbers) const {
PermutationGroup::subgroup(std::vector<int64_t> const &symmetry_numbers) const
try {
std::vector<Permutation> subgroup_permutations;

for (int64_t n_sym : symmetry_numbers) {

if ((0 > n_sym) || (n_sym >= n_symmetries_)) {
Log.err("Error building subgroup of PermutationGroup: "
"invalid symmetry index");
XDIAG_THROW("Error building subgroup of PermutationGroup: "
"invalid symmetry index");
}
subgroup_permutations.push_back(permutations_[n_sym]);
}

return PermutationGroup(subgroup_permutations);
} catch (Error const &e) {
XDIAG_RETHROW(e);
}

bool PermutationGroup::operator==(PermutationGroup const &rhs) const {
Expand All @@ -101,7 +118,7 @@ PermutationGroup::iterator_t PermutationGroup::end() const {
std::ostream &operator<<(std::ostream &out, PermutationGroup const &group) {
out << "n_sites : " << group.n_sites() << "\n";
out << "n_symmetries : " << group.n_symmetries() << "\n";
for (auto const& p : group){
for (auto const &p : group) {
out << p;
}
return out;
Expand Down
2 changes: 2 additions & 0 deletions xdiag/symmetries/permutation_group.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class PermutationGroup {
public:
PermutationGroup() = default;
explicit PermutationGroup(std::vector<Permutation> const &permutations);
explicit PermutationGroup(
VectorPermutation const &permutations); // auxiliary for julia

int64_t n_sites() const;
int64_t n_symmetries() const;
Expand Down

0 comments on commit 68693ad

Please sign in to comment.