catalyst
catalyst copied to clipboard
`sortTopologically` incorrectly moves nullary operations (Breaks Order)
Currently, both commute_ppr and merge_ppr_ppm use the sortTopologically function to sort a range of operations in said block in topological order.
However, this approach will lead to a problem where one of nullary operation (e.g. quantum.device_release) is reorder incorrectly (should not move).
Example: test.mlir
func.func @test_commute_1(%q1 : !quantum.bit){
%100 = arith.constant 100 : i64
quantum.device shots(%100) ["librtd_null_qubit.dylib", "NullQubit", "{'track_resources': False}"]
%0 = qec.ppr ["Z"](4) %q1 : !quantum.bit
%1 = qec.ppr ["Z"](8) %0 : !quantum.bit
%2 = qec.ppr ["Z"](4) %1 : !quantum.bit
quantum.device_release
func.return
}
After perform quantum-opt --commute-ppr test.mlir, we got:
func.func @test_commute_1(%arg0: !quantum.bit) {
%0 = qec.ppr ["Z"](8) %arg0 : !quantum.bit
quantum.device_release
%1 = qec.ppr ["Z"](8) %0 : !quantum.bit
%2 = qec.ppr ["Z"](8) %1 : !quantum.bit
return
}
quantum.device_release should not be moved to the top.
Proposed solution:
- set the list of operation that needed to order in
isOperandReadyinsortTopologically.
This is not only solve the problem, but also reduce the sorting time as well.
https://github.com/llvm/llvm-project/blob/3d4156700ef2ea678c3dabf796bc8622ac0d7ecb/mlir/lib/Analysis/TopologicalSortUtils.cpp#L102 this convenience version only takes in a block and will sort everything in the block
https://github.com/llvm/llvm-project/blob/3d4156700ef2ea678c3dabf796bc8622ac0d7ecb/mlir/lib/Analysis/TopologicalSortUtils.cpp#L56 this underlying version seems to be able to explicitly take in a list of ops to sort
You are right! @paul0403 That's exactly I proposed the solution for that problem.