Commit 60596c31 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

adding setBitMap to Function, updates to get qcor working with qcs


Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent c78d130d
Pipeline #59215 passed with stages
in 10 minutes and 53 seconds
......@@ -75,6 +75,9 @@ public:
return i;
}
void setBitMap(const std::vector<int> bMap) override {}
const std::vector<int> getBitMap() override {return std::vector<int>{};}
std::list<InstPtr> getInstructions() override { return instructions; }
void removeInstruction(const int idx) override {
......
......@@ -25,6 +25,7 @@ void GateFunction::mapBits(std::vector<int> bitMap) {
for (auto i : instructions) {
i->mapBits(bitMap);
}
setBitMap(bitMap);
}
void GateFunction::persist(std::ostream &outStream) {
......@@ -74,6 +75,16 @@ void GateFunction::load(std::istream &inStream) {
auto &kernel = doc["kernels"].GetArray()[0];
functionName = kernel["function"].GetString();
if (kernel.HasMember("bitmap")) {
auto bitMapArr = kernel["bitmap"].GetArray();
std::vector<int> bitMap;
for (int i = 0; i < bitMapArr.Size(); i++) {
bitMap.push_back(bitMapArr[i].GetInt());
}
setBitMap(bitMap);
}
auto instructionsArray = kernel["instructions"].GetArray();
for (int i = 0; i < instructionsArray.Size(); i++) {
......
......@@ -72,6 +72,10 @@ public:
return 0;
}
void setBitMap(const std::vector<int> bMap) override {bitMap = bMap;}
const std::vector<int> getBitMap() override {return bitMap;}
bool hasBeenBitMapped() override {return !bitMap.empty();}
void persist(std::ostream &outStream) override;
void load(std::istream &inStream) override;
......@@ -257,6 +261,9 @@ protected:
std::string tag = "";
std::map<std::string, InstructionParameter> options;
std::vector<int> bitMap;
};
} // namespace quantum
......
......@@ -18,7 +18,7 @@
namespace xacc {
namespace quantum {
class ConditionalFunction : public virtual GateFunction {
class ConditionalFunction : public GateFunction {
protected:
int qbitIdx;
......
......@@ -125,17 +125,26 @@ PauliOperator::observe(std::shared_ptr<Function> function) {
if (gateName == "X") {
auto hadamard =
gateRegistry->createInstruction("H", std::vector<int>{qbit});
if (function->hasBeenBitMapped()) {
hadamard->mapBits(function->getBitMap());
}
gateFunction->addInstruction(hadamard);
} else if (gateName == "Y") {
auto rx = gateRegistry->createInstruction("Rx", std::vector<int>{qbit});
InstructionParameter p(pi / 2.0);
rx->setParameter(0, p);
if (function->hasBeenBitMapped()) {
rx->mapBits(function->getBitMap());
}
gateFunction->addInstruction(rx);
}
}
if (!spinInst.isIdentity()) {
for (auto m : measurements) {
if (function->hasBeenBitMapped()) {
m->mapBits(function->getBitMap());
}
gateFunction->addInstruction(m);
}
}
......
......@@ -49,6 +49,15 @@ template <class W, class B> std::string JsonVisitor<W, B>::write() {
writer->String("function");
writer->String(f->name());
if (f->hasBeenBitMapped()) {
writer->String("bitmap");
writer->StartArray();
for (auto& b : f->getBitMap()) {
writer->Int(b);
}
writer->EndArray();
}
// All functions have instructions, start
// that array here.
writer->String("instructions");
......
#include "QCSAccelerator.hpp"
#include <algorithm>
#include <pybind11/embed.h>
#include "CountGatesOfTypeVisitor.hpp"
#include <pybind11/numpy.h>
......@@ -76,8 +75,10 @@ std::shared_ptr<IR> MapToPhysical::transform(std::shared_ptr<IR> ir) {
}
for (auto &inst : probEdges) {
if (!problemGraph->edgeExists(inst.first, inst.second)) {
problemGraph->addEdge(inst.first, inst.second, 1.0);
}
}
// std::cout << "\n";
// problemGraph->write(std::cout);
......@@ -93,6 +94,8 @@ std::shared_ptr<IR> MapToPhysical::transform(std::shared_ptr<IR> ir) {
physicalMap.push_back(logical2Physical[kv.second[0]]);
}
// std::cout << "Physical bits:\n";
// for (auto& b : physicalMap) std::cout << b << "\n";
function->mapBits(physicalMap);
}
......@@ -160,10 +163,10 @@ void QCSAccelerator::execute(std::shared_ptr<AcceleratorBuffer> buffer,
quilStr =
"DECLARE ro BIT[" + std::to_string(buffer->size()) + "]\n" + quilStr;
std::shared_ptr<py::scoped_interpreter> guard;
if (!xacc::isPyApi) {
guard = std::make_shared<py::scoped_interpreter>();
}
// std::shared_ptr<pybind11::scoped_interpreter> guard;
// py::print("quil:\n", quilStr);
auto pyquil = py::module::import("pyquil");
......@@ -173,14 +176,13 @@ void QCSAccelerator::execute(std::shared_ptr<AcceleratorBuffer> buffer,
program.attr("wrap_in_numshots_loop")(shots);
auto qc = get_qc(backend);
auto compiled = qc.attr("compile")(program);
py::array_t<int> results = qc.attr("run")(compiled);
auto shape = results.request().shape;
// py::print(shape[0]);
// py::print(shape[1]);
//py::print(results);
// py::print(shape[0]);
// py::print(shape[1]);
// py::print(results);
// py::print("QUIL\n");
// py::print(quilStr);
//py::print(buffer->size());
......@@ -216,7 +218,7 @@ std::vector<std::shared_ptr<AcceleratorBuffer>> QCSAccelerator::execute(
auto tmpBuffer = createBuffer(f->name(), buffer->size());
high_resolution_clock::time_point t1 = high_resolution_clock::now();
xacc::info("Execution " + std::to_string(counter) + ": " + f->name());
// xacc::info("Execution " + std::to_string(counter) + ": " + f->name());
execute(tmpBuffer, f);
high_resolution_clock::time_point t2 = high_resolution_clock::now();
......
......@@ -36,6 +36,8 @@
#include "CLIParser.hpp"
#include "RemoteAccelerator.hpp"
#include <pybind11/embed.h>
#include <dlfcn.h>
#define RAPIDJSON_HAS_STDSTRING 1
......@@ -51,14 +53,14 @@ namespace quantum {
class MapToPhysical : public xacc::IRTransformation {
protected:
std::vector<std::pair<int, int>> _edges;
public:
MapToPhysical(std::vector<std::pair<int, int>> &edges) : _edges(edges) {}
std::shared_ptr<IR> transform(std::shared_ptr<IR> ir) override;
bool hardwareDependent() { return true; }
bool hardwareDependent() override { return true; }
const std::string name() const override { return "qcs-map-qubits"; }
const std::string description() const override { return ""; }
};
/**
*
*/
......@@ -68,6 +70,8 @@ protected:
std::vector<std::pair<int, int>> latticeEdges;
Document latticeJson;
pybind11::scoped_interpreter * guard;
public:
QCSAccelerator() : Accelerator() {}
......@@ -82,10 +86,10 @@ public:
* @return
*/
std::shared_ptr<AcceleratorBuffer> createBuffer(const std::string &varId,
const int size);
const int size) override;
virtual std::shared_ptr<AcceleratorBuffer>
createBuffer(const std::string &varId);
std::shared_ptr<AcceleratorBuffer>
createBuffer(const std::string &varId) override;
void execute(std::shared_ptr<AcceleratorBuffer> buffer,
const std::shared_ptr<Function> function) override;
......@@ -93,7 +97,7 @@ public:
execute(std::shared_ptr<AcceleratorBuffer> buffer,
const std::vector<std::shared_ptr<Function>> functions) override;
virtual void initialize() {
void initialize() override {
void*const libpython_handle = dlopen("libpython3.6m.so", RTLD_LAZY | RTLD_GLOBAL);
if (xacc::optionExists("qcs-backend")) {
auto backend = xacc::getOption("qcs-backend");
......@@ -119,9 +123,13 @@ public:
latticeEdges.push_back({std::stoi(split[0]), std::stoi(split[1])});
}
}
if (!xacc::isPyApi && !guard) {
guard = new pybind11::scoped_interpreter();
}
}
std::vector<std::pair<int, int>> getAcceleratorConnectivity() {
std::vector<std::pair<int, int>> getAcceleratorConnectivity() override{
if (!latticeEdges.empty()) {
return latticeEdges;
}
......@@ -133,13 +141,13 @@ public:
* @param NBits
* @return
*/
virtual bool isValidBufferSize(const int NBits);
bool isValidBufferSize(const int NBits) override;
/**
* This Accelerator models QPU Gate accelerators.
* @return
*/
virtual AcceleratorType getType() { return AcceleratorType::qpu_gate; }
AcceleratorType getType() override { return AcceleratorType::qpu_gate; }
/**
* We have no need to transform the IR for this Accelerator,
......@@ -160,20 +168,19 @@ public:
* Users can set the api-key, execution type, and number of triels
* from the command line with these options.
*/
virtual OptionPairs getOptions() {
OptionPairs getOptions() override {
OptionPairs desc{{"qcs-shots", "Provide the number of trials to execute."},
{"qcs-backend", ""}};
return desc;
}
virtual const std::string name() const { return "qcs"; }
virtual const std::string description() const { return ""; }
const std::string name() const override { return "qcs"; }
const std::string description() const override { return ""; }
/**
* The destructor
*/
virtual ~QCSAccelerator() {}
virtual ~QCSAccelerator() { delete guard;}
};
} // namespace quantum
......
......@@ -48,6 +48,10 @@ public:
*/
virtual const int nInstructions() = 0;
virtual void setBitMap(const std::vector<int> bitMap) = 0;
virtual const std::vector<int> getBitMap() = 0;
virtual bool hasBeenBitMapped() {return false;}
void setBits(const std::vector<int> bits) override {
return;
}
......
......@@ -63,6 +63,10 @@ public:
virtual const std::vector<int> bits() = 0;
virtual void setBits(const std::vector<int> bits) = 0;
// Often times we need to map logical bits to physical bits,
// when that happens we should set hte bitMap for future reference
// virtual void setBitMap(const std::vector<int> bitMap) = 0;
/**
* Return this Instruction's parameter at the given index.
*
......
Markdown is supported
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