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

Implement ExpectationMP.process_counts #5241

Closed
albi3ro opened this issue Feb 21, 2024 · 3 comments · Fixed by #5256
Closed

Implement ExpectationMP.process_counts #5241

albi3ro opened this issue Feb 21, 2024 · 3 comments · Fixed by #5256
Assignees
Labels
good first issue Good for newcomers

Comments

@albi3ro
Copy link
Contributor

albi3ro commented Feb 21, 2024

Context

Currently, our sample-based measurement processes that inherit from SampleMeasurement implement a process_samples method. This method is capable of turning any samples (array of int64) into the required measurement result. This method is currently used internally by default.qubit.

@qml.qnode(qml.device('default.qubit'))
def circuit():
    qml.Hadamard(0)
    return qml.sample(wires=(0,1))

samples = circuit(shots=100)
qml.expval(qml.Z(0)).process_samples(samples, wire_order=qml.wires.Wires((0,1)))

But external devices and plugins may want to store sampling information in the form of a counts dictionary instead, like {'00': 45, '01': 55}. This representation is much more condensed and memory efficient. Because of this, we have recently added a SampleMeasurement.process_counts method, and implemented it for ProbabilityMP.

Implementation Details

The task is to implement ExpectationMP.process_counts(counts, wire_order) in such a way that expectation values can be computed from a counts dictionary.

>>> counts = {'000': 100, '100': 100 }
>>> wire_order = qml.wires.Wires((0,1,2))
>>> qml.expval(qml.Z(0)).process_counts(counts, wire_order)
0.0
>>> qml.expval(qml.Z(1)).process_counts(counts, wire_order)
1.0

Note that the counts are assumed to be taken in the basis for the provided observable already, with any diagonalizing gates already applied.

Requirements

A completed PR should:

@albi3ro albi3ro added the good first issue Good for newcomers label Feb 21, 2024
@Tarun-Kumar07
Copy link
Contributor

Hello @albi3ro ,

I'm new to Pennylane and eager to start contributing. Could I begin by working on this particular issue?

Thank you!

@albi3ro
Copy link
Contributor Author

albi3ro commented Feb 22, 2024

Thanks for reaching out @Tarun-Kumar07 . I've assigned you the issue.

Feel free to reach out with any additional questions, or open a draft PR for early feedback.

Tarun-Kumar07 added a commit to Tarun-Kumar07/pennylane that referenced this issue Feb 25, 2024
Tarun-Kumar07 added a commit to Tarun-Kumar07/pennylane that referenced this issue Feb 25, 2024
@Tarun-Kumar07
Copy link
Contributor

Hello @albi3ro , I opened a PR.
Can you please have a look at it?
Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants