@@ -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
6987ModuleOp 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