Skip to content

Commit

Permalink
Fixed VQE mode and bitstring length
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Claudino <[email protected]>
  • Loading branch information
danclaudino committed Dec 21, 2023
1 parent 59bf2ee commit 001398f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,26 @@ void HPCVirtDecorator::initialize(const HeterogeneousMap &params) {
n_virtual_qpus = params.get<int>("n-virtual-qpus");
}

shots = -1;
if (params.keyExists<int>("shots")) {
shots = params.get<int>("shots");
if (shots < 1) {
xacc::error("Invalid 'shots' parameter.");
}
}

isVqeMode = (shots < 1);
if (params.keyExists<bool>("vqe-mode")) {
isVqeMode = params.get<bool>("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.");
}
}

Expand Down Expand Up @@ -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;
};

Expand All @@ -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);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProcessGroup> qpuComm;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,13 @@ std::shared_ptr<xacc::Observable> 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 "
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 001398f

Please sign in to comment.