Clustering using Quantum Kernels — Tutorial
We use Qiskit and sklearn to perform some clustering on toy datasets. We use the qiskit_machine_learning library to produce a quantum kernel. This kernel is then applied for Spectral CLustering in sklearn.
The whole tutorial is available here as a jupyter notebook.
This tutorial is based on a paper by Suzuki et. al., which itself is a study of the pioneering paper Supervised learning with quantum-enhanced feature spaces by Havlíček et.al. [1,2]. Suzuki et. al in their paper, study a two qubit classifier. The input data points are mapped to a unitary operator,
The input data is mapped to a unitary operator. U(x) is the unitary gate used to form the quantum circuit.
The encoding functions are given as
Encoding functions that transform the input data points to a higher dimensional feature space.
Step 0 — Imports
We need some utility functions for handling data and plotting. We also need the qiskit, qiskit_machine_learning and the sklearn libraries.
Step 1 — Encoding feature maps
Suzuki et. al. present five encoding functions
The five encoding functions from Suzuki et. al.
In addition we include the encoding function used in Havlíček et.al.
Encoding function from Havlíček et. al., whcih is also the default encoding function for PauliFeatureMap
This is the default encoding function for the PauliFeatureMap in qiskit-machine-learning
The encoding functions are are implemented as below
Step 2 — Quantum kernels
The input data points are mapped to a unitary operator,
The input data is mapped to a unitary operator. U(x) is the unitary gate used to form the quantum circuit.
This is implemented as a quantum circuit using PauliFeatureMaps. We specify the types of gates used, in this case ZI, IZ and ZZ as given in equation 6. The encoding function is then specified in the data_map_func. The circuit repetitions can also be specified.
Step 3 — Clustering
The quantum kernels prepared above can now be used in Spectral Clustering. We also include KMeans and Mean Shift clustering for comparison and to give an intuition of how the Quantum Kernels operate.
Results
The results of the experiment are shown below
For each dataset, the quantum kernel based clustering varies depending on the encoding function. Equations 11 and 12 tend to perform better for the Circles dataset. This of course is expected from their cosinusoidal form. The performance can be optimized further by tweaking parameters such as circuit repetitions, entanglement and paulis.
Nevertheless, an important aspect highlighted in this experiment is the encoding function. The functions given in Suzuki et. al. carry an intuition of how the data is distributed. This means that for new datasets there is a need to “learn” these encoding functions. A possible approach maybe quantum Generational Adversarial Networks (qGAN). We will explore them soon.
I conclude here with a tip from Amira Abbas in a recent video on quantum machine learning, “Set your expectations correctly.”
References
- Suzuki, Y., Yano, H., Gao, Q. et al. Analysis and synthesis of feature map for kernel-based quantum classifier. Quantum Mach. Intell. 2, 9 (2020). https://doi.org/10.1007/s42484-020-00020-y
- Havlíček, V., Córcoles, A.D., Temme, K. et al. Supervised learning with quantum-enhanced feature spaces. Nature 567, 209–212 (2019). https://doi.org/10.1038/s41586-019-0980-2
- arXiv:2101.11020 [quant-ph]
- Quantum Kernel Machine Learning — Qiskit Machine Learning 0.1.0 documentation