Refactor post-processing in measurements_from_samples#2605
Conversation
multiphaseCFD
left a comment
There was a problem hiding this comment.
LGTM! Thanks @lillian542 .
frontend/catalyst/python_interface/transforms/quantum/measurements_from_samples.py
Outdated
Show resolved
Hide resolved
frontend/catalyst/python_interface/transforms/quantum/measurements_from_samples.py
Show resolved
Hide resolved
mudit2812
left a comment
There was a problem hiding this comment.
This looks great Lillian. A few final comments. I also wanted to ask, is there any plan to support dynamic shots? Seems like a pretty important feature if we want to link this pass to the pennylane transform
frontend/catalyst/python_interface/transforms/quantum/measurements_from_samples.py
Outdated
Show resolved
Hide resolved
frontend/catalyst/python_interface/transforms/quantum/measurements_from_samples.py
Outdated
Show resolved
Hide resolved
frontend/catalyst/python_interface/transforms/quantum/measurements_from_samples.py
Outdated
Show resolved
Hide resolved
frontend/catalyst/python_interface/transforms/quantum/measurements_from_samples.py
Outdated
Show resolved
Hide resolved
frontend/catalyst/python_interface/transforms/quantum/measurements_from_samples.py
Outdated
Show resolved
Hide resolved
frontend/catalyst/python_interface/transforms/quantum/measurements_from_samples.py
Outdated
Show resolved
Hide resolved
…ents_from_samples.py Co-authored-by: Mudit Pandey <[email protected]>
When we were scoping it early, my impression was we are alright with figuring out dynamic shots later, but this would definitely be a good thing to revisit before we link the tape transform. |
frontend/catalyst/python_interface/transforms/quantum/measurements_from_samples.py
Show resolved
Hide resolved
mudit2812
left a comment
There was a problem hiding this comment.
Looks great. My only blocking comment is about removing the change to get_constant_from_ssa and updating any affected tests accordingly.
Context:
One of several PRs for [sc-107541]. This one addresses refactoring to follow the new guidelines in the qnode transform guide
regarding transforms that add post-processing
Description of the Change:
We refactor to follow the new guidelines in the new qnode transform guide. The main difference is we create a separate function for each quantum.node that calls the quantum node and applies post-processing on the results, i.e. we go from including post-processing inside the qnode:
to calling the qnode (which returns samples), and performing post-processing on the results:
Key changes are:
AddPostProcessingPattern, that inserts a classical function that calls the quantum_node for each in the module. This should be generic for re-use with other 1:1 passes that add post-processing (but won't work for passes that create multiple quantum.nodes meant to be recombined in post-processing)match_and_rewritefunctions to look forFuncOps instead of measurement processes, and apply the pattern to each quantum.nodeMeasurementsFromSamplespattern that gets the module thequantum.nodeis nested in, walks through it, and finds the CallOp for thequantum.node, so that it is available as in insertion point for post-processing.update_returnsfunction that updates the operations and types returned in the outer function and the qnode to reflect the conversion to samples an the addition of a post-processing stepquantum.nodes - this is technically true currently, because weighted shot distribution forsplit-non-commutingisn't added yet, but its not a safe assumption to make.Benefits:
More future-facing technical implementation (follows new convention, won't break when shot distribution is added). Will be easier to maintain and improves readability of the IR (especially when there are multiple measurements output). Maintains cleaner separation between hybrid/quantum execution and purely classical post-processing.
Possible Drawbacks:
Not sure if/how these changes will impact performance. We previously "matched" directly with the MPs in the pattern rewriter, and now we match with all FuncOps, check if they are
quantum.nodes, and then walk through that to find the MPs. I'm not sure whether this will impact performance meaningfully.Note that this will need to be moved from xDSL to MLIR eventually, so in terms of efficiency, "good enough for now" is fine.
Related GitHub Issues:
None