Skip to content

Commit dce1d64

Browse files
committed
fix test
1 parent 53ff436 commit dce1d64

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

test/Transforms/BasisConversion/all-qir-gates.qke

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,8 @@ module {
304304
// CHECK: %[[VAL_87:.*]] = cc.cast unsigned %[[VAL_85]] : (i1) -> i8
305305
// CHECK: cc.store %[[VAL_87]], %[[VAL_86]] : !cc.ptr<i8>
306306
// CHECK: %[[VAL_88:.*]] = quake.alloca !quake.ref
307-
// CHECK: quake.rz<adj> (%[[VAL_4]]) %[[VAL_88]] : (f64, !quake.ref) -> ()
307+
// CHECK: %[[VAL_89:.*]] = arith.negf %[[VAL_4]] : f64
308+
// CHECK: quake.rz (%[[VAL_89]]) %[[VAL_88]] : (f64, !quake.ref) -> ()
308309
// CHECK: quake.rx<adj> (%[[VAL_0]]) %[[VAL_88]] : (f64, !quake.ref) -> ()
309310
// CHECK: quake.ry<adj> (%[[VAL_3]]) %[[VAL_88]] : (f64, !quake.ref) -> ()
310311
// CHECK: quake.rz<adj> (%[[VAL_2]]) %[[VAL_88]] : (f64, !quake.ref) -> ()

unittests/Optimizer/DecompositionPatternsTest.cpp

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,29 @@ class DecompositionPatternsTest : public ::testing::Test {
4444
std::unique_ptr<MLIRContext> context;
4545
};
4646

47-
// Helper to parse control count from gate string like "x(1)" or "z(2)"
48-
std::pair<std::string, size_t> parseGateSpec(StringRef gateSpec) {
47+
// Helper to parse gate spec like "x(1)", "z(2)", "rx<adj>", "rx<adj>(1)"
48+
struct GateSpec {
49+
std::string name;
50+
size_t numControls;
51+
bool isAdj;
52+
};
53+
54+
GateSpec parseGateSpec(StringRef gateSpec) {
55+
bool isAdj = false;
56+
57+
// Strip <adj> if present
58+
auto adjPos = gateSpec.find("<adj>");
59+
std::string specStr;
60+
if (adjPos != StringRef::npos) {
61+
isAdj = true;
62+
specStr =
63+
gateSpec.substr(0, adjPos).str() + gateSpec.substr(adjPos + 5).str();
64+
gateSpec = specStr;
65+
}
66+
4967
auto pos = gateSpec.find('(');
5068
if (pos == StringRef::npos) {
51-
return {gateSpec.str(), 0};
69+
return {gateSpec.str(), 0, isAdj};
5270
}
5371

5472
std::string gateName = gateSpec.substr(0, pos).str();
@@ -62,12 +80,12 @@ std::pair<std::string, size_t> parseGateSpec(StringRef gateSpec) {
6280
numStr.consumeInteger(10, numControls);
6381
}
6482

65-
return {gateName, numControls};
83+
return {gateName, numControls, isAdj};
6684
}
6785

6886
// Helper function to create a test module with a single gate operation
6987
ModuleOp createTestModule(MLIRContext *context, StringRef gateSpec) {
70-
auto [gateName, numControls] = parseGateSpec(gateSpec);
88+
auto [gateName, numControls, isAdj] = parseGateSpec(gateSpec);
7189

7290
// Limit the number of controls to 2
7391
numControls = std::min<size_t>(numControls, 2);
@@ -161,6 +179,13 @@ ModuleOp createTestModule(MLIRContext *context, StringRef gateSpec) {
161179
ADD_FAILURE() << "unknown gate: " << gateName;
162180
}
163181

182+
// Set adjoint attribute on the last-created quantum op if needed
183+
if (isAdj) {
184+
auto &lastOp = entry->back();
185+
if (isa<quake::OperatorInterface>(&lastOp))
186+
lastOp.setAttr("is_adj", builder.getUnitAttr());
187+
}
188+
164189
builder.create<func::ReturnOp>(loc);
165190
return module;
166191
}

0 commit comments

Comments
 (0)