Skip to content

Commit

Permalink
Debug QUnit::TryDecompose()
Browse files Browse the repository at this point in the history
  • Loading branch information
WrathfulSpatula committed Nov 6, 2024
1 parent 8457950 commit 610b9cf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 28 deletions.
22 changes: 14 additions & 8 deletions include/qunit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,16 @@ class QUnit : public QParity, public QInterface {

const bitLenInt length = dest->GetQubitCount();

if ((start + length) > qubitCount) {
throw std::invalid_argument("QUnit::TryDecompose qubit range out-of-bounds!");
}

for (bitLenInt i = 0U; i < length; ++i) {
if (!shards[i].unit) {
QEngineShard& shard = shards[start + i];
if (!shard.unit) {
continue;
}
if (!shards[i].unit->isBinaryDecisionTree()) {
if (!shard.unit->isBinaryDecisionTree()) {
return QInterface::TryDecompose(start, dest, error_tol);
}
}
Expand All @@ -135,17 +140,18 @@ class QUnit : public QParity, public QInterface {
Swap(start + i, qubitCount - (i + 1U));
}

const bool isSeparable = TryDetach(nStart);
if (TryDetach(nStart)) {
Decompose(qubitCount - length, dest);
for (bitLenInt i = shift; i > 0U; --i) {
dest->Swap(i - 1U, dest->GetQubitCount() - i);
}
return true;
}

for (bitLenInt i = shift; i > 0U; --i) {
Swap(start + (i - 1U), qubitCount - i);
}

if (isSeparable) {
Decompose(start, dest);
return true;
}

return false;
}

Expand Down
27 changes: 7 additions & 20 deletions src/qunit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,10 @@ void QUnit::Detach(bitLenInt start, bitLenInt length, QUnitPtr dest)

bool QUnit::TryDetach(bitLenInt length)
{
if (!length || (length > qubitCount)) {
if (!length || (length == qubitCount)) {
return true;
}
if (length > qubitCount) {
throw std::invalid_argument("QUnit::Detach range is out-of-bounds!");
}

Expand Down Expand Up @@ -417,26 +420,10 @@ bool QUnit::TryDetach(bitLenInt length)
// After ordering all subunits contiguously, since the top level mapping is a contiguous array, all subunit sets are
// also contiguous. From the lowest index bits, they are mapped simply for the length count of bits involved in the
// entire subunit.
std::map<QBdtPtr, bitLenInt> decomposedUnits;
for (bitLenInt i = 0U; i < length; ++i) {
QEngineShard& shard = shards[start + i];
QBdtPtr unit = std::dynamic_pointer_cast<QBdt>(shard.unit);

if (unit == NULL) {
continue;
}

if (decomposedUnits.find(unit) == decomposedUnits.end()) {
decomposedUnits[unit] = start + i;
const bitLenInt subLen = subunits[unit];
const bitLenInt origLen = unit->GetQubitCount();
if ((subLen != origLen) && !unit->IsSeparable(shard.mapped)) {
return false;
}
}
}
QEngineShard& shard = shards[start];
QBdtPtr unit = std::dynamic_pointer_cast<QBdt>(shard.unit);

return true;
return (unit == NULL) || (shard.mapped == 0U) || unit->IsSeparable(shard.mapped);
}

QInterfacePtr QUnit::EntangleInCurrentBasis(
Expand Down

0 comments on commit 610b9cf

Please sign in to comment.