Skip to main content

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

Release files for pennylane-keras3-layer 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for pennylane-keras3-layer 1.1.0
File Interpreter ABI Platform
pennylane_keras3_layer-1.1.0-py3-none-any.whl Python 3 none any Details

Release files / pennylane_keras3_layer-1.1.0-py3-none-any.whl

Download URL pennylane_keras3_layer-1.1.0-py3-none-any.whl
Size 12.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cd016c07b80af8d0ef683b82e9fbcca6271cdd0c5f846e501571ebe06084e03e
BLAKE2b-256 checksum
How to use checksums
aef1e803cdef7518d3284104d727d95135f07d9e16282386a1b38c962753cc52
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.12

Release history Release notifications | RSS feed

This release

1.1.0 This release

1 release file

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page