Giter Site home page Giter Site logo

qmeans's Introduction

GitHub license Linter codecov

Clustering example
Q-Means algorithm implementation using Qiskit compatible with Scikit-Learn.

The q-means leverages quantum computing to calculate distances for the centroid assignment part of the k-means unsupervised learning algorithm. It shares the same general steps its classical counterpart has, and is used alongside quantum simulators and quantum devices. This implementation uses Qiskit and is compatible with the scikit-learn library in order to exploit the capabilities for machine learning offered by scikit-learn. You can use the q-means in the same way you would use the k-means, and many options are available for data encoding and job batching.

❗ Job batching is introduced as a way to speed up training time on quantum devices for larger datasets. [2]

Installation

The project is publicly available in PyPI and can be installed using the following command:

pip install qmeans

Usage

Example code for usage of the qmeans algorithm:

import numpy as np
import pandas as pd
from qmeans.qkmeans import *

backend = AerSimulator()
X = pd.DataFrame(np.array([[1, 2], [1, 4], [1, 0], [10, 2], [10, 4], [10, 0]]))
q_means = QuantumKMeans(backend, n_clusters=2, verbose=True)
q_means.fit(X)
print(q_means.labels_)

Requirements

Python 3.6+

Q-Means uses Qiskit for quantum computations, Scikit-Learn for data preparation and estimator formatting, and Pandas and Numpy for data manipulation.

Publications

[1]: D. Quiroga, P. Date and R. Pooser, "Discriminating Quantum States with Quantum Machine Learning," 2021 IEEE International Conference on Quantum Computing and Engineering (QCE), 2021, pp. 481-482, doi: 10.1109/QCE52317.2021.00088.

[2]: D. Quiroga, P. Date and R. Pooser, "Discriminating Quantum States with Quantum Machine Learning," 2021 International Conference on Rebooting Computing (ICRC), 2021, pp. 56-63, doi: 10.1109/ICRC53822.2021.00018.

[3]: D. Quiroga, J. Botia, "Q-means clustering coherent noise tolerance analysis," International Congress EXPOIngenieria, 2022, pp. 437-443.

License

This source code is free and open source, released under the Apache License, Version 2.0.

qmeans's People

Contributors

raijeku avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

qmeans's Issues

QiskitError: "Cannot unroll the circuit to the given basis

I'm attempting to run the usage example but I want to use the ibmq_qasm_simulator instead of the aer_simulator_statevector

The code:

backend = provider.get_backend("ibmq_qasm_simulator")
X = pd.DataFrame(np.array([[1, 2], [1, 4], [1, 0], [10, 2], [10, 4], [10, 0]]))
qk_means = QuantumKMeans(backend, n_clusters=2, verbose=True)
qk_means.fit(X)
print(qk_means.labels_)

The error message:

Data is:     0  1
0   1  2
1   1  4
2   1  0
3  10  2
4  10  4
5  10  0
Iteration 0
Output exceeds the [size limit](command:workbench.action.openSettings?%5B%22notebook.output.textLineLimit%22%5D). Open the full output data [in a text editor](command:workbench.action.openLargeOutput?5a07abf9-11ba-4018-b8a5-3504edf05547)---------------------------------------------------------------------------
QiskitError                               Traceback (most recent call last)
Cell In[10], line 4
      2 X = pd.DataFrame(np.array([[1, 2], [1, 4], [1, 0], [10, 2], [10, 4], [10, 0]]))
      3 qk_means = QuantumKMeans(backend, n_clusters=2, verbose=True)
----> 4 qk_means.fit(X)
      5 print(qk_means.labels_)

