Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Upgrade Diagnosis Capabilities #20

Merged
merged 8 commits into from
Sep 19, 2024
11 changes: 9 additions & 2 deletions include/backend/dd/DDSimDebug.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ struct DDSimulationState {
std::vector<size_t> callReturnStack;
std::map<size_t, std::map<std::string, std::string>> callSubstitutions;
std::vector<std::pair<size_t, size_t>> restoreCallReturnStack;
std::map<size_t, std::vector<size_t>> dataDependencies;
std::map<size_t, std::vector<std::pair<size_t, size_t>>> dataDependencies;
std::map<size_t, std::set<size_t>> functionCallers;
std::set<size_t> breakpoints;
std::vector<std::set<std::string>> targetQubits;
std::vector<std::vector<std::string>> targetQubits;

bool paused;

Expand Down Expand Up @@ -127,8 +128,14 @@ bool checkAssertion(DDSimulationState* ddsim,
std::unique_ptr<Assertion>& assertion);
std::string getClassicalBitName(DDSimulationState* ddsim, size_t index);
size_t variableToQubit(DDSimulationState* ddsim, const std::string& variable);
std::pair<size_t, size_t> variableToQubitAt(DDSimulationState* ddsim,
const std::string& variable,
size_t instruction);
bool isSubStateVectorLegal(const Statevector& full,
std::vector<size_t>& targetQubits);
std::vector<std::vector<Complex>>
getPartialTraceFromStateVector(const Statevector& sv,
const std::vector<size_t>& traceOut);

std::vector<std::string> getTargetVariables(DDSimulationState* ddsim,
size_t instruction);
6 changes: 5 additions & 1 deletion include/backend/dd/DDSimDiagnostics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <map>
#include <memory>
#include <set>
#include <vector>

struct DDSimulationState;

Expand All @@ -17,12 +18,15 @@ struct DDDiagnostics {
DDSimulationState* simulationState;
std::map<size_t, std::set<size_t>> zeroControls;
std::map<size_t, std::set<size_t>> nonZeroControls;

std::map<size_t, std::set<std::vector<size_t>>> actualQubits;
};

size_t dddiagnosticsGetNumQubits(Diagnostics* self);
size_t dddiagnosticsGetInstructionCount(Diagnostics* self);
Result dddiagnosticsInit([[maybe_unused]] Diagnostics* self);
Result dddiagnosticsInit(Diagnostics* self);
Result dddiagnosticsGetDataDependencies(Diagnostics* self, size_t instruction,
bool includeCallers,
bool* instructions);
Result dddiagnosticsGetInteractions(Diagnostics* self, size_t beforeInstruction,
size_t qubit, bool* qubitsAreInteracting);
Expand Down
2 changes: 1 addition & 1 deletion include/backend/diagnostics.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct Diagnostics {
size_t (*getNumQubits)(Diagnostics* self);
size_t (*getInstructionCount)(Diagnostics* self);
Result (*getDataDependencies)(Diagnostics* self, size_t instruction,
bool* instructions);
bool includeCallers, bool* instructions);
Result (*getInteractions)(Diagnostics* self, size_t beforeInstruction,
size_t qubit, bool* qubitsAreInteracting);
Result (*getZeroControlInstructions)(Diagnostics* self, bool* instructions);
Expand Down
8 changes: 4 additions & 4 deletions include/common/parsing/CodePreprocessing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include <cstddef>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <utility>
#include <vector>

struct Block {
Expand All @@ -18,7 +18,7 @@ struct Instruction {
size_t lineNumber;
std::string code;
std::unique_ptr<Assertion> assertion;
std::set<std::string> targets;
std::vector<std::string> targets;

size_t originalCodeStartPosition;
size_t originalCodeEndPosition;
Expand All @@ -32,13 +32,13 @@ struct Instruction {

std::map<std::string, std::string> callSubstitution;

std::vector<size_t> dataDependencies;
std::vector<std::pair<size_t, size_t>> dataDependencies;

Block block;
std::vector<size_t> childInstructions;
Instruction(size_t inputLineNumber, std::string inputCode,
std::unique_ptr<Assertion>& inputAssertion,
std::set<std::string> inputTargets, size_t startPos,
std::vector<std::string> inputTargets, size_t startPos,
size_t endPos, size_t successor, bool isFuncCall,
std::string function, bool inFuncDef, bool isFuncDef,
Block inputBlock);
Expand Down
2 changes: 2 additions & 0 deletions include/common/parsing/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ std::string replaceString(std::string str, const std::string& from,
const std::string& to);

std::string removeWhitespace(std::string str);

bool variablesEqual(const std::string& v1, const std::string& v2);
96 changes: 95 additions & 1 deletion src/backend/dd/DDSimDebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <cstring>
#include <exception>
#include <iostream>
#include <iterator>
#include <memory>
#include <numeric>
#include <random>
Expand Down Expand Up @@ -854,6 +855,52 @@ Result destroyDDSimulationState(DDSimulationState* self) {
}

//-----------------------------------------------------------------------------------------

std::vector<std::string> getTargetVariables(DDSimulationState* ddsim,
size_t instruction) {
std::vector<std::string> result;
size_t parentFunction = -1ULL;
size_t i = instruction;
while (true) {
if (ddsim->functionDefinitions.find(i) !=
ddsim->functionDefinitions.end()) {
parentFunction = i;
break;
}
if (ddsim->instructionTypes[i] == RETURN) {
break;
}
if (i == 0) {
break;
}
i--;
}

const auto parameters = parentFunction != -1ULL
? ddsim->targetQubits[parentFunction]
: std::vector<std::string>{};
for (const auto& target : ddsim->targetQubits[instruction]) {
if (std::find(parameters.begin(), parameters.end(), target) !=
parameters.end()) {
result.push_back(target);
continue;
}
const auto foundRegister =
std::find_if(ddsim->qubitRegisters.begin(), ddsim->qubitRegisters.end(),
[target](const QubitRegisterDefinition& reg) {
return reg.name == target;
});
if (foundRegister != ddsim->qubitRegisters.end()) {
for (size_t j = 0; j < foundRegister->size; j++) {
result.push_back(target + "[" + std::to_string(j) + "]");
}
} else {
result.push_back(target);
}
}
return result;
}

size_t variableToQubit(DDSimulationState* ddsim, const std::string& variable) {
auto declaration = replaceString(variable, " ", "");
declaration = replaceString(declaration, "\t", "");
Expand Down Expand Up @@ -892,6 +939,42 @@ size_t variableToQubit(DDSimulationState* ddsim, const std::string& variable) {
throw std::runtime_error("Unknown variable name " + var);
}

std::pair<size_t, size_t> variableToQubitAt(DDSimulationState* ddsim,
const std::string& variable,
size_t instruction) {
size_t sweep = instruction;
size_t functionDef = -1ULL;
while (sweep < ddsim->instructionTypes.size()) {
if (std::find(ddsim->functionDefinitions.begin(),
ddsim->functionDefinitions.end(),
sweep) != ddsim->functionDefinitions.end()) {
functionDef = sweep;
break;
}
if (ddsim->instructionTypes[sweep] == RETURN) {
break;
}
sweep--;
}

if (functionDef == -1ULL) {
// In the global scope, we can just use the register's index.
return {variableToQubit(ddsim, variable), functionDef};
}

// In a gate-local scope, we have to define qubit indices relative to the
// gate.
const auto& targets = ddsim->targetQubits[functionDef];

const auto found = std::find(targets.begin(), targets.end(), variable);
if (found == targets.end()) {
throw std::runtime_error("Unknown variable name " + variable);
}

return {static_cast<size_t>(std::distance(targets.begin(), found)),
functionDef};
}

double complexMagnitude(Complex& c) {
return std::sqrt(c.real * c.real + c.imaginary * c.imaginary);
}
Expand Down Expand Up @@ -1283,6 +1366,7 @@ std::string preprocessAssertionCode(const char* code,
ddsim->qubitRegisters.clear();
ddsim->successorInstructions.clear();
ddsim->dataDependencies.clear();
ddsim->functionCallers.clear();
ddsim->targetQubits.clear();

for (auto& instruction : instructions) {
Expand All @@ -1293,7 +1377,17 @@ std::string preprocessAssertionCode(const char* code,
ddsim->instructionEnds.push_back(instruction.originalCodeEndPosition);
ddsim->dataDependencies.insert({instruction.lineNumber, {}});
for (const auto& dependency : instruction.dataDependencies) {
ddsim->dataDependencies[instruction.lineNumber].push_back(dependency);
ddsim->dataDependencies[instruction.lineNumber].emplace_back(
dependency.first, dependency.second);
}
if (instruction.isFunctionCall) {
const size_t successorInFunction = instruction.successorIndex;
const size_t functionIndex = successorInFunction - 1;
if (ddsim->functionCallers.find(functionIndex) ==
ddsim->functionCallers.end()) {
ddsim->functionCallers.insert({functionIndex, {}});
}
ddsim->functionCallers[functionIndex].insert(instruction.lineNumber);
}

// what exactly we do with each instruction depends on its type:
Expand Down
Loading
Loading