Skip to content

Commit

Permalink
fix 🐛 Fix bug on "gate" in gate names (#8)
Browse files Browse the repository at this point in the history
* fix: 🐛 Fix bug that could make execution crash if a custom gate name ends with "gate"

* refactor: ⚰️ Remove unnecessary old test
  • Loading branch information
DRovara authored Aug 9, 2024
1 parent 89928c6 commit 7469237
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 25 deletions.
1 change: 1 addition & 0 deletions include/backend/dd/DDSimDebug.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ struct DDSimulationState {
std::vector<InstructionType> instructionTypes;
std::vector<size_t> instructionStarts;
std::vector<size_t> instructionEnds;
std::set<size_t> functionDefinitions;
std::map<size_t, std::unique_ptr<Assertion>> assertionInstructions;
std::map<size_t, size_t> successorInstructions;
std::vector<QubitRegisterDefinition> qubitRegisters;
Expand Down
4 changes: 3 additions & 1 deletion include/common/parsing/CodePreprocessing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct Instruction {
bool isFunctionCall;
std::string calledFunction;
bool inFunctionDefinition;
bool isFunctionDefition;

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

Expand All @@ -39,7 +40,8 @@ struct Instruction {
std::unique_ptr<Assertion>& inputAssertion,
std::set<std::string> inputTargets, size_t startPos,
size_t endPos, size_t successor, bool isFuncCall,
std::string function, bool inFuncDef, Block inputBlock);
std::string function, bool inFuncDef, bool isFuncDef,
Block inputBlock);
};

struct FunctionDefinition {
Expand Down
8 changes: 5 additions & 3 deletions src/backend/dd/DDSimDebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -725,8 +725,8 @@ Result ddsimSetBreakpoint(SimulationState* self, size_t desiredPosition,
const size_t start = ddsim->instructionStarts[i];
const size_t end = ddsim->instructionEnds[i];
if (desiredPosition >= start && desiredPosition <= end) {
if (ddsim->processedCode.substr(start, end - start).find("gate ") !=
std::string::npos) {
if (ddsim->functionDefinitions.find(i) !=
ddsim->functionDefinitions.end()) {
// Breakpoint may be located in a sub-gate of the gate definition.
for (auto j = i + 1; j < ddsim->instructionTypes.size(); j++) {
const size_t startSub = ddsim->instructionStarts[j];
Expand Down Expand Up @@ -1091,6 +1091,7 @@ std::string preprocessAssertionCode(const char* code,
auto instructions = preprocessCode(code, ddsim->processedCode);
std::vector<std::string> correctLines;
ddsim->instructionTypes.clear();
ddsim->functionDefinitions.clear();
ddsim->instructionStarts.clear();
ddsim->instructionEnds.clear();
ddsim->callSubstitutions.clear();
Expand Down Expand Up @@ -1127,11 +1128,12 @@ std::string preprocessAssertionCode(const char* code,
ddsim->instructionTypes.push_back(ASSERTION);
ddsim->assertionInstructions.insert(
{instruction.lineNumber, std::move(instruction.assertion)});
} else if (instruction.code.find("gate") != std::string::npos) {
} else if (instruction.isFunctionDefition) {
if (!instruction.inFunctionDefinition) {
correctLines.push_back(
validCodeFromChildren(instruction, instructions));
}
ddsim->functionDefinitions.insert(instruction.lineNumber);
ddsim->instructionTypes.push_back(NOP);
} else if (instruction.isFunctionCall) {
if (!instruction.inFunctionDefinition) {
Expand Down
16 changes: 10 additions & 6 deletions src/common/parsing/CodePreprocessing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ Instruction::Instruction(size_t inputLineNumber, std::string inputCode,
std::unique_ptr<Assertion>& inputAssertion,
std::set<std::string> inputTargets, size_t startPos,
size_t endPos, size_t successor, bool isFuncCall,
std::string function, bool inFuncDef, Block inputBlock)
std::string function, bool inFuncDef, bool isFuncDef,
Block inputBlock)
: lineNumber(inputLineNumber), code(std::move(inputCode)),
assertion(std::move(inputAssertion)), targets(std::move(inputTargets)),
originalCodeStartPosition(startPos), originalCodeEndPosition(endPos),
successorIndex(successor), isFunctionCall(isFuncCall),
calledFunction(std::move(function)), inFunctionDefinition(inFuncDef),
block(std::move(inputBlock)) {}
isFunctionDefition(isFuncDef), block(std::move(inputBlock)) {}

std::string sweepBlocks(const std::string& code,
std::map<std::string, std::string>& blocks) {
Expand Down Expand Up @@ -222,7 +223,7 @@ preprocessCode(const std::string& code, size_t startIndex,
std::unique_ptr<Assertion> a(nullptr);
instructions.emplace_back(i - subInstructions.size() - 1, line, a,
targets, trueStart, trueEnd, i + 1, false, "",
false, block);
false, true, block);
for (auto& instr : subInstructions) {
instructions.back().childInstructions.push_back(instr.lineNumber);
}
Expand All @@ -234,7 +235,8 @@ preprocessCode(const std::string& code, size_t startIndex,
'}', instructions[instructions.size() - 1].originalCodeEndPosition);
const Block noBlock{false, ""};
instructions.emplace_back(i, "RETURN", a, targets, closingBrace,
closingBrace, 0, false, "", true, noBlock);
closingBrace, 0, false, "", true, false,
noBlock);
i++;
pos = end + 1;

Expand All @@ -252,11 +254,13 @@ preprocessCode(const std::string& code, size_t startIndex,
if (isAssert) {
auto a = parseAssertion(line, block.code);
instructions.emplace_back(i, line, a, targets, trueStart, trueEnd, i + 1,
isFunctionCall, calledFunction, false, block);
isFunctionCall, calledFunction, false, false,
block);
} else {
std::unique_ptr<Assertion> a(nullptr);
instructions.emplace_back(i, line, a, targets, trueStart, trueEnd, i + 1,
isFunctionCall, calledFunction, false, block);
isFunctionCall, calledFunction, false, false,
block);

variableUsages.insert({i, parseParameters(line)});
}
Expand Down
24 changes: 9 additions & 15 deletions test/test_custom_code.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,13 @@ TEST_F(CustomCodeTest, ResetGate) {
ASSERT_TRUE(complexEquality(result, -1.0, 0.0));
}

TEST_F(CustomCodeTest, DependenciesWithJumps) {
loadCode(3, 1,
"gate entangle q0, q1, q2 {\n"
" cx q0, q1;\n"
" cx q0, q2;\n"
" barrier q2;\n"
"}\n"
"\n"
"h q[0];\n"
"\n"
"entangle q[0], q[1], q[2];\n"
"\n"
"h q[2];\n"
"\n"
"barrier q[2];");
TEST_F(CustomCodeTest, GateInGateName) {
loadCode(1, 1,
"gate my_gate q0 {"
" x q0;"
"}"
"my_gate q[0];"
"measure q[0] -> c[0];"
"assert-eq q[0] { 0, 1 }");
state->runSimulation(state);
}

0 comments on commit 7469237

Please sign in to comment.