谷歌又在搞“量子霸权”?量子机器学习开源库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学习与发展的干货

如转载,请后台留言,遵守转载规范