Skip to content

Commit

Permalink
Optimize/debug QPager::Compose()
Browse files Browse the repository at this point in the history
  • Loading branch information
WrathfulSpatula committed Jul 12, 2021
1 parent 3eb872f commit c6b3443
Showing 1 changed file with 44 additions and 15 deletions.
59 changes: 44 additions & 15 deletions src/qpager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,40 +438,69 @@ void QPager::CombineAndOpControlled(
CombineAndOp(fn, bits);
}

bitLenInt QPager::Compose(QPagerPtr toCopy) { return Compose(toCopy, qubitCount); }

bitLenInt QPager::Compose(QPagerPtr toCopy, bitLenInt start)
bitLenInt QPager::Compose(QPagerPtr toCopy)
{
if ((qubitCount + toCopy->qubitCount) > maxQubits) {
throw std::invalid_argument(
"Cannot instantiate a QPager with greater capacity than environment variable QRACK_MAX_PAGING_QB.");
}

// TODO: Avoid CombineEngines();
CombineEngines();

bitLenInt qpp = qubitCount;
bitLenInt qpp = qubitsPerPage();
bitLenInt tcqpp = toCopy->qubitsPerPage();

if ((qpp + tcqpp) > maxPageQubits) {
tcqpp = (maxPageQubits <= qpp) ? 1U : (maxPageQubits - qpp);
toCopy->SeparateEngines(tcqpp, true);
}

bitCapIntOcl i;
bitCapIntOcl maxI = ((bitCapIntOcl)toCopy->qPages.size() - 1U);
if ((qpp + tcqpp) > maxPageQubits) {
qpp = (maxPageQubits <= tcqpp) ? 1U : (maxPageQubits - tcqpp);
SeparateEngines(qpp, true);
}

bitLenInt pqc = pagedQubitCount();

bitCapIntOcl i, j;
bitCapIntOcl maxJ = ((bitCapIntOcl)toCopy->qPages.size() - 1U);
std::vector<QEnginePtr> nQPages;

QEnginePtr engine = qPages[0];
for (i = 0; i < maxI; i++) {
nQPages.push_back(std::dynamic_pointer_cast<QEngine>(engine->Clone()));
nQPages.back()->Compose(toCopy->qPages[i], start);
for (i = 0; i < qPages.size(); i++) {
QEnginePtr engine = qPages[i];
for (j = 0; j < maxJ; j++) {
nQPages.push_back(std::dynamic_pointer_cast<QEngine>(engine->Clone()));
nQPages.back()->Compose(toCopy->qPages[j]);
}
nQPages.push_back(engine);
nQPages.back()->Compose(toCopy->qPages[maxJ]);
}
nQPages.push_back(engine);
nQPages.back()->Compose(toCopy->qPages[maxI], start);

qPages = nQPages;

bitLenInt toRet = qubitCount;
SetQubitCount(qubitCount + toCopy->qubitCount);

ROL(pqc, qpp, pqc + toCopy->qubitCount);

return toRet;
}

bitLenInt QPager::Compose(QPagerPtr toCopy, bitLenInt start)
{
if (start == qubitCount) {
return Compose(toCopy);
}

if ((qubitCount + toCopy->qubitCount) > maxQubits) {
throw std::invalid_argument(
"Cannot instantiate a QPager with greater capacity than environment variable QRACK_MAX_PAGING_QB.");
}

// TODO: Avoid CombineEngines();
CombineEngines();
toCopy->CombineEngines();

qPages[0]->Compose(toCopy->qPages[0]);

SetQubitCount(qubitCount + toCopy->qubitCount);

return start;
Expand Down

0 comments on commit c6b3443

Please sign in to comment.