Commit 713b715b authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

minor updates for work with qcor



Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent c4599603
Pipeline #116613 passed with stage
in 12 minutes and 12 seconds
......@@ -191,7 +191,8 @@ PauliOperator::observe(std::shared_ptr<CompositeInstruction> function) {
// new measurement instructions buffer names to be the same.
// Here we assume that all instructions have the same buffer name
std::string buf_name = "";
if (!function->getInstruction(0)->getBufferNames().empty()) {
if (function->nInstructions() > 0 && !function->getInstruction(0)->getBufferNames().empty()) {
buf_name = function->getInstruction(0)->getBufferNames()[0];
}
......
......@@ -35,3 +35,4 @@ endif()
add_subdirectory(optimal_control)
install (FILES utils/OperatorPool.hpp DESTINATION include/quantum/gate)
\ No newline at end of file
......@@ -104,6 +104,8 @@ void IBMAccelerator::initialize(const HeterogeneousMap &params) {
}
}
chosenBackend = availableBackends[backend];
defaults_response =
get(IBM_API_URL,
IBM_CREDENTIALS_PATH + "/devices/" + backend + "/defaults", {},
......
......@@ -47,6 +47,7 @@ std::string hex_string_to_binary_string(std::string hex) {
void AerAccelerator::initialize(const HeterogeneousMap &params) {
m_options = params;
noise_model.clear();
m_simtype = "qasm";
connectivity.clear();
......@@ -97,7 +98,7 @@ void AerAccelerator::initialize(const HeterogeneousMap &params) {
noise_model["errors"] = elements;
// noise_model["x90_gates"] = std::vecto
std::cout << "NoiseModelJson:\n" << noise_model.dump(4) << "\n";
// std::cout << "NoiseModelJson:\n" << noise_model.dump(4) << "\n";
} else if (params.stringExists("noise-model")) {
noise_model = nlohmann::json::parse(params.getString("noise-model"));
}
......
......@@ -27,7 +27,15 @@ public:
// Accelerator interface impls
void initialize(const HeterogeneousMap &params = {}) override;
void updateConfiguration(const HeterogeneousMap &config) override {
initialize(config);
if (config.keyExists<int>("shots")) {
m_shots = config.get<int>("shots");
m_options.insert("shots", m_shots);
}
if (config.stringExists("backend")) {
m_options.insert("backend", config.getString("backend"));
// backend changed so reinit
initialize(m_options);
}
};
const std::vector<std::string> configurationKeys() override { return {}; }
BitOrder getBitOrder() override { return BitOrder::MSB; }
......@@ -49,6 +57,7 @@ private:
std::string m_simtype = "qasm";
nlohmann::json noise_model;
std::vector<std::pair<int, int>> connectivity;
HeterogeneousMap m_options;
};
} // namespace quantum
} // namespace xacc
......@@ -62,19 +62,16 @@ void SwapShort::apply(std::shared_ptr<CompositeInstruction> program,
auto staq = xacc::getCompiler("staq");
auto src = staq->translate(program);
// std::cout << "initial translate:\n" << src << "\n";
// parse that to get staq ast
auto prog = parser::parse_string(src);
mapping::Device device(qpu->getSignature(), nQubits,
adj); //, oneq,couplings);
adj);
// map qreg_NAME -> q
auto layout = mapping::compute_basic_layout(device, *prog);
mapping::apply_layout(layout, device, *prog);
// layout = mapping::compute_basic_layout(device, *prog);
// mapping::apply_layout(layout, device, *prog);
// std::cout << *prog << "\n";
mapping::map_onto_device(device, *prog);
// map prog back to staq src string and
......@@ -83,7 +80,6 @@ void SwapShort::apply(std::shared_ptr<CompositeInstruction> program,
prog->pretty_print(ss);
src = ss.str();
// std::cout << "Final:\n" << src << "\n";
auto ir = staq->compile(src);
// reset the program and add optimized instructions
......
#include "qalloc.hpp"
#include "xacc.hpp"
#include <algorithm>
namespace xacc {
namespace internal_compiler {
......@@ -7,7 +8,25 @@ template <typename T> struct empty_delete {
empty_delete() {}
void operator()(T *const) const {}
};
qreg::qreg(const int n) { buffer = xacc::qalloc(n).get(); }
std::string qreg::random_string(std::size_t length) {
auto randchar = []() -> char {
const char charset[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
const size_t max_index = (sizeof(charset) - 1);
return charset[rand() % max_index];
};
std::string str(length, 0);
std::generate_n(str.begin(), length, randchar);
return str;
}
qreg::qreg(const int n) {
buffer = xacc::qalloc(n);
auto name = "qreg_"+random_string(5);
xacc::storeBuffer(name, buffer);
}
qreg::qreg(const qreg &other)
: buffer(other.buffer), been_named_and_stored(other.been_named_and_stored) {
}
......@@ -15,12 +34,12 @@ qreg::qreg(const qreg &other)
qubit qreg::operator[](const std::size_t i) {
return std::make_pair(buffer->name(), i);
}
qreg &qreg::operator=(const qreg &q) {
buffer = q.buffer;
return *this;
}
// qreg &qreg::operator=(const qreg &q) {
// buffer = q.buffer;
// return *this;
// }
AcceleratorBuffer *qreg::results() { return buffer; }
AcceleratorBuffer *qreg::results() { return buffer.get(); }
std::map<std::string, int> qreg::counts() {
return buffer->getMeasurementCounts();
}
......@@ -37,9 +56,7 @@ void qreg::setNameAndStore(const char *name) {
}
}
void qreg::store() {
auto buffer_as_shared = std::shared_ptr<AcceleratorBuffer>(
buffer, empty_delete<AcceleratorBuffer>());
xacc::storeBuffer(buffer_as_shared);
xacc::storeBuffer(buffer);
}
int qreg::size() { return buffer->size(); }
void qreg::addChild(qreg &q) {
......
......@@ -15,6 +15,7 @@
#include <map>
#include <string>
#include <memory>
namespace xacc {
class AcceleratorBuffer;
......@@ -23,28 +24,29 @@ namespace internal_compiler {
using qubit = std::pair<std::string, std::size_t>;
class qreg {
private:
std::string random_string(std::size_t length);
protected:
AcceleratorBuffer *buffer;
std::shared_ptr<AcceleratorBuffer> buffer;
bool been_named_and_stored = false;
public:
qreg() = default;
qreg(const int n);
qreg (const qreg& other);
qreg(const qreg &other);
qubit operator[](const std::size_t i);
qreg& operator=(const qreg& q);
AcceleratorBuffer *results();
std::map<std::string, int> counts();
double exp_val_z();
void reset();
int size();
void addChild(qreg& q);
void addChild(qreg &q);
void setName(const char *name);
void setNameAndStore(const char *name);
std::string name();
void store();
void print();
double weighted_sum(Observable* obs);
double weighted_sum(Observable *obs);
};
} // namespace internal_compiler
......@@ -52,7 +54,6 @@ public:
xacc::internal_compiler::qreg qalloc(const int n);
// __qpu__ indicates this functions is for the QCOR Clang Syntax Handler
// and annotated with quantum for the LLVM IR CodeGen
#define __qpu__ [[clang::syntax(qcor)]] __attribute__((annotate("quantum")))
......
......@@ -187,6 +187,12 @@ void storeBuffer(const std::string name,
if (allocated_buffers.count(name)) {
error("Invalid buffer name to store: " + name);
}
// if this buffer is in here already before we
// set its new name, we should remove it from the allocation
if (allocated_buffers.count(buffer->name())) {
allocated_buffers.erase(buffer->name());
}
buffer->setName(name);
allocated_buffers.insert({name, buffer});
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment