2025年4月24日木曜日

Visualizing and Understanding QSVC

[Abstract] 
QSVC (Quantum Support Vector Classifier) ​​can sometimes produce new classification decision boundaries that are different from those of classical SVC. An example of this was reported in a previous article. This time, we visualized (part of) the QSVC process that leads to this point. Visualization is not simply beautiful or easy to understand. While it can help us understand the mechanism in concrete terms, it can also be a trigger for new questions to arise, encouraging progress. Javier's book “QML Unlocked” was very useful, and is mentioned at the end of this article.

🔴What to visualize
A small dataset for visualization, 16 samples of Gaussian Parity, was prepared. It consists of two features and a label. The parts to be visualized in the overall flow of QSVC are shown below.
  1. Normalization (scaling) of input data: No need for visualization.
  2. Reduction of input data dimension: Not necessary in this case, since it is two-dimensional (2 features).
  3. Display of input data: Draw data samples in a 2D scatter plot. → Figure 1
  4. Convert input data to quantum state: Display quantum state on Bloch sphere. → Figure 2
  5. Create quantum kernel matrix: Represented as a heat map. → Figure 3
  6. Perform SVC learning: Display the resulting classification decision boundary in 2D. → Figure 4

🔴Visualization of Quantum Feature Map

Here, we use ZZFeatureMap, which is equipped in Qiskit, to convert each sample of the input data in Figure 1 into a quantum state (feature vector). The result is plotted on two Bloch spheres, as shown in Figure 2. As shown in the figure, ZZFeatureMap is a system (considered to be four-dimensional) that includes two quantum bits of entanglement and phase shift, so quantum states are displayed in both q0 and q1.

🔴Visualization of Quantum Kernel Matrix

Next, we create a quantum kernel matrix using the results of the feature map. Each element is the fidelity calculated by the inner product of two combinations of quantum states. Figure 3 shows this matrix as a heat map. The brighter it is, the higher the similarity.
Classical SVC also implicitly calculates the corresponding similarity using a kernel function during training. On the other hand, quantum QSVC calculates all similarities in advance to create a kernel matrix, which is then passed to classical SVC training. SVC training has the option kernel='precomputed'. QSVC repeatedly runs the quantum circuit for all combinations of inputs to create a quantum kernel matrix.

Now, this quantum kernel matrix does not contain any original label information. Therefore, classification cannot be performed using this matrix alone. Learning  (SVC learning) using this matrix and label information is required. However, even with just Figure 3, it is possible to predict whether two samples are likely to fall into the same class or into different classes.

For example, the similarity between the vectors of sample numbers 0 and 2 is quite low, as shown in [A] in Figure 3. As shown in Figure 2, the two are almost orthogonal. And they have different labels. Therefore, it is highly likely that they will be placed in different classes.
Furthermore, the similarity between the vectors of sample numbers 4 and 8 is quite high, as shown in [B] in Figure 3. As shown in Figure 2, the two are also heading in a similar direction. And they have the same label. Therefore, it can be predicted that they will be highly likely to be placed in the same class.

🔴Visualization of classification decision boundary

Next, using the quantum kernel matrix in Figure 3 and the original label information, classification training is performed. This training is the same as classical SVC training. The resulting decision boundary is shown in Figure 4. The relationship between data points 0 and 2 [A] and the relationship between 4 and 8 [B] are as expected above.

🔴The decisive difference between quantum QSVC and classical SVC

The decisive difference between QSVC and classical SVC is not whether the kernel matrix is ​​calculated in advance, but in which space the vector inner product (similarity) is calculated. In classical SVC, the input data is also classified after mapping it to a high-dimensional space using a kernel function. However, the properties and structure of that space are different from QSVC.
In QSVC, it is important to know what kind of quantum feature map is used to convert the input into a quantum state. Whether or not QSVC's superiority is demonstrated depends on whether the feature map can realize a mapping that is difficult to realize classically.

🔴[Reference book] Introduction to Javier's "QML Unlocked"

