Qlearnkit is a python library implementing some fundamental Machine Learning models and algorithms for a gated quantum computer, built on top of Qiskit and, optionally, Pennylane.
We recommend installing qlearnkit with pip
pip install qlearnkitNote: pip will install the latest stable qlearnkit. However, the main branch of qlearnkit is in (not so active) development. If you want to test the latest scripts or functions please refer to development notes.
Via pip, you can install qlearnkit with the optional extension
packages dependent on pennylane. To do so, run
pip install qlearnkit['pennylane']You can also use qlearnkit via Docker building the image from the provided Dockerfile
docker build -t qlearnkit -f docker/Dockerfile .then you can use it like this
docker run -it --rm -v $PWD:/tmp -w /tmp qlearnkit python ./script.pyNow that Qlearnkit is installed, it's time to begin working with the Machine Learning module. Let's try an experiment using the QKNN Classifier algorithm to train and test samples from a data set to see how accurately the test set can be classified.
from qlearnkit.algorithms import QKNeighborsClassifier
from qlearnkit.encodings import AmplitudeEncoding
from qiskit import BasicAer
from qiskit.utils import QuantumInstance, algorithm_globals
from qlearnkit.datasets import load_iris
seed = 42
algorithm_globals.random_seed = seed
train_size = 32
test_size = 8
n_features = 4 # all features
# Use iris data set for training and test data
X_train, X_test, y_train, y_test = load_iris(train_size, test_size, n_features)
quantum_instance = QuantumInstance(BasicAer.get_backend('qasm_simulator'),
shots=1024,
optimization_level=1,
seed_simulator=seed,
seed_transpiler=seed)
encoding_map = AmplitudeEncoding(n_features=n_features)
qknn = QKNeighborsClassifier(
n_neighbors=3,
quantum_instance=quantum_instance,
encoding_map=encoding_map
)
qknn.fit(X_train, y_train)
print(f"Testing accuracy: "
f"{qknn.score(X_test, y_test):0.2f}")The documentation is available here.
Alternatively, you can build and browse it locally as follows:
first make sure to have pandoc installed
sudo apt install pandocthen run
make docthen simply open docs/_build/index.html with your favourite browser, e.g.
brave docs/_build/index.htmlAfter cloning this repository, create a virtual environment
python3 -m venv .venvand activate it
source .venv/bin/activatenow you can install the requirements
pip install -r requirements-dev.txtnow run the tests
make testMake sure to run
pre-commit installto set up the git hook scripts. Now pre-commit will run automatically on git commit!
The Quantum LSTM model is adapted from this article from Riccardio Di Sipio, but the Quantum part has been changed entirely according to the architecture described in this paper.
The project is licensed under the Apache License 2.0.