量子计算 qiskit_将Tensorflow和Qiskit集成到量子机器学习中

量子计算 qiskit

总览 (Overview)

There exist two popular integrations of quantum computing packages in standard deep learning libraries:

标准深度学习库中存在两种流行的量子计算软件包集成:

  1. Tensorflow and Cirq as Tensorflow Quantum

    Tensorflow和Cirq作为Tensorflow Quantum

  2. Pytorch and Qiskit

    Pytorch和Qiskit

In this article, we will be talking about integrating Qiskit in custom Keras layers.

在本文中,我们将讨论如何将Qiskit集成到自定义Keras图层中。

介绍 (Introduction)

Quantum machine learning has an interesting application of assisting classical neural networks with quantum layers that involve computation not realisable classically. Recent work in academia has stressed on applications of quantum-assisted deep learning which can have complex activations, better representation, and other salient features not achievable in classical networks.

量子机器学习在将经典神经网络与量子层相辅相成方面具有有趣的应用,其中涉及无法经典实现的计算。 学术界的最新工作强调了量子辅助深度学习的应用,该应用可能具有复杂的激活,更好的表示以及其他经典网络无法实现的显着特征。

For the implementation side, this means finding a way to integrate quantum processing in normal deep neural networks. Several ways exist to achieve this. Here, we discuss integrating Qiskit as subclasses of Keras layers. Let’s get started.

对于实现方面,这意味着找到一种在常规深度神经网络中集成量子处理的方法。 存在几种实现此目的的方法。 在这里,我们讨论将Qiskit集成为Keras图层的子类。 让我们开始吧。

定义量子层 (Defining the quantum layer)

This obviously depends on specific application. The thing to keep in mind is to be consistent in the inputs and outputs this layer has. With eager_execution as default in Tensorflow 2.x, it is only natural to fall back to numpy arrays as our default inputs to and outputs from all quantum layers.

这显然取决于特定的应用程序。 要记住的是,该层的输入和输出要保持一致。 在tensorflow 2.x eager_execution作为默认设置,自然而然地退回到numpy数组作为我们对所有量子层的默认输入和输出。

Sample quantum layer
样品量子层

This is an arbitrary quantum layer taking in four inputs and outputting a numpy array of length 4. We calculate the expectations of standard Pauli operators, create a list, and return it. This layer would change according to the specifics of the underlying application.

这是一个任意的量子层,接受四个输入并输出一个长度为4的numpy数组。我们计算标准Pauli运算符的期望值,创建一个列表,然后将其返回。 该层将根据基础应用程序的细节进行更改。

与量子层相互作用 (Interacting with the quantum layer)

Now we need to create a Keras layer that integrates the quantum layer we defined earlier. For this, we need to extend the tensorflow.keras.Layer class that allows writing custom layers for Keras models.

现在,我们需要创建一个Keras层,该层集成了我们先前定义的量子层。 为此,我们需要扩展tensorflow.keras.Layer类,以允许为Keras模型编写自定义层。

Subclassed Layer
子层

The layer receives inputs according to the batch_size of training. So we need to make sure every training example goes through the quantum layer to create an output, thus the need for the loop.

该层根据训练的batch_size接收输入。 因此,我们需要确保每个训练示例都经过量子层以创建输出,因此需要循环。

tf.executing_eagerly() is important as it will allow us to convert the default input Tensor to a numpy array via inputs.numpy() . If application depends on Tensorflow 1.x, tf.enable_eager_execution() may be called to enable eager execution. Eager execution, intuitively, allows us to access the values of the tensor as they become available. If it is disabled, we need to bring in the complexity of tensorflow’s sessions and execution graphs.

tf.executing_eagerly()很重要,因为它将允许我们通过inputs.numpy()将默认输入Tensor转换为numpy数组。 如果应用程序依赖于Tensorflow 1.x,则可以调用tf.enable_eager_execution()启用急切执行。 直观地执行急切操作可以使我们在张量值可用时对其进行访问。 如果禁用它,我们需要引入tensorflow会话和执行图的复杂性。

建立模型 (Building the model)

Building the model is straightforward. We may build a functional model or a Sequential model for our use.

建立模型很简单。 我们可以建立一个功能模型或一个顺序模型供我们使用。

Model workflow
模型工作流程

Compiling the model with run_eagerly is important. I have observed some saved models don’t work as expected after loading them if they are not compiled with run_eagerly set to True.

使用run_eagerly编译模型很重要。 我观察到某些保存的模型如果未将run_eagerly设置为True编译,则在加载它们后将无法按预期工作。

保存和加载模型 (Saving and loading the model)

Saving proceeds as normal. Loading, however, asks you to define the custom class for the custom layer we created.

照常保存收益。 但是,加载会要求您为我们创建的自定义图层定义自定义类。

Saving and loading models
保存和加载模型

The custom_objects allows you to define custom objects. It takes a dictionary with keys corresponding to the custom layers you have, and their values corresponding to the custom classes you want the custom layers to be associated with. This has a subtle caveat: it is possible to change implementations of these layers between different training sessions of some model. If you have a model trained on the quantum layer given previously in this post, it is possible to load such a model and retrain it on some other quantum logic.

custom_objects允许您定义自定义对象。 它需要一个字典,该字典的keys与您拥有的自定义图层相对应,其values与您希望与自定义图层关联的自定义类相对应。 这有一个细微的警告:可以在某些模型的不同培训课程之间更改这些层的实现。 如果您有一个在本文前面给出的量子层上训练过的模型,则可以加载这样的模型并在其他一些量子逻辑上对其进行重新训练。

结论 (Conclusion)

This post is a basic skeleton to set up a working quantum assisted deep learning architecture using Qiskit and Tensorflow. Some things are to be noted:

这篇文章是使用Qiskit和Tensorflow建立可行的量子辅助深度学习架构的基本框架。 需要注意一些事项:

  1. Calculating the expectation values is a major bottleneck. An enormous slowdown may be experienced for large batch sizes since we have completely done away with the advantage matrix multiplication in classical deep learning had.

    计算期望值是一个主要瓶颈。 对于大批量生产,可能会经历极大的速度下降,因为我们已经完全消除了经典深度学习中矩阵乘法的优势。
  2. Expectation values may be really small. An appropriate scalar multiplier may be used to scale them up.

    期望值可能确实很小。 可以使用适当的标量乘数来放大它们。

Hope this will get you started with putting Qiskit powered layers in Tensorflow networks. Have a great day :)

希望这会帮助您开始在Tensorflow网络中放置由Qiskit支持的层。 祝你有美好的一天 :)

翻译自: https://towardsdatascience.com/integrating-tensorflow-and-qiskit-for-quantum-machine-learning-7fa6b14d5294

量子计算 qiskit

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值