There are few books in Japanese to learn quantum machine learning. The ones already published have a strong theoretical aspect and are not very suitable for beginners. In the meantime, the English book by Javier Mancilla Montero, shown in Figure 5, has very easy-to-understand explanations. Although the above example is my own original work, I obtained very useful information from this book, so I would like to briefly introduce the contents of this book.
It is a compact book (228 pages in total) with 10 chapters. Chapters 1-3 are an overview of quantum computing and machine learning. Chapters 4-7 cover QSVC, and chapters 8-10 cover more advanced topics such as VQC (Variational Quantum Classifier).

Overall, the book focuses on the explanation of QSVC. Although there are few mathematical expressions, the description is precise and quite deep. The explanation of creating a quantum kernel using several types of feature maps is excellent. Going further, it also attempts to combine them for SVC learning (multi-kernel learning). It explains this concretely using quantum platforms such as Qiskit and PennyLane. All the codes provided were able to run perfectly in my local environment.

Those who reach Chapter 4 for the first time may be a little confused. This is because the chapter explains code that demonstrates (1) MinMaxScaling, (2) dimensionality reduction using principal component analysis, (3) quantum state generation using ZZFeatureMap, (4) quantum kernel matrix generation, and (5) SVC learning all at once for a large dataset consisting of 1,000 samples with 20 features. However, there is no need to worry, because the following chapters explain dimensionality reduction in detail in Chapter 5, FeatureMap in Chapter 6, and SVC learning using quantum kernels in Chapter 7. In other words, the overall picture is shown first, followed by specific discussions.

For large-scale input data, creating feature maps and quantum kernel matrices, and training using them takes a lot of time in simulation. Although this book is not a programming book, as a countermeasure, it also provides multi-core parallel processing code using the PennyLane library and Python joblib. In fact, on my M1-mac-mini (8 cores), I was able to achieve a speedup of more than four times.

----------------------------------------------------------
🟢Special thanks to Javier Mancilla Montero, Ph.D. for posting this article of mine on Linkedin, see below.
----------------------------------------------------------

2025年4月23日水曜日

量子サポートベクタ分類QSVCのビジュアル化

【要旨】QSVC(Quantum Support Vector Classifier)により、古典SVCの場合とは異なる新たな分類決定境界が得られる場合がある。その一例を先の記事で報告した。今回は、そこに至るQSVCの過程(の一部)をビジュアル化してみた。ビジュアル化は、単に、美しい、分かり易いではない。具体的に仕組みを納得できる一方、新たな疑問が湧くきっかけともなり、進歩を促す。

🔴何をビジュアル化するのか
 ビジュアル化のための小さなデータセット、Gaussian Parityの16サンプルを用意した。2つのfeatureとラベルから成る。QSVC全体の流れにおいて、ビジュアル化する箇所を以下に示す。
  1. 入力データの正規化(スケーリング):ビジュアル化の必要は特になし。
  2. 入力データの次元削減:今回は2次元(2 features)なので不要。
  3. 入力データの表示:データサンプルを2D散布図に描く。→図2
  4. 入力データを量子状態に変換:ブロッホ球に量子状態を表示。→図3
  5. 量子カーネル行列の作成:ヒートマップとして表現。→図4
  6. SVC学習を行う:結果としての分類決定境界を2D表示。→図5

🔴Quantum Feature Mapの可視化
 ここでは、Qiskitに装備されているZZFeatureMapによって、図2の入力データを量子状態(特徴ベクトル)に変換する。その結果を、図3の示すとおり、2つのブロッホ球に描いた。ZZFeatureMapは、図の通り、2量子ビットのEntanglementとPhase Shiftを含む一つの(4次元と見做せる)システムであるため、q0とq1の両方に量子状態を表示する。

