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.

PyPI

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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

pennylane_keras3_layer-1.1.0-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for pennylane_keras3_layer-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cd016c07b80af8d0ef683b82e9fbcca6271cdd0c5f846e501571ebe06084e03e
MD5 ce7bccffef1eb77573271b83876f6b24
BLAKE2b-256 aef1e803cdef7518d3284104d727d95135f07d9e16282386a1b38c962753cc52

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