Skip to content

Commit 9f0f983

Browse files
committed
Address the Rabbit's comments
1 parent 78c8938 commit 9f0f983

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
@@ -326,7 +326,8 @@ class MLIRQasmImporter final : public qasm3::InstVisitor {
326326
break;
327327
}
328328
default:
329-
break;
329+
throw qasm3::CompilerError("Unsupported declaration type.",
330+
stmt->debugInfo);
330331
}
331332

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

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

473-
Value condition = translateCondition(stmt->condition, stmt->debugInfo);
475+
auto condition = translateCondition(stmt->condition, stmt->debugInfo);
474476
const bool hasElse = !stmt->elseStatements.empty();
475477

476478
auto ifOp =
@@ -581,7 +583,8 @@ class MLIRQasmImporter final : public qasm3::InstVisitor {
581583
size_t ctrlIdx = 0;
582584
for (const auto& [n, positive] : ctrlSpec) {
583585
for (size_t i = 0; i < n; ++i, ++ctrlIdx) {
584-
if (expandedOperands[ctrlIdx].size() != 1) {
586+
if (ctrlIdx >= expandedOperands.size() ||
587+
expandedOperands[ctrlIdx].size() != 1) {
585588
throw qasm3::CompilerError("Control operand must be a single qubit.",
586589
stmt->debugInfo);
587590
}
@@ -660,7 +663,7 @@ class MLIRQasmImporter final : public qasm3::InstVisitor {
660663

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

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

753762
// 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)