🔴Quantum Kernel Matrixの可視化
 次に、Feature Mapの結果を使って量子カーネル行列を作る。その各要素は、2つの量子状態の組合わせについて、その内積で計算された類似度(fidelity)である。この行列を、ヒートマップとして表現したものが図4である。明るいほど、類似度が高い。

 古典SVCでも、これに該当する類似度を学習中に暗黙にカーネル関数で計算する。一方、量子QSVCの場合は、予め全部の類似度を計算してカーネル行列を作り、古典SVCの学習へ渡すのである。SVCの学習には、kernel='precomputed'というオプションがある。QSVCでは、入力の全ての組み合わせについて、量子回路を繰り返し実行し、量子カーネル行列を作る。

 さて、この量子カーネル行列には、元のラベル情報は一切入っていない。したがって、これだけで分類を行うことはできない。この行列とラベル情報を使った学習(SVC学習)が必要である。だが、図4だけでも、2つのサンプルが同じクラスに入り易いか、または別々のクラスになり易いかを予想することはできる。

 例えば、サンプル番号0と2のベクトルの類似度は図4の[A]の通りかなり低い。図3でも両者はほぼ直交している。そして、両者は異なるラベルを持っている。したがって、両者は別々のクラスに入る可能性が高いであろう。
 また、サンプル番号4と8のベクトルの類似度は図4の[B]の通りかなり高い。図3でも両者の向きは近い。そして、両者は同一ラベルを持っている。したがって、両者は同じクラスに入る可能性が高いと予想できる。

🔴分類の決定境界の可視化
 次に、図4の量子カーネル行列と元のラベル情報を使って、クラス分けの学習が行われる。この学習は、古典SVCの学習と同じものである。その結果としての決定境界を図5に示した。データポイント0と2の関係[A]、4と8の関係[B]は、上述の予想通りである。

🔴量子QSVCと古典SVCの決定的な違い
 QSVCと古典SVCの決定的な違いは、カーネル行列を事前に計算するか否かではなく、どの空間においてベクトルの内積(類似度)を計算するかである。古典SVCにおいても、カーネル関数を用いることで入力データを高次元空間へ写像した後に分類を行う。しかし、その空間の性質や構造はQSVCとは異なる。
 QSVCでは、どのような量子Feature Mapによって入力を量子状態に変換するかが重要である。QSVCの優位性が発揮されるか否かは、そのFeature Mapが古典的には実現困難な写像を実現できるか否かによる。

🔴[参考書] "QML Unlocked"(量子機械学習の扉を開く)の紹介
 量子機械学習を学ぶための日本語の書籍は少ない。既刊のものは理論的側面が強く初心者にはあまり向かない。そんななか、図6に示した、Javier Mancilla Montero氏による英語の書籍は説明がとてもわかりやすい。今回の上述の事例は私のオリジナルではあるが、この書籍から大いに参考になる情報を得たので、簡単に本書の内容を紹介したい。
 小型本(全228ページ)で、10章からなる。第1〜3章は、量子コンピューティングと機械学習の概説である。第4〜7章はQSVCを、また、第8〜10章はより進んだVQC(Variational Quantum Classifier)などを扱っている。

 全体的に見て、QSVCの解説に重点が置かれている。数式はほとんど出てこないが、叙述は精緻であり、かなり深い。いくつかの種類のFeature Mapを使って量子カーネルを作る説明が優れている。さらに進んで、それらを組み合わせてSVC学習させること(Multi Kernel Learning)も試みている。それを、QiskitやPennyLaneなどの量子プラットフォームを用いて具体的に説明している。提供されている全てのコードは、私のローカル環境で完全に動かすことができた。

 初めてChapter4に達した人は、ちょっと戸惑うかもしれない。そこでは、20 featuresの1000サンプルからなる大きなデータセットに対して、(1)MinMaxScaling、(2)主成分分析による次元削減、(3)ZZFeatureMapによる量子状態生成、(4)量子カーネル行列の生成、(5)SVC学習、の全てを一気にデモするコードを解説しているからである。しかし、心配ご無用である。続くChapter5では次元削減が、Chapter6ではFeatureMapが、Chapter7では量子カーネルを使ったSVC学習が、詳細に説明されているからである。つまり、先に全体像が示され、その後、各論が続いている。

 大規模入力データの場合、Feature Mapと量子カーネル行列の作成、およびそれを用いた学習には、シミュレーションでは多くの時間を要する。本書は、プログラミングの本ではないが、その対策として、PennyLaneのライブラリとPythonのjoblibを使って、マルチコア向け並列処理コードも提供している。実際、私のM1-mac-mini(8コア)では、4倍を超える高速化が実現できた。

