Skip to content

Commit

Permalink
reverseWires() -> getReverseWires()
Browse files Browse the repository at this point in the history
  • Loading branch information
WrathfulSpatula committed May 31, 2024
1 parent 9b67ca5 commit 79793dd
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions pennylane_qrack/qrack_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,6 @@ struct QrackDevice final : public Catalyst::Runtime::QuantumDevice {
static constexpr bool QRACK_RESULT_TRUE_CONST = true;
static constexpr bool QRACK_RESULT_FALSE_CONST = false;

inline auto reverseWires() -> void
{
const bitLenInt count = qsim->GetQubitCount();
const bitLenInt end = count - 1U;
const bitLenInt maxLcv = count >> 1U;
for (bitLenInt i = 0U; i < maxLcv; ++i) {
qsim->Swap(i, end - i);
}
}

inline auto getDeviceWires(const std::vector<QubitIdType> &wires) -> std::vector<bitLenInt>
{
std::vector<bitLenInt> res;
Expand All @@ -57,6 +47,14 @@ struct QrackDevice final : public Catalyst::Runtime::QuantumDevice {
return res;
}

inline auto getReverseWires(const std::vector<QubitIdType> &wires) -> std::vector<bitLenInt>
{
std::vector<bitLenInt> res;
res.reserve(wires.size());
std::transform(wires.begin(), wires.end(), std::back_inserter(res), [](auto w) { return (bitLenInt)w; });
return res;
}

inline auto wiresToMask(const std::vector<bitLenInt> &wires) -> bitCapInt
{
bitCapInt mask = Qrack::ZERO_BCI;
Expand Down Expand Up @@ -745,16 +743,14 @@ struct QrackDevice final : public Catalyst::Runtime::QuantumDevice {
void PartialProbs(DataView<double, 1> &p, const std::vector<QubitIdType> &wires) override
{
RT_FAIL_IF((size_t)Qrack::pow2(wires.size()) != p.size(), "Invalid size for the pre-allocated probabilities vector");
auto &&dev_wires = getDeviceWires(wires);
reverseWires();
auto &&dev_wires = getReverseWires(wires);
#if FPPOW == 6
qsim->ProbBitsAll(dev_wires, &(*(p.begin())));
#else
std::unique_ptr<Qrack::real1> _p(new Qrack::real1[p.size()]);
qsim->ProbBitsAll(dev_wires, _p.get());
std::copy(_p.get(), _p.get() + p.size(), p.begin());
#endif
reverseWires();
}
void Sample(DataView<double, 2> &samples, size_t shots) override
{
Expand Down Expand Up @@ -782,14 +778,12 @@ struct QrackDevice final : public Catalyst::Runtime::QuantumDevice {
// that could be instead implied by the size of "samples."
RT_FAIL_IF(samples.size() != shots, "Invalid size for the pre-allocated samples");

auto &&dev_wires = getDeviceWires(wires);
auto &&dev_wires = getReverseWires(wires);
std::vector<bitCapInt> qPowers(dev_wires.size());
for (size_t i = 0U; i < qPowers.size(); ++i) {
qPowers[i] = Qrack::pow2((bitLenInt)dev_wires[i]);
}
reverseWires();
auto q_samples = qsim->MultiShotMeasureMask(qPowers, shots);
reverseWires();

auto samplesIter = samples.begin();
for (size_t shot = 0U; shot < shots; ++shot) {
Expand Down Expand Up @@ -841,14 +835,12 @@ struct QrackDevice final : public Catalyst::Runtime::QuantumDevice {
RT_FAIL_IF(eigvals.size() != numElements || counts.size() != numElements,
"Invalid size for the pre-allocated counts");

auto &&dev_wires = getDeviceWires(wires);
auto &&dev_wires = getReverseWires(wires);
std::vector<bitCapInt> qPowers(dev_wires.size());
for (size_t i = 0U; i < qPowers.size(); ++i) {
qPowers[i] = Qrack::pow2(dev_wires[i]);
}
reverseWires();
auto q_samples = qsim->MultiShotMeasureMask(qPowers, shots);
reverseWires();

std::iota(eigvals.begin(), eigvals.end(), 0);
std::fill(counts.begin(), counts.end(), 0);
Expand Down

0 comments on commit 79793dd

Please sign in to comment.