Skip to content

Commit bebb1f7

Browse files
committed
Address the Rabbit's comments
1 parent 9ec699b commit bebb1f7

3 files changed

Lines changed: 34 additions & 21 deletions

File tree

mlir/include/mlir/Dialect/QC/Translation/TranslateQASM3ToQC.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,20 @@
1919
namespace mlir::qc {
2020

2121
/**
22-
* @brief Import a QASM3 program directly into the QC dialect.
22+
* @brief Translate an OpenQASM3 program to the QC dialect.
2323
*
24-
* @details Bypasses qc::QuantumComputation: the parser produces an AST walked
25-
* by an InstVisitor that emits QC dialect ops via QCProgramBuilder.
26-
* Returns nullptr on failure (diagnostics written to llvm::errs()).
24+
* @param context MLIRContext to create the module in.
25+
* @param filename Path to the input OpenQASM3 file.
2726
*/
2827
[[nodiscard]] OwningOpRef<ModuleOp>
2928
translateQASM3ToQC(MLIRContext* context, const std::string& filename);
3029

30+
/**
31+
* @brief Translate an OpenQASM3 program to the QC dialect.
32+
*
33+
* @param context MLIRContext to create the module in.
34+
* @param input Stream containing the OpenQASM3 program.
35+
*/
3136
[[nodiscard]] OwningOpRef<ModuleOp> translateQASM3ToQC(MLIRContext* context,
3237
std::istream& input);
3338

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

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,8 @@ class MLIRQasmImporter final : public qasm3::InstVisitor {
325325
break;
326326
}
327327
default:
328-
break;
328+
throw qasm3::CompilerError("Unsupported declaration type.",
329+
stmt->debugInfo);
329330
}
330331

331332
// Handle initializer (measure only)
@@ -430,11 +431,12 @@ class MLIRQasmImporter final : public qasm3::InstVisitor {
430431
}
431432
for (size_t i = 0; i < qubits.size(); ++i) {
432433
// Use the MeasureOp directly to capture the i1 result for if/else
433-
auto measureOp = MeasureOp::create(
434-
builder, qubits[i], builder.getStringAttr(bits[i].registerName),
435-
builder.getI64IntegerAttr(bits[i].registerSize),
436-
builder.getI64IntegerAttr(bits[i].registerIndex));
437-
Value result = measureOp.getResult();
434+
auto result =
435+
MeasureOp::create(builder, qubits[i],
436+
builder.getStringAttr(bits[i].registerName),
437+
builder.getI64IntegerAttr(bits[i].registerSize),
438+
builder.getI64IntegerAttr(bits[i].registerIndex))
439+
.getResult();
438440

439441
// Track the result for use in if/else conditions
440442
const auto& regName = bits[i].registerName;
@@ -469,7 +471,7 @@ class MLIRQasmImporter final : public qasm3::InstVisitor {
469471
return;
470472
}
471473

472-
Value condition = translateCondition(stmt->condition, stmt->debugInfo);
474+
auto condition = translateCondition(stmt->condition, stmt->debugInfo);
473475
const bool hasElse = !stmt->elseStatements.empty();
474476

475477
auto ifOp =
@@ -580,7 +582,8 @@ class MLIRQasmImporter final : public qasm3::InstVisitor {
580582
size_t ctrlIdx = 0;
581583
for (const auto& [n, positive] : ctrlSpec) {
582584
for (size_t i = 0; i < n; ++i, ++ctrlIdx) {
583-
if (expandedOperands[ctrlIdx].size() != 1) {
585+
if (ctrlIdx >= expandedOperands.size() ||
586+
expandedOperands[ctrlIdx].size() != 1) {
584587
throw qasm3::CompilerError("Control operand must be a single qubit.",
585588
stmt->debugInfo);
586589
}
@@ -659,7 +662,7 @@ class MLIRQasmImporter final : public qasm3::InstVisitor {
659662

660663
// Check that no qubit appears twice across targets and controls.
661664
llvm::SmallDenseSet<Value> seen;
662-
for (auto q : concat<const Value>(iterQubits, posControls, negControls)) {
665+
for (auto q : concat<Value>(iterQubits, posControls, negControls)) {
663666
if (!seen.insert(q).second) {
664667
throw qasm3::CompilerError("Duplicate qubit in gate '" + id +
665668
"' operands.",
@@ -731,7 +734,7 @@ class MLIRQasmImporter final : public qasm3::InstVisitor {
731734
if (const auto binaryExpr =
732735
std::dynamic_pointer_cast<qasm3::BinaryExpression>(condition)) {
733736
throw qasm3::CompilerError(
734-
"Register comparisons cannot be translated to QC at the moment",
737+
"Register comparisons cannot be translated to QC at the moment.",
735738
debugInfo);
736739
}
737740

@@ -742,11 +745,17 @@ class MLIRQasmImporter final : public qasm3::InstVisitor {
742745
unaryExpr->op == qasm3::UnaryExpression::BitwiseNot);
743746
const auto idExpr = std::dynamic_pointer_cast<qasm3::IndexedIdentifier>(
744747
unaryExpr->operand);
745-
Value bitVal = lookupBitValue(idExpr, debugInfo);
748+
if (!idExpr) {
749+
throw qasm3::CompilerError("Unary expression has unsupported operand.",
750+
debugInfo);
751+
}
752+
auto bitVal = lookupBitValue(idExpr, debugInfo);
746753
// Negate: XOR with true
747-
Value trueVal = arith::ConstantOp::create(
748-
builder, builder.getIntegerAttr(builder.getI1Type(), 1));
749-
return arith::XOrIOp::create(builder, bitVal, trueVal);
754+
auto trueVal =
755+
arith::ConstantOp::create(
756+
builder, builder.getIntegerAttr(builder.getI1Type(), 1))
757+
.getResult();
758+
return arith::XOrIOp::create(builder, bitVal, trueVal).getResult();
750759
}
751760

752761
// Case 3: Single bit (c[0] — truthy)

mlir/tools/mqt-cc/mqt-cc.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ static llvm::cl::opt<bool> enableHadamardLifting(
8787
*/
8888
static OwningOpRef<ModuleOp> loadQASMFile(StringRef filename,
8989
MLIRContext* context) {
90-
return mlir::qc::translateQASM3ToQC(context, filename.str());
90+
return qc::translateQASM3ToQC(context, filename.str());
9191
}
9292

9393
/**
@@ -134,15 +134,14 @@ int main(int argc, char** argv) {
134134

135135
// Set up MLIR context with all required dialects
136136
DialectRegistry registry;
137-
registry.insert<mlir::qc::QCDialect>();
137+
registry.insert<qc::QCDialect>();
138138
registry.insert<qco::QCODialect>();
139139
registry.insert<arith::ArithDialect>();
140140
registry.insert<cf::ControlFlowDialect>();
141141
registry.insert<func::FuncDialect>();
142142
registry.insert<memref::MemRefDialect>();
143143
registry.insert<scf::SCFDialect>();
144144
registry.insert<LLVM::LLVMDialect>();
145-
registry.insert<mlir::memref::MemRefDialect>();
146145
registry.insert<qtensor::QTensorDialect>();
147146

148147
MLIRContext context(registry);

0 commit comments

Comments
 (0)