Qiskit Machine Learning: A library of quantum computing machine learning experiments
Project description
Qiskit Machine Learning
What is Qiskit Machine Learning?
Qiskit Machine Learning introduces fundamental computational building blocks, such as Quantum Kernels and Quantum Neural Networks, used in various applications including classification and regression.
This library is part of the Qiskit Community ecosystem, a collection of high-level codes that are based
on the Qiskit software development kit. As of version 0.7
, Qiskit Machine Learning is co-maintained
by IBM and the Hartree Center, part of the UK Science and
Technologies Facilities Council (STFC).
The Qiskit Machine Learning framework aims to be:
- User-friendly, allowing users to quickly and easily prototype quantum machine learning models without the need of extensive quantum computing knowledge.
- Flexible, providing tools and functionalities to conduct proof-of-concepts and innovative research in quantum machine learning for both beginners and experts.
- Extensible, facilitating the integration of new cutting-edge features leveraging Qiskit's architectures, patterns and related services.
What are the main features of Qiskit Machine Learning?
Kernel-based methods
The FidelityQuantumKernel
class uses the Fidelity
)
algorithm. It computes kernel matrices for datasets and can be combined with a Quantum Support Vector Classifier (QSVC
)
or a Quantum Support Vector Regressor (QSVR
)
to solve classification or regression problems respectively. It is also compatible with classical kernel-based machine learning algorithms.
Quantum Neural Networks (QNNs)
Qiskit Machine Learning defines a generic interface for neural networks, implemented by two core (derived) primitives:
-
EstimatorQNN
: Leverages theEstimator
primitive, combining parametrized quantum circuits with quantum mechanical observables. The output is the expected value of the observable. -
SamplerQNN
: Leverages theSampler
primitive, translating bit-string counts into the desired outputs.
To train and use neural networks, Qiskit Machine Learning provides learning algorithms such as the NeuralNetworkClassifier
and NeuralNetworkRegressor
.
Finally, built on these, the Variational Quantum Classifier (VQC
)
and the Variational Quantum Regressor (VQR
)
take a feature map and an ansatz to construct the underlying QNN automatically using high-level syntax.
Integration with PyTorch
The TorchConnector
integrates QNNs with PyTorch.
Thanks to the gradient algorithms in Qiskit Machine Learning, this includes automatic differentiation.
The overall gradients computed by PyTorch during the backpropagation take into account quantum neural
networks, too. The flexible design also allows the building of connectors to other packages in the future.
Installation and documentation
We encourage installing Qiskit Machine Learning via the pip
tool, a Python
package manager.
pip install qiskit-machine-learning
pip
will install all dependencies automatically, so that you will always have the most recent
stable version.
If you want to work instead on the very latest work-in-progress versions of Qiskit Machine Learning, either to try features ahead of their official release or if you want to contribute to the library, then you can install from source. For more details on how to do so and much more, follow the instructions in the documentation.
Optional Installs
-
PyTorch may be installed either using command
pip install 'qiskit-machine-learning[torch]'
to install the package or refer to PyTorch getting started. When PyTorch is installed, theTorchConnector
facilitates its use of quantum computed networks. -
Sparse may be installed using command
pip install 'qiskit-machine-learning[sparse]'
to install the package. Sparse being installed will enable the usage of sparse arrays and tensors. -
NLopt is required for the global optimizers.
NLopt
can be installed manually withpip install nlopt
on Windows and Linux platforms, or withbrew install nlopt
on MacOS using the Homebrew package manager. For more information, refer to the installation guide.
Migration to Qiskit 1.x
[!NOTE] Qiskit Machine Learning depends on Qiskit, which will be automatically installed as a dependency when you install Qiskit Machine Learning. From version
0.8
of Qiskit Machine Learning, Qiskit1.0
or above will be required. If you have a pre-1.0
version of Qiskit installed in your environment (however it was installed), you should upgrade to1.x
to continue using the latest features. You may refer to the official Qiskit 1.0 Migration Guide for detailed instructions and examples on how to upgrade Qiskit.
Creating Your First Machine Learning Programming Experiment in Qiskit
Now that Qiskit Machine Learning is installed, it's time to begin working with the Machine Learning module. Let's try an experiment using VQC (Variational Quantum Classifier) algorithm to train and test samples from a data set to see how accurately the test set can be classified.
from qiskit.circuit.library import TwoLocal, ZZFeatureMap
from qiskit_machine_learning.optimizers import COBYLA
from qiskit_machine_learning.utils import algorithm_globals
from qiskit_machine_learning.algorithms import VQC
from qiskit_machine_learning.datasets import ad_hoc_data
seed = 1376
algorithm_globals.random_seed = seed
# Use ad hoc data set for training and test data
feature_dim = 2 # dimension of each data point
training_size = 20
test_size = 10
# training features, training labels, test features, test labels as np.ndarray,
# one hot encoding for labels
training_features, training_labels, test_features, test_labels = ad_hoc_data(
training_size=training_size, test_size=test_size, n=feature_dim, gap=0.3
)
feature_map = ZZFeatureMap(feature_dimension=feature_dim, reps=2, entanglement="linear")
ansatz = TwoLocal(feature_map.num_qubits, ["ry", "rz"], "cz", reps=3)
vqc = VQC(
feature_map=feature_map,
ansatz=ansatz,
optimizer=COBYLA(maxiter=100),
)
vqc.fit(training_features, training_labels)
score = vqc.score(test_features, test_labels)
print(f"Testing accuracy: {score:0.2f}")
More examples
Learning path notebooks may be found in the Machine Learning tutorials section of the documentation and are a great place to start.
Another good place to learn the fundamentals of quantum machine learning is the Quantum Machine Learning notebooks from the original Qiskit Textbook. The notebooks are convenient for beginners who are eager to learn quantum machine learning from scratch, as well as understand the background and theory behind algorithms in Qiskit Machine Learning. The notebooks cover a variety of topics to build understanding of parameterized circuits, data encoding, variational algorithms etc., and in the end the ultimate goal of machine learning - how to build and train quantum ML models for supervised and unsupervised learning. The Textbook notebooks are complementary to the tutorials of this module; whereas these tutorials focus on actual Qiskit Machine Learning algorithms, the Textbook notebooks more explain and detail underlying fundamentals of quantum machine learning.
How can I contribute?
If you'd like to contribute to Qiskit, please take a look at our contribution guidelines. This project adheres to the Qiskit code of conduct. By participating, you are expected to uphold this code.
We use GitHub issues for tracking requests and bugs. Please
join the Qiskit Slack community
and use the #qiskit-machine-learning
channel for discussions and short questions.
For questions that are more suited for a forum, you can use the Qiskit tag in [Stack Overflow]
(https://stackoverflow.com/questions/tagged/qiskit).
Humans behind Qiskit Machine Learning
Qiskit Machine Learning was inspired, authored and brought about by the collective work of a team of researchers and software engineers. This library continues to grow with the help and work of many people, who contribute to the project at different levels.
How can I cite Qiskit Machine Learning?
If you use Qiskit, please cite as per the provided BibTeX file.
License
This project uses the Apache License 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 qiskit_machine_learning-0.8.0.tar.gz
.
File metadata
- Download URL: qiskit_machine_learning-0.8.0.tar.gz
- Upload date:
- Size: 216.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f7a4856e066340d49679f196eb624ee36bc916d5b46b3a9a31a8bc70b9a3f21f |
|
MD5 | c6f838d535fea9ca09854f689f17ad03 |
|
BLAKE2b-256 | e4344b810fee56a8601bc5bd0f4c87edb266ac2ec8125bd5cccdb4063768ea01 |
Provenance
The following attestation bundles were made for qiskit_machine_learning-0.8.0.tar.gz
:
Publisher:
deploy-code.yml
on qiskit-community/qiskit-machine-learning
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
qiskit_machine_learning-0.8.0.tar.gz
- Subject digest:
f7a4856e066340d49679f196eb624ee36bc916d5b46b3a9a31a8bc70b9a3f21f
- Sigstore transparency entry: 148154470
- Sigstore integration time:
- Predicate type:
File details
Details for the file qiskit_machine_learning-0.8.0-py3-none-any.whl
.
File metadata
- Download URL: qiskit_machine_learning-0.8.0-py3-none-any.whl
- Upload date:
- Size: 237.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e204c9cd75c322584005854702612dc40e1d45616f6020e8cd9ab08dfe76246b |
|
MD5 | 3d52b7a16c8040f95a363b8409c4eaef |
|
BLAKE2b-256 | 2cb9173506b0818b982acac3aa5d2fdff65025ece84f0725086319e6509e6ba1 |
Provenance
The following attestation bundles were made for qiskit_machine_learning-0.8.0-py3-none-any.whl
:
Publisher:
deploy-code.yml
on qiskit-community/qiskit-machine-learning
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
qiskit_machine_learning-0.8.0-py3-none-any.whl
- Subject digest:
e204c9cd75c322584005854702612dc40e1d45616f6020e8cd9ab08dfe76246b
- Sigstore transparency entry: 148154472
- Sigstore integration time:
- Predicate type: