Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -315,6 +315,7 @@ _📚 Refer to the [GitHub Release Notes](https://github.com/munich-quantum-tool

<!-- PR links -->

[#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
Expand Down
6 changes: 5 additions & 1 deletion mlir/tools/mqt-cc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
43 changes: 37 additions & 6 deletions mlir/tools/mqt-cc/mqt-cc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <exception>
#include <llvm/Support/CommandLine.h>
#include <llvm/Support/InitLLVM.h>
#include <llvm/Support/SourceMgr.h>
Expand All @@ -38,7 +43,7 @@ namespace {

// Command-line options
const cl::opt<std::string> INPUT_FILENAME(cl::Positional,
cl::desc("<input .mlir file>"),
cl::desc("<input .mlir/.qasm file>"),
cl::init("-"));

const cl::opt<std::string> OUTPUT_FILENAME("o", cl::desc("Output filename"),
Expand Down Expand Up @@ -69,6 +74,27 @@ const cl::opt<bool>

} // namespace

/**
* @brief Load and parse a .qasm file
*/
static OwningOpRef<ModuleOp> 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
*/
Expand All @@ -78,7 +104,8 @@ static OwningOpRef<ModuleOp> 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;
}

Expand Down Expand Up @@ -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<qc::QCDialect>();
registry.insert<mlir::qc::QCDialect>();
registry.insert<qco::QCODialect>();
registry.insert<arith::ArithDialect>();
registry.insert<cf::ControlFlowDialect>();
Expand All @@ -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<ModuleOp> 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;
}

Expand Down
Loading