Skip to content

Commit

Permalink
Implemented Accelerator:retrieve()
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Claudino <claudinodc@ornl.gov>
  • Loading branch information
danclaudino committed Oct 25, 2024
1 parent c2dd5e4 commit fb97b3a
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 4 deletions.
16 changes: 16 additions & 0 deletions quantum/plugins/ibm/accelerator/IBMAccelerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,22 @@ void IBMAccelerator::cancel() {
}
}

void IBMAccelerator::retrieve(const std::string jobId, std::shared_ptr<xacc::AcceleratorBuffer> buffer) {

const std::string path("/runtime/jobs/" + jobId + "/results");
auto resultsJson = get(IBM_API_URL, path, headers);
auto hexs = json::parse(resultsJson)["results"][0]["data"]["q_c"]["samples"];

std::vector<int> samples;
for (const auto& sample_hex : hexs) {
int sample_int = std::stoi(sample_hex.get<std::string>(), nullptr, 16);
// Convert the integer to a binary string
samples.push_back(sample_int);
}
buffer->setSamples(samples);
return;
}

std::vector<std::pair<int, int>> IBMAccelerator::getConnectivity() {

if (!xacc::container::contains(availableBackends, backend)) {
Expand Down
5 changes: 1 addition & 4 deletions quantum/plugins/ibm/accelerator/IBMAccelerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@
#define QUANTUM_GATE_ACCELERATORS_IBMACCELERATOR_HPP_

#include "InstructionIterator.hpp"
//#include "Properties.hpp"
#include "Accelerator.hpp"
#include <bitset>
#include <type_traits>
//#include "Backends.hpp"
//#include "Properties.hpp"
#include "IRTransformation.hpp"
#include "Json.hpp"
//#include "QObjGenerator.hpp"

using namespace xacc;
using json = nlohmann::json;
Expand Down Expand Up @@ -73,6 +69,7 @@ std::string hex_string_to_binary_string(std::string hex);
class IBMAccelerator : public Accelerator {
public:
void cancel() override;
void retrieve(const std::string jobId, std::shared_ptr<xacc::AcceleratorBuffer> buffer) override;

std::map<std::string, std::map<int, int>> name2QubitMap;

Expand Down
1 change: 1 addition & 0 deletions xacc/accelerator/Accelerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class Accelerator : public Identifiable {
}

virtual void cancel(){};
virtual void retrieve(const std::string jobId, std::shared_ptr<xacc::AcceleratorBuffer> buffer){};

virtual std::vector<std::pair<int, int>> getConnectivity() {
return std::vector<std::pair<int, int>>{};
Expand Down
37 changes: 37 additions & 0 deletions xacc/accelerator/AcceleratorBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@

using namespace rapidjson;

std::string intToBinary(int num, const int nBits) {

std::string binary;
while (num > 0) {
binary.insert(0, 1, (num & 1) ? '1' : '0');
num >>= 1;
}

// Pad with leading zeros to ensure minimum bit width
if (binary.size() < nBits) {
binary.insert(0, nBits - binary.size(), '0');
}

return binary;
}

namespace xacc {

bool CheckEqualVisitor::operator()(const int &i) const {
Expand Down Expand Up @@ -754,4 +770,25 @@ void AcceleratorBuffer::load(std::istream &stream) {
}
}

void AcceleratorBuffer::setSample(const int sample) {

auto bitString = intToBinary(sample, size());
appendMeasurement(bitString);
_samples.push_back(sample);
}

void AcceleratorBuffer::setSamples(const std::vector<int> samples) {
for (auto s : samples) {
setSample(s);
}
}

int AcceleratorBuffer::getSample(const int index) {
return _samples[index];
}

std::vector<int> AcceleratorBuffer::getSamples() {
return _samples;
}

} // namespace xacc
6 changes: 6 additions & 0 deletions xacc/accelerator/AcceleratorBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class HeterogenousMap2ExtraInfo
class AcceleratorBuffer {

protected:
std::vector<int> _samples;
std::map<std::string, int> bitStringToCounts;
std::string bufferId;
int nBits;
Expand Down Expand Up @@ -268,6 +269,11 @@ class AcceleratorBuffer {
return getInformation(key);
}

void setSample(const int sample);
void setSamples(const std::vector<int> samples);
int getSample(const int index);
std::vector<int> getSamples();

virtual ~AcceleratorBuffer() {}
};

Expand Down

0 comments on commit fb97b3a

Please sign in to comment.