feat(qml): Add QLSTM implementation based on arXiv:2009.01783#8
feat(qml): Add QLSTM implementation based on arXiv:2009.01783#8
Conversation
This PR implements Quantum Long Short-Term Memory (QLSTM) with a 6-VQC architecture as described in Figure 5 of the paper. ## New Features ### QLSTM Module (src/qml/) - QLSTMCell with 6 VQCs: - VQC₁-VQC₄: Forget, Input, Cell, Output gates - VQC₅: Hidden state processing (h_t = VQC₅(o_t ⊙ tanh(c_t))) - VQC₆: Output processing (y_t = VQC₆(h_t)) - QLSTM layer for sequence processing - QLSTMTrainer for basic gradient descent training - VQC types: Simple, BasicEntangling, StronglyEntangling - Data encoding strategies: Angle, Amplitude, IQP ### Visualization (src/vis/qlstm.rs) - SVG diagram generation for QLSTM cell architecture - VQC circuit visualization - Text-based diagram output ### Example (examples/qlstm_demo.rs) - Comprehensive demo showcasing QLSTM functionality - Generates SVG diagrams for visualization ### Tests (tests/test_qml_qlstm.rs) - Unit tests for QLSTM cell and layer - Gradient accuracy tests - Smoke tests for training tasks References: - arXiv:2009.01783 (Quantum Long Short-Term Memory) - PennyLane Learning to Learn with Quantum Neural Networks
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 3 potential issues.
Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.
This PR is being reviewed by Cursor Bugbot
Details
You are on the Bugbot Free tier. On this plan, Bugbot will review limited PRs each billing cycle.
To receive Bugbot reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.
- Fixed forward() to properly slice parameters for each stacked layer - Each layer now gets its own parameter slice instead of all params - Added forward_with_state() to return final hidden/cell states - Updated step() to use first layer's params when full param set provided - Fixed return value semantics: forward() now returns outputs (y_t), not h_t - Added tests for stacked QLSTM with and without return_sequences Fixes panic: 'Parameter count mismatch: expected N, got N*num_layers' when using QLSTM.with_num_layers(n) where n > 1
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.
The train method was returning (params, best_loss) where params were the final parameters after all iterations, but best_loss may have been achieved at an earlier iteration. This mismatch meant the returned loss didn't correspond to the returned parameters. Now tracks best_params alongside best_loss and returns consistent values that can be verified by evaluating the model with the returned parameters.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.
The forward_activated method was documented as applying a 'tanh-like' activation but simply returned the raw forward() output unchanged. Changes: - Apply tanh() to each Z expectation value in forward_activated - Update documentation to accurately describe the behavior - Add test to verify tanh transformation is applied correctly
- Fix truncation bug in stacked QLSTM when input_size != hidden_size - Intermediate layers now correctly use hidden_size as input_size - QLSTM stores separate cells per layer with appropriate configurations - Move QLSTM unit tests from src/qml/qlstm.rs to tests/test_qml_qlstm.rs - Add tests for stacked QLSTM parameter counts and no-truncation behavior
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.
Fixed a bug where gate vectors (f_t, i_t, c_tilde, o_t) and output vectors (h_t, y_t) were not properly padded when VQC output had fewer elements than hidden_size. Previously, .take(hidden_size).collect() only truncated but didn't pad, causing dimension mismatches when hadamard_product/add_vec used zip which silently truncates to the shorter length. The fix applies the same padding pattern used for cell_state: - f_t, i_t, o_t (sigmoid gates): pad with 0.5 (sigmoid midpoint) - c_tilde (tanh gate): pad with 0.0 (tanh midpoint) - h_t, y_t (outputs): pad with 0.0 Added test_qlstm_num_qubits_less_than_hidden_size to verify the fix.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.
…loss The cross_entropy_loss function expected probability values in [0, 1], but QLSTM/VQC outputs are Z expectation values in [-1, 1]. Negative values were silently clamped to epsilon, producing incorrect loss values. Changes: - cross_entropy_loss now always transforms Z expectations from [-1, 1] to probabilities in [0, 1] using: p = (z + 1) / 2 - Added cross_entropy_loss_probabilities for pre-normalized [0, 1] inputs - Added comprehensive documentation explaining the transformation - Added tests verifying correct handling of negative Z expectations - Exported new function in mod.rs and prelude.rs
Summary
This PR implements Quantum Long Short-Term Memory (QLSTM) with a 6-VQC architecture as described in Figure 5 of the paper arXiv:2009.01783.
New Features
QLSTM Module (
src/qml/)h_t = VQC₅(o_t ⊙ tanh(c_t)))y_t = VQC₆(h_t))Visualization (
src/vis/qlstm.rs)Example (
examples/qlstm_demo.rs)qlstm_cell.svg,qlstm_full.svg,qlstm_vqc.svgTests (
tests/test_qml_qlstm.rs)Architecture Diagram
References
Note
Introduces a Quantum ML stack centered on a 6‑VQC QLSTM, plus visualization, examples, and tests.
src/qml/withqlstm(6‑VQC cell,QLSTMlayer, trainer, MSE/cross‑entropy losses),vqc(VQC types/builders/forward), anddata_encoding(Angle/Amplitude/IQP)src/vis/qlstm.rs): text and SVG diagrams; implementsVisualizableand re-exports invisexamples/qlstm_demo.rsdemonstrating forward pass, training, gradients, VQC types, and diagram generationtests/test_qml_qlstm.rsfor forward/stacked behavior, gradients, basic tasks, and lossessrc/lib.rsandsrc/prelude.rsWritten by Cursor Bugbot for commit becb5b2. This will update automatically on new commits. Configure here.