Skip to content

Commit

Permalink
Fix stabilizer Copy()
Browse files Browse the repository at this point in the history
  • Loading branch information
WrathfulSpatula committed Nov 3, 2024
1 parent 70adca6 commit 6d3ec4d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 22 deletions.
13 changes: 10 additions & 3 deletions include/qstabilizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,17 @@ class QStabilizer : public QInterface {
}

using QInterface::Copy;
void Copy(QInterfacePtr orig)
void Copy(QInterfacePtr orig) { Copy(std::dynamic_pointer_cast<QStabilizer>(orig)); }
void Copy(QStabilizerPtr orig)
{
throw std::domain_error("Can't TryDecompose() on QStabilizerHybrid! (If you know the system is exactly "
"separable, just use Decompose() instead.)");
QInterface::Copy(std::dynamic_pointer_cast<QInterface>(orig));
rawRandBools = orig->rawRandBools;
rawRandBoolsRemaining = orig->rawRandBoolsRemaining;
phaseOffset = orig->phaseOffset;
maxStateMapCacheQubitCount = orig->maxStateMapCacheQubitCount;
r = orig->r;
x = orig->x;
z = orig->z;
}

public:
Expand Down
38 changes: 29 additions & 9 deletions include/qstabilizerhybrid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class QStabilizerHybrid : public QParity, public QInterface {
protected:
bool useHostRam;
bool doNormalize;
bool isSparse;
bool useTGadget;
bool isRoundingFlushed;
bitLenInt thresholdQubits;
Expand Down Expand Up @@ -321,27 +320,48 @@ class QStabilizerHybrid : public QParity, public QInterface {

QInterfacePtr CloneBody(bool isCopy);
using QInterface::Copy;
void Copy(QInterfacePtr orig)
{
throw std::domain_error("Can't TryDecompose() on QStabilizerHybrid! (If you know the system is exactly "
"separable, just use Decompose() instead.)");
void Copy(QInterfacePtr orig) { Copy(std::dynamic_pointer_cast<QStabilizerHybrid>(orig)); }
void Copy(QStabilizerHybridPtr orig)
{
QInterface::Copy(std::dynamic_pointer_cast<QInterface>(orig));
useHostRam = orig->useHostRam;
doNormalize = orig->doNormalize;
useTGadget = orig->useTGadget;
isRoundingFlushed = orig->isRoundingFlushed;
thresholdQubits = orig->thresholdQubits;
ancillaCount = orig->ancillaCount;
deadAncillaCount = orig->deadAncillaCount;
maxEngineQubitCount = orig->maxEngineQubitCount;
maxAncillaCount = orig->maxAncillaCount;
maxStateMapCacheQubitCount = orig->maxStateMapCacheQubitCount;
separabilityThreshold = orig->separabilityThreshold;
roundingThreshold = orig->roundingThreshold;
devID = orig->devID;
phaseFactor = orig->phaseFactor;
logFidelity = orig->logFidelity;
engine = orig->engine;
stabilizer = orig->stabilizer;
deviceIDs = orig->deviceIDs;
engineTypes = orig->engineTypes;
cloneEngineTypes = orig->cloneEngineTypes;
shards = orig->shards;
stateMapCache = orig->stateMapCache;
}

public:
QStabilizerHybrid(std::vector<QInterfaceEngine> eng, bitLenInt qBitCount, const bitCapInt& initState = ZERO_BCI,
qrack_rand_gen_ptr rgp = nullptr, const complex& phaseFac = CMPLX_DEFAULT_ARG, bool doNorm = false,
bool randomGlobalPhase = true, bool useHostMem = false, int64_t deviceId = -1, bool useHardwareRNG = true,
bool useSparseStateVec = false, real1_f norm_thresh = REAL1_EPSILON, std::vector<int64_t> devList = {},
bool ignored = false, real1_f norm_thresh = REAL1_EPSILON, std::vector<int64_t> devList = {},
bitLenInt qubitThreshold = 0U, real1_f separation_thresh = _qrack_qunit_sep_thresh);

QStabilizerHybrid(bitLenInt qBitCount, const bitCapInt& initState = ZERO_BCI, qrack_rand_gen_ptr rgp = nullptr,
const complex& phaseFac = CMPLX_DEFAULT_ARG, bool doNorm = false, bool randomGlobalPhase = true,
bool useHostMem = false, int64_t deviceId = -1, bool useHardwareRNG = true, bool useSparseStateVec = false,
bool useHostMem = false, int64_t deviceId = -1, bool useHardwareRNG = true, bool ignored = false,
real1_f norm_thresh = REAL1_EPSILON, std::vector<int64_t> devList = {}, bitLenInt qubitThreshold = 0U,
real1_f separation_thresh = _qrack_qunit_sep_thresh)
: QStabilizerHybrid({ QINTERFACE_OPTIMAL_BASE }, qBitCount, initState, rgp, phaseFac, doNorm, randomGlobalPhase,
useHostMem, deviceId, useHardwareRNG, useSparseStateVec, norm_thresh, devList, qubitThreshold,
separation_thresh)
useHostMem, deviceId, useHardwareRNG, ignored, norm_thresh, devList, qubitThreshold, separation_thresh)
{
}

Expand Down
9 changes: 6 additions & 3 deletions include/qunitclifford.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@ class QUnitClifford : public QInterface {
complex phaseOffset;
std::vector<CliffordShard> shards;

void Copy(QInterfacePtr orig)
using QInterface::Copy;
void Copy(QInterfacePtr orig) { Copy(std::dynamic_pointer_cast<QUnitClifford>(orig)); }
void Copy(QUnitCliffordPtr orig)
{
throw std::domain_error("Can't TryDecompose() on QUnitClifford! (If you know the system is exactly separable, "
"just use Decompose() instead.)");
QInterface::Copy(std::dynamic_pointer_cast<QInterface>(orig));
phaseOffset = orig->phaseOffset;
shards = orig->shards;
}

void CombinePhaseOffsets(QStabilizerPtr unit)
Expand Down
13 changes: 6 additions & 7 deletions src/qstabilizerhybrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ QStabilizerHybrid::QStabilizerHybrid(std::vector<QInterfaceEngine> eng, bitLenIn
: QInterface(qBitCount, rgp, doNorm, useHardwareRNG, randomGlobalPhase, norm_thresh)
, useHostRam(useHostMem)
, doNormalize(doNorm)
, isSparse(useSparseStateVec)
, useTGadget(true)
, isRoundingFlushed(false)
, thresholdQubits(qubitThreshold)
Expand Down Expand Up @@ -121,15 +120,15 @@ QUnitCliffordPtr QStabilizerHybrid::MakeStabilizer(const bitCapInt& perm)
QInterfacePtr QStabilizerHybrid::MakeEngine(const bitCapInt& perm)
{
QInterfacePtr toRet = CreateQuantumInterface(engineTypes, qubitCount, perm, rand_generator, phaseFactor,
doNormalize, randGlobalPhase, useHostRam, devID, useRDRAND, isSparse, (real1_f)amplitudeFloor, deviceIDs,
doNormalize, randGlobalPhase, useHostRam, devID, useRDRAND, false, (real1_f)amplitudeFloor, deviceIDs,
thresholdQubits, separabilityThreshold);
toRet->SetConcurrency(GetConcurrencyLevel());
return toRet;
}
QInterfacePtr QStabilizerHybrid::MakeEngine(const bitCapInt& perm, bitLenInt qbCount)
{
QInterfacePtr toRet = CreateQuantumInterface(engineTypes, qbCount, perm, rand_generator, phaseFactor, doNormalize,
randGlobalPhase, useHostRam, devID, useRDRAND, isSparse, (real1_f)amplitudeFloor, deviceIDs, thresholdQubits,
randGlobalPhase, useHostRam, devID, useRDRAND, false, (real1_f)amplitudeFloor, deviceIDs, thresholdQubits,
separabilityThreshold);
toRet->SetConcurrency(GetConcurrencyLevel());
return toRet;
Expand Down Expand Up @@ -365,7 +364,7 @@ void QStabilizerHybrid::CacheEigenstate(bitLenInt target)
QInterfacePtr QStabilizerHybrid::CloneBody(bool isCopy)
{
QStabilizerHybridPtr c = std::make_shared<QStabilizerHybrid>(cloneEngineTypes, qubitCount, ZERO_BCI, rand_generator,
phaseFactor, doNormalize, randGlobalPhase, useHostRam, devID, useRDRAND, isSparse, (real1_f)amplitudeFloor,
phaseFactor, doNormalize, randGlobalPhase, useHostRam, devID, useRDRAND, false, (real1_f)amplitudeFloor,
std::vector<int64_t>{}, thresholdQubits, separabilityThreshold);

if (engine) {
Expand Down Expand Up @@ -589,7 +588,7 @@ bitLenInt QStabilizerHybrid::Compose(QStabilizerHybridPtr toCopy, bitLenInt star
QInterfacePtr QStabilizerHybrid::Decompose(bitLenInt start, bitLenInt length)
{
QStabilizerHybridPtr dest = std::make_shared<QStabilizerHybrid>(engineTypes, length, ZERO_BCI, rand_generator,
phaseFactor, doNormalize, randGlobalPhase, useHostRam, devID, useRDRAND, isSparse, (real1_f)amplitudeFloor,
phaseFactor, doNormalize, randGlobalPhase, useHostRam, devID, useRDRAND, false, (real1_f)amplitudeFloor,
std::vector<int64_t>{}, thresholdQubits, separabilityThreshold);

Decompose(start, dest);
Expand Down Expand Up @@ -660,7 +659,7 @@ bitLenInt QStabilizerHybrid::Allocate(bitLenInt start, bitLenInt length)
}

QStabilizerHybridPtr nQubits = std::make_shared<QStabilizerHybrid>(cloneEngineTypes, length, ZERO_BCI,
rand_generator, phaseFactor, doNormalize, randGlobalPhase, useHostRam, devID, useRDRAND, isSparse,
rand_generator, phaseFactor, doNormalize, randGlobalPhase, useHostRam, devID, useRDRAND, false,
(real1_f)amplitudeFloor, std::vector<int64_t>{}, thresholdQubits, separabilityThreshold);
return Compose(nQubits, start);
}
Expand Down Expand Up @@ -809,7 +808,7 @@ complex QStabilizerHybrid::GetAmplitudeOrProb(const bitCapInt& perm, bool isProb
}
QEnginePtr aEngine = std::dynamic_pointer_cast<QEngine>(
CreateQuantumInterface(et, ancillaCount, ZERO_BCI, rand_generator, ONE_CMPLX, false, false, useHostRam, devID,
useRDRAND, isSparse, (real1_f)amplitudeFloor, deviceIDs, thresholdQubits, separabilityThreshold));
useRDRAND, false, (real1_f)amplitudeFloor, deviceIDs, thresholdQubits, separabilityThreshold));

#if ENABLE_COMPLEX_X2
std::vector<complex2> top;
Expand Down

0 comments on commit 6d3ec4d

Please sign in to comment.