Skip to main content

A library to introduce Keras 3 support to PennyLane with full support for multi-backend training

Project description

PennyLane Keras Layer

A library to introduce Keras 3 support to PennyLane with full support for multi-backend training.

Overview

This package provides integration between PennyLane (a quantum computing framework) and Keras 3, enabling the use of quantum layers in neural networks with support for multiple backends.

Key Features

  • 🔄 Multi-Backend Support: Works seamlessly with JAX, TensorFlow, and PyTorch
  • 🎯 Easy Integration: Drop-in replacement for standard Keras layers
  • ⚡ High Performance: JIT compilation support with JAX backend
  • 💾 Serialization: Full support for model saving and loading
  • 🔧 Flexible Configuration: Customizable quantum circuit parameters
  • 📊 Data Re-Uploading: Implements powerful quantum ML paradigm

Installation

From source

git clone https://github.com/vinayak19th/pennylane-keras-layer.git
cd pennylane-keras-layer
pip install -e .

Development installation

For development, install with additional dependencies:

pip install -e ".[dev]"

Requirements

  • Python >= 3.8
  • PennyLane >= 0.30.0
  • Keras >= 3.0.0
  • NumPy >= 1.21.0

Quick Start

Option 1: Data Re-Uploading Circuit Layer

import os
os.environ["KERAS_BACKEND"] = "jax"  # Set backend before importing keras

import numpy as np
import keras
from pennylane_keras_layer import KerasDRCircuitLayer

# Create sample data
X_train = np.linspace(-np.pi, np.pi, 100).reshape(-1, 1)
y_train = np.sin(X_train)

# Create a Data Re-Uploading quantum layer
q_layer = KerasDRCircuitLayer(layers=3, scaling=1.0, num_wires=1)

# Build a model
model = keras.Sequential([
    keras.layers.Input(shape=(1,)),
    q_layer
])

# Compile and train
model.compile(optimizer="adam", loss="mse")
model.fit(X_train, y_train, epochs=50)

Option 2: Generic Circuit Layer with Custom QNode

import pennylane as qml
from pennylane_keras_layer import KerasCircuitLayer

# Define a custom quantum circuit
dev = qml.device('default.qubit', wires=2)

@qml.qnode(dev)
def custom_circuit(weights, inputs):
    qml.RX(inputs[0], wires=0)
    qml.Rot(*weights[0], wires=0)
    qml.CNOT(wires=[0, 1])
    qml.Rot(*weights[1], wires=1)
    return qml.expval(qml.PauliZ(0))

# Create a generic quantum layer
weight_shapes = {"weights": (2, 3)}
q_layer = KerasCircuitLayer(custom_circuit, weight_shapes, output_dim=1)

# Use in a Keras model
model = keras.Sequential([
    keras.layers.Input(shape=(1,)),
    q_layer
])

Documentation

Comprehensive documentation is available in the docs/ directory:

Examples

Check out the examples/ directory for usage examples:

# Run basic example
python examples/basic_example.py

# Run Fourier series approximation demo
python examples/fourier_series_example.py

Development

Running tests

The package includes a comprehensive test suite in the tests/ directory:

# Run all tests
pytest tests/

# Run with coverage
pytest tests/ --cov=pennylane_keras_layer --cov-report=term-missing

# Run specific test file
pytest tests/test_layer.py -v

# Run only integration tests
pytest tests/ -m integration

# Run tests with specific backend
pytest tests/ --backend=tensorflow  # or torch, or jax

Testing with different backends:

  • TensorFlow: pytest tests/ --backend=tensorflow (requires pip install tensorflow)
  • PyTorch: pytest tests/ --backend=torch (requires pip install torch)
  • JAX: pytest tests/ --backend=jax (requires pip install jax jaxlib)

See tests/README.md for detailed information about the test suite.

Code formatting

black pennylane_keras_layer/ tests/

Linting

flake8 pennylane_keras_layer/ tests/

Contributing

Contributions are welcome! Please see our Contributing Guide for:

  • How to report bugs
  • How to suggest features
  • Development setup
  • Code standards
  • Pull request process

Feel free to submit a Pull Request or open an issue!

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • PennyLane - Quantum machine learning framework
  • Keras - Deep learning API

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pennylane_keras3_layer-1.0.0.tar.gz (19.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pennylane_keras3_layer-1.0.0-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file pennylane_keras3_layer-1.0.0.tar.gz.

File metadata

  • Download URL: pennylane_keras3_layer-1.0.0.tar.gz
  • Upload date:
  • Size: 19.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for pennylane_keras3_layer-1.0.0.tar.gz
Algorithm Hash digest
SHA256 dcbdbbcceaa7653ef7a3b03244fbc59fade6342b743ce72eebd6114fccbb049e
MD5 286ade216b319fb27d0d1bae05a98c68
BLAKE2b-256 332ddcbc0dfb96c64498527dd21a2a1c54dabc356b59ae90a5a33e9a97edf32a

See more details on using hashes here.

File details

Details for the file pennylane_keras3_layer-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pennylane_keras3_layer-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 92b6c1fb9bfa164918866dcc552fe93e943ed0dbf01db947c950aca7016b6f99
MD5 3106f985c0e9428dfc258407be6af7ff
BLAKE2b-256 4fa207653c45d2f3e2fdb16c7b0b486d2dc4236f58f8290227ee53ac97fc5f9e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page