Metadata-Version: 2.4
Name: qkan-cheb
Version: 0.1.0
Summary: Quantum Kolmogorov-Arnold Networks, implemented in Qiskit
Author-email: Akshat Tripathi <st179052@gmail.com>
License: MIT License
        
        Copyright (c) 2026  AKSHAT TRIPATHI
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
Project-URL: Homepage, https://github.com/Akshat-911/qkan
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: qiskit>=1.0
Requires-Dist: qiskit-aer
Requires-Dist: numpy
Requires-Dist: scipy
Dynamic: license-file

# QKAN — Quantum Kolmogorov-Arnold Networks in Qiskit

A Python/Qiskit implementation of Quantum Kolmogorov-Arnold Networks (QKAN),
based on the block-encoding and Quantum Singular Value Transformation (QSVT)
construction described in:

> Ivashkov et al., *Quantum Kolmogorov-Arnold Networks* (arXiv preprint).

This library implements the full forward-pass construction from the paper —
diagonal block-encoding of classical inputs, dilation, Chebyshev polynomial
application via QSVT, weighted products, linear combination of unitaries
(LCU), and the final node-summation step — as composable, independently
tested Qiskit circuits. It also includes a gradient-free training pipeline
for fitting QKAN models to small target problems.

This is a research/coursework implementation intended for small-scale
verification and experimentation, not a production or performance-optimized
library.

## Installation

```bash
pip install qkan
```

Or, for local development:

```bash
git clone https://github.com/<your-username>/qkan.git
cd qkan
pip install -e ".[dev]"
```

## What's implemented

**Forward pass (Theorem 4 / Equation 5 of the paper):**
- `BlockEncoding` — a wrapper class carrying a Qiskit circuit plus its
  block-encoding metadata (system qubits, ancilla qubits, normalization,
  error bound)
- `diagonal_block_encoding` — encodes a classical vector as a diagonal
  block-encoding (Lemma 10)
- `dilate` — repeats each diagonal entry K times (Step 1 of a QKAN layer)
- `chebyshev_block_encoding` — applies a Chebyshev polynomial via QSVT,
  using the closed-form phase angles from Lemma 8
- `product` — multiplies two block-encodings together (Lemma 6)
- `lcu` — linear combination of block-encodings (Lemma 5)
- `sum_over_p` — sums a layer's output over input nodes via a Hadamard
  sandwich
- `cheb_qkan_layer` / `cheb_qkan_layer_from_be` — assembles a full CHEB-QKAN
  layer, with the latter supporting layer stacking
- `qkan_forward` — chains multiple layers into a full multi-layer QKAN model

**Training (gradient-free):**
- `qkan_loss`, `qkan_loss_multi`, `qkan_loss_batch` — mean-squared-error
  loss functions for single-example, scalar-batch, and vector-batch training
- `flatten_weights` / `unflatten_weights` — utilities for use with
  `scipy.optimize`
- Training uses Nelder-Mead (gradient-free) optimization, since weights are
  encoded as circuit rotation parameters rather than autodiff-compatible
  variables

## Quick example

```python
import numpy as np
from qkan.model.qkan_model import qkan_forward

# A single CHEB-QKAN layer: 2 inputs, 2 outputs, Chebyshev degree 1
x = np.array([0.6, 0.8])
weights_list = [np.random.uniform(-0.3, 0.3, size=(2, 4))]  # (degree+1, N*M)

be_output = qkan_forward(x, weights_list, degree=1)
prediction = np.diag(be_output.to_matrix())
print(prediction)
```

Training example (fitting a small target vector):

```python
from scipy.optimize import minimize
from qkan.training.utils import flatten_weights, unflatten_weights
from qkan.training.loss import qkan_loss

x = np.array([0.6, 0.8])
target = np.array([0.5, 0.2])
degree = 0

initial_weights = [np.random.uniform(-0.3, 0.3, size=(1, 4))]
shapes = [w.shape for w in initial_weights]
flat = flatten_weights(initial_weights)

result = minimize(qkan_loss, flat, args=(shapes, x, target, degree),
                   method="Nelder-Mead")
trained_weights = [np.tanh(w) for w in unflatten_weights(result.x, shapes)]
```

## Known limitations

- `N` (input width) and `M` (output width per layer) must be powers of 2,
  due to the `dilate` and `diagonal_block_encoding` implementations.
- Verification relies on `BlockEncoding.to_matrix()`, which computes the
  full dense unitary of a circuit — this is only tractable for small qubit
  counts (roughly up to ~10-12 qubits), and is intended purely for
  correctness checking, not production use.
- Training uses gradient-free optimization (Nelder-Mead), not analytic
  gradients (e.g. the parameter-shift rule) — this is simpler to implement
  but scales less efficiently with the number of parameters.
- Tested at small scale (a handful of input/output nodes, 1-2 layers, low
  Chebyshev degree); the algorithmic construction itself is scale-general
  and paper-faithful, but has not been verified at larger scale due to
  classical simulation cost.

## Running tests

```bash
pip install -e ".[dev]"
pytest
```

## Reference

Ivashkov et al., *Quantum Kolmogorov-Arnold Networks*.

## License

MIT — see [LICENSE](LICENSE).
