Skip to content

Commit a0f6d69

Browse files
committed
enable --buffer-deallocation-pipeline;
Add (proper) memory effects for quantum ops. Add TODO for mqbc measurement op's memory effects.
1 parent 51050d9 commit a0f6d69

File tree

4 files changed

+27
-14
lines changed

4 files changed

+27
-14
lines changed

frontend/catalyst/pipelines.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ def get_bufferization_stage(options: CompileOptions) -> List[str]:
240240
"func.func(buffer-hoisting)",
241241
"func.func(buffer-loop-hoisting)",
242242
"func.func(promote-buffers-to-stack)",
243-
# "buffer-deallocation-pipeline",
243+
"buffer-deallocation-pipeline",
244244
"convert-arraylist-to-memref",
245245
"convert-bufferization-to-memref",
246246
"canonicalize", # Must be after convert-bufferization-to-memref

mlir/include/MBQC/IR/MBQCOps.td

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ include "mlir/IR/AttrTypeBase.td"
1919
include "mlir/IR/EnumAttr.td"
2020

2121
include "Quantum/IR/QuantumDialect.td"
22+
//include "Quantum/IR/QuantumOps.td"
2223

2324
include "MBQC/IR/MBQCDialect.td"
2425

@@ -54,7 +55,12 @@ def MeasurementPlaneAttr : EnumAttr<MBQC_Dialect, MeasurementPlane, "measurement
5455
// MBQC dialect operations.
5556
//===----------------------------------------------------------------------===//
5657

