Skip to main content

RAQun is a Python package for comparing ML algorithms

Project description

RAQun 🦝

RAQun is a Package that implements QML algorithms using PennyLane and scikit-learn. It provides a unified en intuitive interface to test and evaluate some QML algorithms for clustering and classification tasks.

Features

  • Quantum Algorithms:
    • QDBSCAN: Quantum Density-Based Spatial Clustering of Applications with Noise.
    • QEuclidean: Quantum Euclidean Distance based Classifier.
    • QHAC: Quantum Hierarchical Agglomerative Clustering.
    • QMeans: Quantum K-Means Clustering.
    • QSC: Quantum Spectral Clustering.
  • Quantum Components: Essential components for QML, including Eigen, InnerProduct, InnerProduct1R and VQE.

Requirements

  • Python >= 3.10
  • pennylane >= 0.44
  • pennylane-lightning >= 0.44
  • scikit-learn >= 1.8
  • numpy >= 2.4

Installation

You can intall this package from source code. Cloning the repo and installing it using pip:

git clone https://github.com/JoseArmandoNM/RAQun
cd RAQun
pip install .

Note: Use pip install -e . if you plan to modify the source code of the package (editable mode).

Quick Start Guide

In the following sections, we show how to use the elements of RAQun.

Utils

RAQun provides a set of utility functions to facilitate the implementation of QML algorithms.

The following examples show how to use the utility functions:

Maths Functions

from raqun.utils.maths import *
print(log_t(5))

counts = {'00': 524, '01': 200, '10': 112, '11': 112}

probsArr = probs(counts, 4)

dists = dist(probsArr)

print(dists)

Qun Functions

from raqun.utils.qun import *

ctrl = ctrlGen(5, 3)
print(ctrl)

mat = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
mat = np.array(mat)
paddedMat = padder(mat)
print(paddedMat)

Circuits

The following examples show how to use the quantum circuits provided by RAQun:

Inner Product Circuits

InnerProduct (Swap Test) and InnerProduct1R (Hadamard Test) calculate the inner product/distance between a single vector and a matrix of vectors.

from raqun.circuits import InnerProduct, InnerProduct1R
import numpy as np

vec = np.array([1, 2])
vecs = np.array([[1, 2], [2, 1], [0, 1]])

circ_swap = InnerProduct(vec, vecs)
counts_swap = circ_swap.qnode()
print("Swap Test counts:", counts_swap)

circ_hadamard = InnerProduct1R(vec, vecs)
counts_hadamard = circ_hadamard.qnode()
print("Hadamard Test counts:", counts_hadamard)

Eigen

The Eigen circuit uses Quantum Phase Estimation (QPE) to find the eigenvectors of a matrix.

from raqun.circuits import Eigen
import numpy as np

X = np.array([[1.0, 0.5], [0.5, 1.0]])

eigen = Eigen(X)
eigenvectors = eigen.vectors(k=1)
print("Eigenvectors from QPE:", eigenvectors)

VQE

The VQE circuit uses the Variational Quantum Eigensolver algorithm to find eigenvectors.

from raqun.circuits import VQE
import numpy as np

X = np.array([[1.0, 0.5], [0.5, 1.0]])

vqe = VQE(X)
optimal_vector = vqe.vqeOpt(iter=20)
print("Optimal state from VQE:", optimal_vector)

The following example shows how to use QDBSCAN with the RAQun package:

import numpy as np
from raqun.algorithms import QDBSCAN

# Create some test data
X = np.array([[1, 2], [1, 4], [1, 0], [10, 2], [10, 4], [10, 0]])

# Initialize and fit the QDBSCAN algorithm
qdbscan = QDBSCAN(eps=1.0, minSamples=2)
labels = qdbscan.fit(X)

print("Cluster labels:", labels)

Algorithms

The following examples show how to use the algorithms provided by RAQun:

QDBSCAN

The QDBSCAN algorithm is a quantum version of the Density-Based Spatial Clustering of Applications with Noise algorithm.

import numpy as np
from raqun.algorithms import QDBSCAN

# Create some test data
X = np.array([[1, 2], [1, 4], [1, 0], [10, 2], [10, 4], [10, 0]])

# Initialize and fit the QDBSCAN algorithm
qdbscan = QDBSCAN(eps=1.0, minSamples=2)
labels = qdbscan.fit(X)

print("Cluster labels:", labels)

QEuclidean

The QEuclidean algorithm is a quantum-hybrid Euclidean distance-based classifier.

from raqun.algorithms import QEuclidean
import numpy as np

# Training data and labels
X_train = np.array([[1, 2], [10, 10], [2, 1], [11, 12]])
y_train = np.array([0, 1, 0, 1])

# Initialize and train the QEuclidean model
model = QEuclidean(metric="hadamard")
model.fit(X_train, y_train)

# Predict a new sample
new_sample = np.array([2, 2])
prediction = model.predict(new_sample)
print("Predicted class:", prediction)

QHAC

The QHAC algorithm is a quantum-hybrid Agglomerative Hierarchical Clustering algorithm.

from raqun.algorithms import QHAC
import numpy as np

X = np.array([[1, 2], [1, 4], [10, 2], [10, 4]])

# Initialize the model for 2 clusters
qhac = QHAC(k=2, paramType='data')

# Fit and return labels using different linkages
labels = qhac.fit(X, 'single', 'complete')
print("Labels for single and complete linkages:\\n", labels)

QMeans

The QMeans algorithm is a quantum-hybrid K-Means clustering algorithm.

from raqun.algorithms import QMeans
import numpy as np

X = np.array([[1, 2], [1, 4], [10, 2], [10, 4]])

# Initialize for 2 clusters with a maximum of 50 iterations
qmeans = QMeans(k=2, maxIters=50, metric='hadamard')

labels = qmeans.fit(X)
print("Cluster labels:", labels)

QSC

The QSC algorithm is a quantum-hybrid Spectral Clustering algorithm.

from raqun.algorithms import QSC
import numpy as np

X = np.array([[1, 2], [1, 4], [10, 2], [10, 4]])

# Initialize for 2 clusters and neighborhood distance (eps) of 2.0
qsc = QSC(k=2, eps=2.0, paramType='data', eigen='classical')

labels = qsc.fit(X)
print("Cluster labels:", labels)

Desarrollo y Pruebas (Testing)

Para ejecutar el conjunto de pruebas y asegurarte de que todo funciona correctamente, puedes usar pytest:

pip install pytest
pytest tests/

Authors

License

This project is licensed under the terms of the license provided in the LICENSE file.

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

raqun-0.1.0.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

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

raqun-0.1.0-py3-none-any.whl (21.2 kB view details)

Uploaded Python 3

File details

Details for the file raqun-0.1.0.tar.gz.

File metadata

  • Download URL: raqun-0.1.0.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for raqun-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9437d173c615cfd61dcb6260f20d0cdefa3bc4a0abed1d6792a844dfe22a0731
MD5 67520022d4444d5e2c689e2fefe48bc3
BLAKE2b-256 13c074cb6cadfdf108b69e7a1ddb11ea6fd73d4e0add387736cac23d41912b10

See more details on using hashes here.

File details

Details for the file raqun-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: raqun-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 21.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for raqun-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b8ada1597cdf3b352ce126ea4f90b3f63fd1c20a3ad7db474e38d8ff3dba0239
MD5 148fb033e40e974f675a207c4351898f
BLAKE2b-256 ab5ec17cadfa91bd785f519e42ce0adcceb57bc7e5a1deaed73c72d70ee1596f

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