pennylane icon indicating copy to clipboard operation
pennylane copied to clipboard

`defer_measurements` improvements

Open mudit2812 opened this issue 2 years ago • 3 comments

Context: defer_measurements had some restrictions added to it due to the changes needed for the new features that have been added. This includes not supporting custom wire labels, and some forgotten changes.

Description of the Change:

  • Added custom qnode transform so that qnode transformations also provide device wires as an argument.
  • Added support for custom wire labels. When the object being transformed is a qnode with a device with wires=None or a tape, new wires are added to the tape with labels mv{i}, where {i} is an integer. When a qnode is transformed with a device that does not have None wires, users have to specify extra wires for the mid-circuit measurements, and those will be used for storing mid-circuit measurements.
  • The transform now updates the wires of qml.probs, qml.sample and qml.counts to the original tape wires if no observable or wires are specified. This ensures that any wires that are added by the transform (or any wires that users add to devices for storing the mid-circuit measurements) do not get included in the measurement results.

TODO:

  • [x] Split transform into multiple functions.

Benefits: Custom wire labels are supported and the results for terminal measurements are more correct.

Possible Drawbacks:

  • Users can't use mv{i} as wire labels. Though this is not really a drawback since users could not use any custom wire labels before.
  • For the changes to measurements, users could get confused when the extra wires they add to the device are not included in the results.
  • Moreover, if the extra wires they specify are not the last wires of the device, there will be wires that store mid-circuit measurement results interspersed within the circuit, which can be even more confusing. I've added documentation to make this clear.

Related GitHub Issues:

mudit2812 avatar Sep 21 '23 21:09 mudit2812

@mudit2812 do we need this for the release?

trbromley avatar Oct 18 '23 14:10 trbromley

@mudit2812 do we need this for the release?

No, but it would be nice to have.

mudit2812 avatar Oct 18 '23 19:10 mudit2812

@mudit2812 do we need this for the release?

No, but it would be nice to have.

Looks worthwhile if we can do it. Let us know if you need help from the rest of the team

trbromley avatar Oct 19 '23 13:10 trbromley

Thanks @mudit2812! Closing this for now, but let's make sure to record these improvements in Shortcut so that we can draw from the backlog in future.

trbromley avatar May 24 '24 13:05 trbromley