2025年4月18日金曜日

量子機械学習の第一歩QSVC(Quantum Support Vector Classifier)

【要旨】古典的手法では見つけられなかった(or できなかった)ことが、量子的手法では可能になったという事例があれば勇気付けられる。その一つとして、量子計算を用いたQSVC(Quantum Support Vector Classifier)が、古典SVCよりも高い精度で分類できる一つの例を示す。この例に対しては、古典SVCでは見つからなかった、新たな分類決定境界が、量子QSVCで見つけられたと言える。これは、量子機械学習に取り組む上で意義がある事例と思われる。

🔴対象としたデータセットGaussian-Parity
 今回用いたデータセットGaussian-Parityの一例を図1に示す。右側の表は、その内容である。2つの特徴量(Feature1、Feature2)とラベル(0 or 1)で構成される300サンプルが含まれる。これをMiniMax(0〜π)スケーリングして散布図にしたものが左側の図である。ラベル0とラベル1のデータが、斜めに交差して分布しているので、境界線を引くのは難しそうに見える。

🔴古典的SVCによるクラス分け
 
このデータセットに対して、まず、古典SVCでクラス分けした。300サンプルの7割を訓練用として学習させた結果を、残り3割のサンプルをテスト用として評価した分類の精度は81%であった。学習結果を反映した分類の決定境界を図2に示した。かなりよく分類できていると思われる。SVCの威力が感じられた。(SVCに与える種々のパラメータの値で結果は変動するが。)

🔴量子的QSVCによるクラス分け
 次に、量子的QSVCでクラス分けした。今回は、Qiskitに装備されているライブラリQSVCを量子回路シミュレーションで実行した。学習の条件はSVCの場合と同じである。分類精度は、90%となり、SVCの場合よりもかなり高まった。それは、図3に示す通り、図2とは異なる決定境界が得られたことによる。このように、SVCでは見つからなかった新たな決定境界が得られた理由は、(技術詳細は略すが)少ない量子ビットnでも、2のn乗次元の広い量子状態の空間を探索できることによるのであろう。すなわち、多様なカーネル行列と呼ばれる情報を古典的なSVCの仕組みに与えることができる。


🔴古典的SVCと量子的QSVCとの関係
 詳細は、別のブログ記事で後日議論したいが、図4に両者の関係の概要を示した。Javier Mancilla M.氏の著書"QML Unlocked"が非常に参考になったので、その中のFigure 16を引用し、加筆した。(この書籍のレビュー結果の記事も別途書く予定である。)

 QSVMも、学習(訓練)には、古典SVMの仕組みをそのまま使う。それができるようにするため、量子回路を用いて計算する所が2箇所ある。1箇所目は、元の入力数値データを量子状態にマッピングするQuantum Feature Mapである。さらにその前に、次元削減(主成分分析PCAなど)を施す必要がある。なぜなら、一つの特性(Feature)につき1量子ビットが必要なため、利用量子ビット数を抑えるためである。2箇所目は、マップされた量子状態同士の類似度を計算してKernel Matrixと呼ばれるものを作る部分である。このカーネル行列を従来のSVMの仕組みに与える。

🔴今後の予定
  1. Quantum Feature MapやKernel Matrixを構成する方法はいくつかあるので、それらも調査し、試行する。
  2. 量子コンピュータ実機で動かす。現状では、性能(計算速度)は古典コンピュータに及ばないが、実機での試行で得られる知見は貴重である。
