Skip to content

Commit a2614d7

Browse files
authored
Fix regression in nightly tests for IONQ (#2656)
* Fixes bug in nightly tests and base-profile targets. * Add regression test. * Add another round of casting to see if that helps. * Use __adj instead. * Fix issues. * Simplify. --------- Signed-off-by: Eric Schweitz <[email protected]>
1 parent 28c86a4 commit a2614d7

File tree

11 files changed

+296
-35
lines changed

11 files changed

+296
-35
lines changed

lib/Optimizer/Builder/Intrinsics.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,8 @@ static constexpr IntrinsicCode intrinsicTable[] = {
451451
func.func private @__quantum__qis__t__body(!qir_qubit)
452452
func.func private @__quantum__qis__sdg__body(!qir_qubit)
453453
func.func private @__quantum__qis__tdg__body(!qir_qubit)
454+
func.func private @__quantum__qis__s__adj(!qir_qubit)
455+
func.func private @__quantum__qis__t__adj(!qir_qubit)
454456
func.func private @__quantum__qis__u3__body(f64, f64, f64, !qir_qubit)
455457
func.func private @__quantum__qis__reset__body(!qir_qubit)
456458
func.func private @__quantum__qis__mz__body(!qir_qubit, !qir_result) attributes {passthrough = ["irreversible"]}

lib/Optimizer/CodeGen/ConvertToQIRAPI.cpp

+73-33
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,21 @@ static std::string getGateFunctionPrefix(Operation *op) {
5656

5757
constexpr std::array<std::string_view, 2> filterAdjointNames = {"s", "t"};
5858

59-
template <typename OP>
59+
template <typename M, typename OP>
6060
std::pair<std::string, bool> generateGateFunctionName(OP op) {
6161
auto prefix = getGateFunctionPrefix(op.getOperation());
6262
auto gateName = getGateName(op.getOperation());
6363
if (op.isAdj()) {
6464
if (std::find(filterAdjointNames.begin(), filterAdjointNames.end(),
65-
gateName) != filterAdjointNames.end())
66-
prefix += "dg";
65+
gateName) != filterAdjointNames.end()) {
66+
if constexpr (M::dgSuffix) {
67+
prefix += "dg";
68+
} else {
69+
if (!op.getControls().empty())
70+
return {prefix + "dg__ctl", false};
71+
return {prefix + "__adj", false};
72+
}
73+
}
6774
}
6875
if (!op.getControls().empty())
6976
return {prefix + "__ctl", false};
@@ -429,8 +436,14 @@ struct DiscriminateOpToCallRewrite
429436
cudaq::opt::QIRReadResultBody,
430437
adaptor.getOperands());
431438
} else {
432-
rewriter.replaceOpWithNewOp<cudaq::cc::PoisonOp>(disc,
433-
rewriter.getI1Type());
439+
auto loc = disc.getLoc();
440+
// NB: the double cast here is to avoid folding the pointer casts.
441+
auto i64Ty = rewriter.getI64Type();
442+
auto unu =
443+
rewriter.create<cudaq::cc::CastOp>(loc, i64Ty, adaptor.getOperands());
444+
auto ptrI1Ty = cudaq::cc::PointerType::get(rewriter.getI1Type());
445+
auto du = rewriter.create<cudaq::cc::CastOp>(loc, ptrI1Ty, unu);
446+
rewriter.replaceOpWithNewOp<cudaq::cc::LoadOp>(disc, du);
434447
}
435448
return success();
436449
}
@@ -1465,7 +1478,7 @@ struct FullQIR {
14651478

14661479
template <typename QuakeOp>
14671480
static std::string quakeToFuncName(QuakeOp op) {
1468-
auto [prefix, _] = generateGateFunctionName(op);
1481+
auto [prefix, _] = generateGateFunctionName<Self>(op);
14691482
return prefix;
14701483
}
14711484

@@ -1523,19 +1536,15 @@ struct FullQIR {
15231536
static Type getLLVMPointerType(MLIRContext *ctx) {
15241537
return GetLLVMPointerType<opaquePtr>(ctx);
15251538
}
1539+
1540+
static constexpr bool dgSuffix = true;
15261541
};
15271542

15281543
/// The base modifier class for the "profile QIR" APIs.
15291544
template <bool opaquePtr>
15301545
struct AnyProfileQIR {
15311546
using Self = AnyProfileQIR;
15321547

1533-
template <typename QuakeOp>
1534-
static std::string quakeToFuncName(QuakeOp op) {
1535-
auto [prefix, isBarePrefix] = generateGateFunctionName(op);
1536-
return isBarePrefix ? prefix + "__body" : prefix;
1537-
}
1538-
15391548
static void populateRewritePatterns(RewritePatternSet &patterns,
15401549
TypeConverter &typeConverter) {
15411550
auto *ctx = patterns.getContext();
@@ -1546,23 +1555,8 @@ struct AnyProfileQIR {
15461555
SubveqOpRewrite<Self>,
15471556

15481557
/* Irregular quantum operators. */
1549-
CustomUnitaryOpPattern<Self>, ExpPauliOpPattern, ResetOpPattern<Self>,
1550-
1551-
/* Regular quantum operators. */
1552-
QuantumGatePattern<Self, quake::HOp>,
1553-
QuantumGatePattern<Self, quake::PhasedRxOp>,
1554-
QuantumGatePattern<Self, quake::R1Op>,
1555-
QuantumGatePattern<Self, quake::RxOp>,
1556-
QuantumGatePattern<Self, quake::RyOp>,
1557-
QuantumGatePattern<Self, quake::RzOp>,
1558-
QuantumGatePattern<Self, quake::SOp>,
1559-
QuantumGatePattern<Self, quake::SwapOp>,
1560-
QuantumGatePattern<Self, quake::TOp>,
1561-
QuantumGatePattern<Self, quake::U2Op>,
1562-
QuantumGatePattern<Self, quake::U3Op>,
1563-
QuantumGatePattern<Self, quake::XOp>,
1564-
QuantumGatePattern<Self, quake::YOp>,
1565-
QuantumGatePattern<Self, quake::ZOp>>(typeConverter, ctx);
1558+
CustomUnitaryOpPattern<Self>, ExpPauliOpPattern, ResetOpPattern<Self>>(
1559+
typeConverter, ctx);
15661560
commonQuakeHandlingPatterns(patterns, typeConverter, ctx);
15671561
commonClassicalHandlingPatterns(patterns, typeConverter, ctx);
15681562
}
@@ -1597,15 +1591,38 @@ struct BaseProfileQIR : public AnyProfileQIR<opaquePtr> {
15971591
using Self = BaseProfileQIR;
15981592
using Base = AnyProfileQIR<opaquePtr>;
15991593

1594+
template <typename QuakeOp>
1595+
static std::string quakeToFuncName(QuakeOp op) {
1596+
auto [prefix, isBarePrefix] = generateGateFunctionName<Self>(op);
1597+
return isBarePrefix ? prefix + "__body" : prefix;
1598+
}
1599+
16001600
static void populateRewritePatterns(RewritePatternSet &patterns,
16011601
TypeConverter &typeConverter) {
16021602
Base::populateRewritePatterns(patterns, typeConverter);
16031603
patterns
1604-
.insert<DiscriminateOpToCallRewrite<Self>, MeasurementOpPattern<Self>>(
1605-
typeConverter, patterns.getContext());
1604+
.insert<DiscriminateOpToCallRewrite<Self>, MeasurementOpPattern<Self>,
1605+
1606+
/* Regular quantum operators. */
1607+
QuantumGatePattern<Self, quake::HOp>,
1608+
QuantumGatePattern<Self, quake::PhasedRxOp>,
1609+
QuantumGatePattern<Self, quake::R1Op>,
1610+
QuantumGatePattern<Self, quake::RxOp>,
1611+
QuantumGatePattern<Self, quake::RyOp>,
1612+
QuantumGatePattern<Self, quake::RzOp>,
1613+
QuantumGatePattern<Self, quake::SOp>,
1614+
QuantumGatePattern<Self, quake::SwapOp>,
1615+
QuantumGatePattern<Self, quake::TOp>,
1616+
QuantumGatePattern<Self, quake::U2Op>,
1617+
QuantumGatePattern<Self, quake::U3Op>,
1618+
QuantumGatePattern<Self, quake::XOp>,
1619+
QuantumGatePattern<Self, quake::YOp>,
1620+
QuantumGatePattern<Self, quake::ZOp>>(typeConverter,
1621+
patterns.getContext());
16061622
}
16071623

16081624
static constexpr bool discriminateToClassical = false;
1625+
static constexpr bool dgSuffix = false;
16091626
};
16101627

16111628
/// The QIR adaptive profile modifier class.
@@ -1614,15 +1631,38 @@ struct AdaptiveProfileQIR : public AnyProfileQIR<opaquePtr> {
16141631
using Self = AdaptiveProfileQIR;
16151632
using Base = AnyProfileQIR<opaquePtr>;
16161633

1634+
template <typename QuakeOp>
1635+
static std::string quakeToFuncName(QuakeOp op) {
1636+
auto [prefix, isBarePrefix] = generateGateFunctionName<Self>(op);
1637+
return isBarePrefix ? prefix + "__body" : prefix;
1638+
}
1639+
16171640
static void populateRewritePatterns(RewritePatternSet &patterns,
16181641
TypeConverter &typeConverter) {
16191642
Base::populateRewritePatterns(patterns, typeConverter);
16201643
patterns
1621-
.insert<DiscriminateOpToCallRewrite<Self>, MeasurementOpPattern<Self>>(
1622-
typeConverter, patterns.getContext());
1644+
.insert<DiscriminateOpToCallRewrite<Self>, MeasurementOpPattern<Self>,
1645+
1646+
/* Regular quantum operators. */
1647+
QuantumGatePattern<Self, quake::HOp>,
1648+
QuantumGatePattern<Self, quake::PhasedRxOp>,
1649+
QuantumGatePattern<Self, quake::R1Op>,
1650+
QuantumGatePattern<Self, quake::RxOp>,
1651+
QuantumGatePattern<Self, quake::RyOp>,
1652+
QuantumGatePattern<Self, quake::RzOp>,
1653+
QuantumGatePattern<Self, quake::SOp>,
1654+
QuantumGatePattern<Self, quake::SwapOp>,
1655+
QuantumGatePattern<Self, quake::TOp>,
1656+
QuantumGatePattern<Self, quake::U2Op>,
1657+
QuantumGatePattern<Self, quake::U3Op>,
1658+
QuantumGatePattern<Self, quake::XOp>,
1659+
QuantumGatePattern<Self, quake::YOp>,
1660+
QuantumGatePattern<Self, quake::ZOp>>(typeConverter,
1661+
patterns.getContext());
16231662
}
16241663

16251664
static constexpr bool discriminateToClassical = true;
1665+
static constexpr bool dgSuffix = true;
16261666
};
16271667

16281668
//===----------------------------------------------------------------------===//

test/AST-Quake/base_profile-1.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,12 @@ struct comprehensive {
114114
// BASE: tail call void @__quantum__qis__t__body(%Qubit* nonnull inttoptr (i64 4 to %Qubit*))
115115
// BASE: tail call void @__quantum__qis__t__body(%Qubit* nonnull inttoptr (i64 5 to %Qubit*))
116116
// BASE: tail call void @__quantum__qis__t__body(%Qubit* nonnull inttoptr (i64 6 to %Qubit*))
117-
// BASE: tail call void @__quantum__qis__tdg__body(%Qubit* nonnull inttoptr (i64 5 to %Qubit*))
117+
// BASE: tail call void @__quantum__qis__t__adj(%Qubit* nonnull inttoptr (i64 5 to %Qubit*))
118118
// BASE: tail call void @__quantum__qis__s__body(%Qubit* null)
119119
// BASE: tail call void @__quantum__qis__s__body(%Qubit* nonnull inttoptr (i64 4 to %Qubit*))
120120
// BASE: tail call void @__quantum__qis__s__body(%Qubit* nonnull inttoptr (i64 5 to %Qubit*))
121121
// BASE: tail call void @__quantum__qis__s__body(%Qubit* nonnull inttoptr (i64 6 to %Qubit*))
122-
// BASE: tail call void @__quantum__qis__sdg__body(%Qubit* null)
122+
// BASE: tail call void @__quantum__qis__s__adj(%Qubit* null)
123123
// BASE: tail call void @__quantum__qis__rx__body(double 5.612300e+00, %Qubit* null)
124124
// BASE: tail call void @__quantum__qis__rx__body(double 5.612300e+00, %Qubit* nonnull inttoptr (i64 5 to %Qubit*))
125125
// BASE: tail call void @__quantum__qis__rx__body(double -5.612300e+00, %Qubit* null)

0 commit comments

Comments
 (0)