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

dynamic_one_shot uses tapes with shot-vectors and jitting takes advantage of it #5617

Merged
merged 103 commits into from
May 10, 2024

Conversation

vincentmr
Copy link
Contributor

@vincentmr vincentmr commented May 1, 2024

Before submitting

Please complete the following checklist when submitting a PR:

  • All new features must include a unit test.
    If you've fixed a bug or added code that should be tested, add a test to the
    test directory!

  • All new functions and code must be clearly commented and documented.
    If you do make documentation changes, make sure that the docs build and
    render correctly by running make docs.

  • Ensure that the test suite passes, by running make test.

  • Add a new entry to the doc/releases/changelog-dev.md file, summarizing the
    change, and including a link back to the PR.

  • The PennyLane source code conforms to
    PEP8 standards.
    We check all of our code against Pylint.
    To lint modified files, simply pip install pylint, and then
    run pylint pennylane/path/to/file.py.

When all the above are checked, delete everything above the dashed
line and fill in the pull request template.


Context:
dynamic_one_shot creates n-shots tapes which is wasteful.

Description of the Change:
Create a single tape with a shot-vector which indicates to the device how many times to repeat the tape execution.

Benefits:
For a tape like

dev = qml.device("default.qubit", shots=2000, seed=jax.random.PRNGKey(123))

@qml.qnode(dev, diff_method=None)
def func(x, y):
    qml.RX(x, wires=0)
    m0 = qml.measure(0, reset=False, postselect=1)
    qml.cond(m0, qml.RY)(y, wires=1)
    return qml.expval(qml.PauliZ(0))


params = np.pi / 4 * np.ones(2)

The execution times are as follows (Latitude laptop):

  • 12.7 s : vanilla Python
  • 8.2 s : jax.vmap
  • 11.1 s : jax.jit + jax.vmap + compilation
  • 6.89 ms : jax.jit + jax.vmap

Possible Drawbacks:

Related GitHub Issues:
[sc-62097]

mudit2812 and others added 30 commits March 7, 2024 12:37
Co-authored-by: Christina Lee <christina@xanadu.ai>
Copy link
Contributor

@dime10 dime10 left a comment

Choose a reason for hiding this comment

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

This looks good to me 👍

pennylane/_qubit_device.py Show resolved Hide resolved
pennylane/devices/qubit/simulate.py Outdated Show resolved Hide resolved
Base automatically changed from feature/dynamic_samples_jit to master May 9, 2024 14:30
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.

Changelog?

@vincentmr vincentmr requested a review from albi3ro May 10, 2024 13:15
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.

Might be worth investigating if we can somehow use use multi-processing with this in the future.

Thanks for doing all this investigation!

@vincentmr
Copy link
Contributor Author

Might be worth investigating if we can somehow use use multi-processing with this in the future.

That's actually the original idea behind having multiple tapes instead of shots. On the other hand, I think multi-processing might be better turned-on at the QNode level, or maybe with a decorator. I think any device with a batch of tapes could be executing them in parallel inside sub-processes.

@dime10
Copy link
Contributor

dime10 commented May 10, 2024

Might be worth investigating if we can somehow use use multi-processing with this in the future.

That's actually the original idea behind having multiple tapes instead of shots. On the other hand, I think multi-processing might be better turned-on at the QNode level, or maybe with a decorator. I think any device with a batch of tapes could be executing them in parallel inside sub-processes.

Why couldn't you parallelize with shots instead of tapes?

@vincentmr
Copy link
Contributor Author

Why couldn't you parallelize with shots instead of tapes?

The current multiprocessing logic live here. It is not a fundamental limitation, it's just our UI is not set up for it.

@vincentmr vincentmr enabled auto-merge (squash) May 10, 2024 18:25
@vincentmr vincentmr merged commit 9c9b6ba into master May 10, 2024
38 checks passed
@vincentmr vincentmr deleted the feature/batched_tape branch May 10, 2024 18:55
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

4 participants