Skip to content

Commit

Permalink
Approx. sampling -> weak sampling
Browse files Browse the repository at this point in the history
  • Loading branch information
WrathfulSpatula committed Jul 16, 2023
1 parent 204e95a commit 30923ba
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 45 deletions.
2 changes: 1 addition & 1 deletion include/pinvoke_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ MICROSOFT_QUANTUM_DECL void ResetUnitaryFidelity(_In_ uintq sid);
MICROSOFT_QUANTUM_DECL void SetSdrp(_In_ uintq sid, _In_ double sdrp);
MICROSOFT_QUANTUM_DECL void SetReactiveSeparate(_In_ uintq sid, _In_ bool irs);
MICROSOFT_QUANTUM_DECL void SetTInjection(_In_ uintq sid, _In_ bool iti);
MICROSOFT_QUANTUM_DECL void SetStabilizerApproxSampling(_In_ uintq sid, _In_ bool she);
MICROSOFT_QUANTUM_DECL void SetStabilizerWeakSampling(_In_ uintq sid, _In_ bool sws);

#if !(FPPOW < 6 && !ENABLE_COMPLEX_X2)
MICROSOFT_QUANTUM_DECL void TimeEvolve(_In_ uintq sid, _In_ double t, _In_ uintq n,
Expand Down
22 changes: 11 additions & 11 deletions include/qinterface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2550,23 +2550,23 @@ class QInterface : public ParallelFor {
*/
virtual bool GetTInjection() { return false; }
/**
* Set option to allow QStabilizerHybrid to sample approximately.
* Set option to allow QStabilizerHybrid to sample weakly.
*
* If approximate sampling is enabled during use of special gate set Clifford+RZ with QStabilizerHybrid, then
* terminal measurement and sampling approximate the effect of buffered RZ gates with the closest Clifford phase
* gate, plus a probabilistic 'correction' gate, for difference between requested phase angle and closest Clifford
* phase transformation.
* If weak sampling is enabled during use of special gate set Clifford+RZ with QStabilizerHybrid, then terminal
* measurement and sampling approximate the effect of buffered RZ gates with the closest Clifford phase gate, plus a
* probabilistic 'correction' gate, for difference between requested phase angle and closest Clifford phase
* transformation.
*/
virtual void SetStabilizerApproxSampling(bool isApprox) {}
virtual void SetStabilizerWeakSampling(bool isWeak) {}
/**
* Get option to allow QStabilizerHybrid to sample approximately.
*
* If approximate sampling is enabled during use of special gate set Clifford+RZ with QStabilizerHybrid, then
* terminal measurement and sampling approximate the effect of buffered RZ gates with the closest Clifford phase
* gate, plus a probabilistic 'correction' gate, for difference between requested phase angle and closest Clifford
* phase transformation.
* If weak sampling is enabled during use of special gate set Clifford+RZ with QStabilizerHybrid, then terminal
* measurement and sampling approximate the effect of buffered RZ gates with the closest Clifford phase gate, plus a
* probabilistic 'correction' gate, for difference between requested phase angle and closest Clifford phase
* transformation.
*/
virtual bool GetStabilizerApproxSampling() { return false; }
virtual bool GetStabilizerWeakSampling() { return false; }

/**
* Clone this QInterface
Expand Down
6 changes: 3 additions & 3 deletions include/qstabilizerhybrid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class QStabilizerHybrid : public QParity, public QInterface {
bool doNormalize;
bool isSparse;
bool useTGadget;
bool isApproxSampling;
bool isWeakSampling;
bitLenInt thresholdQubits;
bitLenInt ancillaCount;
bitLenInt maxEngineQubitCount;
Expand Down Expand Up @@ -269,8 +269,8 @@ class QStabilizerHybrid : public QParity, public QInterface {

void SetTInjection(bool useGadget) { useTGadget = useGadget; }
bool GetTInjection() { return useTGadget; }
void SetStabilizerApproxSampling(bool isApprox) { isApproxSampling = isApprox; }
bool GetStabilizerApproxSampling() { return isApproxSampling; }
void SetStabilizerWeakSampling(bool isApprox) { isWeakSampling = isApprox; }
bool GetStabilizerWeakSampling() { return isWeakSampling; }

void Finish()
{
Expand Down
4 changes: 2 additions & 2 deletions src/pinvoke_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2824,11 +2824,11 @@ MICROSOFT_QUANTUM_DECL void SetTInjection(_In_ uintq sid, _In_ bool irs)
}
}

MICROSOFT_QUANTUM_DECL void SetStabilizerApproxSampling(_In_ uintq sid, _In_ bool she)
MICROSOFT_QUANTUM_DECL void SetStabilizerWeakSampling(_In_ uintq sid, _In_ bool sws)
{
SIMULATOR_LOCK_GUARD_VOID(sid)
try {
simulator->SetStabilizerApproxSampling(she);
simulator->SetStabilizerWeakSampling(sws);
} catch (const std::exception& ex) {
simulatorErrors[sid] = 1;
std::cout << ex.what() << std::endl;
Expand Down
14 changes: 7 additions & 7 deletions src/qstabilizerhybrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ QStabilizerHybrid::QStabilizerHybrid(std::vector<QInterfaceEngine> eng, bitLenIn
, doNormalize(doNorm)
, isSparse(useSparseStateVec)
, useTGadget(true)
, isApproxSampling(false)
, isWeakSampling(false)
, thresholdQubits(qubitThreshold)
, ancillaCount(0U)
, maxEngineQubitCount(27U)
Expand Down Expand Up @@ -179,7 +179,7 @@ void QStabilizerHybrid::FlushIfBlocked(bitLenInt control, bitLenInt target, bool
// Hakop Pashayan, Oliver Reardon-Smith, Kamil Korzekwa, and Stephen D. Bartlett
// PRX Quantum 3, 020361 – Published 23 June 2022

if (!useTGadget || (!isApproxSampling && (ancillaCount >= maxAncillaCount))) {
if (!useTGadget || (!isWeakSampling && (ancillaCount >= maxAncillaCount))) {
// The option to optimize this case is off.
SwitchToEngine();
return;
Expand Down Expand Up @@ -940,8 +940,8 @@ void QStabilizerHybrid::Mtrx(const complex* lMtrx, bitLenInt target)
complex mtrx[4U];
if (!wasCached) {
std::copy(lMtrx, lMtrx + 4U, mtrx);
} else if (!engine && useTGadget && (target < qubitCount) &&
(isApproxSampling || (ancillaCount < maxAncillaCount)) && !IS_PHASE(lMtrx) && !IS_INVERT(lMtrx) &&
} else if (!engine && useTGadget && (target < qubitCount) && (isWeakSampling || (ancillaCount < maxAncillaCount)) &&
!IS_PHASE(lMtrx) && !IS_INVERT(lMtrx) &&
(shard->IsPhase() || shard->IsInvert() || shard->IsHPhase() || shard->IsHInvert())) {

if (shard->IsHPhase() || shard->IsHInvert()) {
Expand Down Expand Up @@ -1419,7 +1419,7 @@ bitCapInt QStabilizerHybrid::MAll()
return toRet;
}

if (isApproxSampling && ancillaCount && !IsLogicalProbBuffered()) {
if (isWeakSampling && ancillaCount && !IsLogicalProbBuffered()) {
WeakSampleAncillae();
}

Expand Down Expand Up @@ -1528,7 +1528,7 @@ std::map<bitCapInt, int> QStabilizerHybrid::MultiShotMeasureMask(const std::vect

std::map<bitCapInt, int> results;

if (!IsProbBuffered() || (isApproxSampling && !IsLogicalProbBuffered())) {
if (!IsProbBuffered() || (isWeakSampling && !IsLogicalProbBuffered())) {
std::mutex resultsMutex;
par_for(0U, shots, [&](const bitCapIntOcl& shot, const unsigned& cpu) {
const bitCapInt sample = SampleClone(qPowers);
Expand Down Expand Up @@ -1613,7 +1613,7 @@ void QStabilizerHybrid::MultiShotMeasureMask(
return;
}

if (!IsProbBuffered() || (isApproxSampling && !IsLogicalProbBuffered())) {
if (!IsProbBuffered() || (isWeakSampling && !IsLogicalProbBuffered())) {
par_for(0U, shots,
[&](const bitCapIntOcl& shot, const unsigned& cpu) { shotsArray[shot] = (unsigned)SampleClone(qPowers); });

Expand Down
24 changes: 12 additions & 12 deletions test/benchmarks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ void benchmarkLoopVariable(std::function<void(QInterfacePtr, bitLenInt)> fn, bit
if (disable_reactive_separation) {
qftReg->SetReactiveSeparate(false);
}
if (enable_approx_sampling) {
qftReg->SetStabilizerApproxSampling(true);
if (enable_weak_sampling) {
qftReg->SetStabilizerWeakSampling(true);
}
avgt = 0.0;
sampleFailureCount = 0;
Expand Down Expand Up @@ -177,8 +177,8 @@ void benchmarkLoopVariable(std::function<void(QInterfacePtr, bitLenInt)> fn, bit
if (disable_reactive_separation) {
qftReg->SetReactiveSeparate(false);
}
if (enable_approx_sampling) {
qftReg->SetStabilizerApproxSampling(true);
if (enable_weak_sampling) {
qftReg->SetStabilizerWeakSampling(true);
}

sampleFailureCount++;
Expand Down Expand Up @@ -206,8 +206,8 @@ void benchmarkLoopVariable(std::function<void(QInterfacePtr, bitLenInt)> fn, bit
if (disable_reactive_separation) {
qftReg->SetReactiveSeparate(false);
}
if (enable_approx_sampling) {
qftReg->SetStabilizerApproxSampling(true);
if (enable_weak_sampling) {
qftReg->SetStabilizerWeakSampling(true);
}

sampleFailureCount++;
Expand Down Expand Up @@ -256,8 +256,8 @@ void benchmarkLoopVariable(std::function<void(QInterfacePtr, bitLenInt)> fn, bit
if (disable_reactive_separation) {
qftReg->SetReactiveSeparate(false);
}
if (enable_approx_sampling) {
qftReg->SetStabilizerApproxSampling(true);
if (enable_weak_sampling) {
qftReg->SetStabilizerWeakSampling(true);
}

sampleFailureCount++;
Expand Down Expand Up @@ -4076,8 +4076,8 @@ TEST_CASE("test_universal_circuit_digital_cross_entropy", "[supreme]")
if (disable_reactive_separation) {
goldStandard->SetReactiveSeparate(false);
}
if (enable_approx_sampling) {
goldStandard->SetStabilizerApproxSampling(true);
if (enable_weak_sampling) {
goldStandard->SetStabilizerWeakSampling(true);
}

for (d = 0; d < Depth; d++) {
Expand Down Expand Up @@ -4210,8 +4210,8 @@ TEST_CASE("test_universal_circuit_digital_cross_entropy", "[supreme]")
if (disable_reactive_separation) {
testCase->SetReactiveSeparate(false);
}
if (enable_approx_sampling) {
testCase->SetStabilizerApproxSampling(true);
if (enable_weak_sampling) {
testCase->SetStabilizerWeakSampling(true);
}

std::map<bitCapInt, int> testCaseResult;
Expand Down
5 changes: 2 additions & 3 deletions test/benchmarks_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ qrack_rand_gen_ptr rng;
bool enable_normalization = false;
bool disable_t_injection = false;
bool disable_reactive_separation = false;
bool enable_approx_sampling = false;
bool enable_weak_sampling = false;
bool disable_terminal_measurement = false;
bool use_host_dma = false;
bool disable_hardware_rng = false;
Expand Down Expand Up @@ -126,8 +126,7 @@ int main(int argc, char* argv[])
Opt(disable_t_injection)["--disable-t-injection"](
"Disable reverse t-injection gadget, in stabilizer simulator.") |
Opt(disable_reactive_separation)["--disable-reactive-separation"]("Disable QUnit 'reactive' separation") |
Opt(enable_approx_sampling)["--enable-approx-sampling"](
"Enable approximate sampling, in stabilizer simulator.") |
Opt(enable_weak_sampling)["--enable-weask-sampling"]("Enable weak sampling, in stabilizer simulator.") |
Opt(disable_terminal_measurement)["--disable-terminal-measurement"](
"Disable final measurement step in benchmarks") |
Opt(use_host_dma)["--use-host-dma"](
Expand Down
9 changes: 4 additions & 5 deletions test/test_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ qrack_rand_gen_ptr rng;
bool enable_normalization = false;
bool disable_t_injection = false;
bool disable_reactive_separation = false;
bool enable_approx_sampling = false;
bool enable_weak_sampling = false;
bool use_host_dma = false;
bool disable_hardware_rng = false;
bool async_time = false;
Expand Down Expand Up @@ -109,8 +109,7 @@ int main(int argc, char* argv[])
Opt(disable_t_injection)["--disable-t-injection"](
"Disable reverse t-injection gadget, in stabilizer simulator.") |
Opt(disable_reactive_separation)["--disable-reactive-separation"]("Disable QUnit 'reactive' separation") |
Opt(enable_approx_sampling)["--enable-approx-sampling"](
"Enable approximate sampling, in stabilizer simulator.") |
Opt(enable_weak_sampling)["--enable-weak-sampling"]("Enable weak sampling, in stabilizer simulator.") |
Opt(disable_hardware_rng)["--disable-hardware-rng"]("Modern Intel chips provide an instruction for hardware "
"random number generation, which this option turns off. "
"(Hardware generation is on by default, if available.)") |
Expand Down Expand Up @@ -486,7 +485,7 @@ QInterfaceTestFixture::QInterfaceTestFixture()
if (disable_reactive_separation) {
qftReg->SetReactiveSeparate(false);
}
if (enable_approx_sampling) {
qftReg->SetStabilizerApproxSampling(true);
if (enable_weak_sampling) {
qftReg->SetStabilizerWeakSampling(true);
}
}
2 changes: 1 addition & 1 deletion test/tests.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ extern qrack_rand_gen_ptr rng;
extern bool enable_normalization;
extern bool disable_t_injection;
extern bool disable_reactive_separation;
extern bool enable_approx_sampling;
extern bool enable_weak_sampling;
extern bool disable_terminal_measurement;
extern bool use_host_dma;
extern bool disable_hardware_rng;
Expand Down

0 comments on commit 30923ba

Please sign in to comment.