Package: qiskit-machine-learning
Qiskit Machine LearningQiskit Machine Learning introduces fundamental computational building blocks - such as Quantum Kernels
and Quantum Neural Networks - used in different applications, including classification and regression.
On the one hand, this design is very easy to use and allows users to rapidly prototype a first model
without deep quantum computing knowledge. On the other hand, Qiskit Machine Learning is very flexible,
and users can easily extend it to support cutting-edge quantum machine learning research.Qiskit Machine Learning provides theFidelityQuantumKernelclass that makes use of theFidelityalgorithm introduced in Qiskit and can be easily used to directly compute
kernel matrices for given datasets or can be passed to a Quantum Support Vector ClassifierQSVCor
Quantum Support Vector RegressorQSVRto quickly start solving classification or regression problems.
It also can be used with many other existing kernel-based machine learning algorithms from established
classical frameworks.Qiskit Machine Learning defines a generic interface for neural networks that is implemented by different
quantum neural networks. Two core implementations are readily provided, such as theEstimatorQNN,
and theSamplerQNN.
TheEstimatorQNNleverages theEstimatorprimitive from Qiskit and
allows users to combine parametrized quantum circuits with quantum mechanical observables. The circuits can be constructed using, for example, building blocks
from Qiskit’s circuit library, and the QNN’s output is given by the expected value of the observable.
TheSamplerQNNleverages another primitive introduced in Qiskit, theSamplerprimitive.
This neural network translates quasi-probabilities of bitstrings estimated by the primitive into a desired output. This
translation step can be used to interpret a given bitstring in a particular context, e.g. translating it into a set of classes.The neural networks include the functionality to evaluate them for a given input as well as to compute the
corresponding gradients, which is important for efficient training. To train and use neural networks,
Qiskit Machine Learning provides a variety of learning algorithms such as theNeuralNetworkClassifierandNeuralNetworkRegressor.
Both take a QNN as input and then use it in a classification or regression context.
To allow an easy start, two convenience implementations are provided - the Variational Quantum ClassifierVQCas well as the Variational Quantum RegressorVQR.
Both take just a feature map and an ansatz and construct the underlying QNN automatically.In addition to the models provided directly in Qiskit Machine Learning, it has theTorchConnector,
which allows users to integrate all of our quantum neural networks directly into thePyTorchopen source machine learning library. Thanks to Qiskit’s gradient algorithms, this includes automatic
differentiation - the overall gradients computed byPyTorchduring the backpropagation take into
account quantum neural networks, too. The flexible design also allows the building of connectors
to other packages in the future.InstallationWe encourage installing Qiskit Machine Learning via the pip tool (a python package manager).pipinstallqiskit-machine-learningpipwill handle all dependencies automatically and you will always install the latest
(and well-tested) version.If you want to work on the very latest work-in-progress versions, either to try features ahead of
their official release or if you want to contribute to Machine Learning, then you can install from source.
To do this follow the instructions in thedocumentation.Optional InstallsPyTorch, may be installed either using commandpip install 'qiskit-machine-learning[torch]'to install the
package or refer to PyTorchgetting started. When PyTorch
is installed, theTorchConnectorfacilitates its use of quantum computed networks.Sparse, may be installed using commandpip install 'qiskit-machine-learning[sparse]'to install the
package. Sparse being installed will enable the usage of sparse arrays/tensors.Creating Your First Machine Learning Programming Experiment in QiskitNow 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.fromqiskit.circuit.libraryimportTwoLocal,ZZFeatureMapfromqiskit_algorithms.optimizersimportCOBYLAfromqiskit_algorithms.utilsimportalgorithm_globalsfromqiskit_machine_learning.algorithmsimportVQCfromqiskit_machine_learning.datasetsimportad_hoc_dataseed=1376algorithm_globals.random_seed=seed# Use ad hoc data set for training and test datafeature_dim=2# dimension of each data pointtraining_size=20test_size=10# training features, training labels, test features, test labels as np.ndarray,# one hot encoding for labelstraining_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}")Further examplesLearning path notebooks may be found in theMachine Learning tutorialssection
of the documentation and are a great place to start.Another good place to learn the fundamentals of quantum machine learning is theQuantum Machine Learningcourse
on the Qiskit Textbook's website. The course is very 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 course covers 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 course is complementary to the tutorials of this module, where the tutorials focus
on actual Qiskit Machine Learning algorithms, the course more explains and details underlying fundamentals
of quantum machine learning.Contribution GuidelinesIf you'd like to contribute to Qiskit, please take a look at ourcontribution guidelines.
This project adheres to Qiskit'scode of conduct.
By participating, you are expected to uphold this code.We useGitHub issuesfor tracking requests and bugs. Pleasejoin the Qiskit Slack communityand for discussion and simple questions.
For questions that are more suited for a forum, we use theQiskittag inStack Overflow.Authors and CitationMachine Learning was inspired, authored and brought about by the collective work of a team of researchers.
Machine Learning continues to grow with the help and work ofmany people, who contribute
to the project at different levels.
If you use Qiskit, please cite as per the providedBibTeX file.LicenseThis project uses theApache License 2.0.