Skip to content

Commit

Permalink
stp eigen amend
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangrb00 committed May 7, 2024
1 parent 5a85fbe commit ae0b4fa
Show file tree
Hide file tree
Showing 12 changed files with 484 additions and 403 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ project(stp LANGUAGES CXX)
set(STP_CXX_STANDARD "17" CACHE STRING "C++ standard")
set(CMAKE_CXX_STANDARD ${STP_CXX_STANDARD})
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS_RELEASE "-O3")

# Options
option(STP_EXAMPLES "Build examples" ON)
option(STP_TEST "Build tests" ON)

add_compile_options(-O4 -W -Wall -Wextra)

# Add main include files
add_subdirectory(include)

Expand Down
2 changes: 1 addition & 1 deletion benchmarks/t1.bench
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ n4 = LUT 0x1(n2)
n5 = LUT 0x8(n3, n4)
n6 = LUT 0xe(n1, n2)
n7 = LUT 0x1(n5)
n8 = LUT 0x6(n6, n7)
n8 = LUT 0x6(n6, n7)
5 changes: 3 additions & 2 deletions examples/dag_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ int main(int argc, char **argv)
std::cout << "***************************************" << std::endl;
circuit_normalize_impl cn(c, false);
std::string m1 = cn.run_str(false);
std::string m2 = cn.run_str(true);
std::cout << "old methon\n";
std::cout << "old methon\n";
std::cout << m1 << "\n";

std::string m2 = cn.run_str(true);
std::cout << "new methon\n";
std::cout << m2 << "\n";

Expand Down
76 changes: 76 additions & 0 deletions examples/mc_mul_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <iostream>
#include <stp/stp_eigen.hpp>

using Eigen::MatrixXi;

void print(const matrix_chain& mc)
{
int i = 1;
for(const matrix& m : mc)
{
std::cout << "M" << i << ": " << m.rows() << "x" << m.cols() << "\n";
i++;
}
}

matrix random_generation(int row, int col)
{
matrix result(row, col);
for(int i = 0; i < row; i++)
for(int j = 0; j < col; j++)
result(i, j) = rand() % 2;
return result;
}

void test1()
{
matrix_chain mc;
for(int i = 0; i < 22; i++)
{
mc.push_back(random_generation(2, 4));
}
// print(mc);
matrix r1 = stp::matrix_chain_multiply(mc, true);
matrix r2 = stp::matrix_chain_multiply(mc, true, stp::mc_multiply_method::sequence);
assert(r1 == r2);
}

void test2()
{
matrix_chain mc;
for(int i = 0; i < 22; i++)
{
mc.push_back(random_generation(4, 2));
}
// print(mc);
matrix r1 = stp::matrix_chain_multiply(mc, true);
matrix r2 = stp::matrix_chain_multiply(mc, true, stp::mc_multiply_method::sequence);
assert(r1 == r2);
}

void test3()
{
matrix_chain mc;
for(int i = 0; i < 100; i++)
{
if(rand() % 2 == 1) mc.push_back(random_generation(4, 2));
else mc.push_back(random_generation(2, 4));
}
// print(mc);
matrix r1 = stp::matrix_chain_multiply(mc, true);
matrix r2 = stp::matrix_chain_multiply(mc, true, stp::mc_multiply_method::sequence);
assert(r1 == r2);
}


int main()
{
std::cout << "-------------------------------------------------------\n";
test1();
std::cout << "-------------------------------------------------------\n";
test2();
std::cout << "-------------------------------------------------------\n";
test3();
std::cout << "-------------------------------------------------------\n";
return 0;
}
47 changes: 47 additions & 0 deletions examples/stp_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <iostream>
#include <stp/stp_eigen.hpp>

using Eigen::MatrixXi;

matrix random_generation(int row, int col)
{
matrix result(row, col);
for(int i = 0; i < row; i++)
for(int j = 0; j < col; j++)
result(i, j) = rand() % 2;
return result;
}

