Skip to content

Commit ff5fae7

Browse files
committed
Address the Rabbit's comments
1 parent 6a586ad commit ff5fae7

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)
@@ -433,11 +434,12 @@ class MLIRQasmImporter final : public qasm3::InstVisitor {
433434
}
434435
for (size_t i = 0; i < qubits.size(); ++i) {
435436
// Use the MeasureOp directly to capture the i1 result for if/else
436-
auto measureOp = MeasureOp::create(
437-
builder, qubits[i], builder.getStringAttr(bits[i].registerName),
438-
builder.getI64IntegerAttr(bits[i].registerSize),
439-
builder.getI64IntegerAttr(bits[i].registerIndex));
440-
Value result = measureOp.getResult();
437+
auto result =
438+
MeasureOp::create(builder, qubits[i],
439+
builder.getStringAttr(bits[i].registerName),
440+
builder.getI64IntegerAttr(bits[i].registerSize),
441+
builder.getI64IntegerAttr(bits[i].registerIndex))
442+
.getResult();
441443

442444
// Track the result for use in if/else conditions
443445
const auto& regName = bits[i].registerName;
@@ -472,7 +474,7 @@ class MLIRQasmImporter final : public qasm3::InstVisitor {
472474
return;
473475
}
474476

475-
Value condition = translateCondition(stmt->condition, stmt->debugInfo);
477+
auto condition = translateCondition(stmt->condition, stmt->debugInfo);
476478
const bool hasElse = !stmt->elseStatements.empty();
477479

478480
auto ifOp =
@@ -585,7 +587,8 @@ class MLIRQasmImporter final : public qasm3::InstVisitor {
585587
size_t ctrlIdx = 0;
586588
for (const auto& [n, positive] : ctrlSpec) {
587589
for (size_t i = 0; i < n; ++i, ++ctrlIdx) {
588-
if (expandedOperands[ctrlIdx].size() != 1) {
590+
if (ctrlIdx >= expandedOperands.size() ||
591+
expandedOperands[ctrlIdx].size() != 1) {
589592
throw qasm3::CompilerError("Control operand must be a single qubit.",
590593
stmt->debugInfo);
591594
}
@@ -664,7 +667,7 @@ class MLIRQasmImporter final : public qasm3::InstVisitor {
664667

665668
// Check that no qubit appears twice across targets and controls.
666669
llvm::SmallDenseSet<Value> seen;
667-
for (auto q : concat<const Value>(iterQubits, posControls, negControls)) {
670+
for (auto q : concat<Value>(iterQubits, posControls, negControls)) {
668671
if (!seen.insert(q).second) {
669672
throw qasm3::CompilerError("Duplicate qubit in gate '" + id +
670673
"' operands.",
@@ -817,7 +820,7 @@ class MLIRQasmImporter final : public qasm3::InstVisitor {
817820
if (const auto binaryExpr =
818821
std::dynamic_pointer_cast<qasm3::BinaryExpression>(condition)) {
819822
throw qasm3::CompilerError(
820-
"Register comparisons cannot be translated to QC at the moment",
823+
"Register comparisons cannot be translated to QC at the moment.",
821824
debugInfo);
822825
}
823826

@@ -828,11 +831,17 @@ class MLIRQasmImporter final : public qasm3::InstVisitor {
828831
unaryExpr->op == qasm3::UnaryExpression::BitwiseNot);
829832
const auto idExpr = std::dynamic_pointer_cast<qasm3::IndexedIdentifier>(
830833
unaryExpr->operand);
831-
Value bitVal = lookupBitValue(idExpr, debugInfo);
834+
if (!idExpr) {
835+
throw qasm3::CompilerError("Unary expression has unsupported operand.",
836+
debugInfo);
837+
}
838+
auto bitVal = lookupBitValue(idExpr, debugInfo);
832839
// Negate: XOR with true
833-
Value trueVal = arith::ConstantOp::create(
834-
builder, builder.getIntegerAttr(builder.getI1Type(), 1));
835-
return arith::XOrIOp::create(builder, bitVal, trueVal);
840+
auto trueVal =
841+
arith::ConstantOp::create(
842+
builder, builder.getIntegerAttr(builder.getI1Type(), 1))
843+
.getResult();
844+
return arith::XOrIOp::create(builder, bitVal, trueVal).getResult();
836845
}
837846

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