Skip to content

kilickursat/dc-qiskit-qml

 
 

Repository files navigation

Data Cybernetics qiskit-qml

Travis

Codecov coverage

Codacy grade

Read the Docs

PyPI

PyPI - Python Version

qiskit is an open-source compilation framework capable of targeting various types of hardware and a high-performance quantum computer simulator with emulation capabilities and various compiler plug-ins.

This library implements so far one quantum machine learning classifier which has been introduced by F.Petruccione, M. Schuld and M. Fingerhuth (http://stacks.iop.org/0295-5075/119/i=6/a=60002). Athough this is the only classifier implemented so far, this library is to be used as a repository for more classifiers using qiskit as a background framework.

Features

  • Distance & Majority based Hadamard-gate classifier

    • Generic real valued vector space input data (slow)
    • Binary valued vector space input data (faster)
    • Feature map pre-processing for non-linear classification

Installation

This library requires Python version 3.5 and above, as well as qiskit. Installation of this library, as well as all dependencies, can be done using pip:

$ python -m pip install dc_qiskit_qml

To test that the algorithms are working correctly you can run

$ make test

Getting started

You can check out the classifier as follows

import numpy as np
from sklearn.datasets import load_iris
from sklearn.preprocessing import StandardScaler, Normalizer
from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline

import qiskit

from dc_qiskit_qml.feature_maps import NormedAmplitudeEncoding
from dc_qiskit_qml.distance_based.hadamard import QmlHadamardNeighborClassifier
from dc_qiskit_qml.distance_based.hadamard.state import QmlGenericStateCircuitBuilder
from dc_qiskit_qml.distance_based.hadamard.state.sparsevector import MöttönenStatePreparation

X, y = load_iris(True)
# Only the first two features and only get two labels
# This is a toy example!
X = np.asarray([x[0:2] for x, yy in zip(X, y) if yy != 2])
y = np.asarray([yy for x, yy in zip(X, y) if yy != 2])

preprocessing_pipeline = Pipeline([
    ('scaler',  StandardScaler()),
    ('l2norm', Normalizer(norm='l2', copy=True))
])
X = preprocessing_pipeline.fit_transform(X, y)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.10)

# Using the generic wave function (state vector) routine using the 'Möttönen'
# state preparation algorithm
initial_state_builder = QmlGenericStateCircuitBuilder(MöttönenStatePreparation())

# The normed amplitude encoding ensures that the data is normalized
# This is a somewhat unnecessary step as above we do that already
feature_map = NormedAmplitudeEncoding()

execution_backend: BaseBackend = qiskit.Aer.get_backend('qasm_simulator')
qml = QmlHadamardNeighborClassifier(backend=execution_backend,
                                    shots=8192,
                                    classifier_circuit_factory=initial_state_builder,
                                    feature_map=feature_map)

qml.fit(X_train, y_train)
prediction = qml.predict(X_test)

"Test Accuracy: {}".format(
    sum([1 if p == t else 0 for p, t in zip(prediction, y_test)])/len(prediction)
)

prediction_train = qml.predict(X_train)
"Train Accuracy: {}".format(
    sum([1 if p == t else 0 for p, t in zip(prediction_train, y_train)])/len(prediction_train)
)

The details are a bit more involved as to how this works and the classifier can be configured with a circuit factory or a feature map.

Please refer to the documentation of the dc qiskit qml library .

Contributing

We welcome contributions - simply fork the repository of this plugin, and then make a pull request containing your contribution. All contributers to this plugin will be listed as authors on the releases.

We also encourage bug reports, suggestions for new features and enhancements, and even links to cool projects or applications built on PennyLane.

Authors

Carsten Blank

Support

If you are having issues, please let us know by posting the issue on our Github issue tracker.

License

The data cybernetics qiskit algorithms plugin is free and open source, released under the Apache License, Version 2.0.

About

Quantum Machine Learning algorithms

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 98.7%
  • Makefile 1.3%