Skip to content

Commit 0221058

Browse files
committed
✨ Update QubitType handling in QCO and QC dialects to support static allocation, modifying builders and operations accordingly.
1 parent 0b81e9c commit 0221058

8 files changed

Lines changed: 34 additions & 11 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ def AllocOp : QCOp<"alloc", [MemoryEffects<[MemAlloc]>]> {
6565

6666
let builders = [
6767
OpBuilder<(ins), [{
68-
build($_builder, $_state, QubitType::get($_builder.getContext()), nullptr, nullptr, nullptr);
68+
build($_builder, $_state, QubitType::get($_builder.getContext(), /*isStatic=*/false), nullptr, nullptr, nullptr);
6969
}]>,
7070
OpBuilder<(ins "::mlir::StringAttr":$register_name,
7171
"::mlir::IntegerAttr":$register_size,
7272
"::mlir::IntegerAttr":$register_index), [{
73-
build($_builder, $_state, QubitType::get($_builder.getContext()),
73+
build($_builder, $_state, QubitType::get($_builder.getContext(), /*isStatic=*/false),
7474
register_name, register_size, register_index);
7575
}]>
7676
];

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,18 @@ def QubitType : QCType<"Qubit", "qubit"> {
2525
QC dialect. Operations using this type modify qubits in place using
2626
reference semantics, similar to how classical imperative languages handle
2727
mutable references.
28+
29+
`!qc.qubit` (default) denotes a dynamically allocated qubit.
30+
`!qc.qubit<static>` denotes a qubit that has been mapped to a fixed
31+
hardware position.
2832
}];
33+
let parameters = (ins DefaultValuedParameter<"bool", "false">:$isStatic);
34+
let builders = [
35+
TypeBuilder<(ins), [{
36+
return $_get($_ctxt, /*isStatic=*/false);
37+
}]>
38+
];
39+
let hasCustomAssemblyFormat = 1;
2940
}
3041

3142
#endif // MLIR_DIALECT_QC_IR_QCTYPES_TD

mlir/include/mlir/Dialect/QCO/IR/QCOOps.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ def AllocOp : QCOOp<"alloc", [MemoryEffects<[MemAlloc]>]> {
6565

6666
let builders = [
6767
OpBuilder<(ins), [{
68-
build($_builder, $_state, QubitType::get($_builder.getContext()), nullptr, nullptr, nullptr);
68+
build($_builder, $_state, QubitType::get($_builder.getContext(), /*isStatic=*/false), nullptr, nullptr, nullptr);
6969
}]>,
7070
OpBuilder<(ins "::mlir::StringAttr":$register_name,
7171
"::mlir::IntegerAttr":$register_size,
7272
"::mlir::IntegerAttr":$register_index), [{
73-
build($_builder, $_state, QubitType::get($_builder.getContext()),
73+
build($_builder, $_state, QubitType::get($_builder.getContext(), /*isStatic=*/false),
7474
register_name, register_size, register_index);
7575
}]>
7676
];

mlir/include/mlir/Dialect/QCO/IR/QCOTypes.td

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,24 @@ def QubitType : QCOType<"Qubit", "qubit"> {
2626
and produce new output qubits following value semantics and the SSA
2727
paradigm, enabling powerful dataflow analysis and optimization.
2828

29+
`!qco.qubit` (default) denotes a dynamically allocated qubit.
30+
`!qco.qubit<static>` denotes a qubit that has been mapped to a fixed
31+
hardware position.
32+
2933
Example:
3034
```mlir
3135
%q0 = qco.alloc : !qco.qubit
3236
%q1 = qco.h %q0 : !qco.qubit -> !qco.qubit
3337
%q2 = qco.x %q1 : !qco.qubit -> !qco.qubit
3438
```
3539
}];
40+
let parameters = (ins DefaultValuedParameter<"bool", "false">:$isStatic);
41+
let builders = [
42+
TypeBuilder<(ins), [{
43+
return $_get($_ctxt, /*isStatic=*/false);
44+
}]>
45+
];
46+
let hasCustomAssemblyFormat = 1;
3647
}
3748

3849
#endif // MLIR_DIALECT_QCO_IR_QCOTYPES_TD

mlir/lib/Conversion/JeffToQCO/JeffToQCO.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@ class JeffToQCOTypeConverter final : public TypeConverter {
902902
addConversion([](Type type) { return type; });
903903

904904
addConversion([ctx](jeff::QubitType /*type*/) -> Type {
905-
return qco::QubitType::get(ctx);
905+
return qco::QubitType::get(ctx, /*isStatic=*/false);
906906
});
907907
}
908908
};

mlir/lib/Dialect/QCO/IR/Modifiers/CtrlOp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ struct ReduceCtrl final : OpRewritePattern<CtrlOp> {
126126
op->setAttr(opResultSegmentsAttrName, newSegments);
127127

128128
// Add a block argument for the target qubit
129-
auto arg = op.getBody()->addArgument(QubitType::get(rewriter.getContext()),
130-
op.getLoc());
129+
auto arg = op.getBody()->addArgument(
130+
QubitType::get(rewriter.getContext(), /*isStatic=*/false), op.getLoc());
131131

132132
// Replace the current GPhaseOp with a PhaseOp
133133
const OpBuilder::InsertionGuard guard(rewriter);

mlir/lib/Dialect/QCO/IR/QCOOps.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ parseTargetAliasing(OpAsmParser& parser, Region& region,
7171
// Hard-code QubitType since targets in qco.ctrl are always qubits.
7272
// This avoids double-binding type($targets_in) in the assembly format
7373
// while keeping the parser simple and the assembly format clean.
74-
newArg.type = QubitType::get(parser.getBuilder().getContext());
74+
newArg.type =
75+
QubitType::get(parser.getBuilder().getContext(), /*isStatic=*/false);
7576
blockArgs.push_back(newArg);
7677

7778
} while (succeeded(parser.parseOptionalComma()));

mlir/lib/Dialect/QTensor/IR/Operations/AllocOp.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ void AllocOp::build(OpBuilder& builder, OperationState& result, Value size) {
3131
assert(*sizeValue > 0 && "qtensor.alloc size must be positive");
3232
}
3333

34-
auto resultType =
35-
RankedTensorType::get({sizeValue ? *sizeValue : ShapedType::kDynamic},
36-
qco::QubitType::get(builder.getContext()));
34+
auto resultType = RankedTensorType::get(
35+
{sizeValue ? *sizeValue : ShapedType::kDynamic},
36+
qco::QubitType::get(builder.getContext(), /*isStatic=*/false));
3737
build(builder, result, resultType, size);
3838
}
3939

0 commit comments

Comments
 (0)