PyTorch Autograd Engine & QuantumLayer Interface for QML - #28
Open
Rao-Adarsh wants to merge 10 commits into
Open
Rao-Adarsh wants to merge 10 commits into
Rao-Adarsh wants to merge 10 commits into
Conversation
Added run_kwargs parameter to the constructor for additional flexibility.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR introduces native PyTorch integration, it uses the torch.autograd.Function to implement the Paramater Shift Rule, allowing users to optimize parameterized quantum circuits using standard PyTorch optimizers (like Adam or SGD).
ParameterShiftFunction: A custom PyTorch autograd engine. Because the parameter-shift rule requires$2N$ independent circuit evaluations, this function utilizes concurrent.futures.ThreadPoolExecutor to evaluate the forward and backward shifted circuits concurrently.
QuantumLayer (torch.nn.Module): A high-level wrapper:
1. Dynamically binds PyTorch weight tensors to the IntermediateCircuitRepresentation (ICR).
2. Hardware-Ready: Natively computes exact mathematical expectation values via the QpiAI-QSV-Local simulator. If a user sets device_name to real hardware, it automatically falls back to an empirical counts-based measurement logic.
Verification Tests: Added comprehensive tests in test_torch_interface.py verifying the computed parameter-shift gradients against exact analytical gradients and PyTorch's native gradcheck.
Usage example:
Future Prospects ?:$2N$ different circuits (because you are shifting the parameters forward and backward $N$ times). Currently, circuit.run() only accepts one circuit configuration at a time. It cannot accept a 2D matrix containing $2N$ different parameter vectors. Because of this limitation, the torch_integration.py uses Python's concurrent.futures to spawn $2N$ separate Python threads. If the core SDK is updated such that you could pass the circuit to the simulator exactly once, along with a 2D matrix of all $2N$ parameter shifts and everything is done using Numba/ C++ instead of python by the StatevectorSimulator, it could decrease the runtimes.
Currently, parameter binding and parameter batching are managed at the Python wrapper layer (QuantumLayer dynamically rebuilds the circuit for each shift).
The parameter-shift rule requires you to run
Disclaimer: This PR has been developed with generous use of Generative AI.