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

Track qpu executions and shots on default qubit #4628

Merged
merged 18 commits into from Oct 3, 2023
Merged

Conversation

albi3ro
Copy link
Contributor

@albi3ro albi3ro commented Sep 22, 2023

Example behavior:

def get_totals(tape):
    dev = qml.device('default.qubit')
    with dev.tracker:
        dev.execute(tape)
    return dev.tracker.totals
# commuting expvals
>>> tape = qml.tape.QuantumScript([], [qml.expval(qml.PauliX(0)), qml.expval(qml.PauliX(1))], shots=10)
>>> get_totals(tape)
{'executions': 1, 'shots': 10, 'batches': 1, 'simulations': 1}

# Hamiltonian with computed grouping indices
>>> H = qml.Hamiltonian([1, 1], [qml.PauliX(0), qml.PauliX(1)], grouping_type="qwc")
>>> tape = qml.tape.QuantumScript([], [qml.expval(H)], shots=10)
>>> get_totals(tape)
{'executions': 1, 'shots': 10, 'batches': 1, 'simulations': 1}

# Hamiltonian without computed grouping indices
>>> H = qml.Hamiltonian([1, 1], [qml.PauliX(0), qml.PauliX(1)])
>>> tape = qml.tape.QuantumScript([], [qml.expval(H)], shots=10)
>>> get_totals(tape)
{'executions': 2, 'shots': 20, 'batches': 1, 'simulations': 1}

# combining expvals with computational basis measurements
>>> tape = qml.tape.QuantumScript([], [qml.expval(qml.PauliX(0)), qml.probs(wires=0)], shots=10)
>>> get_totals(tape)
{'executions': 2, 'shots': 20, 'batches': 1, 'simulations': 1}

# classical shadows
>>> tape = qml.tape.QuantumScript([], [qml.shadow_expval(H0), qml.probs(wires=(0, 1))], shots=10)
>>> get_totals(tape)
{'executions': 11, 'shots': 20, 'batches': 1, 'simulations': 1}

# batch size scaling
>>> tape = qml.tape.QuantumScript([qml.RX((1.2, 2.3), wires=0)], [qml.expval(qml.PauliZ(0))], shots=10)
>>> get_totals(tape)
{'executions': 2, 'shots': 20, 'batches': 1, 'simulations': 1}

@codecov
Copy link

codecov bot commented Sep 22, 2023

Codecov Report

All modified lines are covered by tests ✅

Comparison is base (78f8123) 99.63% compared to head (0dcc80f) 99.63%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #4628   +/-   ##
=======================================
  Coverage   99.63%   99.63%           
=======================================
  Files         376      376           
  Lines       33617    33649   +32     
=======================================
+ Hits        33493    33525   +32     
  Misses        124      124           
Files Coverage Δ
pennylane/devices/default_qubit.py 100.00% <100.00%> (ø)
pennylane/devices/qubit/sampling.py 100.00% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@albi3ro
Copy link
Contributor Author

albi3ro commented Sep 22, 2023

[sc-42389]

@timmysilv
Copy link
Contributor

this looks great, but i'm wondering if we should change the naming to match how things were before. executions kinda already meant qpu_executions on DQL, so maybe we can do:

  • executions -> simulator_passes
  • qpu_executions -> executions

or something like that. if you make this change, tests/test_vqe.py should fail, and I think that's a good thing (in other words it will need updating to mark this PR as complete).

@trbromley
Copy link
Contributor

Thanks @albi3ro!

this looks great, but i'm wondering if we should change the naming to match how things were before. executions kinda already meant qpu_executions on DQL, so maybe we can do:

* `executions` -> `simulator_passes`

* `qpu_executions` -> `executions`

or something like that. if you make this change, tests/test_vqe.py should fail, and I think that's a good thing (in other words it will need updating to mark this PR as complete).

Agreed - would be great not to introduce a breaking change here. Though this way around there is the issue that simulator_passes doesn't make sense on a real QPU. Maybe in that case we set this number to be zero or None.

@timmysilv
Copy link
Contributor

I think the tracker only shows things that you add to it, right? so on real QPUs, you just don't add simulator_passes.

@albi3ro
Copy link
Contributor Author

albi3ro commented Sep 25, 2023

@trbromley @timmysilv

For an analytic case, do we want "executions" to match qpu executions or simulator passes?

@trbromley
Copy link
Contributor

@trbromley @timmysilv

For an analytic case, do we want "executions" to match qpu executions or simulator passes?

@albi3ro in the analytic case, I think it's fine if executions is the number of executions that would be used by default.qubit in finite-shots mode or equivalently on a similar QPU device. We can also store simulator_passes.

I think the above is compatible with what we were doing before, i.e., the old default.qubit would show 2 executions in:

import pennylane as qml

dev = qml.device('default.qubit', wires=1)

@qml.qnode(dev)
def f(x):
    qml.RX(x, wires=0)
    return qml.expval(qml.PauliX(0)), qml.expval(qml.PauliY(0)) 

with qml.Tracker(dev) as tracker:
    f(0.8)

assert tracker.totals['executions'] == 2

I think the tracker only shows things that you add to it, right? so on real QPUs, you just don't add simulator_passes.

@timmysilv yes that works, but can we make sure to clearly document this in the Tracker docs?

@albi3ro albi3ro requested a review from timmysilv October 2, 2023 13:07
Copy link
Contributor

@timmysilv timmysilv left a comment

Choose a reason for hiding this comment

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

just a comment on the changelog, otherwise lgtm!

albi3ro and others added 2 commits October 2, 2023 11:47
@albi3ro albi3ro added the review-ready 👌 PRs which are ready for review by someone from the core team. label Oct 2, 2023
Copy link
Contributor

@lillian542 lillian542 left a comment

Choose a reason for hiding this comment

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

Looks good! There's a couple things I'm not sure about.

tests/devices/test_default_qubit_tracking.py Outdated Show resolved Hide resolved
@albi3ro albi3ro enabled auto-merge (squash) October 3, 2023 15:34
@albi3ro albi3ro merged commit 057c776 into master Oct 3, 2023
39 checks passed
@albi3ro albi3ro deleted the shot-information branch October 3, 2023 16:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
review-ready 👌 PRs which are ready for review by someone from the core team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants