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);
5 changes: 4 additions & 1 deletion include/backend/dd/DDSimDiagnostics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@
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;

Check warning on line 21 in include/backend/dd/DDSimDiagnostics.hpp

View workflow job for this annotation

GitHub Actions / 🇨‌ Lint / 🚨 Lint

include/backend/dd/DDSimDiagnostics.hpp:21:34 [misc-include-cleaner]

no header providing "std::vector" is directly included
};

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
7 changes: 4 additions & 3 deletions include/common/parsing/CodePreprocessing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
#include <cstddef>
#include <map>
#include <memory>
#include <set>

Check warning on line 8 in include/common/parsing/CodePreprocessing.hpp

View workflow job for this annotation

GitHub Actions / 🇨‌ Lint / 🚨 Lint

include/common/parsing/CodePreprocessing.hpp:8:1 [misc-include-cleaner]

included header set is not used directly
#include <string>
#include <utility>
#include <vector>

struct Block {
Expand All @@ -18,7 +19,7 @@
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 +33,13 @@

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);
93 changes: 92 additions & 1 deletion src/backend/dd/DDSimDebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,50 @@
}

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

std::vector<std::string> getTargetVariables(DDSimulationState* ddsim,
size_t instruction) {
std::vector<std::string> result;
const auto& targets = ddsim->targetQubits[instruction];

Check warning on line 861 in src/backend/dd/DDSimDebug.cpp

View workflow job for this annotation

GitHub Actions / 🇨‌ Lint / 🚨 Lint

src/backend/dd/DDSimDebug.cpp:861:15 [clang-diagnostic-unused-variable]

unused variable 'targets'
Fixed Show fixed Hide fixed
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;
}
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 +936,42 @@
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)),

Check warning on line 971 in src/backend/dd/DDSimDebug.cpp

View workflow job for this annotation

GitHub Actions / 🇨‌ Lint / 🚨 Lint

src/backend/dd/DDSimDebug.cpp:971:36 [misc-include-cleaner]

no header providing "std::distance" is directly included
functionDef};
}

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

for (auto& instruction : instructions) {
Expand All @@ -1293,7 +1374,17 @@
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