Skip to content

Commit d9ada9c

Browse files
committed
Address the Rabbit's comments from #1671
1 parent c216c15 commit d9ada9c

5 files changed

Lines changed: 65 additions & 13 deletions

File tree

mlir/lib/Dialect/QC/Translation/TranslateQASM3ToQC.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,10 @@ class MLIRQasmImporter final : public qasm3::InstVisitor {
300300
}
301301
declarations.emplace(id, stmt);
302302

303+
if (stmt->isConst) {
304+
return; // nothing to emit
305+
}
306+
303307
const auto ty = std::get<1>(stmt->type);
304308
const auto sizedTy =
305309
std::dynamic_pointer_cast<qasm3::DesignatedType<uint64_t>>(ty);
@@ -340,9 +344,6 @@ class MLIRQasmImporter final : public qasm3::InstVisitor {
340344
visitMeasureAssignment(lhsId, measureExpr, stmt->debugInfo);
341345
return;
342346
}
343-
if (stmt->isConst) {
344-
return; // nothing to emit
345-
}
346347
throw qasm3::CompilerError(
347348
"Only measure statements are supported as initializers.",
348349
stmt->debugInfo);
@@ -734,11 +735,14 @@ class MLIRQasmImporter final : public qasm3::InstVisitor {
734735
for (size_t i = 0; i < gate.targetNames.size(); ++i) {
735736
for (auto target : gateOperands[i]) {
736737
auto* it = llvm::find(targets, target);
738+
size_t index = 0;
737739
if (it == targets.end()) {
740+
index = targets.size();
738741
targets.push_back(target);
742+
} else {
743+
index = static_cast<size_t>(std::distance(targets.begin(), it));
739744
}
740-
targetsMap[gate.targetNames[i]].push_back(
741-
std::distance(targets.begin(), it));
745+
targetsMap[gate.targetNames[i]].push_back(index);
742746
}
743747
}
744748

mlir/unittests/Dialect/QC/Translation/test_qasm3_translation.cpp

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,20 @@ class QASM3TranslationTest
6868

6969
} // namespace
7070

71+
static void singleNegControlledX(qc::QCProgramBuilder& b) {
72+
auto q = b.allocQubitRegister(2);
73+
b.x(q[0]);
74+
b.cx(q[0], q[1]);
75+
b.x(q[0]);
76+
}
77+
78+
static void mixedControlledX(qc::QCProgramBuilder& b) {
79+
auto q = b.allocQubitRegister(3);
80+
b.x(q[1]);
81+
b.mcx({q[0], q[1]}, q[2]);
82+
b.x(q[1]);
83+
}
84+
7185
TEST_P(QASM3TranslationTest, ProgramEquivalence) {
7286
const auto name = " (" + GetParam().name + ")";
7387
const auto& source = GetParam().source;
@@ -148,6 +162,11 @@ INSTANTIATE_TEST_SUITE_P(
148162
QASM3TranslationTestCase{"X", qasm::x, MQT_NAMED_BUILDER(qc::x)},
149163
QASM3TranslationTestCase{"SingleControlledX", qasm::singleControlledX,
150164
MQT_NAMED_BUILDER(qc::singleControlledX)},
165+
QASM3TranslationTestCase{"SingleNegControlledX",
166+
qasm::singleNegControlledX,
167+
MQT_NAMED_BUILDER(singleNegControlledX)},
168+
QASM3TranslationTestCase{"MixedControlledX", qasm::mixedControlledX,
169+
MQT_NAMED_BUILDER(mixedControlledX)},
151170
QASM3TranslationTestCase{"MultipleControlledX",
152171
qasm::multipleControlledX,
153172
MQT_NAMED_BUILDER(qc::multipleControlledX)},
@@ -341,12 +360,12 @@ INSTANTIATE_TEST_SUITE_P(
341360
qasm::barrierMultipleQubits,
342361
MQT_NAMED_BUILDER(qc::barrierMultipleQubits)},
343362
QASM3TranslationTestCase{"CtrlTwo", qasm::ctrlTwo,
344-
MQT_NAMED_BUILDER(mlir::qc::ctrlTwo)},
363+
MQT_NAMED_BUILDER(qc::ctrlTwo)},
345364
QASM3TranslationTestCase{"CtrlTwoMixed", qasm::ctrlTwoMixed,
346-
MQT_NAMED_BUILDER(mlir::qc::ctrlTwoMixed)},
365+
MQT_NAMED_BUILDER(qc::ctrlTwoMixed)},
347366
QASM3TranslationTestCase{"SimpleIf", qasm::simpleIf,
348-
MQT_NAMED_BUILDER(mlir::qc::simpleIf)},
367+
MQT_NAMED_BUILDER(qc::simpleIf)},
349368
QASM3TranslationTestCase{"IfTwoQubits", qasm::ifTwoQubits,
350-
MQT_NAMED_BUILDER(mlir::qc::ifTwoQubits)},
369+
MQT_NAMED_BUILDER(qc::ifTwoQubits)},
351370
QASM3TranslationTestCase{"IfElse", qasm::ifElse,
352-
MQT_NAMED_BUILDER(mlir::qc::ifElse)}));
371+
MQT_NAMED_BUILDER(qc::ifElse)}));

