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

Pool of devices and async execution of multiple active devices supports #381

Merged
merged 27 commits into from
Dec 19, 2023

Conversation

maliasadi
Copy link
Member

@maliasadi maliasadi commented Dec 5, 2023

Context:

  • The runtime now supports multiple active devices managed via a device pool. The new RTDevice data-class and RTDeviceStatus along with the thread_local device instance pointer enable the runtime to better scope the lifetime of device instances concurrently. With these changes, one can create multiple active devices and execute multiple programs in a multithreaded environment.

  • Add DeviceReleaseOp to the Quantum MLIR dialect. This will be lowered to the __quantum__rt__device_release() runtime instruction updating the status of the device instance from Active to Inactive. The runtime will reuse this deactivated instance instead of creating a new one automatically at runtime in a multi-qnode workflow when another device with identical specifications is requested.

[sc-47651]

Copy link

codecov bot commented Dec 5, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (d8ba956) 99.56% compared to head (61c003c) 99.56%.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #381   +/-   ##
=======================================
  Coverage   99.56%   99.56%           
=======================================
  Files          43       43           
  Lines        7546     7559   +13     
  Branches      497      497           
=======================================
+ Hits         7513     7526   +13     
  Misses         17       17           
  Partials       16       16           

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

doc/changelog.md Outdated Show resolved Hide resolved
@maliasadi maliasadi added the runtime Pull requests that update the runtime label Dec 6, 2023
@maliasadi maliasadi changed the title [Runtime] Pool of devices and async execution of multiple active devices supports Pool of devices and async execution of multiple active devices supports Dec 6, 2023
maliasadi added a commit that referenced this pull request Dec 8, 2023
We currently lower the device name, kwargs, and library with 3
`__quantum__rt__device(key, spec)` calls. The runtime initializes the
targeted device after getting the device name and kwargs at the third
call:

```
__quantum__rt__device("rtd_name", "lightning.qubit");
__quantum__rt__device("rtd_kwargs", "{...}");
__quantum__rt__device("rtd_lib", "rtd_lightning.so"); <--- init device
```

This PR reduces the number of these device calls into 1 single
`__quantum__rt__device_init("rtd_lib", "rtd_name", "rtd_kwargs")` call
and deprecates the `__quantum__rt__device` instruction. The runtime is
able to initialize the device without necessarily storing partial device
info or implying any specific order to define these specifications in
the IR and the runtime. This PR is related to
#381 and will help better
scope the lifetime of device instances. We also add
`__quantum__rt__device_release()` to release the initialized device by
request (this'll be used in the async support).

---------

Co-authored-by: David Ittah <dime10@users.noreply.github.com>
Copy link
Contributor

@rmoyard rmoyard left a comment

Choose a reason for hiding this comment

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

It looks great! 💯 I have some questions

demos/adaptive_circuits_demo.ipynb Show resolved Hide resolved
doc/changelog.md Outdated Show resolved Hide resolved
runtime/lib/capi/ExecutionContext.hpp Show resolved Hide resolved
runtime/lib/capi/ExecutionContext.hpp Outdated Show resolved Hide resolved
runtime/lib/capi/ExecutionContext.hpp Outdated Show resolved Hide resolved
runtime/lib/capi/ExecutionContext.hpp Show resolved Hide resolved
runtime/lib/capi/ExecutionContext.hpp Outdated Show resolved Hide resolved
runtime/lib/capi/ExecutionContext.hpp Outdated Show resolved Hide resolved
runtime/lib/capi/ExecutionContext.hpp Outdated Show resolved Hide resolved
doc/changelog.md Outdated Show resolved Hide resolved
Copy link
Collaborator

@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.

Great work @maliasadi 🎉 🎉

I mostly have some comments and questions to pin down the role of different components, but happy to approve otherwise :)

doc/changelog.md Outdated Show resolved Hide resolved
mlir/include/Quantum/IR/QuantumOps.td Outdated Show resolved Hide resolved
mlir/lib/Gradient/Transforms/GradMethods/Adjoint.cpp Outdated Show resolved Hide resolved
mlir/lib/Gradient/Transforms/GradMethods/Adjoint.cpp Outdated Show resolved Hide resolved
runtime/lib/capi/ExecutionContext.hpp Outdated Show resolved Hide resolved
runtime/lib/capi/ExecutionContext.hpp Show resolved Hide resolved
runtime/lib/capi/ExecutionContext.hpp Outdated Show resolved Hide resolved
runtime/lib/capi/RuntimeCAPI.cpp Outdated Show resolved Hide resolved
runtime/lib/capi/RuntimeCAPI.cpp Outdated Show resolved Hide resolved
@erick-xanadu
Copy link
Contributor

There's a frontend test in test_compiler that tests the writing a program using MLIR as a string. I think we need to add the new quantum instruction release there as well.

@erick-xanadu
Copy link
Contributor

We also need to reconsider what happens now if the runtime throws an exception.

Copy link
Contributor

@erick-xanadu erick-xanadu left a comment

Choose a reason for hiding this comment

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

@maliasadi, this is looking good! I will mark this as approved but I now think we should wait to merge it until the async support is more worked out. I would like the async support for finite difference to be working well. You mentioned you wanted this merge as it would facilitate my work but I think it is ok, I can always rebase :). I'll let you and @rmoyard coordinate on this.

PS: don't forget to fix the frontend test in test_compiler.py

@maliasadi
Copy link
Member Author

@erick-xanadu Thanks! There is no rush to merge this PR if it doesn't contribute to your progress. I'll apply the remaining suggestions today and will ping you for the rebase. Let's ensure end-to-end support before merging 💯

Copy link
Contributor

@rmoyard rmoyard left a comment

Choose a reason for hiding this comment

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

Great work @maliasadi! Thanks for answering my questions! I let you answer David's comments and questions (that I also share) and you can consider that is all good on my side 👍

@maliasadi maliasadi added the compiler Pull requests that update the compiler label Dec 18, 2023
Copy link
Collaborator

@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.

Thanks @maliasadi, this looks great! A few last questions about the interaction of the CAPI and the execution context, then I'm happy with it 💯

Makefile Outdated Show resolved Hide resolved
runtime/tests/Test_LightningCoreQIS.cpp Outdated Show resolved Hide resolved
runtime/lib/capi/ExecutionContext.hpp Outdated Show resolved Hide resolved
runtime/lib/capi/ExecutionContext.hpp Outdated Show resolved Hide resolved
runtime/lib/capi/RuntimeCAPI.cpp Outdated Show resolved Hide resolved
runtime/lib/capi/RuntimeCAPI.cpp Outdated Show resolved Hide resolved
Copy link
Collaborator

@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.

Nice work! 🥇

@maliasadi maliasadi merged commit 677234f into main Dec 19, 2023
19 checks passed
@maliasadi maliasadi deleted the maa/async_runtime branch December 19, 2023 15:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler Pull requests that update the compiler runtime Pull requests that update the runtime
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants