From 68693ad155ea6b7d64b0a3bd8c789839ff1b3735 Mon Sep 17 00:00:00 2001 From: Alexander Wietek Date: Tue, 27 Aug 2024 21:05:28 +0200 Subject: [PATCH] Added auxiliary functions for wrapping vectors of Op and Permutation in Julia --- julia/src/operators/op.cpp | 12 +++++-- julia/src/operators/opsum.cpp | 4 +-- julia/src/symmetries/permutation.cpp | 4 +++ julia/src/symmetries/permutation_group.cpp | 5 ++- misc/release_todo.txt | 2 ++ xdiag/operators/op.cpp | 3 ++ xdiag/operators/op.hpp | 10 ++++++ xdiag/operators/opsum.cpp | 2 ++ xdiag/operators/opsum.hpp | 1 + xdiag/symmetries/permutation.cpp | 7 +++- xdiag/symmetries/permutation.hpp | 10 ++++++ xdiag/symmetries/permutation_group.cpp | 39 ++++++++++++++++------ xdiag/symmetries/permutation_group.hpp | 2 ++ 13 files changed, 80 insertions(+), 21 deletions(-) diff --git a/julia/src/operators/op.cpp b/julia/src/operators/op.cpp index 43ffcfeb..af4bdaa1 100644 --- a/julia/src/operators/op.cpp +++ b/julia/src/operators/op.cpp @@ -3,6 +3,7 @@ namespace xdiag::julia { void define_op(jlcxx::Module &mod) { + mod.add_type("cxx_Op") .constructor<>() .constructor("cxx_VectorOp") + .constructor<>() + .method("push_back", &VectorOp::push_back); } } // namespace xdiag::julia diff --git a/julia/src/operators/opsum.cpp b/julia/src/operators/opsum.cpp index 04b05330..9d1140eb 100644 --- a/julia/src/operators/opsum.cpp +++ b/julia/src/operators/opsum.cpp @@ -4,11 +4,9 @@ namespace xdiag::julia { void define_opsum(jlcxx::Module &mod) { - mod.add_type>("StdVectorOpSum"); - mod.add_type("cxx_OpSum") .constructor<>() - .constructor const &>() + .constructor() .method("size", [](OpSum const &ops) { JULIA_XDIAG_CALL_RETURN(ops.size()) }) .method("defined", diff --git a/julia/src/symmetries/permutation.cpp b/julia/src/symmetries/permutation.cpp index c2e993a7..7298920a 100644 --- a/julia/src/symmetries/permutation.cpp +++ b/julia/src/symmetries/permutation.cpp @@ -14,6 +14,10 @@ void define_permutation(jlcxx::Module &mod) { mod.method("to_string", [](Permutation const &p) { return to_string(p); }); + mod.add_type("cxx_VectorPermutation") + .constructor<>() + .method("push_back", &VectorPermutation::push_back); + } } // namespace xdiag::julia diff --git a/julia/src/symmetries/permutation_group.cpp b/julia/src/symmetries/permutation_group.cpp index 643d33d9..cadbbbad 100644 --- a/julia/src/symmetries/permutation_group.cpp +++ b/julia/src/symmetries/permutation_group.cpp @@ -4,17 +4,16 @@ namespace xdiag::julia { void define_permutation_group(jlcxx::Module &mod) { - mod.add_type>("StdVectorPermutation"); - mod.add_type("cxx_PermutationGroup") .constructor<>() - .constructor const &>() + .constructor() .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 diff --git a/misc/release_todo.txt b/misc/release_todo.txt index 1601358b..0a368742 100644 --- a/misc/release_todo.txt +++ b/misc/release_todo.txt @@ -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 diff --git a/xdiag/operators/op.cpp b/xdiag/operators/op.cpp index 77b3eb4f..c788f4b3 100644 --- a/xdiag/operators/op.cpp +++ b/xdiag/operators/op.cpp @@ -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 VectorOp::vector() const { return v_; } + } // namespace xdiag diff --git a/xdiag/operators/op.hpp b/xdiag/operators/op.hpp index bb04303b..93b12527 100644 --- a/xdiag/operators/op.hpp +++ b/xdiag/operators/op.hpp @@ -55,4 +55,14 @@ std::vector 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 vector() const; +private: + std::vector v_; +}; + } // namespace xdiag diff --git a/xdiag/operators/opsum.cpp b/xdiag/operators/opsum.cpp index bbbe5cb3..663fcab3 100644 --- a/xdiag/operators/opsum.cpp +++ b/xdiag/operators/opsum.cpp @@ -9,6 +9,8 @@ namespace xdiag { OpSum::OpSum(std::vector 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 { diff --git a/xdiag/operators/opsum.hpp b/xdiag/operators/opsum.hpp index 49a2b46f..3660dded 100644 --- a/xdiag/operators/opsum.hpp +++ b/xdiag/operators/opsum.hpp @@ -13,6 +13,7 @@ class OpSum { OpSum() = default; explicit OpSum(std::vector const &ops); + explicit OpSum(VectorOp const &ops); // auxiliary for julia int64_t size() const; bool defined(std::string name) const; diff --git a/xdiag/symmetries/permutation.cpp b/xdiag/symmetries/permutation.cpp index 5e202f97..6b91eb87 100644 --- a/xdiag/symmetries/permutation.cpp +++ b/xdiag/symmetries/permutation.cpp @@ -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 VectorPermutation::vector() const { return v_; } } // namespace xdiag diff --git a/xdiag/symmetries/permutation.hpp b/xdiag/symmetries/permutation.hpp index a4867837..96b51d08 100644 --- a/xdiag/symmetries/permutation.hpp +++ b/xdiag/symmetries/permutation.hpp @@ -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 vector() const; + +private: + std::vector v_; +}; } // namespace xdiag diff --git a/xdiag/symmetries/permutation_group.cpp b/xdiag/symmetries/permutation_group.cpp index d70fe3e0..ddcb5915 100644 --- a/xdiag/symmetries/permutation_group.cpp +++ b/xdiag/symmetries/permutation_group.cpp @@ -4,12 +4,14 @@ #include #include +#include #include #include namespace xdiag { -PermutationGroup::PermutationGroup(std::vector const &permutations) +PermutationGroup::PermutationGroup( + std::vector const &permutations) try : n_sites_(permutations.size() > 0 ? permutations[0].size() : 0), n_symmetries_(permutations.size()), permutations_(permutations), inverse_(n_symmetries_) { @@ -17,8 +19,9 @@ PermutationGroup::PermutationGroup(std::vector const &permutations) // 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"); } } @@ -27,7 +30,8 @@ PermutationGroup::PermutationGroup(std::vector 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"); } } @@ -37,8 +41,9 @@ PermutationGroup::PermutationGroup(std::vector 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"); } } } @@ -49,13 +54,22 @@ PermutationGroup::PermutationGroup(std::vector 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_; } @@ -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 const &symmetry_numbers) const { +PermutationGroup::subgroup(std::vector const &symmetry_numbers) const + try { std::vector 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 { @@ -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; diff --git a/xdiag/symmetries/permutation_group.hpp b/xdiag/symmetries/permutation_group.hpp index c9edc72b..dd5c3be7 100644 --- a/xdiag/symmetries/permutation_group.hpp +++ b/xdiag/symmetries/permutation_group.hpp @@ -10,6 +10,8 @@ class PermutationGroup { public: PermutationGroup() = default; explicit PermutationGroup(std::vector const &permutations); + explicit PermutationGroup( + VectorPermutation const &permutations); // auxiliary for julia int64_t n_sites() const; int64_t n_symmetries() const;