Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate Contracts->Contract #435

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions include/UniTensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4454,6 +4454,22 @@ namespace cytnx {

See also \link cytnx::UniTensor::contract UniTensor.contract \endlink

*/
UniTensor Contract(const std::vector<UniTensor> &TNs, const std::string &order,
const bool &optimal);

/**
@deprecated
@brief Contract multiple UniTensor by tracing the ranks with common labels with pairwise
operation.
@param[in] TNs the Tensors.
@param[in] order desired contraction order.
@param[in] optimal wheather to find the optimal contraction order automatically.
@return
[UniTensor]

See also \link cytnx::UniTensor::contract UniTensor.contract \endlink

*/
UniTensor Contracts(const std::vector<UniTensor> &TNs, const std::string &order,
const bool &optimal);
Expand All @@ -4477,6 +4493,26 @@ namespace cytnx {

See also \link cytnx::UniTensor::contract UniTensor.contract \endlink

*/
template <class... T>
UniTensor Contract(const UniTensor &in, const T &...args, const std::string &order,
const bool &optimal) {
std::vector<UniTensor> TNlist;
_resolve_CT(TNlist, in, args...);
return Contract(TNlist, order, optimal);
}

/**
@deprecated
@brief Contract multiple UniTensor by tracing the ranks with common labels with pairwise
operation.
@param in the Tensors.
@param args the Tensors.
@return
[UniTensor]

See also \link cytnx::UniTensor::contract UniTensor.contract \endlink

*/
template <class... T>
UniTensor Contracts(const UniTensor &in, const T &...args, const std::string &order,
Expand Down
14 changes: 12 additions & 2 deletions pybind/unitensor_py.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1372,8 +1372,18 @@ void unitensor_binding(py::module &m) {
.def("get_qindices", [](UniTensor &self, const cytnx_uint64 &bidx){return self.get_qindices(bidx);});
; // end of object line

m.def("Contract", Contract, py::arg("Tl"), py::arg("Tr"), py::arg("cacheL") = false,
py::arg("cacheR") = false);
// m.def("Contract", Contract, py::arg("Tl"), py::arg("Tr"), py::arg("cacheL") = false,
// py::arg("cacheR") = false);
m.def(
"Contract",
[](const UniTensor &inL, const UniTensor &inR, const bool &cacheL,
const bool &cacheR) -> UniTensor { return Contract(inL, inR, cacheL, cacheR); },
py::arg("Tl"), py::arg("Tr"), py::arg("cacheL") = false, py::arg("cacheR") = false);
m.def(
"Contract",
[](const std::vector<UniTensor> &TNs, const std::string &order,
const bool &optimal) -> UniTensor { return Contract(TNs, order, optimal); },
py::arg("TNs"), py::arg("order") = "", py::arg("optimal") = true);
m.def(
"Contracts",
[](const std::vector<UniTensor> &TNs, const std::string &order,
Expand Down
4 changes: 4 additions & 0 deletions src/UniTensor_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,10 @@ namespace cytnx {
}

void _resolve_CT(std::vector<UniTensor> &TNlist){};
UniTensor Contract(const std::vector<UniTensor> &TNs, const std::string &order = "",
const bool &optimal = true) {
return Contracts(TNs, order, optimal);
}
UniTensor Contracts(const std::vector<UniTensor> &TNs, const std::string &order = "",
const bool &optimal = true) {
cytnx_error_msg(TNs.size() < 2, "[ERROR][Contracts] should have more than 2 TNs to contract.%s",
Expand Down
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ add_executable(
Network_test.cpp
UniTensor_base_test.cpp
ncon_test.cpp
Contracts_test.cpp
Contract_test.cpp
BlockUniTensor_test.cpp
DenseUniTensor_test.cpp
Accessor_test.cpp
Expand Down
49 changes: 49 additions & 0 deletions tests/Contract_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "Contract_test.h"

using namespace std;
using namespace cytnx;

TEST_F(ContractTest, Contract_denseUt_optimal_order) {
UniTensor res = Contract({utdnA, utdnB, utdnC}, "", true);
EXPECT_TRUE(AreNearlyEqTensor(res.get_block(), utdnAns.get_block(), 1e-12));
}

TEST_F(ContractTest, Contract_denseUt_default_order) {
UniTensor res = Contract({utdnA, utdnB, utdnC}, "", false);
EXPECT_TRUE(AreNearlyEqTensor(res.get_block(), utdnAns.get_block(), 1e-12));
}

TEST_F(ContractTest, Contract_denseUt_specified_order) {
UniTensor res =
Contract({utdnA.set_name("A"), utdnB.set_name("B"), utdnC.set_name("C")}, "(C,(A,B))", false);
EXPECT_TRUE(AreNearlyEqTensor(res.get_block().contiguous(), utdnAns.get_block(), 1e-12));
}

TEST_F(ContractTest, Contract_denseUt_optimal_specified_order) {
UniTensor res =
Contract({utdnA.set_name("A"), utdnB.set_name("B"), utdnC.set_name("C")}, "(C,(A,B))", true);
EXPECT_TRUE(AreNearlyEqTensor(res.get_block().contiguous(), utdnAns.get_block(), 1e-12));
}

// Deprecated Contracts
TEST_F(ContractTest, Contracts_denseUt_optimal_order) {
UniTensor res = Contracts({utdnA, utdnB, utdnC}, "", true);
EXPECT_TRUE(AreNearlyEqTensor(res.get_block(), utdnAns.get_block(), 1e-12));
}

TEST_F(ContractTest, Contracts_denseUt_default_order) {
UniTensor res = Contracts({utdnA, utdnB, utdnC}, "", false);
EXPECT_TRUE(AreNearlyEqTensor(res.get_block(), utdnAns.get_block(), 1e-12));
}

TEST_F(ContractTest, Contracts_denseUt_specified_order) {
UniTensor res =
Contracts({utdnA.set_name("A"), utdnB.set_name("B"), utdnC.set_name("C")}, "(C,(A,B))", false);
EXPECT_TRUE(AreNearlyEqTensor(res.get_block().contiguous(), utdnAns.get_block(), 1e-12));
}

TEST_F(ContractTest, Contracts_denseUt_optimal_specified_order) {
UniTensor res =
Contracts({utdnA.set_name("A"), utdnB.set_name("B"), utdnC.set_name("C")}, "(C,(A,B))", true);
EXPECT_TRUE(AreNearlyEqTensor(res.get_block().contiguous(), utdnAns.get_block(), 1e-12));
}
6 changes: 3 additions & 3 deletions tests/Contracts_test.h → tests/Contract_test.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef _H_contracts_test
#define _H_contracts_test
#ifndef _H_contract_test
#define _H_contract_test

#include "cytnx.hpp"
#include <gtest/gtest.h>
Expand All @@ -8,7 +8,7 @@
using namespace cytnx;
using namespace TestTools;

class ContractsTest : public ::testing::Test {
class ContractTest : public ::testing::Test {
public:
// std::pair<std::vector<cytnx::UniTensor>, std::vector<std::vector<cytnx::cytnx_int64>>> input;
UniTensor utdnA = UniTensor(arange(0, 8, 1, Type.ComplexDouble)).reshape({2, 2, 2});
Expand Down
26 changes: 0 additions & 26 deletions tests/Contracts_test.cpp

This file was deleted.

2 changes: 1 addition & 1 deletion tests/gpu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ add_executable(
Network_test.cpp
UniTensor_base_test.cpp
ncon_test.cpp
Contracts_test.cpp
Contract_test.cpp
BlockUniTensor_test.cpp
DenseUniTensor_test.cpp
Accessor_test.cpp
Expand Down
49 changes: 49 additions & 0 deletions tests/gpu/Contract_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "Contract_test.h"

using namespace std;
using namespace cytnx;

TEST_F(ContractTest, gpu_Contract_denseUt_optimal_order) {
UniTensor res = Contract({utdnA, utdnB, utdnC}, "", true);
EXPECT_TRUE(AreNearlyEqTensor(res.get_block(), utdnAns.get_block(), 1e-12));
}

TEST_F(ContractTest, gpu_Contract_denseUt_default_order) {
UniTensor res = Contract({utdnA, utdnB, utdnC}, "", false);
EXPECT_TRUE(AreNearlyEqTensor(res.get_block(), utdnAns.get_block(), 1e-12));
}

TEST_F(ContractTest, gpu_Contract_denseUt_specified_order) {
UniTensor res =
Contract({utdnA.set_name("A"), utdnB.set_name("B"), utdnC.set_name("C")}, "(C,(A,B))", false);
EXPECT_TRUE(AreNearlyEqTensor(res.get_block().contiguous(), utdnAns.get_block(), 1e-12));
}

TEST_F(ContractTest, gpu_Contract_denseUt_optimal_specified_order) {
UniTensor res =
Contract({utdnA.set_name("A"), utdnB.set_name("B"), utdnC.set_name("C")}, "(C,(A,B))", true);
EXPECT_TRUE(AreNearlyEqTensor(res.get_block().contiguous(), utdnAns.get_block(), 1e-12));
}

// Deprecated Contracts
TEST_F(ContractTest, gpu_Contracts_denseUt_optimal_order) {
UniTensor res = Contracts({utdnA, utdnB, utdnC}, "", true);
EXPECT_TRUE(AreNearlyEqTensor(res.get_block(), utdnAns.get_block(), 1e-12));
}

TEST_F(ContractTest, gpu_Contracts_denseUt_default_order) {
UniTensor res = Contracts({utdnA, utdnB, utdnC}, "", false);
EXPECT_TRUE(AreNearlyEqTensor(res.get_block(), utdnAns.get_block(), 1e-12));
}

TEST_F(ContractTest, gpu_Contracts_denseUt_specified_order) {
UniTensor res =
Contracts({utdnA.set_name("A"), utdnB.set_name("B"), utdnC.set_name("C")}, "(C,(A,B))", false);
EXPECT_TRUE(AreNearlyEqTensor(res.get_block().contiguous(), utdnAns.get_block(), 1e-12));
}

TEST_F(ContractTest, gpu_Contracts_denseUt_optimal_specified_order) {
UniTensor res =
Contracts({utdnA.set_name("A"), utdnB.set_name("B"), utdnC.set_name("C")}, "(C,(A,B))", true);
EXPECT_TRUE(AreNearlyEqTensor(res.get_block().contiguous(), utdnAns.get_block(), 1e-12));
}
6 changes: 3 additions & 3 deletions tests/gpu/Contracts_test.h → tests/gpu/Contract_test.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef _H_contracts_test
#define _H_contracts_test
#ifndef _H_contract_test
#define _H_contract_test

#include "cytnx.hpp"
#include <gtest/gtest.h>
Expand All @@ -8,7 +8,7 @@
using namespace cytnx;
using namespace TestTools;

class ContractsTest : public ::testing::Test {
class ContractTest : public ::testing::Test {
public:
// std::pair<std::vector<cytnx::UniTensor>, std::vector<std::vector<cytnx::cytnx_int64>>> input;
UniTensor utdnA =
Expand Down
26 changes: 0 additions & 26 deletions tests/gpu/Contracts_test.cpp

This file was deleted.

Loading