Commit 8a7d0dc5 authored by Zachary Parks's avatar Zachary Parks
Browse files

adding bit-to-qubit map to AcceleratorBuffer to work with new IBM schemas, ROErrorDecorator, etc


Signed-off-by: default avatarZachary Parks <1zp@ornl.gov>
parent f6db2ba5
......@@ -116,8 +116,10 @@ std::vector<std::shared_ptr<AcceleratorBuffer>> ROErrorDecorator::execute(
auto nextInst = it.next();
if (nextInst->name() == "Measure") {
auto bits = nextInst->bits();
for (auto &b : bits)
for (auto &b : bits) {
supportSet.insert(b);
xacc::info(std::to_string(b));
}
}
}
return supportSet;
......@@ -165,33 +167,37 @@ std::vector<std::shared_ptr<AcceleratorBuffer>> ROErrorDecorator::execute(
for (auto &kv : tmpCounts) {
nShots += kv.second;
}
int counter = 0;
for (auto &b : buffers) {
auto counts = b->getMeasurementCounts();
auto functionName = b->name();
auto f = nameToFunction[functionName];
auto fSupports = supportSets[functionName];
auto exp_val = b->getExpectationValueZ();
auto bitMap = b->bit2IndexMap;
auto fixedExp = 0.0;
for (auto &kv : counts) {
auto prod = 1.0;
std::string bitString = kv.first;
auto count = kv.second;
for (auto& j : fSupports) {
xacc::info(functionName);
xacc::info(bitString);
xacc::info("bit: " + std::to_string(j) + " map: " + std::to_string(b->bit2IndexMap[j]));
std::stringstream s;
auto denom = (1.0 - piplus[j]);
auto numerator =
(bitString[bitString.length() - 1 - j] == '1' ? -1 : 1) - piminus[j];
(bitString[bitString.length() - 1 - bitMap[j]] == '1' ? -1 : 1) - piminus[j];
prod *= (numerator / denom);
}
fixedExp += ((double)count / (double)nShots) * prod;
}
xacc::info("exp val: " + std::to_string(fixedExp));
// Correct in case our shift has gone outside physical bounds
if (fixedExp > 1.0) {fixedExp = 1.0;}
if (fixedExp < -1.0) {fixedExp = -1.0;}
if (fixedExp > 1.0) { fixedExp = 1.0; }
if (fixedExp < -1.0) { fixedExp = -1.0; }
xacc::info("raw: " + std::to_string(exp_val));
xacc::info("fix: " + std::to_string(fixedExp));
b->addExtraInfo("ro-fixed-exp-val-z", ExtraInfo(fixedExp));
counter++;
......
......@@ -320,7 +320,7 @@ IBMAccelerator::processInput(std::shared_ptr<AcceleratorBuffer> buffer,
auto visitor =
std::make_shared<QObjectExperimentVisitor>(kernel->name(), uniqueBits);
name2QubitMap.insert(std::make_pair(kernel->name(), visitor->qubit2MemorySlot));
InstructionIterator it(kernel);
int memSlots = 0;
while (it.hasNext()) {
......@@ -516,7 +516,10 @@ IBMAccelerator::processResponse(std::shared_ptr<AcceleratorBuffer> buffer,
auto currentExperiment = experiments[i];
auto tmpBuffer =
createBuffer(currentExperiment.get_header().get_name(), buffer->size());
tmpBuffer->setBitIndexMap(name2QubitMap[currentExperiment.get_header().get_name()]);
for (auto &kv : tmpBuffer->bit2IndexMap){
xacc::info("bit: " + std::to_string(kv.first) + ", mapped: " + std::to_string(kv.second));
}
auto counts = resultsArray[i].get_data().get_counts();
for (auto &kv : counts) {
......
......@@ -85,6 +85,7 @@ public:
void cancel() override;
std::map<std::string, std::map<int, int>> name2QubitMap;
/**
* Create, store, and return an AcceleratorBuffer with the given
* variable id string and of the given number of bits.
......
......@@ -47,11 +47,12 @@ protected:
Experiment experiment;
std::string experimentName;
int nTotalQubits = 0;
std::map<int, int> qubit2MemorySlot;
std::vector<int> usedMemorySlots;
public:
int maxMemorySlots = 0;
std::map<int, int> qubit2MemorySlot;
const std::string name() const override { return "qobject-visitor"; }
const std::string description() const override {
......
......@@ -56,6 +56,12 @@ operator()(const std::map<int, std::vector<int>> &i) const {
mpark::get<std::map<int, std::vector<int>>>(extraInfo).begin());
}
bool CheckEqualVisitor::
operator()(const std::map<int, int> &i) const {
return std::equal(i.begin(), i.end(),
mpark::get<std::map<int, int>>(extraInfo).begin());
}
bool CheckEqualVisitor::
operator()(const std::vector<std::pair<double, double>> &i) const {
return std::equal(
......@@ -103,6 +109,15 @@ template<class T> void ToJsonVisitor<T>::operator()(const std::map<int, std::vec
writer.EndObject();
}
template<class T> void ToJsonVisitor<T>::operator()(const std::map<int, int> &i) const {
writer.StartObject();
for (auto &kv : i){
writer.Key(std::to_string(kv.first));
writer.Int(kv.second);
}
writer.EndObject();
}
template<class T> void ToJsonVisitor<T>::
operator()(const std::vector<std::pair<double, double>> &i) const {
writer.StartArray();
......@@ -409,6 +424,16 @@ void AcceleratorBuffer::print(std::ostream &stream) {
writer.EndObject();
}
if (!cacheFile) {
writer.Key("Bitmap");
writer.StartObject();
for (auto &kv : bit2IndexMap){
writer.Key(std::to_string(kv.first));
writer.Int(kv.second);
}
writer.EndObject();
}
if (!children.empty()) {
writer.Key("Children");
writer.StartArray();
......@@ -434,7 +459,13 @@ void AcceleratorBuffer::print(std::ostream &stream) {
}
// end measurement object
writer.EndObject();
writer.Key("Bitmap");
writer.StartObject();
for (auto &kv : pair.second->getBitMap()){
writer.Key(std::to_string(kv.first));
writer.Int(kv.second);
}
writer.EndObject();
// End child object
writer.EndObject();
}
......@@ -507,6 +538,7 @@ void AcceleratorBuffer::load(std::istream &stream) {
// Here we have the case of an object([key:value])
if (value.IsObject()) {
std::map<int, std::vector<int>> map;
std::map<int, int> map2;
for (auto itr2 = value.MemberBegin(); itr2 != value.MemberEnd();
++itr2) {
auto keyIsInt = true;
......@@ -523,12 +555,19 @@ void AcceleratorBuffer::load(std::istream &stream) {
for (int i = 0; i < arr.Size(); i++)
vec.push_back(arr[i].GetInt());
map.insert({key, vec});
} else if (itr2->value.IsInt() && keyIsInt) {
auto val = itr2->value.GetInt();
map2.insert({key, val});
} else {
break;
break;
}
}
addExtraInfo(itr->name.GetString(), ExtraInfo(map));
if (!map.empty()){
addExtraInfo(itr->name.GetString(), ExtraInfo(map));
}
if (!map2.empty()){
addExtraInfo(itr->name.GetString(), ExtraInfo(map2));
}
}
}
}
......@@ -542,6 +581,15 @@ void AcceleratorBuffer::load(std::istream &stream) {
}
}
if (!cacheFile){
auto &bitMap = doc["AcceleratorBuffer"]["Bitmap"];
std::map<int, int> tmpMap;
for (auto itr = bitMap.MemberBegin(); itr != bitMap.MemberEnd(); ++ itr) {
tmpMap.insert({std::stoi(itr->name.GetString()), itr->value.GetInt()});
}
setBitIndexMap(tmpMap);
}
auto children = doc["AcceleratorBuffer"]["Children"].GetArray();
for (auto &c : children) {
auto childBuffer =
......@@ -595,6 +643,12 @@ void AcceleratorBuffer::load(std::istream &stream) {
childBuffer->appendMeasurement(itr->name.GetString(),
itr->value.GetInt());
}
auto &bitMap = c["Bitmap"];
std::map<int, int> tmpMap;
for (auto itr = bitMap.MemberBegin(); itr != bitMap.MemberEnd(); ++itr){
tmpMap.insert({std::stoi(itr->name.GetString()), itr->value.GetInt()});
}
childBuffer->setBitIndexMap(tmpMap);
appendChild(c["name"].GetString(), childBuffer);
}
......
......@@ -28,7 +28,8 @@ using AcceleratorBufferChildPair =
using ExtraInfo = mpark::variant<int, double, std::string, std::vector<int>,
std::vector<double>, std::vector<std::string>,
std::map<int, std::vector<int>>,
std::vector<std::pair<double, double>>>;
std::vector<std::pair<double, double>>,
std::map<int, int>>;
using AddPredicate = std::function<bool(ExtraInfo &)>;
......@@ -48,6 +49,7 @@ public:
bool operator()(const std::vector<std::string> &i) const;
bool operator()(const std::map<int, std::vector<int>> &i) const;
bool operator()(const std::vector<std::pair<double, double>> &i) const;
bool operator()(const std::map<int, int> &i) const;
};
template<typename Writer>
......@@ -66,6 +68,7 @@ public:
void operator()(const std::vector<std::string> &i);
void operator()(const std::map<int, std::vector<int>> &i);
void operator()(const std::vector<std::pair<double, double>> &i) const;
void operator()(const std::map<int, int> &i) const;
};
class ExtraInfo2InstructionParameter
......@@ -98,6 +101,12 @@ public:
return InstructionParameter(0);
}
InstructionParameter
operator()(const std::map<int, int> &i) const {
XACCLogger::instance()->error(
"Cannot cast map<int, int> ExtraInfo to InstructionParameter");
return InstructionParameter(0);
}
InstructionParameter
operator()(const std::vector<std::pair<double, double>> &i) const {
return InstructionParameter(i);
}
......@@ -128,6 +137,11 @@ public:
"Cannot cast vector<int, int> InstructionParameter to ExtraInfo.");
return ExtraInfo(0);
}
// ExtraInfo operator()(const std::map<int, int> &i) const {
// XACCLogger::instance()->error(
// "Cannot cast map<int, int> InstructionParameter to ExtraInfo.");
// return ExtraInfo(0);
// }
ExtraInfo operator()(const std::complex<double> &i) const {
XACCLogger::instance()->error(
"Cannot cast complex InstructionParameter to ExtraInfo.");
......@@ -182,6 +196,8 @@ public:
*/
AcceleratorBuffer(const AcceleratorBuffer &other);
std::map<int, int> bit2IndexMap;
void useAsCache() { cacheFile = true; }
void appendChild(const std::string name,
std::shared_ptr<AcceleratorBuffer> buffer);
......@@ -200,6 +216,10 @@ public:
std::shared_ptr<AcceleratorBuffer> clone();
std::map<int, int> getBitMap() { return bit2IndexMap; }
void setBitIndexMap(const std::map<int, int> bitMap) { bit2IndexMap = bitMap; }
const int nChildren() { return getChildren().size(); }
/**
* Return all children with ExtraInfo infoName equal
......
......@@ -68,6 +68,7 @@ TEST(AcceleratorBufferTester, checkLoad) {
"vqe-nQPU-calls": 0
},
"Measurements": {},
"Bitmap": {},
"Children": [
{
"name": "Z1",
......@@ -80,6 +81,10 @@ TEST(AcceleratorBufferTester, checkLoad) {
"Measurements": {
"00": 323,
"01": 701
},
"Bitmap": {
"0": 0,
"1": 1
}
},
{
......@@ -93,6 +98,10 @@ TEST(AcceleratorBufferTester, checkLoad) {
"Measurements": {
"00": 323,
"01": 701
},
"Bitmap": {
"0": 0,
"1": 1
}
}
]
......@@ -104,10 +113,15 @@ TEST(AcceleratorBufferTester, checkLoad) {
buffer.load(s);
EXPECT_EQ("q", buffer.name());
EXPECT_EQ(2, buffer.size());
auto mmap = buffer.getBitMap();
std::stringstream ss;
buffer.print(ss);
std::cout << "HELLO: " << ss.str() << "\n";
// std::cout << "HELLO: " << ss.str() << "\n";
std::stringstream st;
for (auto &kv : mmap){
st << std::to_string(kv.first) << ", " << std::to_string(kv.second);
}
std::cout << st.str() << std::endl;
EXPECT_EQ(ss.str(), bufferStr);
}
TEST(AcceleratorBufferTester, checkLoadDwave) {
......@@ -156,6 +170,12 @@ TEST(AcceleratorBufferTester, checkLoadDwave) {
},
"Measurements": {
"010001": 100
},
"Bitmap": {
"0": 0,
"1": 1,
"5": 5,
"6": 3
}
}
})dwaveBuffer";
......@@ -170,6 +190,8 @@ TEST(AcceleratorBufferTester, checkLoadDwave) {
buffer.print(ss);
EXPECT_EQ(ss.str(), dwaveBuffer);
std::cout << ss.str() << "\n";
std::cout << dwaveBuffer << std::endl;
}
TEST(AcceleratorBufferTester, checkEmptyParametersBug) {
......
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