量子计算 qiskit_将Tensorflow和Qiskit集成到量子机器学习中
量子計算 qiskit
總覽 (Overview)
There exist two popular integrations of quantum computing packages in standard deep learning libraries:
標準深度學習庫中存在兩種流行的量子計算軟件包集成:
Tensorflow and Cirq as Tensorflow Quantum
Tensorflow和Cirq作為Tensorflow Quantum
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建立可行的量子輔助深度學習架構的基本框架。 需要注意一些事項:
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
總結
以上是生活随笔為你收集整理的量子计算 qiskit_将Tensorflow和Qiskit集成到量子机器学习中的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 全球最先进!苹果A17芯片遭曝光 性能有
- 下一篇: iPhone 15 Ultra渲染图出炉