Hello, every one. I am trying to construct a quantum convolutional nerural network with the article Hybrid Quantum-Classical Convolutional Neural Networks. In my codes, I write a convolutional kernel, and like classical convolutional neural networks, sliding convolution kernels. I also use the maxpooling layers. After two quantum convolutional and two maxpooling layers, adding the classical fully connections layers. But there is an error.
-- coding: utf-8 --
import pennylane as qml
from pennylane import numpy as np
from pennylane.templates import RandomLayers
import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
import unitary
n_epochs = 30 # Number of optimization epochs
n_layers = 1 # Number of random layers
n_train = 50 # Size of the train dataset
n_test = 30 # Size of the test dataset
PREPROCESS = True # If False, skip quantum processing and load data from SAVE_PATH
np.random.seed(0) # Seed for NumPy random number generator
tf.random.set_seed(0) # Seed for TensorFlow random number generator
#要想测量出值来,必须加上@qml.qnode(devl)
mnist_dataset = keras.datasets.mnist
(train_images, train_labels), (test_images, test_labels) = mnist_dataset.load_data()
Reduce dataset size
train_images = train_images[:n_train]
train_labels = train_labels[:n_train]
test_images = test_images[:n_test]
test_labels = test_labels[:n_test]
Normalize pixel values within 0 and 1
train_images = train_images / 255
test_images = test_images / 255
Add extra dimension for convolution channels
train_images = tf.reshape(train_images,[-1,784])
test_images = tf.reshape(test_images,[-1,784])
train_images = tf.constant(train_images)
test_images = tf.constant(train_images)
train_labels = tf.one_hot(train_labels, depth=10)
test_labels = tf.one_hot(test_labels, depth=10)
#数据编码
def embedding(x):
index=0
for i in range(2):
for j in range(2):
qml.RY(np.pi * x[i][j], wires=index)
index = index+1
#滤波器
def conv_filter1(U,params):
U(params, wires=[0,1])
U(params, wires=[2,3])
U(params, wires=[1,2])
#卷积滤波器线路
dev1 = qml.device(“default.qubit”, wires = 4)
@qml.qnode(dev1)
def circuit_block(x,U,params,U_params):
# Encoding of 4 classical input values
param1 = params[0:U_params]
param2 = params[U_params: 2 * U_params]
param3 = params[2 * U_params: 3 * U_params]
param4 = params[3 * U_params: 4 * U_params]
embedding(x)
conv_filter1(U, param1)
conv_filter1(U, param2)
conv_filter1(U, param3)
conv_filter1(U, param4)
# Measurement producing 4 classical output values
return [qml.expval(qml.PauliZ(j)) for j in range(4)]
def qconv(images,conv_param,U,params,U_params):
stride, pad = conv_param[‘stride’], conv_param[‘pad’]
if(images.shape[1]=1):
H, W = images.shape #N表示数量,C表示通道,H表示图像高,W表示图像宽
x_padded = np.pad(images, (pad, pad), mode=‘constant’)#填充
#滤波器的维数2*2
HH =2
WW=2
F=1
H_new = 1 + (H + 2 * pad - HH) // stride
W_new = 1 + (W + 2 * pad - WW) // stride
s = stride
out = np.zeros((H_new, W_new)) #卷积后的输出初始化
for j in range(H_new):
for k in range(W_new):
#print x_padded[i, :, j*s:HH+j*s, k*s:WW+k*s].shape
#print w[f].shape
#print b.shape
#print np.sum((x_padded[i, :, j*s:HH+j*s, k*s:WW+k*s] * w[f]))
q_out = circuit_block(x_padded[js:HH+js, ks:WW+ks],U, params,U_params)
q_sum = 0
for i in range(4):
q_sum = q_sum + q_out[i]
out[j, k] = q_sum
return out
def qmaxpool(x,pool_param):
HH, WW = pool_param[‘pool_height’], pool_param[‘pool_width’]
s = pool_param[‘stride’]
H, W = x.shape
H_new = 1 + (H - HH) // s
W_new = 1 + (W - WW) // s
out = np.zeros((H_new, W_new))
for k in range(H_new):
for l in range(W_new):
window = x[ks:HH+ks, ls:WW+ls]
out[k, l] = np.max(window)
return out
dev = qml.device(“default.qubit”, wires = 4)
@qml.qnode(dev)
def qnode(inputs,params):
# filter_size = 2 #滤波器的大小2*2
#conv_param = {‘stride’: 1, ‘pad’: (filter_size - 1) // 2}
conv_param = {‘stride’: 1, ‘pad’: 1}
pool_param = {‘pool_height’: 2, ‘pool_width’: 2, ‘stride’: 2}
if(inputs.shape[0]==784):
inputs = tf.reshape(inputs,[28,28])
qconv1 = qconv(inputs, conv_param, unitary.U_TTN, params[0:8], U_params = 2)
qmaxpool1 = qmaxpool(qconv1, pool_param)
qconv2 = qconv(qmaxpool1, conv_param, unitary.U_TTN, params[8:16], U_params = 2)
qmaxpool2 = qmaxpool(qconv2, pool_param)
qmaxpool2 = tf.reshape(qmaxpool2,[49])
return qmaxpool2
weight_shapes = {“params”: (16,1)} #参数名必须和qnode中的参数名一致
qlayer = qml.qnn.KerasLayer(qnode, weight_shapes, output_dim = 10)
clayer1 = tf.keras.layers.Dense(10)
clayer2 = tf.keras.layers.Dense(10, activation=“softmax”)
q_model = tf.keras.models.Sequential([qlayer, clayer1, clayer2])
opt = tf.keras.optimizers.SGD(learning_rate=0.5)
q_model.compile(
optimizer=‘adam’,
loss=“sparse_categorical_crossentropy”,
metrics=[“accuracy”],
) #model.compile()方法用于在配置训练方法时,告知训练时用的优化器、损失函数和准确率评测标准
q_history = q_model.fit( #数据集数据类型必须是张量,将训练数据在模型中训练一定次数,返回loss和测量指标
train_images,
train_labels,
validation_data=(test_images,test_labels),
batch_size=4,
epochs=2,
verbose=2,
)
Can you give me some advice ? thank you