Skip to content

Commit

Permalink
refactor: 🎨 Resolve CodeQL warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
DRovara committed Aug 9, 2024
1 parent 61c59ed commit 52128a2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
26 changes: 25 additions & 1 deletion src/backend/dd/DDSimDebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,10 @@ Result ddsimStepForward(SimulationState* self) {
}
ddsim->previousInstructionStack.emplace_back(currentInstruction);

// The exact action we take depends on the type of the next instruction:
// - ASSERTION: check the assertion and step back if it fails.
// - Non-SIMULATE: just step to the next instruction.
// - SIMULATE: run the corresponding operation on the DD backend.
if (ddsim->instructionTypes[currentInstruction] == ASSERTION) {
auto& assertion = ddsim->assertionInstructions[currentInstruction];
const auto failed = !checkAssertion(ddsim, assertion);
Expand All @@ -310,6 +314,8 @@ Result ddsimStepForward(SimulationState* self) {

qc::MatrixDD currDD;
if ((*ddsim->iterator)->getType() == qc::Measure) {
// Perform a measurement of the desired qubits, based on the amplitudes of
// the current state.
auto qubitsToMeasure = (*ddsim->iterator)->getTargets();
auto classicalBits =
dynamic_cast<qc::NonUnitaryOperation*>(ddsim->iterator->get())
Expand Down Expand Up @@ -340,11 +346,13 @@ Result ddsimStepForward(SimulationState* self) {
}

ddsim->iterator++;
ddsim->previousInstructionStack.clear();
ddsim->previousInstructionStack
.clear(); // after measurements, we can no longer step back.
ddsim->restoreCallReturnStack.clear();
return OK;
}
if ((*ddsim->iterator)->getType() == qc::Reset) {
// Perform the desired qubits. This will first perform a measurement.
auto qubitsToMeasure = (*ddsim->iterator)->getTargets();
ddsim->iterator++;
ddsim->previousInstructionStack.clear();
Expand All @@ -370,10 +378,13 @@ Result ddsimStepForward(SimulationState* self) {
return OK;
}
if ((*ddsim->iterator)->getType() == qc::Barrier) {
// Do not do anything.
ddsim->iterator++;
return OK;
}
if ((*ddsim->iterator)->isClassicControlledOperation()) {
// For classic-controlled operations, we need to read the values of the
// classical register first.
const auto* op =
dynamic_cast<qc::ClassicControlledOperation*>((*ddsim->iterator).get());
const auto& controls = op->getControlRegister();
Expand All @@ -390,6 +401,7 @@ Result ddsimStepForward(SimulationState* self) {
currDD = ddsim->dd->makeIdent();
}
} else {
// For all other operations, we just take the next gate to apply.
currDD = dd::getDD(ddsim->iterator->get(),
*ddsim->dd); // retrieve the "new" current operation
}
Expand Down Expand Up @@ -1076,6 +1088,17 @@ std::string preprocessAssertionCode(const char* code,
for (const auto& dependency : instruction.dataDependencies) {
ddsim->dataDependencies[instruction.lineNumber].push_back(dependency);
}

// what exactly we do with each instruction depends on its type:
// - RETURN instructions are not simulated.
// - RETURN and ASSERTION instructions are not added to the final code.
// - Custom gate definitions are also not simulated.
// - Function calls are simulated but will be replaced by their flattened
// instructions later.
// - For register declarations, we store the register name.
// - Instructions inside function definitions are not added to the final
// code because they were already added when the function definition was
// first encountered.
if (instruction.code == "RETURN") {
ddsim->instructionTypes.push_back(RETURN);
} else if (instruction.assertion != nullptr) {
Expand Down Expand Up @@ -1156,6 +1179,7 @@ std::string preprocessAssertionCode(const char* code,
}
}

// Transform all the correct lines into a single code string.
const auto result = std::accumulate(
correctLines.begin(), correctLines.end(), std::string(),
[](const std::string& a, const std::string& b) { return a + b; });
Expand Down
4 changes: 3 additions & 1 deletion src/backend/dd/DDSimDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ Result createDDDiagnostics(DDDiagnostics* self, DDSimulationState* state) {
return self->interface.init(&self->interface);
}

Result destroyDDDiagnostics([[maybe_unused]] DDDiagnostics* self) { return OK; }
Result destroyDDDiagnostics(DDDiagnostics* self) {
self->simulationState = nullptr;

Check failure

Code scanning / CodeQL

Missing return statement Error

Function destroyDDDiagnostics should return a value of type Result but does not return a value here
}

Check warning on line 45 in src/backend/dd/DDSimDiagnostics.cpp

View workflow job for this annotation

GitHub Actions / 🇨‌ Lint / 🚨 Lint

src/backend/dd/DDSimDiagnostics.cpp:45:1 [clang-diagnostic-return-type]

non-void function does not return a value

size_t dddiagnosticsGetNumQubits(Diagnostics* self) {
const auto* ddd = toDDDiagnostics(self);
Expand Down
4 changes: 3 additions & 1 deletion src/common/parsing/CodePreprocessing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ std::string sweepBlocks(const std::string& code,
std::string result = code;
size_t start = 0;
int level = 0;
for (size_t pos = 0; pos < result.size(); pos++) {
size_t pos = 0;
while (pos < result.size()) {
auto c = result[pos];
if (c == '{') {
if (level == 0) {
Expand All @@ -50,6 +51,7 @@ std::string sweepBlocks(const std::string& code,
pos = start;
}
}
pos++;
}
return result;
}
Expand Down

0 comments on commit 52128a2

Please sign in to comment.