diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b2d12057..c177b9865 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ This project adheres to [Semantic Versioning], with the exception that minor rel ### Added -- ✨ Add initial infrastructure for new QC and QCO MLIR dialects ([#1264], [#1402], [#1428], [#1430], [#1436], [#1443], [#1446], [#1464], [#1465]) ([**@burgholzer**], [**@denialhaag**], [**@taminob**], [**@DRovara**], [**@li-mingbao**]) +- ✨ Add initial infrastructure for new QC and QCO MLIR dialects ([#1264], [#1402], [#1428], [#1430], [#1436], [#1443], [#1446], [#1464], [#1465], [#1470]) ([**@burgholzer**], [**@denialhaag**], [**@taminob**], [**@DRovara**], [**@li-mingbao**]) ### Changed @@ -315,6 +315,7 @@ _📚 Refer to the [GitHub Release Notes](https://github.com/munich-quantum-tool +[#1470]: https://github.com/munich-quantum-toolkit/core/pull/1470 [#1466]: https://github.com/munich-quantum-toolkit/core/pull/1466 [#1465]: https://github.com/munich-quantum-toolkit/core/pull/1465 [#1464]: https://github.com/munich-quantum-toolkit/core/pull/1464 diff --git a/mlir/tools/mqt-cc/CMakeLists.txt b/mlir/tools/mqt-cc/CMakeLists.txt index 610787616..14d5649a4 100644 --- a/mlir/tools/mqt-cc/CMakeLists.txt +++ b/mlir/tools/mqt-cc/CMakeLists.txt @@ -14,7 +14,11 @@ target_link_libraries( # Required for parsing .mlir files MLIRParser # Required for file I/O - MLIRSupport) + MLIRSupport + # Required for OpenQASM parsing + MQT::CoreQASM + MQT::CoreIR + MLIRQCTranslation) llvm_update_compile_flags(mqt-cc) mlir_check_all_link_libraries(mqt-cc) export_executable_symbols_for_plugins(mqt-cc) diff --git a/mlir/tools/mqt-cc/mqt-cc.cpp b/mlir/tools/mqt-cc/mqt-cc.cpp index c7de85585..fd25e8d5e 100644 --- a/mlir/tools/mqt-cc/mqt-cc.cpp +++ b/mlir/tools/mqt-cc/mqt-cc.cpp @@ -8,10 +8,15 @@ * Licensed under the MIT License */ +#include "ir/QuantumComputation.hpp" #include "mlir/Compiler/CompilerPipeline.h" #include "mlir/Dialect/QC/IR/QCDialect.h" +#include "mlir/Dialect/QC/Translation/TranslateQuantumComputationToQC.h" #include "mlir/Dialect/QCO/IR/QCODialect.h" +#include "qasm3/Exception.hpp" +#include "qasm3/Importer.hpp" +#include #include #include #include @@ -38,7 +43,7 @@ namespace { // Command-line options const cl::opt INPUT_FILENAME(cl::Positional, - cl::desc(""), + cl::desc(""), cl::init("-")); const cl::opt OUTPUT_FILENAME("o", cl::desc("Output filename"), @@ -69,6 +74,27 @@ const cl::opt } // namespace +/** + * @brief Load and parse a .qasm file + */ +static OwningOpRef loadQASMFile(StringRef filename, + MLIRContext* context) { + try { + // Parse the input QASM file + const ::qc::QuantumComputation qc = + qasm3::Importer::importf(filename.str()); + // Translate to MLIR dialect QC + return translateQuantumComputationToQC(context, qc); + } catch (const qasm3::CompilerError& exception) { + errs() << "Failed to parse QASM file '" << filename << "': '" + << exception.what() << "'\n"; + } catch (const std::exception& exception) { + errs() << "Failed to load QASM file '" << filename << "': '" + << exception.what() << "'\n"; + } + return nullptr; +} + /** * @brief Load and parse a .mlir file */ @@ -78,7 +104,8 @@ static OwningOpRef loadMLIRFile(StringRef filename, std::string errorMessage; auto file = openInputFile(filename, &errorMessage); if (!file) { - errs() << errorMessage << "\n"; + errs() << "Failed to load file '" << filename << "': '" << errorMessage + << "'\n"; return nullptr; } @@ -107,12 +134,12 @@ static mlir::LogicalResult writeOutput(ModuleOp module, StringRef filename) { int main(int argc, char** argv) { const InitLLVM y(argc, argv); - // Parse command-line options + // Parse command-line options; exit on error and print to stderr cl::ParseCommandLineOptions(argc, argv, "MQT Core Compiler Driver\n"); // Set up MLIR context with all required dialects DialectRegistry registry; - registry.insert(); + registry.insert(); registry.insert(); registry.insert(); registry.insert(); @@ -124,9 +151,13 @@ int main(int argc, char** argv) { context.loadAllAvailableDialects(); // Load the input .mlir file - const auto module = loadMLIRFile(INPUT_FILENAME, &context); + OwningOpRef module; + if (INPUT_FILENAME.getValue().ends_with(".qasm")) { + module = loadQASMFile(INPUT_FILENAME, &context); + } else { + module = loadMLIRFile(INPUT_FILENAME, &context); + } if (!module) { - errs() << "Failed to load input file: " << INPUT_FILENAME << "\n"; return 1; }