Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalize qml.probs() to compute probabilities with respect to an eigenbasis #1692

Merged
merged 22 commits into from
Oct 15, 2021

Conversation

rmoyard
Copy link
Contributor

@rmoyard rmoyard commented Sep 27, 2021

Context:

Previously qml.probs() was not able to accept an Hermitian observable, in order to give the probabilities in another basis than the computational one.

Description of the Change:

Now it is possible to give an optional Hermitian observable that will be used to rotate the circuit before measuring the probabilities. Then we receive the probabilities of the state in the Hermitian eigenbasis.

H = 1 / np.sqrt(2) * np.array([[1, 1], [1, -1]])

@qml.qnode(dev)
def circuit():
    qml.PauliZ(wires=0)
    qml.PauliX(wires=1)
    return qml.probs(op=qml.Hermitian(H, wires=0))

print(circuit())

Related GitHub Issues:

This is a user requested feature, closes #1468

@rmoyard rmoyard added the WIP 🚧 Work-in-progress label Sep 27, 2021
@github-actions
Copy link
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.

@codecov
Copy link

codecov bot commented Sep 27, 2021

Codecov Report

Merging #1692 (64d7fae) into master (06972e8) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1692   +/-   ##
=======================================
  Coverage   99.22%   99.22%           
=======================================
  Files         207      207           
  Lines       15579    15587    +8     
=======================================
+ Hits        15458    15466    +8     
  Misses        121      121           
Impacted Files Coverage Δ
pennylane/measure.py 99.03% <100.00%> (+0.08%) ⬆️

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 06972e8...64d7fae. Read the comment docs.

@antalszava
Copy link
Contributor

[ch7663]

@rmoyard rmoyard changed the title [WIP] Generalize qml.probs() to compute probabilities with respect to an eigenbasis Generalize qml.probs() to compute probabilities with respect to an eigenbasis Oct 6, 2021
@rmoyard rmoyard removed the WIP 🚧 Work-in-progress label Oct 6, 2021
pennylane/_device.py Outdated Show resolved Hide resolved
Copy link
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.

Why are we limiting this to just qml.Hermitian observables? Why not any kind of Observable?

Also, MeasurementProcess uses obs, but then some of the creation functions use op. Since we can only pass in observables, and only one observable, maybe we should standardize the use of ob across the file. Names do matter since the user does have to pass operation in as a keyword argument.

I think we should actually look into using multiple dispatch for these MeasurementProcess factory methods,, but that would be outside the scope of this PR.

Also, a general comment on the formatting changes to files that aren't actually affected by a given PR. Might want to revert default_qubit and _qubit_device.

pennylane/measure.py Outdated Show resolved Hide resolved
pennylane/measure.py Outdated Show resolved Hide resolved
pennylane/_device.py Outdated Show resolved Hide resolved
@josh146
Copy link
Member

josh146 commented Oct 8, 2021

Why are we limiting this to just qml.Hermitian observables? Why not any kind of Observable?

It might be worth asking @cvjjm for more details on the exact use case they were considering in the original issue :)

@cvjjm
Copy link
Contributor

cvjjm commented Oct 8, 2021

I suppose every kind of observable that define diagonalizing_gates() can be supported. Even better, a list of jointly measurable observables could probably be supported as well without too much effort.

@cvjjm
Copy link
Contributor

cvjjm commented Oct 8, 2021

... because then it should be possible to use the exact same pre-measurement rotation machinery that PL also uses for things like expval and var.

@rmoyard rmoyard requested a review from albi3ro October 13, 2021 20:57
Copy link
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.

Just some minor wording changes, but after that, this PR seems to get the job done so I'll approve it.

pennylane/measure.py Outdated Show resolved Hide resolved
pennylane/measure.py Outdated Show resolved Hide resolved
tests/test_prob.py Show resolved Hide resolved
rmoyard and others added 4 commits October 15, 2021 10:04
Co-authored-by: Christina Lee <christina@xanadu.ai>
Co-authored-by: Christina Lee <christina@xanadu.ai>
Copy link
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.

Glad to have this 😃

@rmoyard rmoyard merged commit a765663 into master Oct 15, 2021
@rmoyard rmoyard deleted the generalize_probs branch October 15, 2021 14:41
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.

Generalize qml.probs() to compute probabilities with respect to an eigenbasis
5 participants