Skip to content

feat(qml): Add QLSTM implementation based on arXiv:2009.01783#8

Merged
zazabap merged 7 commits intomainfrom
feature/qlstm-implementation
Jan 18, 2026
Merged

feat(qml): Add QLSTM implementation based on arXiv:2009.01783#8
zazabap merged 7 commits intomainfrom
feature/qlstm-implementation

Conversation

@zazabap
Copy link
Owner

@zazabap zazabap commented Jan 17, 2026

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/)

  • 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: Angle, Amplitude, IQP encoding strategies

Visualization (src/vis/qlstm.rs)

  • SVG diagram generation for QLSTM cell architecture
  • VQC circuit visualization with qubit lines, gates, and measurements
  • Text-based diagram output

Example (examples/qlstm_demo.rs)

  • Comprehensive demo showcasing QLSTM functionality
  • Generates SVG diagrams: qlstm_cell.svg, qlstm_full.svg, qlstm_vqc.svg

Tests (tests/test_qml_qlstm.rs)

  • Unit tests for QLSTM cell and layer
  • Gradient accuracy tests using parameter-shift rule
  • Smoke tests for training tasks (sine wave, addition, XOR)

Architecture Diagram

                          ┌─────┐
    c_{t-1} ─────────────→│  ⊗  │────────────────────→ c_t
                          └──┬──┘
                             │
    [x_t, h_{t-1}] ──┬─→ VQC₁ → σ ─→ f_t ─┘
                     ├─→ VQC₂ → σ ─→ i_t ─┬→ ⊗ ─┬→ ⊕
                     ├─→ VQC₃ → tanh → c̃_t ┘    │
                     └─→ VQC₄ → σ ─→ o_t ─┬→ ⊗ ─┤
                                          │      │
                                    tanh(c_t) ←──┘
                                          │
                                          └─→ VQC₅ ──→ h_t ──→ VQC₆ ──→ y_t

References


Note

Introduces a Quantum ML stack centered on a 6‑VQC QLSTM, plus visualization, examples, and tests.

  • Adds src/qml/ with qlstm (6‑VQC cell, QLSTM layer, trainer, MSE/cross‑entropy losses), vqc (VQC types/builders/forward), and data_encoding (Angle/Amplitude/IQP)
  • New QLSTM/VQC visualization (src/vis/qlstm.rs): text and SVG diagrams; implements Visualizable and re-exports in vis
  • Example app examples/qlstm_demo.rs demonstrating forward pass, training, gradients, VQC types, and diagram generation
  • Comprehensive tests tests/test_qml_qlstm.rs for forward/stacked behavior, gradients, basic tasks, and losses
  • Public API: exports via src/lib.rs and src/prelude.rs

Written by Cursor Bugbot for commit becb5b2. This will update automatically on new commits. Configure here.

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
@zazabap zazabap self-assigned this Jan 17, 2026
@zazabap zazabap added the enhancement New feature or request label Jan 17, 2026
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

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
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

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.
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

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
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

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.
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

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
@zazabap zazabap merged commit ebc19a7 into main Jan 18, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments