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

[Runtime][Frontend] Add support for custom QuantumDevices. #327

Merged
merged 64 commits into from
Oct 26, 2023

Conversation

erick-xanadu
Copy link
Contributor

@erick-xanadu erick-xanadu commented Oct 19, 2023

Context: Changes needed to support third party devices.

Third party devices must implement the Catalyst::Runtime::QuantumDevice interface and add a getCustomDevice method which returns an instance of their device.

extern "C" Catalyst::Runtime::QuantumDevice*
getCustomDevice() { return new CustomDevice(); }

PennyLane plugin devices should also implement

    @staticmethod
    def get_c_interface():
        return path_to_shared_lib

Then the following works:

@qjit
@qml.qnode(qml.device("custom.device", wires=wires))
def circuit():
    ...

Description of the Change: Changes in the frontend to allow any devices that implement a QuantumDevice interface to be loaded into the runtime. Changes to the runtime as well.

Benefits: Third party developers can implement their own devices as long as they follow the spec.

Possible Drawbacks: Benchmarks showed no impact.

Related GitHub Issues:

  • Tests
  • Documentation

Note: RTLD_DEEPBIND is needed for allowing custom QuantumDevices to work. However, RTLD_DEEPBIND is incompatible with sanitizers.

[sc-46413]

@codecov
Copy link

codecov bot commented Oct 19, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (312b12a) 99.56% compared to head (e059a7f) 99.56%.
Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #327   +/-   ##
=======================================
  Coverage   99.56%   99.56%           
=======================================
  Files          41       42    +1     
  Lines        7353     7404   +51     
  Branches      430      432    +2     
=======================================
+ Hits         7321     7372   +51     
  Misses         16       16           
  Partials       16       16           
Files Coverage Δ
frontend/catalyst/pennylane_extensions.py 99.64% <100.00%> (+<0.01%) ⬆️
runtime/lib/capi/ExecutionContext.hpp 100.00% <100.00%> (ø)
runtime/tests/Test_DLManager.cpp 100.00% <100.00%> (ø)
runtime/tests/Test_LightningCoreQIS.cpp 100.00% <ø> (ø)
runtime/tests/Test_LightningDriver.cpp 100.00% <ø> (ø)

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

Custom devices which implement the `QuantumDevice` class must also
implement an

```cpp
extern "C" Catalyst::Runtime::QuantumDevice*
getCustomDevice() { return new CustomDevice(); }
```

PennyLane plugin devices should also implement

```python
    @staticmethod
    def get_c_interface():
        return path_to_shared_lib
```

Then the following works:

```python

@qjit
@qml.qnode(qml.device("custom.device", wires=wires))
def circuit():
    ...
```
See this error message:

You are trying to dlopen a this-file-does-not-exist.so shared
library with RTLD_DEEPBIND flag which is incompatible with sanitizer
runtime (see google/sanitizers#611 for
details). If you want to run this-file-does-not-exist.so library under
sanitizers please remove RTLD_DEEPBIND from dlopen flags.
doc/dev/custom_devices.rst Show resolved Hide resolved
frontend/catalyst/pennylane_extensions.py Outdated Show resolved Hide resolved
frontend/catalyst/pennylane_extensions.py Show resolved Hide resolved
runtime/lib/capi/ExecutionContext.hpp Outdated Show resolved Hide resolved
runtime/Makefile 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
erick-xanadu and others added 3 commits October 24, 2023 14:27
Co-authored-by: Ali Asadi <ali@xanadu.ai>
Co-authored-by: Ali Asadi <ali@xanadu.ai>
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 Show resolved Hide resolved
runtime/tests/Test_LightningCoreQIS.cpp Show resolved Hide resolved
Copy link
Member

@maliasadi maliasadi left a comment

Choose a reason for hiding this comment

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

Amazing work @erick-xanadu 🎉

@maliasadi maliasadi requested a review from mlxd October 24, 2023 20:46
Copy link
Member

@josh146 josh146 left a comment

Choose a reason for hiding this comment

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

Thanks Erick, have only reviewed the docs, but looks good to me!

doc/dev/custom_devices.rst 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.

Very cool 🦝

doc/dev/custom_devices.rst Outdated Show resolved Hide resolved
doc/dev/custom_devices.rst Outdated Show resolved Hide resolved
runtime/lib/capi/CMakeLists.txt Outdated Show resolved Hide resolved
runtime/lib/capi/ExecutionContext.hpp Show resolved Hide resolved
Co-authored-by: David Ittah <dime10@users.noreply.github.com>
@erick-xanadu erick-xanadu merged commit 03e814d into main Oct 26, 2023
19 checks passed
@erick-xanadu erick-xanadu deleted the eochoa/2023-10-17/loading branch October 26, 2023 12:37
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.

5 participants