2025年6月23日月曜日

Comparison of Kernel PCA on Gaussian and Quantum kernel

Auto-translated from Japanese.

[Abstract]
The ad_hoc_data dataset is commonly used in research exploring quantum methods for data classification. An example of applying the KPCA (Kernel Principal Component Analysis) method to this dataset is provided in the IBM tutorial "Qiskit Machine Learning 0.8.3". In the tutorial, a comparison is made between a Gaussian kernel (the classical RBF kernel) and a quantum kernel (based on the ZZFeatureMap). The results demonstrated an example of quantum advantage. This article revisits and reaffirms those findings.

🔴 Characteristics of the Input Dataset: ad_hoc_data
This is an artificial dataset designed to make linear separation (classification) difficult for classical methods but easier for quantum techniques. Although it lacks practical applications, it is often used in studies of quantum advantage. Specifically, the dataset is constructed so that classification is challenging with classical kernels (e.g., Gaussian RBF), but effective with quantum kernels based on quantum feature maps such as ZZFeatureMap. It is primarily a binary classification dataset (labels 0 and 1), split into training and test sets.

    A key parameter, gap, controls how easily the classes can be separated. Larger values generally make the separation easier. Figure 1 shows the sample distribution. The samples for classes 0 and 1 are grouped into small clusters, making linear separation appear difficult.


🔴 Mechanism of KPCA (Kernel Principal Component Analysis) and Classification
KPCA is an extension of traditional PCA using the kernel trick, allowing it to effectively handle datasets with nonlinear structures. First, the input data is implicitly mapped into a high-dimensional feature space using a kernel function. In that space, similarities between samples are calculated, forming a kernel matrix (see Fig. 2). The kernel can be either a classical kernel (e.g., Gaussian RBF) or a quantum kernel (e.g., using a quantum feature map).

    Next, PCA is applied to this kernel matrix to project the data onto the principal components (or, reduce dimensionality).

    At this stage, we are only preparing for classification — no label information is used yet. Interestingly, when constructing a kernel matrix based on fidelity (similarity) using the ZZFeatureMap, a class-like structure (similar to clustering) can emerge even without using labels, particularly in datasets favorable to quantum kernels such as ad_hoc_data. That is, a block-like structure becomes visible in the heatmap of the kernel matrix.

    Figure 2(a) demonstrates this clearly. Most of the data pairs with label 0 (indices 0–49) form a high-similarity block (dark green), as do those with label 1 (indices 50–99). By contrast, in the classical Gaussian kernel case (Fig. 2(b)), this block structure is much less evident.


    After this stage, we proceed to supervised learning by incorporating label information. While Support Vector Classification (SVC) could be used, we opted for Logistic Regression due to its simplicity and compatibility with quantum kernels such as ZZFeatureMap-based.

    The classification results are shown in Fig. 3. They appear to reflect the structure seen in the kernel matrix of Fig. 2. Indeed, the test accuracy was 0.95 using the quantum kernel (Fig. 3(a)) and 0.6 with the classical kernel (Fig. 3(b)). This provides an illustrative example of the potential benefits of quantum kernels.

🔴Reference [Qiskit Machine Learning 0.8.3]

カーネルPCA法における古典カーネルと量子カーネルの比較

[要旨]データのクラス分けに関する量子的手法の研究用として、ad_hoc_datasetがある。これに、KPCA法 (Kernel Principal Component Analysis)を適用する例が、IBMのチュートリアル "Qiskit Machine Learning 0.8.3"にある。その中で、Gaussianカーネル(古典rbfカーネル)と量子カーネル(ZZFeatureMapに基づく)を適用した場合の比較がある。結果として、量子優越性を示す例となっていた。本記事では、これを再確認した。

🔴入力データセット ad_hoc_dataの特徴
 これは、古典的な方法では線形分離(クラス分け)が難しいが、量子的手法ではそれが容易になるように設計された人工的データセットである。実用性はないが、量子優位性(quantum advantage)の研究などで利用される。すなわち、古典的なカーネル(GaussianカーネルRBF等)では分類が難しいが、量子特徴マップ(ZZFeatureMap等)に基づく量子カーネルではうまく分類できるように構成されている。主にバイナリ分類用のデータセット(ラベル 0 と 1)であり、トレーニングセットとテストセットに分けられる。

 このデータセットの生成用のパラメータの一つであるgapは、クラスの分離性を決める。この値が大きいほど、クラス分けは容易になる傾向となる。Fig.1にそのデータサンプルの分布を示す。クラス0とクラス1のデータがそれぞれ小さなグループとなって散在しており、線形分離は困難のように見える。

