From cd7dea0f6f920012f130b975b5a8138cb459ea0d Mon Sep 17 00:00:00 2001 From: Filippo Vicentini <filippovicentini@gmail.com> Date: Thu, 3 Dec 2020 17:00:07 +0100 Subject: [PATCH] Add Row-submachine of density matrix --- .idea/netket_v21.iml | 2 + CMakeLists.txt | 1 + .../density_matrix_machine.hpp | 1 + .../DensityMatrices/density_matrix_row.cc | 62 +++++++++++++++ .../DensityMatrices/density_matrix_row.hpp | 75 +++++++++++++++++++ .../DensityMatrices/py_density_matrix.cc | 57 +++++++++++++- 6 files changed, 197 insertions(+), 1 deletion(-) create mode 100644 .idea/netket_v21.iml create mode 100644 Sources/Machine/DensityMatrices/density_matrix_row.cc create mode 100644 Sources/Machine/DensityMatrices/density_matrix_row.hpp diff --git a/.idea/netket_v21.iml b/.idea/netket_v21.iml new file mode 100644 index 000000000..f08604bb6 --- /dev/null +++ b/.idea/netket_v21.iml @@ -0,0 +1,2 @@ +<?xml version="1.0" encoding="UTF-8"?> +<module classpath="CMake" type="CPP_MODULE" version="4" /> \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index e6eb8f4e4..ef18d3e74 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -304,6 +304,7 @@ set(NETKET_SOURCES Sources/Utils/py_utils.cc Sources/Utils/random_utils.cc Sources/Machine/DensityMatrices/diagonal_density_matrix.cc + Sources/Machine/DensityMatrices/density_matrix_row.cc Sources/Machine/DensityMatrices/ndm_spin_phase.cc Sources/Machine/DensityMatrices/py_abstract_density_matrix.cc Sources/Machine/DensityMatrices/py_abstract_density_matrix.hpp diff --git a/Sources/Machine/DensityMatrices/density_matrix_machine.hpp b/Sources/Machine/DensityMatrices/density_matrix_machine.hpp index 005b93c14..521cdc198 100644 --- a/Sources/Machine/DensityMatrices/density_matrix_machine.hpp +++ b/Sources/Machine/DensityMatrices/density_matrix_machine.hpp @@ -18,6 +18,7 @@ #include "abstract_density_matrix.hpp" #include "diagonal_density_matrix.hpp" +#include "density_matrix_row.hpp" #include "ndm_spin_phase.hpp" #endif // NETKET_DENSITY_MATRIX_MACHINE_HPP diff --git a/Sources/Machine/DensityMatrices/density_matrix_row.cc b/Sources/Machine/DensityMatrices/density_matrix_row.cc new file mode 100644 index 000000000..a95b12fbc --- /dev/null +++ b/Sources/Machine/DensityMatrices/density_matrix_row.cc @@ -0,0 +1,62 @@ +// Copyright 2018 The Simons Foundation, Inc. - All +// Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "density_matrix_row.hpp" + +namespace netket { + +using VectorType = DensityMatrixRow::VectorType; +using VisibleConstType = DensityMatrixRow::VisibleConstType; + +Complex DensityMatrixRow::LogValSingle(VisibleConstType v_col, const any <) { + // return density_matrix_.LogValSingle(v_row_.replicate(v_col.rows(),1), + // v_col, lt); + return density_matrix_.LogValSingle(v_row_, v_col, lt); +} + +VectorType DensityMatrixRow::DerLogSingle(VisibleConstType v_col, + const any <) { + return density_matrix_.DerLogSingle(v_row_, v_col, lt); +} + +void DensityMatrixRow::SetRow(VisibleConstType v) { v_row_ = v; } + +VisibleConstType DensityMatrixRow::GetRow() const { return v_row_; } + +int DensityMatrixRow::Npar() const { return density_matrix_.Npar(); } + +VectorType DensityMatrixRow::GetParameters() { + return density_matrix_.GetParameters(); +} +void DensityMatrixRow::SetParameters(VectorConstRefType pars) { + return density_matrix_.SetParameters(pars); +} +int DensityMatrixRow::Nvisible() const { + return density_matrix_.NvisiblePhysical(); +} + +bool DensityMatrixRow::IsHolomorphic() const noexcept { + return density_matrix_.IsHolomorphic(); +} + +void DensityMatrixRow::Save(const std::string &filename) const { + return density_matrix_.Save(filename); +} + +void DensityMatrixRow::Load(const std::string &filename) { + return density_matrix_.Load(filename); +} + +} // namespace netket \ No newline at end of file diff --git a/Sources/Machine/DensityMatrices/density_matrix_row.hpp b/Sources/Machine/DensityMatrices/density_matrix_row.hpp new file mode 100644 index 000000000..9b8c64c89 --- /dev/null +++ b/Sources/Machine/DensityMatrices/density_matrix_row.hpp @@ -0,0 +1,75 @@ +// Copyright 2018 The Simons Foundation, Inc. - All +// Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef NETKET_DENSITYMATRIX_ROW_HPP +#define NETKET_DENSITYMATRIX_ROW_HPP + +#include "Machine/abstract_machine.hpp" +#include "Utils/random_utils.hpp" +#include "abstract_density_matrix.hpp" + +#include <Eigen/Core> +#include <functional> +#include <limits> +#include <vector> + +namespace netket { +class DensityMatrixRow : public AbstractMachine { + + AbstractDensityMatrix &density_matrix_; + + Eigen::VectorXd v_row_; + + public: + explicit DensityMatrixRow(AbstractDensityMatrix &dm, VisibleConstType v_row) + : AbstractMachine(dm.GetHilbertPhysicalShared()), density_matrix_(dm) { + v_row_.resize(dm.GetHilbertPhysical().Size()); + SetRow(v_row); + } ; + + explicit DensityMatrixRow(AbstractDensityMatrix &dm) + : AbstractMachine(dm.GetHilbertPhysicalShared()), density_matrix_(dm) { + v_row_.resize(dm.GetHilbertPhysical().Size()); + } ; + + const AbstractDensityMatrix &GetFullDensityMatrix() const noexcept { + return density_matrix_; + } + + Complex LogValSingle(VisibleConstType v_col, const any <) override; + + VectorType DerLogSingle(VisibleConstType v, const any <) override; + + void SetRow(VisibleConstType v); + + VisibleConstType GetRow() const; + + int Npar() const override; + + VectorType GetParameters() override; + + void SetParameters(VectorConstRefType pars) override; + + int Nvisible() const override ; + + bool IsHolomorphic() const noexcept override ; + + void Save(const std::string &filename) const override ; + + void Load(const std::string &filename) override ; +}; +} // namespace netket + +#endif // NETKET_DENSITYMATRIX_ROW_HPP diff --git a/Sources/Machine/DensityMatrices/py_density_matrix.cc b/Sources/Machine/DensityMatrices/py_density_matrix.cc index 88a0ebec4..adbf75789 100644 --- a/Sources/Machine/DensityMatrices/py_density_matrix.cc +++ b/Sources/Machine/DensityMatrices/py_density_matrix.cc @@ -18,6 +18,7 @@ #include "Utils/messages.hpp" #include "abstract_density_matrix.hpp" +#include "density_matrix_row.hpp" #include "diagonal_density_matrix.hpp" #include "ndm_spin_phase.hpp" @@ -90,7 +91,43 @@ void AddDiagonalDensityMatrix(py::module &subm) { Args: dm: the density matrix. -)EOF"); +)EOF") + .def_property_readonly( + "parent", &DiagonalDensityMatrix::GetFullDensityMatrix, + R"EOF(abstract_density_matrix: Returns the parent density + matrix of this Row (of the parent density matrix).)EOF"); +} + +void AddDensityMatrixRow(py::module &subm) { + py::class_<DensityMatrixRow, AbstractMachine>(subm, "DensityMatrixRow", R"EOF( + A Machine sampling the Row `v` of a density matrix.)EOF") + .def(py::init<AbstractDensityMatrix &>(), py::keep_alive<1, 2>(), + py::arg("dm"), R"EOF( + + Constructs a new ``DensityMatrixRow`` machine sampling the + row `v` of the provided density matrix. + + Args: + dm: the density matrix. + v: the configuration of the selected row. +)EOF") + .def_property( + "row", &DensityMatrixRow::GetRow, + [](DensityMatrixRow &self, py::array_t<double> xr) { + if (xr.ndim() == 1) { + auto v_row = xr.cast<Eigen::Ref<const VectorXd>>(); + return self.SetRow(v_row); + } else { + throw InvalidInputError{ + "Invalid input dimensions: the row configuration " + "should be a vector."}; + } + }, + R"EOF(double vector: The state identifying the row x of this machine psi(x') = <x|rho|x'>.)EOF") + .def_property_readonly( + "parent", &DensityMatrixRow::GetFullDensityMatrix, + R"EOF(abstract_density_matrix: Returns the parent density + matrix of this Row (of the parent density matrix).)EOF"); } void AddAbstractDensityMatrix(py::module &subm) { @@ -180,6 +217,23 @@ void AddAbstractDensityMatrix(py::module &subm) { R"EOF( Returns the wrapped machine sampling the diagonal of this density matrix. )EOF") + .def( + "row", + [](AbstractDensityMatrix &self, + py::array_t<double> xr) -> DensityMatrixRow { + if (xr.ndim() == 1) { + auto v_row = xr.cast<Eigen::Ref<const VectorXd>>(); + return DensityMatrixRow(self, v_row); + } else { + throw InvalidInputError{ + "Invalid input dimensions: the row configuration should be a " + "vector."}; + } + }, + py::arg("row"), + R"EOF( + Returns the wrapped machine sampling the row 'v' of this density matrix. + )EOF") .def("log_val", [](AbstractDensityMatrix &self, py::array_t<double> xr, py::array_t<double> xc) { @@ -307,6 +361,7 @@ void AddDensityMatrixModule(py::module subm) { AddAbstractDensityMatrix(subm); AddNdmSpinPhase(subm); AddDiagonalDensityMatrix(subm); + AddDensityMatrixRow(subm); } } // namespace netket -- GitLab