mlir/unittests/programs/CMakeLists.txt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,31 @@
77
# Licensed under the MIT License
88

99
add_library(MLIRQASMPrograms qasm_programs.cpp)
10+
target_link_libraries(MLIRQASMPrograms PRIVATE MQT::ProjectOptions)
1011
target_sources(MLIRQASMPrograms PUBLIC FILE_SET HEADERS BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR} FILES
1112
qasm_programs.h)
1213

1314
add_library(MLIRQCPrograms qc_programs.cpp)
14-
target_link_libraries(MLIRQCPrograms PUBLIC MLIRQCProgramBuilder)
15+
target_link_libraries(
16+
MLIRQCPrograms
17+
PUBLIC MLIRQCProgramBuilder
18+
PRIVATE MQT::ProjectOptions)
1519
target_sources(MLIRQCPrograms PUBLIC FILE_SET HEADERS BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR} FILES
1620
qc_programs.h)
1721

1822
add_library(MLIRQCOPrograms qco_programs.cpp)
19-
target_link_libraries(MLIRQCOPrograms PUBLIC MLIRQCOProgramBuilder)
23+
target_link_libraries(
24+
MLIRQCOPrograms
25+
PUBLIC MLIRQCOProgramBuilder
26+
PRIVATE MQT::ProjectOptions)
2027
target_sources(MLIRQCOPrograms PUBLIC FILE_SET HEADERS BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR} FILES
2128
qco_programs.h)
2229

2330
add_library(MLIRQIRPrograms qir_programs.cpp)
24-
target_link_libraries(MLIRQIRPrograms PUBLIC MLIRQIRProgramBuilder)
31+
target_link_libraries(
32+
MLIRQIRPrograms
33+
PUBLIC MLIRQIRProgramBuilder
34+
PRIVATE MQT::ProjectOptions)
2535
target_sources(MLIRQIRPrograms PUBLIC FILE_SET HEADERS BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR} FILES
2636
qir_programs.h)
2737

mlir/unittests/programs/qasm_programs.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,24 @@ qubit[2] q;
132132
ctrl @ x q[0], q[1];
133133
)qasm";
134134

135+
const std::string singleNegControlledX = R"qasm(OPENQASM 3.0;
136+
include "stdgates.inc";
137+
qubit[2] q;
138+
negctrl @ x q[0], q[1];
139+
)qasm";
140+
135141
const std::string multipleControlledX = R"qasm(OPENQASM 3.0;
136142
include "stdgates.inc";
137143
qubit[3] q;
138144
ctrl(2) @ x q[0], q[1], q[2];
139145
)qasm";
140146

147+
const std::string mixedControlledX = R"qasm(OPENQASM 3.0;
148+
include "stdgates.inc";
149+
qubit[3] q;
150+
ctrl @ negctrl @ x q[0], q[1], q[2];
151+
)qasm";
152+
141153
const std::string y = R"qasm(OPENQASM 3.0;
142154
include "stdgates.inc";
143155
qubit[1] q;

mlir/unittests/programs/qasm_programs.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,16 @@ extern const std::string x;
6666
/// Creates a circuit with a single controlled X gate.
6767
extern const std::string singleControlledX;
6868

69+
/// Creates a circuit with a single negatively controlled X gate.
70+
extern const std::string singleNegControlledX;
71+
6972
/// Creates a circuit with a multi-controlled X gate.
7073
extern const std::string multipleControlledX;
7174

75+
/// Creates a circuit with an X gate that is positively and negatively
76+
/// controlled.
77+
extern const std::string mixedControlledX;
78+
7279
/// Creates a circuit with just a Y gate.
7380
extern const std::string y;
7481

0 commit comments

Comments
 (0)