Quantum-enhanced Local Outlier Factor algorithm
Project description
QuantumLOFClassifier – Inspired by Guo et al. (2023)
This repository provides an implementation partially inspired by the algorithm proposed in:
Ming-Chao Guo et al., “Quantum Algorithm for Unsupervised Anomaly Detection” arXiv:2304.08710 (2023)
📘 Overview
QuantumLOFClassifier is a quantum-enhanced Local Outlier Factor (LOF) anomaly detection classifier, using Hadamard-test quantum circuits to estimate inner products between vectors, which are then used to calculate pairwise distances for LOF scoring.
- ✅ Quantum LOF step using Qiskit 2.0.2 compatible circuits
- ✅ LOF score computation
- ✅ Dual downstream models for clean and noisy regions
- ⚠️ Currently lacks Grover-based quantum minimum/average steps
🧠 Algorithm Mapping to Paper (Guo 2023 - arXiv:2304.08710)
| Paper Section | Functionality | Implemented? | Notes |
|---|---|---|---|
| III-A Eq.(7) | Amplitude embedding | ✅ Yes | Implemented via sin² encoding: amplitudes = sin(x_i). |
| III-A Eq.(11) | Swap test ⟨x|y⟩ | ✅ Yes | Implemented manually using CSWAP gates and initialize(). |
| III-A Eq.(15) - Eq.(17) | Distance from inner product | ⚠ Partially | Uses d(x, y) = √(1 - ⟨x|y⟩) assuming normalized inputs (not amplitude-based overlap fidelity). |
| III-A Step 1.6–1.7 | Quantum Minimum Search | ❌ No | Classical sort is used instead of quantum minimum finding. |
| III-B | Quantum LRD (inverse of avg reach dist) | ❌ No | Classical averaging is used for local reachability density calculation. |
| Eq.(2), Eq.(28) | Grover-based anomaly extraction | ❌ No | Anomalies are detected classically using a threshold on LOF score (delta). |
🚀 Installation
pip install quantlof
🧪 Example Usage
from quantum_lof import QuantumLOFClassifier
clf = QuantumLOFClassifier(
n_neighbors=20,
delta=1.5,
quantum_backend='qiskit_simulator', # or actual IBM backend like 'ibm_cairo'
shots=1024,
random_state=42
)
clf.detect_anomalies(X, y)
print(clf.lof_scores_)
anom_idx = clf.get_anomaly_indices()
clean_idx = clf.get_clean_indices()
print("Anomalies indices:", anom_idx)
print("Clean indices:", clean_idx)
⚙️ Core Features
- ✅ Hadamard-test for ⟨x|y⟩ inner products
- ✅ Euclidean distance via Eq. (15–17)
- ✅ k-distance via quantum estimation
- ✅ Local Reachability Density (LRD)
- ✅ LOF scores (Eq. 18) with thresholding
- ✅ Clean/noise classification downstream
- ✅ Fallback to classical when
n > maxsample_for_quantum
🛠️ API
QuantumLOFClassifier(...)
| Argument | Description |
|---|---|
n_neighbors |
Number of neighbors for LOF |
delta |
LOF threshold (LOF ≥ δ → anomaly) |
quantum_backend |
Qiskit backend (e.g. "qiskit_simulator", "ibm_cairo") |
shots |
Number of shots in Hadamard test |
maxsample_for_quantum |
Fallback threshold for classical mode |
clean_model |
Classifier for clean samples (default: SVM) |
noise_model |
Classifier for all samples (default: RandomForest) |
🤖 Implementation Notes
- The quantum inner product estimation uses Hadamard test with controlled inverse
StatePreparation. - Classical LOF score is maintained to allow hybrid quantum–classical behavior.
- The quantum part only replaces pairwise distance calculations.
📜 License
MIT License © 2025 Yuto Mueller
💡 Future Work
- Grover-based minimum search
- QRAM emulation
- Amplitude estimation for LOF
- GPU-accelerated classical fallback
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file quantlof-0.1.5.tar.gz.
File metadata
- Download URL: quantlof-0.1.5.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d8d2cfb0908fbe82f35ecb6bdb771b8ed9e9027063c0b22c03ba552870fc3991
|
|
| MD5 |
1cd37a35bc1cc564a7d1c774cb598af3
|
|
| BLAKE2b-256 |
8962fd292e819be8d61563247fa52b3b4101eeaf3b3ad2834ff4da5ece908ee4
|
File details
Details for the file quantlof-0.1.5-py3-none-any.whl.
File metadata
- Download URL: quantlof-0.1.5-py3-none-any.whl
- Upload date:
- Size: 7.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3721228fbf341054830eb1fd90345c672cebd3a8abf57ed2854b212889fba08c
|
|
| MD5 |
e310413b47b8ef4f5276d447f00afd53
|
|
| BLAKE2b-256 |
446718e1240157748a2090ae223af206ac40211217409d18d793889dee73820b
|