File [~/sw/k-means/.env/lib/python3.10/site-packages/qmeans/qkmeans.py:794](https://file+.vscode-resource.vscode-cdn.net/Users/james/sw/k-means/~/sw/k-means/.env/lib/python3.10/site-packages/qmeans/qkmeans.py:794), in QuantumKMeans.fit(self, X, y, batch)
    792 else: 
    793     if self.map_type == 'probability':
--> 794         distances = np.asarray([[distance(point,centroid,self.backend,self.map_type,self.shots,np.array([norms[i],cluster_norms[j]]),noise_model=self.noise_model) for i, point in X.iterrows()] for j, centroid in normalized_clusters.iterrows()])
    795     elif self.map_type == 'angle':
    796         distances = np.asarray([[distance(point,centroid,self.backend,self.map_type,self.shots,np.array([1,1]),self.norm_relevance,noise_model=self.noise_model) for i, point in X.iterrows()] for j, centroid in normalized_clusters.iterrows()])

File [~/sw/k-means/.env/lib/python3.10/site-packages/qmeans/qkmeans.py:794](https://file+.vscode-resource.vscode-cdn.net/Users/james/sw/k-means/~/sw/k-means/.env/lib/python3.10/site-packages/qmeans/qkmeans.py:794), in (.0)
    792 else: 
    793     if self.map_type == 'probability':
--> 794         distances = np.asarray([[distance(point,centroid,self.backend,self.map_type,self.shots,np.array([norms[i],cluster_norms[j]]),noise_model=self.noise_model) for i, point in X.iterrows()] for j, centroid in normalized_clusters.iterrows()])
    795     elif self.map_type == 'angle':
    796         distances = np.asarray([[distance(point,centroid,self.backend,self.map_type,self.shots,np.array([1,1]),self.norm_relevance,noise_model=self.noise_model) for i, point in X.iterrows()] for j, centroid in normalized_clusters.iterrows()])

File [~/sw/k-means/.env/lib/python3.10/site-packages/qmeans/qkmeans.py:794](https://file+.vscode-resource.vscode-cdn.net/Users/james/sw/k-means/~/sw/k-means/.env/lib/python3.10/site-packages/qmeans/qkmeans.py:794), in (.0)
    792 else: 
    793     if self.map_type == 'probability':
...
    108 unrolled_dag = UnrollCustomDefinitions(
    109     self._equiv_lib, self._basis_gates, target=self._target
    110 ).run(decomposition)

QiskitError: "Cannot unroll the circuit to the given basis, ['u1', 'u2', 'u3', 'u', 'p', 'r', 'rx', 'ry', 'rz', 'cx', 'cy', 'cz', 'csx', 'cp', 'id', 'x', 'y', 'z', 'h', 's', 'sdg', 'sx', 't', 'tdg', 'swap', 'ccx', 'cswap', 'unitary', 'diagonal', 'initialize', 'cu1', 'cu2', 'cu3', 'rxx', 'ryy', 'rzz', 'rzx', 'mcx', 'mcy', 'mcz', 'mcsx', 'mcp', 'mcu1', 'mcu2', 'mcu3', 'mcrx', 'mcry', 'mcrz', 'mcr', 'mcswap', 'multiplexer', 'kraus', 'delay', 'roerror', 'measure']. Instruction reset not found in equivalence library and no rule found to expand."

TypeError: '<=' between instances of 'str' and 'int'

Hello.
I'm trying out your module, for which I thank you, and got a curious error that can't figure out on my own:

I'm using IBM Quantum lab online, and this is what I am doing:

X = pd.read_csv('../Datos/bankmarketing-101.csv', sep=';', usecols=['age', 'job', 'marital'])
# Change categorical data to number
X["marital"] = pd.Categorical(X["marital"])
X["marital"] = X["marital"].cat.codes
# Change categorical data to number 
X["job"] = pd.Categorical(X["job"])
X["job"] = X["job"].cat.codes

# Procesamiento
qk_means = QuantumKMeans(dev, n_clusters=3, verbose=True)
qk_means.fit(X)

An then I get the error:

[100 rows x 3 columns]
Iteration 0
Traceback (most recent call last):
  Input In [11] in <cell line: 30>
    qk_means.fit(X)
  File /opt/conda/lib/python3.8/site-packages/qmeans/qkmeans.py:756 in fit
    else: distances = np.asarray([[distance(point,centroid,self.backend,self.map_type,self.shots,np.array([norms[i],cluster_norms[j]])) for i, point in X.iterrows()] for j, centroid in normalized_clusters.iterrows()])
  File /opt/conda/lib/python3.8/site-packages/qmeans/qkmeans.py:756 in <listcomp>
    else: distances = np.asarray([[distance(point,centroid,self.backend,self.map_type,self.shots,np.array([norms[i],cluster_norms[j]])) for i, point in X.iterrows()] for j, centroid in normalized_clusters.iterrows()])
  File /opt/conda/lib/python3.8/site-packages/qmeans/qkmeans.py:756 in <listcomp>
    else: distances = np.asarray([[distance(point,centroid,self.backend,self.map_type,self.shots,np.array([norms[i],cluster_norms[j]])) for i, point in X.iterrows()] for j, centroid in normalized_clusters.iterrows()])
  File /opt/conda/lib/python3.8/site-packages/qmeans/qkmeans.py:194 in distance
    job = execute(qc,backend=backend, shots=shots)
  File /opt/conda/lib/python3.8/site-packages/qiskit/execute_function.py:299 in execute
    experiments = transpile(
  File /opt/conda/lib/python3.8/site-packages/qiskit/compiler/transpiler.py:327 in transpile
    unique_transpile_args, shared_args = _parse_transpile_args(
  File /opt/conda/lib/python3.8/site-packages/qiskit/compiler/transpiler.py:648 in _parse_transpile_args
    basis_gates = _parse_basis_gates(basis_gates, backend)
  File /opt/conda/lib/python3.8/site-packages/qiskit/compiler/transpiler.py:762 in _parse_basis_gates
    if backend_version <= 1:
TypeError: '<=' not supported between instances of 'str' and 'int'```

I don't see any string in the data, also searched the internet for similar problems similar, to no avail. So I ask you: can you see what is that I'm doing wrong?

Thanks in advance.
--
Nicolás.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.