diff --git a/quantum/plugins/decorators/hpc-virtualization/hpc_virt_decorator.cpp b/quantum/plugins/decorators/hpc-virtualization/hpc_virt_decorator.cpp index 33a12a09a..b07bcaa04 100644 --- a/quantum/plugins/decorators/hpc-virtualization/hpc_virt_decorator.cpp +++ b/quantum/plugins/decorators/hpc-virtualization/hpc_virt_decorator.cpp @@ -54,6 +54,7 @@ void HPCVirtDecorator::initialize(const HeterogeneousMap ¶ms) { n_virtual_qpus = params.get("n-virtual-qpus"); } + shots = -1; if (params.keyExists("shots")) { shots = params.get("shots"); if (shots < 1) { @@ -61,12 +62,18 @@ void HPCVirtDecorator::initialize(const HeterogeneousMap ¶ms) { } } - isVqeMode = (shots < 1); if (params.keyExists("vqe-mode")) { isVqeMode = params.get("vqe-mode"); - if (isVqeMode) { - xacc::info("Enable VQE Mode."); - } + } else { + isVqeMode = (shots < 1); + } + + if (shots >= 1 && isVqeMode || shots < 1 && !isVqeMode) { + xacc::error("Please choose between shot-based simulation or VQE mode."); + } else if (shots >= 1 && isVqeMode) { + xacc::info("Shot-based simulation takes precedence over VQE mode. Running shot-based simulation.."); + } else { + xacc::info("Enable VQE mode."); } } @@ -346,14 +353,12 @@ void HPCVirtDecorator::execute( } // get binary from decimal - const auto getBinary = [=](int n){ + const auto getBinary = [=](int decimal){ std::string s; - while (n != 0) { - s += (n % 2 == 0 ? "0" : "1" ); - n /= 2; - } - //s += std::string(buffer->size() - s.size(), '0'); - std::reverse(s.begin(), s.end()); + do { + s += (decimal % 2 == 0 ? "0" : "1" ); + decimal /= 2; + } while (decimal != 0); return s; }; @@ -377,11 +382,15 @@ void HPCVirtDecorator::execute( for (int b = 0; b < nChildBitStrings; b++) { auto counts = globalCounts[b + countShift]; - if (counts == 0) std::cout << "GOTCHA\n"; auto bitStringDecimal = globalBitStrings[b + countShift]; - auto nBits = name.length() / 2; auto bitString = getBinary(bitStringDecimal); - //auto bitString = getBinary(bitStringDecimal, nBits); + // check if we need to pad zeros + auto nMeasuredBits = std::count_if(name.begin(), name.end(), + [](char c){ return std::string("XYZ").find(c) != std::string::npos; }); + if (nMeasuredBits > bitString.size()) { + bitString += std::string(nMeasuredBits - bitString.size(), '0'); + } + std::reverse(bitString.begin(), bitString.end()); child->appendMeasurement(bitString, counts); } diff --git a/quantum/plugins/decorators/hpc-virtualization/hpc_virt_decorator.hpp b/quantum/plugins/decorators/hpc-virtualization/hpc_virt_decorator.hpp index 64d044c22..58e9e5299 100644 --- a/quantum/plugins/decorators/hpc-virtualization/hpc_virt_decorator.hpp +++ b/quantum/plugins/decorators/hpc-virtualization/hpc_virt_decorator.hpp @@ -27,7 +27,7 @@ class HPCVirtDecorator : public AcceleratorDecorator { protected: bool isVqeMode; - int n_virtual_qpus = 1, shots = -1; + int n_virtual_qpus = 1, shots; // The MPI communicator for each QPU std::shared_ptr qpuComm; diff --git a/quantum/plugins/decorators/hpc-virtualization/tests/HpcVirtTester.cpp b/quantum/plugins/decorators/hpc-virtualization/tests/HpcVirtTester.cpp index ed787be4d..a4fbc73df 100644 --- a/quantum/plugins/decorators/hpc-virtualization/tests/HpcVirtTester.cpp +++ b/quantum/plugins/decorators/hpc-virtualization/tests/HpcVirtTester.cpp @@ -173,13 +173,13 @@ std::shared_ptr getH(const int nq) { TEST(HpcVirtTester, checkH2) { const int n_virt_qpus = 2; const int n_layers = 10; - const std::string acc = "qpp"; + const std::string acc = "qsim"; xacc::set_verbose(true); xacc::ScopeTimer timer("mpi_timing", false); auto accelerator = xacc::getAccelerator(acc); accelerator = xacc::getAcceleratorDecorator( "hpc-virtualization", accelerator, - {{"vqe-mode", false}, {"n-virtual-qpus", n_virt_qpus}}); + {{"vqe-mode", true}, {"n-virtual-qpus", n_virt_qpus}}); auto buffer = xacc::qalloc(2); auto observable = xacc::quantum::getObservable( "pauli", std::string("-0.349833 - 0.388748 Z0 - 0.388748 Z1 + 0.181771 " @@ -222,7 +222,7 @@ TEST(HpcVirtTester, checkMcVQE) { auto accelerator = xacc::getAccelerator(acc); accelerator = xacc::getAcceleratorDecorator( "hpc-virtualization", accelerator, - {{"vqe-mode", false}, {"n-virtual-qpus", n_virt_qpus}}); + {{"vqe-mode", true}, {"n-virtual-qpus", n_virt_qpus}}); auto buffer = xacc::qalloc(nq); auto ansatz = getCircuit(nq); @@ -255,7 +255,7 @@ TEST(HpcVirtTester, checkMcVQE) { } TEST(HpcVirtTester, checkGradients) { - auto accelerator = xacc::getAccelerator("qpp"); + auto accelerator = xacc::getAccelerator("qsim"); accelerator = xacc::getAcceleratorDecorator("hpc-virtualization", accelerator, {{"n-virtual-qpus", 2}}); auto buffer = xacc::qalloc(3);