Skip to content

Commit 699a070

Browse files
committed
Add some docstrings
1 parent b89fa0d commit 699a070

File tree

8 files changed

+301
-61
lines changed

8 files changed

+301
-61
lines changed

mlir/include/mlir/Conversion/JeffToQC/JeffToQC.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
include "mlir/Pass/PassBase.td"
1010

1111
def JeffToQC : Pass<"jeff-to-qc"> {
12-
let summary = "TODO";
12+
let summary = "Convert Jeff operations to QC operations";
1313

1414
let description = [{
1515
TODO

mlir/include/mlir/Conversion/QCToJeff/QCToJeff.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
include "mlir/Pass/PassBase.td"
1010

1111
def QCToJeff : Pass<"qc-to-jeff"> {
12-
let summary = "TODO";
12+
let summary = "Convert QC operations to Jeff operations";
1313

1414
let description = [{
1515
TODO

mlir/include/mlir/Dialect/QC/IR/QCOps.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ def CtrlOp : QCOp<"ctrl",
943943
```mlir
944944
qc.ctrl(%q0) {
945945
qc.x %q1 : !qc.qubit
946-
}
946+
} : !qc.qubit
947947
```
948948
}];
949949

mlir/lib/Conversion/JeffToQC/JeffToQC.cpp

Lines changed: 150 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ using namespace qc;
3030
#define GEN_PASS_DEF_JEFFTOQC
3131
#include "mlir/Conversion/JeffToQC/JeffToQC.h.inc"
3232

33+
/**
34+
* @brief Converts a one-target, zero-parameter Jeff operation to QC
35+
*
36+
* @tparam QCOpType The operation type of the QC operation
37+
* @tparam JeffOpType The operation type of the Jeff operation
38+
* @tparam JeffOpAdaptorType The OpAdaptor type of the Jeff operation
39+
* @param op The Jeff operation instance to convert
40+
* @param adaptor The OpAdaptor of the Jeff operation
41+
* @param rewriter The pattern rewriter
42+
* @return LogicalResult Success or failure of the conversion
43+
*/
3344
template <typename QCOpType, typename JeffOpType, typename JeffOpAdaptorType>
3445
static LogicalResult
3546
convertOneTargetZeroParameter(JeffOpType& op, JeffOpAdaptorType& adaptor,
@@ -59,6 +70,17 @@ convertOneTargetZeroParameter(JeffOpType& op, JeffOpAdaptorType& adaptor,
5970
return success();
6071
}
6172

73+
/**
74+
* @brief Converts a one-target, one-parameter Jeff operation to QC
75+
*
76+
* @tparam QCOpType The operation type of the QC operation
77+
* @tparam JeffOpType The operation type of the Jeff operation
78+
* @tparam JeffOpAdaptorType The OpAdaptor type of the Jeff operation
79+
* @param op The Jeff operation instance to convert
80+
* @param adaptor The OpAdaptor of the Jeff operation
81+
* @param rewriter The pattern rewriter
82+
* @return LogicalResult Success or failure of the conversion
83+
*/
6284
template <typename QCOpType, typename JeffOpType, typename JeffOpAdaptorType>
6385
static LogicalResult
6486
convertOneTargetOneParameter(JeffOpType& op, JeffOpAdaptorType& adaptor,
@@ -88,37 +110,18 @@ convertOneTargetOneParameter(JeffOpType& op, JeffOpAdaptorType& adaptor,
88110
return success();
89111
}
90112

91-
template <typename QCOpType, typename JeffOpType, typename JeffOpAdaptorType>
92-
static LogicalResult
93-
convertTwoTargetZeroParameter(JeffOpType& op, JeffOpAdaptorType& adaptor,
94-
ConversionPatternRewriter& rewriter) {
95-
if (op.getPower() != 1) {
96-
return rewriter.notifyMatchFailure(
97-
op, "Operations with power != 1 are not yet supported");
98-
}
99-
100-
auto target1 = adaptor.getInQubitOne();
101-
auto target2 = adaptor.getInQubitTwo();
102-
103-
if (op.getNumCtrls() != 0) {
104-
auto controls = adaptor.getInCtrlQubits();
105-
rewriter.create<qc::CtrlOp>(op.getLoc(), controls, [&] {
106-
rewriter.create<QCOpType>(op.getLoc(), target1, target2);
107-
});
108-
SmallVector<Value> operands;
109-
operands.reserve(2 + controls.size());
110-
operands.push_back(target1);
111-
operands.push_back(target2);
112-
operands.append(controls.begin(), controls.end());
113-
rewriter.replaceOp(op, operands);
114-
} else {
115-
rewriter.create<QCOpType>(op.getLoc(), target1, target2);
116-
rewriter.replaceOp(op, {target1, target2});
117-
}
118-
119-
return success();
120-
}
121-
113+
/**
114+
* @brief Converts jeff.qubit_alloc to qc.alloc
115+
*
116+
* @par Example:
117+
* ```mlir
118+
* %q = jeff.qubit_alloc : !jeff.qubit
119+
* ```
120+
* is converted to
121+
* ```mlir
122+
* %q = qc.alloc : !qc.qubit
123+
* ```
124+
*/
122125
struct ConvertJeffQubitAllocOpToQC final
123126
: OpConversionPattern<jeff::QubitAllocOp> {
124127
using OpConversionPattern::OpConversionPattern;
@@ -131,6 +134,18 @@ struct ConvertJeffQubitAllocOpToQC final
131134
}
132135
};
133136

137+
/**
138+
* @brief Converts jeff.qubit_free to qc.dealloc
139+
*
140+
* @par Example:
141+
* ```mlir
142+
* jeff.qubit_free %q : !jeff.qubit
143+
* ```
144+
* is converted to
145+
* ```mlir
146+
* qc.dealloc %q : !qc.qubit
147+
* ```
148+
*/
134149
struct ConvertJeffQubitFreeOpToQC final
135150
: OpConversionPattern<jeff::QubitFreeOp> {
136151
using OpConversionPattern::OpConversionPattern;
@@ -144,6 +159,18 @@ struct ConvertJeffQubitFreeOpToQC final
144159
}
145160
};
146161

162+
/**
163+
* @brief Converts jeff.qubit_measure_nd to qc.measure
164+
*
165+
* @par Example:
166+
* ```mlir
167+
* %result, %q_out = jeff.qubit_measure_nd %q_in : i1, !jeff.qubit
168+
* ```
169+
* is converted to
170+
* ```mlir
171+
* %result = qc.measure %q : i1
172+
* ```
173+
*/
147174
struct ConvertJeffQubitMeasureNDOpToQC final
148175
: OpConversionPattern<jeff::QubitMeasureNDOp> {
149176
using OpConversionPattern::OpConversionPattern;
@@ -158,6 +185,18 @@ struct ConvertJeffQubitMeasureNDOpToQC final
158185
}
159186
};
160187

188+
/**
189+
* @brief Converts jeff.reset to qc.qubit_reset
190+
*
191+
* @par Example:
192+
* ```mlir
193+
* %q_out = jeff.qubit_reset %q_in : !jeff.qubit
194+
* ```
195+
* is converted to
196+
* ```mlir
197+
* %result = qc.reset %q : !qc.qubit
198+
* ```
199+
*/
161200
struct ConvertJeffQubitResetOpToQC final
162201
: OpConversionPattern<jeff::QubitResetOp> {
163202
using OpConversionPattern::OpConversionPattern;
@@ -172,6 +211,18 @@ struct ConvertJeffQubitResetOpToQC final
172211
}
173212
};
174213

214+
/**
215+
* @brief Converts jeff.gphase to qc.gphase
216+
*
217+
* @par Example:
218+
* ```mlir
219+
* jeff.gphase(%theta) {is_adjoint = false, num_ctrls = 0 : i8, power = 1 : i8}
220+
* ```
221+
* is converted to
222+
* ```mlir
223+
* qc.gphase(%theta)
224+
* ```
225+
*/
175226
struct ConvertJeffGPhaseOpToQC final : OpConversionPattern<jeff::GPhaseOp> {
176227
using OpConversionPattern::OpConversionPattern;
177228

@@ -250,8 +301,19 @@ DEFINE_ONE_TARGET_ONE_PARAMETER(R1Op, POp)
250301

251302
#undef DEFINE_ONE_TARGET_ONE_PARAMETER
252303

253-
// OneTargetOneParameter
254-
304+
/**
305+
* @brief Converts jeff.u to qc.u
306+
*
307+
* @par Example:
308+
* ```mlir
309+
* %q_out = jeff.u(%theta, %phi, %lambda) {is_adjoint = false, num_ctrls = 0 :
310+
* i8, power = 1 : i8} %q_in : !jeff.qubit
311+
* ```
312+
* is converted to
313+
* ```mlir
314+
* qc.u(%theta, %phi, %lambda) %q : !qc.qubit
315+
* ```
316+
*/
255317
struct ConvertJeffUOpToQC final : OpConversionPattern<jeff::UOp> {
256318
using OpConversionPattern::OpConversionPattern;
257319

@@ -286,25 +348,59 @@ struct ConvertJeffUOpToQC final : OpConversionPattern<jeff::UOp> {
286348
}
287349
};
288350

289-
// TwoTargetZeroParameter
351+
/**
352+
* @brief Converts jeff.swap to qc.swap
353+
*
354+
* @par Example:
355+
* ```mlir
356+
* %q0_out, %q1_out = jeff.swap {is_adjoint = false, num_ctrls = 0 : i8, power =
357+
* 1 : i8} %q0_in %q1_in : !jeff.qubit !jeff.qubit
358+
* ```
359+
* is converted to
360+
* ```mlir
361+
* qc.swap %q0, %q1 : !qc.qubit, !qc.qubit
362+
* ```
363+
*/
364+
struct ConvertJeffSwapOpToQC final : OpConversionPattern<jeff::SwapOp> {
365+
using OpConversionPattern::OpConversionPattern;
290366

291-
#define DEFINE_TWO_TARGET_ZERO_PARAMETER(OP_CLASS_JEFF, OP_CLASS_QC) \
292-
struct ConvertJeff##OP_CLASS_JEFF##ToQC final \
293-
: OpConversionPattern<jeff::OP_CLASS_JEFF> { \
294-
using OpConversionPattern::OpConversionPattern; \
295-
\
296-
LogicalResult \
297-
matchAndRewrite(jeff::OP_CLASS_JEFF op, OpAdaptor adaptor, \
298-
ConversionPatternRewriter& rewriter) const override { \
299-
return convertTwoTargetZeroParameter<qc::OP_CLASS_QC>(op, adaptor, \
300-
rewriter); \
301-
} \
302-
};
367+
LogicalResult
368+
matchAndRewrite(jeff::SwapOp op, OpAdaptor adaptor,
369+
ConversionPatternRewriter& rewriter) const override {
370+
if (op.getPower() != 1) {
371+
return rewriter.notifyMatchFailure(
372+
op, "Operations with power != 1 are not yet supported");
373+
}
303374

304-
DEFINE_TWO_TARGET_ZERO_PARAMETER(SwapOp, SWAPOp)
375+
auto target1 = adaptor.getInQubitOne();
376+
auto target2 = adaptor.getInQubitTwo();
305377

306-
#undef DEFINE_TWO_TARGET_ZERO_PARAMETER
378+
if (op.getNumCtrls() != 0) {
379+
auto controls = adaptor.getInCtrlQubits();
380+
rewriter.create<qc::CtrlOp>(op.getLoc(), controls, [&] {
381+
rewriter.create<qc::SWAPOp>(op.getLoc(), target1, target2);
382+
});
383+
SmallVector<Value> operands;
384+
operands.reserve(2 + controls.size());
385+
operands.push_back(target1);
386+
operands.push_back(target2);
387+
operands.append(controls.begin(), controls.end());
388+
rewriter.replaceOp(op, operands);
389+
} else {
390+
rewriter.create<qc::SWAPOp>(op.getLoc(), target1, target2);
391+
rewriter.replaceOp(op, {target1, target2});
392+
}
393+
394+
return success();
395+
}
396+
};
307397

398+
/**
399+
* @brief Type converter for Jeff-to-QC conversion
400+
*
401+
* @details
402+
* Converts `!jeff.qubit` to `!qc.qubit`.
403+
*/
308404
class JeffToQCTypeConverter final : public TypeConverter {
309405
public:
310406
explicit JeffToQCTypeConverter(MLIRContext* ctx) {
@@ -317,6 +413,12 @@ class JeffToQCTypeConverter final : public TypeConverter {
317413
}
318414
};
319415

416+
/**
417+
* @brief Pass for converting Jeff operations to QC operations
418+
*
419+
* @details
420+
* TODO
421+
*/
320422
struct JeffToQC final : impl::JeffToQCBase<JeffToQC> {
321423
using JeffToQCBase::JeffToQCBase;
322424

mlir/lib/Conversion/QCOToQC/QCOToQC.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ struct ConvertQCOBarrierOp final : OpConversionPattern<qco::BarrierOp> {
761761
* ```mlir
762762
* qc.ctrl(%q0) {
763763
* qc.x %q1 : !qc.qubit
764-
* }
764+
* } : !qc.qubit
765765
* ```
766766
*/
767767
struct ConvertQCOCtrlOp final : OpConversionPattern<qco::CtrlOp> {

0 commit comments

Comments
 (0)