Skip to content

Commit

Permalink
More code simplifying
Browse files Browse the repository at this point in the history
  • Loading branch information
aromanro committed Aug 27, 2024
1 parent a806a93 commit 569a639
Showing 1 changed file with 47 additions and 35 deletions.
82 changes: 47 additions & 35 deletions QCSim/MPSSimulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -482,55 +482,67 @@ namespace QC {
}

inline void SetNewGammas(const MatrixClass& Umatrix, const MatrixClass& Vmatrix, IndexType qubit1, IndexType qubit2, IndexType szl, IndexType sz, IndexType szr)
{
if (sz != szl || sz != szr)
SetNewGammasDif(Umatrix, Vmatrix, qubit1, qubit2, szl, sz, szr);
else
SetNewGammasSame(Umatrix, Vmatrix, qubit1, qubit2, sz);

DivideGammasWithLambdas(qubit1, qubit2, szl, sz, szr);
}

inline void SetNewGammasDif(const MatrixClass& Umatrix, const MatrixClass& Vmatrix, IndexType qubit1, IndexType qubit2, IndexType szl, IndexType sz, IndexType szr)
{
Eigen::Tensor<std::complex<double>, 3> Utensor(szl, 2, sz);
Eigen::Tensor<std::complex<double>, 3> Vtensor(sz, 2, szr);

if (sz != szl || sz != szr)
{
#ifdef _DEBUG
std::cout << "Different sizes, szl=" << szl << " sz=" << sz << " szr=" << szr << std::endl;
std::cout << "Different sizes, szl=" << szl << " sz=" << sz << " szr=" << szr << std::endl;
#endif

for (IndexType k = 0; k < sz; ++k)
for (IndexType j = 0; j < 2; ++j)
for (IndexType i = 0; i < szl; ++i)
{
const IndexType jind = j * szl + i;
Utensor(i, j, k) = (jind < Umatrix.rows()) ? Umatrix(jind, k) : 0;
}
for (IndexType k = 0; k < sz; ++k)
for (IndexType j = 0; j < 2; ++j)
for (IndexType i = 0; i < szl; ++i)
{
const IndexType jind = j * szl + i;
Utensor(i, j, k) = (jind < Umatrix.rows()) ? Umatrix(jind, k) : 0;
}

for (IndexType k = 0; k < szr; ++k)
for (IndexType j = 0; j < 2; ++j)
for (IndexType i = 0; i < sz; ++i)
{
const IndexType jind = j * szr + k;
Vtensor(i, j, k) = (jind < Vmatrix.cols()) ? Vmatrix(i, jind) : 0;
}

gammas[qubit1] = Utensor;
gammas[qubit2] = Vtensor;
}

inline void SetNewGammasSame(const MatrixClass& Umatrix, const MatrixClass& Vmatrix, IndexType qubit1, IndexType qubit2, IndexType sz)
{
Eigen::Tensor<std::complex<double>, 3> Utensor(sz, 2, sz);
Eigen::Tensor<std::complex<double>, 3> Vtensor(sz, 2, sz);

for (IndexType k = 0; k < szr; ++k)
for (IndexType j = 0; j < 2; ++j)
for (IndexType i = 0; i < sz; ++i)
{
const IndexType jind = j * szr + k;
Vtensor(i, j, k) = (jind < Vmatrix.cols()) ? Vmatrix(i, jind) : 0;
}
}
else
{
#ifdef _DEBUG
std::cout << "Same sizes, sz=" << sz << std::endl;
std::cout << "Same sizes, sz=" << sz << std::endl;
#endif

for (IndexType k = 0; k < sz; ++k)
for (IndexType j = 0; j < 2; ++j)
for (IndexType i = 0; i < sz; ++i)
{
const IndexType jchi = j * sz;
IndexType jind = jchi + i;
Utensor(i, j, k) = (jind < Umatrix.rows()) ? Umatrix(jind, k) : 0;

jind = jchi + k;
Vtensor(i, j, k) = (jind < Vmatrix.cols()) ? Vmatrix(i, jind) : 0;
}
}
for (IndexType k = 0; k < sz; ++k)
for (IndexType j = 0; j < 2; ++j)
for (IndexType i = 0; i < sz; ++i)
{
const IndexType jchi = j * sz;
IndexType jind = jchi + i;
Utensor(i, j, k) = (jind < Umatrix.rows()) ? Umatrix(jind, k) : 0;

jind = jchi + k;
Vtensor(i, j, k) = (jind < Vmatrix.cols()) ? Vmatrix(i, jind) : 0;
}

gammas[qubit1] = Utensor;
gammas[qubit2] = Vtensor;

DivideGammasWithLambdas(qubit1, qubit2, szl, sz, szr);
}

inline void DivideGammasWithLambdas(IndexType qubit1, IndexType qubit2, IndexType szl, IndexType sz, IndexType szr)
Expand Down

0 comments on commit 569a639

Please sign in to comment.