谷歌又在搞“量子霸權”?量子機器學習開源庫TFQ或成大飛躍

全文共2338字,預計學習時長

13分鐘


圖源:unsplash


去年10月,《Nature》刊發了谷歌的一篇論文,其研發的Sycammore量子處理器能在200秒內完成傳統超級計算機上萬年的計算量,基於此,谷歌宣稱實現了“量子霸權”。


2020年3月9日,谷歌人工智能確認了TensorFlow Quantum (TFQ)的可用性,這是一個用於快速研發量子機器學習模型的開源庫。這是谷歌“量子霸權”的新招數嗎?


其實,早先還有如Pennylane的其他幾個框架,但都沒有TFQ出色。TFQ作為工具箱出現在這個領域,依舊未被公開。筆者已經瞭解了一些其他框架,但是在研究過TFQ之後,不可否認TFQ是最好的。


如何在參數化量子電路上進行機器學習?


為弄清楚這一點,TFQ的技術負責人馬蘇德·莫西尼提供了示例。他說,“需要注意的是,時空體中打印這種單位運算或隨機旋轉是一種連續的參數化旋轉,模仿了經典電路,比如深度神經網絡中將輸入映射到輸出。”


圖源:unsplash


這就是量子神經網絡的原理。


但是如何創建這些參數化的量子電路呢?


開發混合量子模型的第一步是能夠利用量子運算。為此,TFQ依賴於Cirq(一個近期計算機上實現量子電路的開源平臺)。


Cirq包括定義量子計算所需的基本結構,如量子位、門、電路和計算算符。Cirq背後的理念是提供一個簡單的編程模型,抽象出量子應用的基本構件塊。


能把cirq和TFQ結合起來嗎?挑戰是什麼?


技術障礙1


· 無法導入量子數據。

· 數據和模型都是量子電路中的層。

· 量子數據必須隨時準備。


技術障礙2


· 相對高延遲的CPU——QPU。

· 批量作業被中繼到量子計算機。

· QPU每次運行都需要完整的量子程序。

· QPU在幾微秒內運行。


為使其實用並克服障礙,TFQ團隊在編程背景下提出了一些不可忽視的架構概念。架構標準如下所示:


1.可微分性:須支持量子電路的微分和混合反向傳播。

2.電路批處理:量子數據上傳為量子電路,並行訓練多個不同的電路。

3.執行後端不可知:幾步就能從模擬器切換到真實設備。

4.極簡主義-Cirq和TF間的橋樑:無需用戶重新學習如何與量子計算機交互來解決機器學習問題。


圖源:unsplash


逐步執行


混合判別模型的TFQ管道


步驟1:


準備一個量子數據集:量子數據加載為一個張量,定義為用Cirq編寫的量子電路。張量由量子計算機上的TensorFlow執行以生成量子數據集。


量子數據集為非參數化cirq.Circuit對象被應用於計算機圖表,並使用tfq.convert_to_tensor。


步驟2:


評估量子神經網絡模型:這一步中,研究人員可以使用Cirq製作量子神經網絡的原型,然後將其嵌入TensorFlow計算圖中。


量子模型的構建用cirq.Circuit包含SymPy符號的對象,並且可以使用tfq.AddCircuit分層附加到量子數據源。


步驟3:


樣本或平均值:這一步利用步驟(1)和(2)的幾次運行取平均值。樣本或取平均值通過將量子數據和量子模型送至tfq.Sample,或者tfq.Expectation層。


步驟4:


評估經典神經網絡模型:這一步使用經典深度神經網絡來提取前面步驟中提取的度量間的相關性。由於TFQ與TensorFlow完全兼容,量子模型可直接與其聯繫tf.keras.layers.Layer,如tf.keras.layers.Dense.等對象。


圖源:unsplash


步驟5:


