Path: blob/master/site/en-snapshot/quantum/tutorials/qcnn.ipynb
25118 views
Copyright 2020 The TensorFlow Authors.
Quantum Convolutional Neural Network
This tutorial implements a simplified Quantum Convolutional Neural Network (QCNN), a proposed quantum analogue to a classical convolutional neural network that is also translationally invariant.
This example demonstrates how to detect certain properties of a quantum data source, such as a quantum sensor or a complex simulation from a device. The quantum data source being a cluster state that may or may not have an excitation—what the QCNN will learn to detect (The dataset used in the paper was SPT phase classification).
Setup
Install TensorFlow Quantum:
Now import TensorFlow and the module dependencies:
1. Build a QCNN
1.1 Assemble circuits in a TensorFlow graph
TensorFlow Quantum (TFQ) provides layer classes designed for in-graph circuit construction. One example is the tfq.layers.AddCircuit
layer that inherits from tf.keras.Layer
. This layer can either prepend or append to the input batch of circuits, as shown in the following figure.
The following snippet uses this layer:
Examine the input tensor:
And examine the output tensor:
While it is possible to run the examples below without using tfq.layers.AddCircuit
, it's a good opportunity to understand how complex functionality can be embedded into TensorFlow compute graphs.
1.2 Problem overview
You will prepare a cluster state and train a quantum classifier to detect if it is "excited" or not. The cluster state is highly entangled but not necessarily difficult for a classical computer. For clarity, this is a simpler dataset than the one used in the paper.
For this classification task you will implement a deep MERA-like QCNN architecture since:
Like the QCNN, the cluster state on a ring is translationally invariant.
The cluster state is highly entangled.
This architecture should be effective at reducing entanglement, obtaining the classification by reading out a single qubit.
An "excited" cluster state is defined as a cluster state that had a cirq.rx
gate applied to any of its qubits. Qconv and QPool are discussed later in this tutorial.
1.3 Building blocks for TensorFlow
One way to solve this problem with TensorFlow Quantum is to implement the following:
The input to the model is a circuit tensor—either an empty circuit or an X gate on a particular qubit indicating an excitation.
The rest of the model's quantum components are constructed with
tfq.layers.AddCircuit
layers.For inference a
tfq.layers.PQC
layer is used. This reads and compares it to a label of 1 for an excited state, or -1 for a non-excited state.
1.4 Data
Before building your model, you can generate your data. In this case it's going to be excitations to the cluster state (The original paper uses a more complicated dataset). Excitations are represented with cirq.rx
gates. A large enough rotation is deemed an excitation and is labeled 1
and a rotation that isn't large enough is labeled -1
and deemed not an excitation.
You can see that just like with regular machine learning you create a training and testing set to use to benchmark the model. You can quickly look at some datapoints with:
1.5 Define layers
Now define the layers shown in the figure above in TensorFlow.
1.5.1 Cluster state
The first step is to define the cluster state using Cirq, a Google-provided framework for programming quantum circuits. Since this is a static part of the model, embed it using the tfq.layers.AddCircuit
functionality.
Display a cluster state circuit for a rectangle of cirq.GridQubit
s:
1.5.2 QCNN layers
Define the layers that make up the model using the Cong and Lukin QCNN paper. There are a few prerequisites:
The one- and two-qubit parameterized unitary matrices from the Tucci paper.
A general parameterized two-qubit pooling operation.
To see what you created, print out the one-qubit unitary circuit:
And the two-qubit unitary circuit:
And the two-qubit pooling circuit:
1.5.2.1 Quantum convolution
As in the Cong and Lukin paper, define the 1D quantum convolution as the application of a two-qubit parameterized unitary to every pair of adjacent qubits with a stride of one.
Display the (very horizontal) circuit:
1.5.2.2 Quantum pooling
A quantum pooling layer pools from qubits to qubits using the two-qubit pool defined above.
Examine a pooling component circuit:
1.6 Model definition
Now use the defined layers to construct a purely quantum CNN. Start with eight qubits, pool down to one, then measure .
1.7 Train the model
Train the model over the full batch to simplify this example.
2. Hybrid models
You don't have to go from eight qubits to one qubit using quantum convolution—you could have done one or two rounds of quantum convolution and fed the results into a classical neural network. This section explores quantum-classical hybrid models.
2.1 Hybrid model with a single quantum filter
Apply one layer of quantum convolution, reading out on all bits, followed by a densely-connected neural network.
2.1.1 Model definition
2.1.2 Train the model
As you can see, with very modest classical assistance, the hybrid model will usually converge faster than the purely quantum version.
2.2 Hybrid convolution with multiple quantum filters
Now let's try an architecture that uses multiple quantum convolutions and a classical neural network to combine them.