pennylane icon indicating copy to clipboard operation
pennylane copied to clipboard

Added support for arbitrary computation to mid circuit measurements

Open puzzleshark opened this issue 2 years ago • 1 comments

Now MeasurementValues can have arbitrary functions applied to them.

a = measure(0)
b = measure(1)
out = qml.apply_to_measurements(lambda x, y: sin(2 * x + y)(a, b)
qml.cond(out > 0.5, qml.PauliX)()

this is achieved by extending the MeasurementValue class to a tree structure like data structure.

so for example if we have two measurements

a = MeasurmentValue("m0")
b = MeasurementValue("m1")

These will have data structures looking like

"m0"     "m1"
| \      | \
0  1     0  1

and we add them

out = qml.apply_to_measurements(lambda x, y: x + y)(a, b) 

(or using the shorthand defined for some python __dunder__ methods)

out = a + b

then out will have the following tree structure

"m0"
|   \
"m1" "m1"
|  \  |  \
0   1 1    2

puzzleshark avatar Mar 24 '22 15:03 puzzleshark

Codecov Report

Merging #2375 (fa85cad) into master (17f9d3a) will decrease coverage by 0.22%. The diff coverage is 90.10%.

:exclamation: Current head fa85cad differs from pull request most recent head 52630f8. Consider uploading reports for the commit 52630f8 to get more accurate results

@@            Coverage Diff             @@
##           master    #2375      +/-   ##
==========================================
- Coverage   99.61%   99.38%   -0.23%     
==========================================
  Files         255      245      -10     
  Lines       20885    18950    -1935     
==========================================
- Hits        20805    18834    -1971     
- Misses         80      116      +36     
Impacted Files Coverage Δ
pennylane/measurements.py 95.59% <88.60%> (-4.03%) :arrow_down:
pennylane/__init__.py 100.00% <100.00%> (ø)
pennylane/transforms/defer_measurements.py 100.00% <100.00%> (ø)
pennylane/devices/default_qubit_tf.py 90.16% <0.00%> (-5.15%) :arrow_down:
pennylane/about.py 95.45% <0.00%> (-4.55%) :arrow_down:
pennylane/fourier/coefficients.py 96.77% <0.00%> (-3.23%) :arrow_down:
pennylane/utils.py 97.97% <0.00%> (-2.03%) :arrow_down:
pennylane/ops/functions/generator.py 98.57% <0.00%> (-1.43%) :arrow_down:
pennylane/drawer/mpldrawer.py 98.66% <0.00%> (-1.34%) :arrow_down:
pennylane/fourier/visualize.py 98.70% <0.00%> (-1.30%) :arrow_down:
... and 180 more

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 17f9d3a...52630f8. Read the comment docs.

codecov[bot] avatar Mar 24 '22 17:03 codecov[bot]