int main()
{
//n % p = 0 type matrix multiplication test
for(int i = 0; i < 10; i++)
{
int m = 200;
int n = 4 << i;
int p = 4;
int q = 100;
matrix A = random_generation(m, n);
matrix B = random_generation(p, q);
stp::semi_tensor_product(A, B, true, stp::stp_method::definition_1);
stp::semi_tensor_product(A, B, true, stp::stp_method::definition_2);
stp::semi_tensor_product(A, B, true);
}

std::cout << "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n";

//p % n = 0 type matrix multiplication test
for(int i = 0; i < 10; i++)
{
int m = 200;
int n = 4;
int p = 4 << i;
int q = 100;
matrix A = random_generation(m, n);
matrix B = random_generation(p, q);
stp::semi_tensor_product(A, B, true, stp::stp_method::definition_1);
stp::semi_tensor_product(A, B, true, stp::stp_method::definition_2);
stp::semi_tensor_product(A, B, true);
}
return 0;
}
45 changes: 24 additions & 21 deletions include/stp/stp_circuit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/

/*!
\file stp_eigen.hpp
\file stp_circuit.hpp
\brief header file for some basic STP operations
\author Zhufei Chu
\author Ruibing Zhang
Expand Down Expand Up @@ -118,10 +118,12 @@ class stp_circuit
m_outputs.reserve(20);
}

stp_circuit(const std::string& str) //表达式字符串构造
//表达式字符串构造 f = (a & b & ~c) | (a & ~c) | (~b & ~c)
stp_circuit(const std::string& str)
{

}
}

#pragma region create Primary I / O and node
uint32_t create_pi(const std::string& name)
{
Expand Down Expand Up @@ -277,26 +279,27 @@ class stp_circuit
int tt_length = 1 << inputs_num;
matrix result(2, tt_length);
uint32_t idx = 0;
std::string str = "";
// 0x???
for(int i = 2; i < tt.size(); i++)
{
char c = tt[i];
std::string str = hex2bin(c);
for(int i = str.size() - tt_length; i < str.size(); i++ )
str += hex2bin(tt[i]);
}
for(int i = str.size() - tt_length; i < str.size(); i++ )
{
char t = str[i];
if(t == '0')
{
char t = str[i];
if(t == '0')
{
result(0, idx) = 0;
result(1, idx) = 1;
}
else
{
result(0, idx) = 1;
result(1, idx) = 0;
}
idx++;
result(0, idx) = 0;
result(1, idx) = 1;
}
}
else
{
result(0, idx) = 1;
result(1, idx) = 0;
}
idx++;
}
assert(idx == tt_length);
return result;
}
Expand All @@ -319,10 +322,10 @@ class stp_circuit
if(c == 'd' || c == 'D') return "1101";
if(c == 'e' || c == 'E') return "1110";
if(c == 'f' || c == 'F') return "1111";
std::cout << c << std::endl;
assert(false);
return "";
}

public:
void print_circuit()
{
Expand Down Expand Up @@ -356,7 +359,7 @@ class stp_circuit
}

private:
std::vector<node> m_nodes;
std::vector<node> m_nodes;
std::vector<id> m_inputs;
std::vector<id> m_outputs;

Expand Down
11 changes: 5 additions & 6 deletions include/stp/stp_dag2nmx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ namespace stp

get_chain();

result = se.normalize_matrix( chain );
result = normalize_matrix( chain );
return result;
}

Expand Down Expand Up @@ -116,7 +116,7 @@ namespace stp

chain = expr_to_chain( normal );

result = se.matrix_chain_multiply( chain );
result = matrix_chain_multiply( chain );

return result;
}
Expand Down Expand Up @@ -171,7 +171,7 @@ namespace stp
}
}

matrix get_matritx( const u_int32_t n )
matrix get_matritx( const uint32_t n )
{
if(circuit.is_pi(n))
{
Expand Down Expand Up @@ -200,7 +200,7 @@ namespace stp
{
str.erase(str.begin());
assert(std::stoi(str) == 2);
return se.generate_swap_matrix(2, 2);
return generate_swap_matrix(2, 2);
}
}
return circuit.get_node(n).get_mtx();
Expand Down Expand Up @@ -381,7 +381,7 @@ namespace stp
{
if( other_matrix[normal[i]].substr( 0, 1 ) == "I" )
{
new_chain.push_back( se.kronecker_product( chain[i], chain[i+1]) );
new_chain.push_back( kronecker_product( chain[i], chain[i+1]) );
i++;
}
else
Expand Down Expand Up @@ -429,7 +429,6 @@ namespace stp

private:
matrix result;
stp_eigen se;
const stp_circuit& circuit;
bool verbose;
matrix_chain chain;
Expand Down
Loading

0 comments on commit ae0b4fa

Please sign in to comment.