Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "mlir/Dialect/MQT/Utils/Modifiers.h"
#include "mlir/Dialect/QCO/IR/QCOInterfaces.h"
#include "mlir/Dialect/QCO/IR/QCOOps.h"
#include "mlir/Dialect/QCO/QCOUtils.h"
#include "mlir/Dialect/QCO/Transforms/Passes.h"

#include <llvm/ADT/STLExtras.h>
Expand Down Expand Up @@ -228,6 +229,10 @@ struct HadamardLifting final : impl::HadamardLiftingBase<HadamardLifting> {
void runOnOperation() override {
auto op = getOperation();
auto* ctx = &getContext();
if (failed(qco::verifyLinearity(op))) {
signalPassFailure();
return;
}
Comment on lines +232 to +235

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. I am not sure we should actually be adding this in front of every pass.
Passes can expect valid IR. They should not have to verify that it is valid.
I'd rather drop that in cases like here.
Does that make sense?


// Define the set of patterns to use.
RewritePatternSet patterns(ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
#include <mlir/Dialect/MemRef/IR/MemRef.h>
#include <mlir/IR/BuiltinOps.h>
#include <mlir/IR/DialectRegistry.h>
#include <mlir/IR/OperationSupport.h>
#include <mlir/IR/OwningOpRef.h>
#include <mlir/IR/Value.h>
#include <mlir/IR/Verifier.h>
#include <mlir/Parser/Parser.h>
#include <mlir/Pass/PassManager.h>
#include <mlir/Support/LLVM.h>
#include <mlir/Support/LogicalResult.h>
Expand Down Expand Up @@ -81,6 +84,56 @@ class QCOHadamardLiftingTest : public testing::Test {

} // namespace

TEST_F(QCOHadamardLiftingTest, HandlesUnusedPauliOutput) {
auto input = parseSourceString<ModuleOp>(R"mlir(
module {
func.func @main() {
%q = qco.static 0 : !qco.qubit
%unused = qco.x %q : !qco.qubit -> !qco.qubit
return
}
}
)mlir",
Comment on lines +87 to +96

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is invalid QCO IR.
We should really enforce the invariant that passes may assume IR to be valid.
We may check in tests that this is actually true before and after passes, but the corner case checking in all of our passes can be reduced.
Even more so can the amount of tests that check these invalid IR programs.
This extends well beyond this PR.

&context);
ASSERT_TRUE(input);
ASSERT_TRUE(succeeded(verify(*input)));
OwningOpRef<ModuleOp> original(input->clone());

EXPECT_TRUE(failed(runHadamardLiftingPass(*input)));
EXPECT_TRUE(OperationEquivalence::isEquivalentTo(
input->getOperation(), original->getOperation(),
OperationEquivalence::Flags::None));
}

TEST_F(QCOHadamardLiftingTest, HandlesUnusedCnotControlOutput) {
auto input = parseSourceString<ModuleOp>(R"mlir(
module {
func.func @main() {
%control = qco.static 0 : !qco.qubit
%target = qco.static 1 : !qco.qubit
%unused, %target_out = qco.ctrl(%control) targets(%arg = %target) {
%body = qco.x %arg : !qco.qubit -> !qco.qubit
qco.yield %body : !qco.qubit
} : ({!qco.qubit}, {!qco.qubit})
-> ({!qco.qubit}, {!qco.qubit})
%hadamard = qco.h %target_out : !qco.qubit -> !qco.qubit
%measured, %result = qco.measure %hadamard : !qco.qubit
qco.sink %measured : !qco.qubit
return
}
}
)mlir",
&context);
ASSERT_TRUE(input);
ASSERT_TRUE(succeeded(verify(*input)));
OwningOpRef<ModuleOp> original(input->clone());

EXPECT_TRUE(failed(runHadamardLiftingPass(*input)));
EXPECT_TRUE(OperationEquivalence::isEquivalentTo(
input->getOperation(), original->getOperation(),
OperationEquivalence::Flags::None));
}

// ##################################################
// # Raise Hadamard over uncontrolled Pauli gate Tests
// ##################################################
Expand Down
Loading