Skip to content

Support broadcasting in measure and measure_with_samples#4238

Merged
eddddddy merged 18 commits intomasterfrom
state_measure_broadcast
Jun 16, 2023
Merged

Support broadcasting in measure and measure_with_samples#4238
eddddddy merged 18 commits intomasterfrom
state_measure_broadcast

Conversation

@eddddddy
Copy link
Copy Markdown
Contributor

@eddddddy eddddddy commented Jun 12, 2023

Context:
Part of the development of the new qubit device in PennyLane.

Description of the Change:
Support broadcasting in the measure function in devices/qubit/measure.py and in the measure_with_samples function in devices/qubit/sampling.py. Add a new keyword argument is_state_batched that tells these function if a batch dim exists. Integration with simulate to come in a future PR.

@github-actions
Copy link
Copy Markdown
Contributor

Hello. You may have forgotten to update the changelog!
Please edit doc/releases/changelog-dev.md with:

  • A one-to-two sentence description of the change. You may include a small working example for new features.
  • A link back to this PR.
  • Your name (or GitHub username) in the contributors section.

@eddddddy
Copy link
Copy Markdown
Contributor Author

Changelog entry to come in the last PR of this series.

@codecov
Copy link
Copy Markdown

codecov bot commented Jun 12, 2023

Codecov Report

Merging #4238 (ae3261b) into master (58bdaea) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff            @@
##           master    #4238    +/-   ##
========================================
  Coverage   99.78%   99.78%            
========================================
  Files         352      352            
  Lines       31754    31882   +128     
========================================
+ Hits        31686    31815   +129     
+ Misses         68       67     -1     
Impacted Files Coverage Δ
pennylane/__init__.py 100.00% <100.00%> (ø)
pennylane/devices/qubit/measure.py 100.00% <100.00%> (ø)
pennylane/devices/qubit/sampling.py 100.00% <100.00%> (ø)
pennylane/fermi/conversion.py 100.00% <100.00%> (ø)
pennylane/fermi/fermionic.py 100.00% <100.00%> (ø)
pennylane/qchem/observable_hf.py 100.00% <100.00%> (+1.06%) ⬆️
pennylane/transforms/qcut/cutstrategy.py 100.00% <100.00%> (ø)
pennylane/transforms/qcut/montecarlo.py 100.00% <100.00%> (ø)
pennylane/transforms/qcut/processing.py 100.00% <100.00%> (ø)
pennylane/transforms/qcut/tapes.py 100.00% <100.00%> (ø)
... and 1 more

@eddddddy eddddddy changed the title Support broadcasting in state measurements for new device Support broadcasting in measure and measure_with_samples Jun 13, 2023
@eddddddy eddddddy requested a review from albi3ro June 13, 2023 14:27
@timmysilv timmysilv added this to the v0.31 milestone Jun 15, 2023
@eddddddy eddddddy requested review from a team and albi3ro June 15, 2023 18:43
Copy link
Copy Markdown
Contributor

@albi3ro albi3ro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

@eddddddy eddddddy merged commit dc3078a into master Jun 16, 2023
@eddddddy eddddddy deleted the state_measure_broadcast branch June 16, 2023 00:15
frederikwilde pushed a commit that referenced this pull request Jul 5, 2023
* Support broadcasting in state measurements

* docs for is_state_batched

* Support broadcasting in sample measurements

* Apply suggestions from code review

Co-authored-by: Christina Lee <christina@xanadu.ai>

* black

* black

* Remove total_copies

* fix

---------

Co-authored-by: Christina Lee <christina@xanadu.ai>
PietropaoloFrisoni added a commit that referenced this pull request Oct 29, 2024
)

**Context:** WIth `default.qubit`, parameter broadcasting does not work
with `shadow_expval` (or `classical_shadow`) as the measurement process.

These latest developments might be relevant for historical reasons:

- In [this PR](#4238),
support for parameter broadcasting was added for `measure` and
`measure_with_samples` (presumably as an effort for the new device API).
Specifically, a new keyword argument `is_state_batched` has been
introduced to these functions to deal with the parameter broadcasting
case.

- In [this PR](#4162), the
classical shadow measurement process was integrated into the new device
API as well. A new logical branch was integrated into
`measure_with_samples` adding a new `_measure_classical_shadow
function`. This function did not receive the `is_state_batched
argument`, so apparently supporting parameter broadcasting for classical
shadows was not part of the requirements (the shortcut story was not
linked so I cannot investigate further).

- In [this PR](#4429), the
`is_state_batched` argument has been added to
`_measure_classical_shadow`, but support for parameter broadcasting was
never implemented (the argument is unused).

**Description of the Change:** We added an internal conditional quantum
transform to `default.qubit` such that the
`qml.transforms.broadcast_expand` is conditionally applied if
`ShadowExpvalMP` or `ClassicalShadowMP` are found in the tape
measurement process.

**Benefits:** Now `default.qubit` supports (non-native) parameter
broadcasting with shadow measurement processes.

**Possible Drawbacks:** Since we are applying the `broadcast_expand`
quantum transform, we are not supporting *native* parameter
broadcasting, This should not be a big deal.

**Related GitHub Issues:** #6301 

**Related ShortCut Stories:** [sc-74387]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants