Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion runtime/common/Future.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ sample_result future::get() {
dynamic_cast<QirServerHelper *>(serverHelper.get());
if (!qirServerHelper)
throw std::runtime_error("To support `run` API, " + qpuName +
"must inherit `QirServerHelper` class");
" must inherit `QirServerHelper` class");
if (!inFutureRawOutput)
throw std::runtime_error(
"cudaq::details::future::get() for 'run' requires a raw output "
Expand Down
42 changes: 40 additions & 2 deletions runtime/common/ServerHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

#pragma once

#include "nlohmann/json.hpp"

#include "ExecutionContext.h"
#include "Future.h"
#include "Registry.h"
#include "RuntimeTarget.h"
#include "SampleResult.h"
#include "common/RecordLogParser.h"
#include "nlohmann/json.hpp"
#include <filesystem>

namespace cudaq {
Expand Down Expand Up @@ -168,5 +168,43 @@ class QirServerHelper {
/// @return QIR output log
virtual std::string extractOutputLog(ServerMessage &postJobResponse,
std::string &jobId) = 0;

/// @brief Create a sampling result from the QIR output log
cudaq::sample_result
createSampleResultFromQirOutput(const std::string &qirOutputLog) {
// Parse the QIR output log
cudaq::RecordLogParser parser;
parser.parse(qirOutputLog);

// Get the buffer and length of buffer (in bytes) from the parser.
auto *origBuffer = parser.getBufferPtr();
std::size_t bufferSize = parser.getBufferSize();
char *buffer = static_cast<char *>(malloc(bufferSize));
std::memcpy(buffer, origBuffer, bufferSize);

std::vector<std::vector<bool>> results = {
reinterpret_cast<std::vector<bool> *>(buffer),
reinterpret_cast<std::vector<bool> *>(buffer + bufferSize)};
const auto numShots = results.size();
// Create the counts dictionary
cudaq::CountsDictionary globalCounts;
std::vector<std::string> globalSequentialData;
globalSequentialData.reserve(numShots);
for (const auto &shotResult : results) {
// Each shot is an array of tagged results
std::string bitString;
for (const auto &bitVal : shotResult) {
bitString.append(bitVal ? "1" : "0");
}
// Global register results
globalCounts[bitString]++;
globalSequentialData.push_back(bitString);
}

// Add the global register results
cudaq::ExecutionResult result{globalCounts, GlobalRegisterName};
result.sequentialData = globalSequentialData;
return cudaq::sample_result({result});
}
};
} // namespace cudaq
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "QuantinuumHelper.h"
#include "common/ExtraPayloadProvider.h"
#include "common/Logger.h"
#include "common/RecordLogParser.h"
#include "common/RestClient.h"
#include "common/ServerHelper.h"
#include "cudaq/utils/cudaq_utils.h"
Expand Down Expand Up @@ -551,37 +550,7 @@ QuantinuumServerHelper::processResults(ServerMessage &jobResponse,
resultResponse["data"]["attributes"]["results"];
CUDAQ_DBG("Count result data: {}", qirResults);

cudaq::RecordLogParser parser;
parser.parse(qirResults);

// Get the buffer and length of buffer (in bytes) from the parser.
auto *origBuffer = parser.getBufferPtr();
std::size_t bufferSize = parser.getBufferSize();
char *buffer = static_cast<char *>(malloc(bufferSize));
std::memcpy(buffer, origBuffer, bufferSize);

std::vector<std::vector<bool>> results = {
reinterpret_cast<std::vector<bool> *>(buffer),
reinterpret_cast<std::vector<bool> *>(buffer + bufferSize)};
const auto numShots = results.size();
// Get the result
cudaq::CountsDictionary globalCounts;
std::vector<std::string> globalSequentialData;
globalSequentialData.reserve(numShots);
for (const auto &shotResult : results) {
// Each QSYS shot is an array of tagged results
std::string bitString;
for (const auto &bitVal : shotResult) {
bitString.append(bitVal ? "1" : "0");
}
// Global register results
globalCounts[bitString]++;
globalSequentialData.push_back(bitString);
}

// Add the global register results
cudaq::ExecutionResult result{globalCounts, GlobalRegisterName};
return cudaq::sample_result({result});
return createSampleResultFromQirOutput(qirResults);
}
}

Expand Down
Loading