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

Fix qml.counts measurement in new device #4172

Merged
merged 4 commits into from May 26, 2023
Merged

Fix qml.counts measurement in new device #4172

merged 4 commits into from May 26, 2023

Conversation

eddddddy
Copy link
Collaborator

Context:
Part of the effort to integrate the new device API. Most measurements are supported with only a few weird edge cases (including counts).

Description of the Change:
Previous behaviour:

>>> dev = DefaultQubit2()
>>> qs1 = qml.tape.QuantumScript([qml.RY(1.4, wires=0)], [qml.counts(wires=[0, 1])], shots=15)
>>> dev.execute(qs1)
{'FalseFalse': 9, 'TrueFalse': 6}
>>> qs2 = qml.tape.QuantumScript([qml.RY(1.4, wires=0)], [qml.counts(qml.PauliZ(0))], shots=15)
>>> dev.execute(qs2)
{tensor(-1, requires_grad=True): 10, tensor(1, requires_grad=True): 5}

New behaviour:

>>> dev = DefaultQubit2()
>>> qs1 = qml.tape.QuantumScript([qml.RY(1.4, wires=0)], [qml.counts(wires=[0, 1])], shots=15)
>>> dev.execute(qs1)
{'00': 9, '10': 6}
>>> qs2 = qml.tape.QuantumScript([qml.RY(1.4, wires=0)], [qml.counts(qml.PauliZ(0))], shots=15)
>>> dev.execute(qs2)
{-1,: 10, 1: 5}

@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 May 25, 2023

Codecov Report

Merging #4172 (b8a3ca4) into master (5d8ee2e) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #4172   +/-   ##
=======================================
  Coverage   99.77%   99.77%           
=======================================
  Files         340      340           
  Lines       30561    30566    +5     
=======================================
+ Hits        30491    30496    +5     
  Misses         70       70           
Impacted Files Coverage Δ
pennylane/devices/qubit/sampling.py 100.00% <100.00%> (ø)
pennylane/measurements/counts.py 100.00% <100.00%> (ø)

@eddddddy eddddddy requested review from albi3ro and a team and removed request for albi3ro May 25, 2023 17:35
Copy link
Contributor

@mudit2812 mudit2812 left a comment

Choose a reason for hiding this comment

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

Looks great. Thanks @eddddddy ! 🚀

pennylane/measurements/counts.py 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.

🎉

Just changelog and unit tests for CountsMP.process_samples.

Actually not seeing any unit tests for CountsMP.process_samples in test_counts.py, but this might be a good opportunity to change that. Even simple stuff like:

samples = np.zeros((4,2))
output = CountsMP(wires=(0,1)).process_samples(samples, wire_order=(0,1))
assert output == {'00': 4}

or something. probably messed up the orders, but you know what I mean anyway.

@eddddddy
Copy link
Collaborator Author

@albi3ro There are unit tests in test_counts.py, but those tests use integer arrays for the samples instead of boolean arrays. Isn't this behaviour new for DefaultQubit2? Should I change the previous unit tests to accept boolean arrays instead?

@eddddddy eddddddy requested a review from albi3ro May 26, 2023 14: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.

Thanks for the added tests. Looks great 🎉

@eddddddy eddddddy merged commit 233eec7 into master May 26, 2023
43 checks passed
@eddddddy eddddddy deleted the counts_fix branch May 26, 2023 16:23
@eddddddy
Copy link
Collaborator Author

[sc-38218]

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.

None yet

3 participants