Machine learning (quantum-)algorithms with qiskit as basis
Project description
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_aqml
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.
Support
Source Code: https://github.com/carstenblank/dc-qiskit-qml
Issue Tracker: https://github.com/carstenblank/dc-qiskit-qml/issues
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.
Project details
Release history Release notifications | RSS feed
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
File details
Details for the file dc_qiskit_qml-0.0.3.tar.gz
.
File metadata
- Download URL: dc_qiskit_qml-0.0.3.tar.gz
- Upload date:
- Size: 16.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 145f3401f1ff0e580898622e427aced4c2055464a819cc787b4ad103985c3b23 |
|
MD5 | a19f30ca9930922d4d0a4818ac03f1eb |
|
BLAKE2b-256 | e04afa88841e3c9e159f6fcdb96370d32d48600bef7dca5d5893994923d3feaf |
File details
Details for the file dc_qiskit_qml-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: dc_qiskit_qml-0.0.3-py3-none-any.whl
- Upload date:
- Size: 38.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 11cdef8a480bc128b3337d7d0d6000b996983701356e826449db7ddda30ea85d |
|
MD5 | b18b46272faf68d40c7403b9083ff6b7 |
|
BLAKE2b-256 | 16562bc20fbe57cda52585feb27a21fd2a0fc9d0c28c309905c276ce3c6f629e |