🔴KPCA(Kernel Principal Component Analysis)の仕組みとクラス分け
 KPCAは、"非線形構造データ"の分類に有効なように、通常の主成分分析PCAをKernel Tickによって拡張したものである。すなわち、まず、カーネルを使って、入力データを暗黙的に高次元空間に写像し、その空間での特徴表現(データサンプル間の類似度の計算)を行う。結果としてFig.2に示すようなカーネル行列が得られる。カーネルとしては、古典カーネル(Gaussianカーネルrbf等)や量子カーネル(Quantum Feature mapに基づく)を使うことができる。これに続けて、主成分分析PCAの手法を適用して、その空間での主成分への射影(または次元削減)を行う。

 ここまでは、クラス分類のための準備であり、分類学習はしていない。クラスを示すラベル情報も全く使っていない。しかし驚くべきことに、ad hoc datasetのような量子カーネル向きのデータセットに対して、ZZFeatureMap を用いた fidelity(類似度) に基づくカーネル行列を構成すると、クラス構造(クラスタリング的な構造)が浮かび上がる場合がある。つまり、ラベル情報がなくても、カーネル行列のヒートマップを見ると、ブロック構造(クラスタ)が現れる。

 Fig.2(a)は、まさにそれを示している!すなわち、ラベル0のデータ(番号0〜49)のペアは、ほとんどが高い類似度(濃い緑色)を示すブロックになっている。ラベル1(番号50〜99)についても同様である。しかし、これに反して古典Gaussianカーネルの場合(FIg.2(b))は、そのようなブロック構造が明瞭でない。
 さて、この後、カーネル行列に加えて、ラベル情報を用いた教師付き学習を行う。その際の分類学習器としてSVCを使っても良いのだが、ここでは、より軽量でZZFeatureMapとの相性も良いとされるLogistic Regression(ロジスティック回帰)を使った。

 Fig.3にその結果を示す。この結果は、Fig.2に示したカーネル行列の構造がそのまま反映されているように見える!つまり、テストデータに対する分類精度は、量子カーネルの場合0.95(Fig.3(a))、古典カーネルの場合0.6(Fig.3(b))であった。量子カーネルの可能性を示唆する一例となった!

🔴参考資料 [Qiskit Machine Learning 0.8.3]

2025年6月11日水曜日

Enjoy Observing Classical and Quantum Kernels in SVC

[Abstract]
The Support Vector Classifier (SVC) is a well-known and effective method for classification. One of its key strengths is the kernel trick, which implicitly maps complex, intertwined data into a higher-dimensional space using a kernel function—allowing for linear separation in that space. In this article, we’ll visually explore and enjoy the differences between classical and quantum kernels!

🟢 Input Dataset: Gaussian Parity
Classical SVC is highly powerful and often outperforms current quantum methods. However, quantum techniques offer novel capabilities that classical approaches cannot, which is why research in this area is gaining momentum.In this article, we use a dataset known as Gaussian Parity, which is considered well-suited for quantum methods.

     As shown in Fig. 1, this dataset consists of 80 samples (56 for training and 24 for testing) and two class labels. The data points from each class are interleaved in a diagonally crossed pattern, making linear separation difficult without transformation. This is where the kernel function—mentioned in the abstract—comes into play.

🟢 SVC Results with Classical and Quantum Kernels
Let’s start with the conclusion. Figure 2 shows classification results using SVC trained with (a) a classical kernel (RBF) and (b) a quantum kernel (a kernel matrix based on the ZZFeatureMap). On the test set, classification accuracy was 0.58 for (a) and 0.93 for (b).

     Of course, these results can vary depending on parameter settings. However, in this instance, the quantum kernel significantly outperformed the classical one. Also, the direction of the decision boundary differs considerably between (a) and (b).

🟢 Exploring the Classical Kernel (RBF)
Let’s take a closer look at the classical case. The RBF kernel is not used directly, but rather called internally during SVC training. It implicitly maps the input data into a higher-dimensional space.
Based on the learned model, we can use Principal Component Analysis (PCA) to project this high-dimensional space into three dimensions and visualize the decision function in 3D.

     The top two images in Fig. 3 show this 3D decision function. You can observe two peaks and two valleys. If we slice this 3D surface at the decision function value = 0, the resulting plane gives us the decision boundary.

🟢 Exploring the Quantum Kernel Matrix
Now let’s examine the quantum case. As shown in Fig. 4, the decision function exhibits a more complex pattern, with more peaks and valleys than in the classical case.
When we slice the surface at decision function = 0, we obtain a decision boundary that achieves a classification accuracy of 0.93.

     This example suggests that the quantum method is exploring and learning within a feature space that classical methods cannot access. This may open the door to entirely new possibilities!


2025年6月6日金曜日

初夏の散歩道-厚木市郊外

 6月に入りまだ梅雨前ですが、初夏らしい日が続いています。散歩道(厚木市郊外)で写した数枚の写真をご覧ください。 2025年6月6日午前の散歩にて。

 かっての同僚の先生から、「もうトウモロコシが実っているのですか?北海道の感覚では、もっと後ですよね。そういえば、大通公園のとうきび売りの露店は本当に少なくなりました。」とのコメントをいただきました。そうなんですね。北海道では、「トウモロコシ」とは言わずに、「とうきび」と呼びます。でも、東京近辺に長年住んでいるとそれに合わせてしまったことに気が付く。懐かしい響き。