🔴追加情報
 図2(SVC)に対する図3(QSVC)の優越性は、ある特殊なデータセットに関する偶然の結果なのか?そうではないことを願って、少なくとももう一例、優越性を示す必要があると考え、以下に別のケースも示した。先の例と同様に、QSVCの優越性が見られた!

2025年4月9日水曜日

Reduction of error occurrence in IBM quantum computer (Heron processor)

Theoretically, the success rate of Mermin-Peres magic, a 4-qubit application using quantum entanglement, is 100%. When this was executed on ibm_torino (Heron r1), the success rate was 92% due to errors caused by noise. This is a big improvement from the 86% success rate on ibm_brisbane (Eagle r3) a while ago. The reason for this is that the error rate was 14% on ibm_brisbane, but was reduced to 7.9% on ibm_torino. Figure 1 shows the details.


2025年4月8日火曜日

IBM量子コンピュータ(Heronプロセッサ)におけるエラー発生の低減

【要旨】量子もつれを用いた例題Mermin-Peres-Magicを、IBM量子コンピュータ新鋭機ibm_torino (Heron r1プロセッサ)で実行させた結果、ノイズ等によるエラーの発生率が、以前のibm_brisbane (Eagle r3プロセッサ)に比べて、ほぼ半減することが分かった。これにより、所望の正解が得られる成功率は、86%から92%に大きく改善された。今後出現するであろう華々しい成果は、このような長年の地道な研究開発によるものなのだと実感できた気がする。

🔴誤り低減を目指す量子コンピュータの進展
 Eagle(鷲)は攻撃的で強く、Heron(鷺)はしなやかで強いというイメージがあるという。IBMがそれを念頭において量子コンピュータに命名したのかは定かではない。IBMはこれまで、無償で量子プロセッサEagle r3(マシン名 ibm_kyiv, ibm_brisbane)を提供してきたが、この3月から、新鋭機Heron r1(マシン名 ibm_torino)を追加した。Heronは、Eaglelよりも、大幅にエラー発生率が低減されて強力になったという。そのハードウェアの仕組みは私には分からないが、最大の難題の一つであるエラー低減に向けて着実に進展していることが窺える。

🔴量子コンピューティングプラットフォームQiskitの更新
 IBMは量子コンピューティング開発環境Qiskitを頻繁にバージョンアップしているが、今回、Heronプロセッサの無償公開に合わせて、さらに大幅な改訂を行なった。そのための移行説明会もオンラインで行われた。実際、これまでの量子プログラムは環境を色々と修正しないと動かない状況である。だが、そこを何とか調べて、Heronプロセッサもようやく使えるようになった!!!

🔴量子もつれの例題「Mermin-Peres-Magic」をHeronプロセッサで動かす
 このHeronプロセッサの性能を十分に発揮できるような量子アプリを見つけることは容易ではない。しかし、自分がやれるアプリを少しづつ動かして、自分のスキルを高めることができるだろう。そういう思いで、量子もつれが本質的に効いている「Mermin-Peres-Magic」をやってみた。それがどんな問題かは、過去に書いたこちらの記事をご覧いただきたい。
 ごく簡単に言えば、この問題に対しては、古典的には100%成功する方法は無いが、量子的方法(量子もつれ)では、必ず100%成功させることができる。ただし、実際の量子コンピュータでは、ノイズ等による誤り発生のため、失敗するケースも出てくる。

🔴Heronプロセッサではエラー発生が着実に低減されていた!
 詳しいことは省略するが、図1をご覧いただきたい。この例題を実行した場合のエラーの発生率が、Eagleプロセッサに対して、約半減されることが分かった。すなわち、本来の得たい正解を得る場合の失敗率は、14.0%から7.9%に大幅に低減されたのである。これによって、正解を得る成功率は、86%から92%に上昇した。私自身の、しかも一つの例題の実測結果に過ぎないのだが、これによってモチベーションは確実に高まった!こうして、技術は進歩するものなのだと。