57-
def MeasureInBasisOp : MBQC_Op<"measure_in_basis"> {
58+
def MeasureInBasisOp : MBQC_Op<"measure_in_basis"
59+
// Measurement both read and write since it collapses states
60+
// In fact, MBQC's entire point is to alter the state via measurement.
61+
// TODO: restructure mbqc dialect to see the QuantumMemory Resource in Quantum dialect.
62+
// ,[MemoryEffects<[MemRead<QuantumMemory>, MemWrite<QuantumMemory>]>]
63+
> {
5864
let summary = "A parametric single-qubit projective measurement in an arbitrary basis.";
5965
let description = [{
6066
A parametric single-qubit projective measurement is equivalent to the `quantum.measure`

mlir/include/Quantum/IR/QuantumOps.td

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,23 @@ def NamedObservableAttr : EnumAttr<QuantumDialect, NamedObservable, "named_obser
6363

6464
class Quantum_Op<string mnemonic, list<Trait> traits = []> : Op<QuantumDialect, mnemonic, traits>;
6565

66-
def InitializeOp : Quantum_Op<"init"> {
66+
def InitializeOp : Quantum_Op<"init", [MemoryEffects<[MemAlloc<QuantumMemory>]>]> {
6767
let summary = "Initialize the quantum runtime.";
6868

6969
let assemblyFormat = [{
7070
attr-dict
7171
}];
7272
}
7373

74-
def FinalizeOp : Quantum_Op<"finalize"> {
74+
def FinalizeOp : Quantum_Op<"finalize", [MemoryEffects<[MemFree<QuantumMemory>]>]> {
7575
let summary = "Teardown the quantum runtime.";
7676

7777
let assemblyFormat = [{
7878
attr-dict
7979
}];
8080
}
8181

82-
def DeviceInitOp : Quantum_Op<"device"> {
82+
def DeviceInitOp : Quantum_Op<"device", [MemoryEffects<[MemAlloc<QuantumMemory>]>]> {
8383
let summary = "Initialize a quantum device.";
8484

8585
let arguments = (ins
@@ -95,7 +95,7 @@ def DeviceInitOp : Quantum_Op<"device"> {
9595

9696
}
9797

98-
def DeviceReleaseOp : Quantum_Op<"device_release"> {
98+
def DeviceReleaseOp : Quantum_Op<"device_release", [MemoryEffects<[MemFree<QuantumMemory>]>]> {
9999
let summary = "Release the active quantum device.";
100100

101101
let assemblyFormat = [{
@@ -172,7 +172,7 @@ def DeallocQubitOp : Memory_Op<"dealloc_qb"> {
172172
}];
173173
}
174174

175-
def DeallocOp : Memory_Op<"dealloc"> {
175+
def DeallocOp : Memory_Op<"dealloc", [MemoryEffects<[MemFree<QuantumMemory>]>]> {
176176
let summary = "Deallocate a quantum register.";
177177
let description = [{
178178
}];
@@ -856,7 +856,12 @@ def HamiltonianOp : Observable_Op<"hamiltonian"> {
856856
class Measurement_Op<string mnemonic, list<Trait> traits = []> :
857857
Quantum_Op<mnemonic, traits # [MeasurementProcess]>;
858858

859-
def MeasureOp : Quantum_Op<"measure"> {
859+
def MeasureOp : Quantum_Op<"measure",
860+
// Measurement both read and write since it collapses states
861+
// This is even more true for the mbqc variant, since that one's entire point
862+
// is to alter the state via measurement.
863+
[MemoryEffects<[MemRead<QuantumMemory>, MemWrite<QuantumMemory>]>]
864+
> {
860865
let summary = "A single-qubit projective measurement in the computational basis.";
861866
let description = [{
862867
}];
@@ -876,7 +881,7 @@ def MeasureOp : Quantum_Op<"measure"> {
876881
}];
877882
}
878883

879-
def SampleOp : Measurement_Op<"sample", [AttrSizedOperandSegments]> {
884+
def SampleOp : Measurement_Op<"sample", [AttrSizedOperandSegments, MemoryEffects<[MemAlloc]>]> {
880885
let summary = "Sample eigenvalues from the given observable for the current state";
881886
let description = [{
882887
The `quantum.sample` operation represents the measurement process of sampling eigenvalues
@@ -942,7 +947,8 @@ def SampleOp : Measurement_Op<"sample", [AttrSizedOperandSegments]> {
942947
let hasVerifier = 1;
943948
}
944949

945-
def CountsOp : Measurement_Op<"counts", [AttrSizedOperandSegments, SameVariadicResultSize]> {
950+
def CountsOp : Measurement_Op<"counts", [AttrSizedOperandSegments, SameVariadicResultSize,
951+
MemoryEffects<[MemAlloc]>]> {
946952
let summary = "Compute sample counts for the given observable for the current state";
947953
let description = [{
948954
The `quantum.counts` operation represents the measurement process of sampling eigenvalues
@@ -1001,7 +1007,7 @@ def CountsOp : Measurement_Op<"counts", [AttrSizedOperandSegments, SameVariadicR
10011007
let hasVerifier = 1;
10021008
}
10031009

1004-
def ExpvalOp : Measurement_Op<"expval"> {
1010+
def ExpvalOp : Measurement_Op<"expval", [NoMemoryEffect]> {
10051011
let summary = "Compute the expectation value of the given observable for the current state";
10061012
let description = [{
10071013
The `quantum.expval` operation represents the measurement process of computing the
@@ -1038,7 +1044,7 @@ def ExpvalOp : Measurement_Op<"expval"> {
10381044
}];
10391045
}
10401046

1041-
def VarianceOp : Measurement_Op<"var"> {
1047+
def VarianceOp : Measurement_Op<"var", [NoMemoryEffect]> {
10421048
let summary = "Compute the variance of the given observable for the current state";
10431049
let description = [{
10441050
The `quantum.var` operation represents the measurement process of computing the variance of
@@ -1074,7 +1080,7 @@ def VarianceOp : Measurement_Op<"var"> {
10741080
}];
10751081
}
10761082

1077-
def ProbsOp : Measurement_Op<"probs", [AttrSizedOperandSegments]> {
1083+
def ProbsOp : Measurement_Op<"probs", [AttrSizedOperandSegments, MemoryEffects<[MemAlloc]>]> {
10781084
let summary = "Compute computational basis probabilities for the current state";
10791085
let description = [{
10801086
The `quantum.probs` operation represents the measurement process of computing probabilities
@@ -1120,7 +1126,7 @@ def ProbsOp : Measurement_Op<"probs", [AttrSizedOperandSegments]> {
11201126
let hasVerifier = 1;
11211127
}
11221128

1123-
def StateOp : Measurement_Op<"state", [AttrSizedOperandSegments]> {
1129+
def StateOp : Measurement_Op<"state", [AttrSizedOperandSegments, MemoryEffects<[MemAlloc]>]> {
11241130
let summary = "Return the current statevector";
11251131
let description = [{
11261132
The `quantum.state` operation represents the measurement process of returning the current

mlir/lib/Driver/Pipelines.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ void createBufferizationPipeline(OpPassManager &pm)
8686
pm.addNestedPass<mlir::func::FuncOp>(mlir::bufferization::createBufferHoistingPass());
8787
pm.addNestedPass<mlir::func::FuncOp>(mlir::bufferization::createBufferLoopHoistingPass());
8888
pm.addNestedPass<mlir::func::FuncOp>(mlir::bufferization::createPromoteBuffersToStackPass());
89+
// TODO: what's the cpp for buffer-deallocation-pipeline???
8990
// pm.addPass(mlir::bufferization::createOwnershipBasedBufferDeallocationPass());
9091
pm.addPass(catalyst::createArrayListToMemRefPass());
9192
pm.addPass(mlir::createConvertBufferizationToMemRefPass());

0 commit comments

Comments
 (0)