評估成本函數:類似於傳統的機器學習模型,通過這一步驟,TFQ評估成本函數。如果量子數據被標記,評估成本函數可能基於模型執行分類任務的準確程度,如任務無監督,則基於其他標準。


將分階段(1)到(4)構建的模型打包於tf.keras.Model,允許用戶訪問模塊中的所有損失tf.keras.losses。


步驟6:


評估梯度和更新參數-評估成本函數後,為降低成本,管道中的自由參數應按照預期方向更新。


為支持梯度下降,向TensorFlow反向傳播機制公開量子操作的導數,通過
tfq.differentiators.Differentiatorinterface混合量子-經典反向傳播,量子和經典模型參數都可以針對量子數據進行優化。


圖源:unsplash


編碼演示


<code>#Importing dependencies !pip install --upgrade cirq==0.7.0 !pip install --upgrade tensorflow==2.1.0 !pip install qutip !pip install tensorflow-quantum import cirq import numpy as np import qutip import random import sympy import tensorflow as tf import tensorflow_quantum as tfq #Quantum Dataset def generate_dataset(qubit, theta_a, theta_b,num_samples): """Generate a dataset of points on `qubit` near the twogiven angles; labels for the twoclusters use a one-hot encoding. """ q_data = [] bloch ={"a": [[], [], []], "b": [[], [], []]} labels = [] blob_size =abs(theta_a - theta_b) / 5 for _ inrange(num_samples): coin =random.random() spread_x =np.random.uniform(-blob_size, blob_size) spread_y =np.random.uniform(-blob_size, blob_size) if coin <0.5: label =[1, 0] angle =theta_a + spread_y source ="a" else: label =[0, 1] angle =theta_b + spread_y source ="b" labels.append(label) q_data.append(cirq.Circuit(cirq.ry(-angle)(qubit),cirq.rx(-spread_x)(qubit))) bloch[source][0].append(np.cos(angle)) bloch[source][1].append(np.sin(angle)*np.sin(spread_x)) bloch[source][2].append(np.sin(angle)*np.cos(spread_x)) returntfq.convert_to_tensor(q_data), np.array(labels), bloch #Genrate the dataset qubit = cirq.GridQubit(0, 0) theta_a = 1 theta_b = 4 num_samples = 200 q_data, labels, bloch_p = generate_dataset(qubit,theta_a, theta_b, num_samples #Model #We will use a parameterized rotation about the Y axisfollowed by a Z-axis measurement as the quantum portion of our model. For theclassical portion, we will use a two-unit SoftMax which should learn todistinguish the measurement statistics of the two data sources. # Build the quantum model layer theta = sympy.Symbol('theta') q_model = cirq.Circuit(cirq.ry(theta)(qubit)) q_data_input = tf.keras.Input( shape=(),dtype=tf.dtypes.string) expectation = tfq.layers.PQC(q_model, cirq.Z(qubit)) expectation_output = expectation(q_data_input) # Attach the classical SoftMax classifier classifier = tf.keras.layers.Dense(2,activation=tf.keras.activations.softmax) classifier_output = classifier(expectation_output) model = tf.keras.Model(inputs=q_data_input,outputs=classifier_output) # Standard compilation for classification model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.1), loss=tf.keras.losses.CategoricalCrossentropy()) tf.keras.utils.plot_model(model, show_shapes=True,dpi=70) #Training history = model.fit(x=q_data, y=labels, epochs=50,verbose=0) test_data, _, _ = generate_dataset(qubit, theta_a,theta_b, 1) p = model.predict(test_data)[0] print(f"prob(a)={p[0]:.4f},prob(b)={p[1]:.4f}")/<code>

圖源:unsplash


我們用非常簡單的步驟發掘了量子神經網絡,甚至用TFQ執行了它,這實在令人驚喜。TFQ必將是機器學習史上的一次巨大飛躍。


留言點贊關注

我們一起分享AI學習與發展的乾貨

如轉載,請後臺留言,遵守轉載規範