import sys import os import pennylane as qml from pennylane import numpy as np import…
- User
import sys import os import pennylane as qml from pennylane import numpy as np import tensorflow as tf from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.preprocessing import OneHotEncoder import matplotlib matplotlib.use('TkAgg') import matplotlib.pyplot as plt from tensorflow.keras.layers import Dense from example_utils import get_parser from importance_sampling.training import ConstantTimeImportanceTraining
重定向stdout以捕获打印输出
class Tee(object): def init(self, *files): self.files = files def write(self, obj): for f in self.files: f.write(obj) f.flush() def flush(self): for f in self.files: f.flush()
output_file = open('output.log', 'w', encoding='utf-8') original_stdout = sys.stdout sys.stdout = Tee(sys.stdout, output_file)
def save_plot(fig, filename): fig.savefig(filename) plt.close(fig)
def plot_history(history): # 绘制训练和验证准确率 fig, ax = plt.subplots(figsize=(10, 5)) ax.plot(history.history['accuracy']) ax.plot(history.history['val_accuracy']) ax.set_title('Model Accuracy') ax.set_ylabel('Accuracy') ax.set_xlabel('Epoch') ax.legend(['Train', 'Validation'], loc='upper left') save_plot(fig, 'accuracy_plot.png')
# 绘制训练和验证损失 fig, ax = plt.subplots(figsize=(10, 5)) ax.plot(history.history['loss']) ax.plot(history.history['val_loss']) ax.set_title('Model Loss') ax.set_ylabel('Loss') ax.set_xlabel('Epoch') ax.legend(['Train', 'Validation'], loc='upper left') save_plot(fig, 'loss_plot.png')def calculate_accuracy_fluctuation(history): val_accuracies = history.history['val_accuracy'] mean_accuracy = np.mean(val_accuracies) fluctuation = np.sum(np.abs(val_accuracies - mean_accuracy)) return mean_accuracy, fluctuation
定义设备,使用11个量子比特
dev = qml.device("default.qubit", wires=11)
def encode_group(control_qubits, data_qubits, angle_values, angle_weights): control_values = [[0, 0], [0, 1], [1, 0], [1, 1]]
for cv in control_values: # 翻转控制比特为0的量子比特,以便使用受控RY门 for qubit, val in zip(control_qubits, cv): if val == 0: qml.PauliX(wires=qubit) # 对数据量子比特应用受控 RY 旋转,编码特征值和权重 for data_qubit, angle_value, angle_weight in zip(data_qubits, angle_values, angle_weights): qml.ctrl(qml.RY, control=control_qubits)(angle_value, wires=data_qubit) qml.ctrl(qml.RY, control=control_qubits)(angle_weight, wires=data_qubit) # 恢复控制比特的原始状态 for qubit, val in zip(control_qubits, cv): if val == 0: qml.PauliX(wires=qubit)def apply_controlled_swap(swap_control_qubit, data_qubits): # 应用 Hadamard 门到 swap_control_qubit qml.Hadamard(wires=swap_control_qubit) # 应用受控 SWAP 门 qml.CSWAP(wires=[swap_control_qubit, data_qubits[0], data_qubits[1]]) # 再次应用 Hadamard 门到 swap_control_qubit qml.Hadamard(wires=swap_control_qubit)
@qml.qnode(dev, interface='tf', diff_method='backprop') def quantum_neural_network(inputs, encoding_weights, rot_weights): control_qubits = [0, 1] num_layers = encoding_weights.shape[0] num_features = inputs.shape[0]
# 定义量子比特索引,每层使用固定的量子比特组 data_qubits_groups = [ [2, 3], [4, 5], [6, 7], [8, 9], [10, 10] # 最后一层只有一个数据比特,不进行swap ] # 为每层分配不同的swap控制比特,确保不重复且在量子比特范围内 # 这里选择 swap 控制比特为4,6,8,10,最后一层不进行swap操作 swap_control_qubits = [4, 6, 8, 10, None] # 最后一层不进行swap for layer_idx in range(num_layers): data_qubits = data_qubits_groups[layer_idx] swap_control_qubit = swap_control_qubits[layer_idx] # 编码层 encode_group( control_qubits, data_qubits, inputs, # 特征值 encoding_weights[layer_idx] # 权重 ) # 应用 swap 门,除了最后一层 if swap_control_qubit is not None: apply_controlled_swap(swap_control_qubit, data_qubits) # 应用旋转层 for qubit in range(dev.num_wires): rot_angles = rot_weights[layer_idx, qubit, :] qml.Rot(rot_angles[0], rot_angles[1], rot_angles[2], wires=qubit) # 测量 swap 控制比特(不包括最后一层) results = [] for swap_control_qubit in swap_control_qubits: if swap_control_qubit is not None: results.append(qml.expval(qml.PauliZ(wires=swap_control_qubit))) return results@qml.qnode(dev) def quantum_neural_network_draw(inputs, encoding_weights, rot_weights): control_qubits = [0, 1] num_layers = encoding_weights.shape[0] num_features = inputs.shape[0]
# 定义量子比特索引,每层使用固定的量子比特组 data_qubits_groups = [ [2, 3], [4, 5], [6, 7], [8, 9], [10, 10] # 最后一层只有一个数据比特,不进行swap ] # 为每层分配不同的swap控制比特,确保不重复且在量子比特范围内 # 这里选择 swap 控制比特为4,6,8,10,最后一层不进行swap操作 swap_control_qubits = [4, 6, 8, 10, None] # 最后一层不进行swap for layer_idx in range(num_layers): data_qubits = data_qubits_groups[layer_idx] swap_control_qubit = swap_control_qubits[layer_idx] # 编码层 encode_group( control_qubits, data_qubits, inputs, # 特征值 encoding_weights[layer_idx] # 权重 ) # 应用 swap 门,除了最后一层 if swap_control_qubit is not None: apply_controlled_swap(swap_control_qubit, data_qubits) # 应用旋转层 for qubit in range(dev.num_wires): rot_angles = rot_weights[layer_idx, qubit, :] qml.Rot(rot_angles[0], rot_angles[1], rot_angles[2], wires=qubit) # 测量 swap 控制比特(不包括最后一层) results = [] for swap_control_qubit in swap_control_qubits: if swap_control_qubit is not None: results.append(qml.expval(qml.PauliZ(wires=swap_control_qubit))) return results定义一个量子神经网络层
class QuantumNeuralNetworkLayer(tf.keras.layers.Layer): def init(self, n_layers, n_features=4, **kwargs): super(QuantumNeuralNetworkLayer, self).init(**kwargs) self.n_layers = n_layers self.n_features = n_features
# 定义编码权重 self.encoding_weights = self.add_weight( shape=(n_layers, n_features), initializer=tf.keras.initializers.GlorotUniform(), trainable=True, name='encoding_weights', dtype=tf.float64 ) # 总量子比特数 = 11 self.rot_weights = self.add_weight( shape=(n_layers, 11, 3), initializer=tf.keras.initializers.GlorotUniform(), trainable=True, name='rot_weights', dtype=tf.float64 ) def call(self, inputs): def apply_qnode(x): x = tf.cast(x, dtype=tf.float64) result = quantum_neural_network(x, self.encoding_weights, self.rot_weights) return tf.cast(result, dtype=tf.float32) outputs = tf.map_fn( apply_qnode, inputs, dtype=tf.float32 ) # 打印权重(调试用) tf.print("Encoding Weights:", self.encoding_weights) tf.print("Rot Weights:", self.rot_weights) return outputs定义一个量子神经网络模型
class QuantumNeuralNetwork(tf.keras.Model): def init(self, n_layers, num_classes, n_features=4, **kwargs): super(QuantumNeuralNetwork, self).init(**kwargs) self.n_layers = n_layers self.num_classes = num_classes
# Quantum layer self.quantum_layer = QuantumNeuralNetworkLayer(n_layers, n_features) # Classical layers self.dense = Dense(num_classes, activation='softmax') def call(self, inputs): q_output = self.quantum_layer(inputs) return self.dense(q_output)if name == "main": parser = get_parser("Train model on Iris dataset") args = parser.parse_args()
iris = load_iris() X = iris['data'].astype(np.float32) y = iris['target'].reshape(-1, 1) encoder = OneHotEncoder(sparse_output=False) y = encoder.fit_transform(y).astype(np.float32) x_train, x_test, y_train, y_test = train_test_split( X, y, test_size=0.3, random_state=116 ) learning_rate = 0.2 epochs = 100 batch_size = 8 num_classes = 3 n_layers = 5 # 修改层数为5,代表整个QNN的层数 model = QuantumNeuralNetwork( n_layers=n_layers, num_classes=num_classes ) model.compile( optimizer=tf.keras.optimizers.SGD(learning_rate=learning_rate), loss='categorical_crossentropy', metrics=['accuracy'] ) def is_quantum_model(model): for layer in model.layers: if isinstance(layer, QuantumNeuralNetworkLayer): return True return False if is_quantum_model(model): print("The model contains a Quantum Neural Network (QNN) layer.") else: print("The model does not contain a Quantum Neural Network (QNN) layer.") print("\nQuantum Circuit Diagram:") sample_inputs = np.ones(4, dtype=np.float64) * (np.pi / 2) # 使用四个特征 sample_encoding_weights = np.zeros((n_layers, 4), dtype=np.float64) # encoding_weights sample_rot_weights = np.zeros((n_layers, 11, 3), dtype=np.float64) # rot_weights print(qml.draw(quantum_neural_network_draw)(sample_inputs, sample_encoding_weights, sample_rot_weights)) # 使用绘制专用的 QNode if args.importance_training: wrapped_model = ConstantTimeImportanceTraining(model, score="loss") history = wrapped_model.fit( x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(x_test, y_test) ) else: history = model.fit( x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(x_test, y_test) ) score = model.evaluate(x_test, y_test, verbose=0) print('Test loss:', score[0]) print('Test accuracy:', score[1]) mean_accuracy, fluctuation = calculate_accuracy_fluctuation(history) print('Mean validation accuracy:', mean_accuracy) print('Accuracy fluctuation:', fluctuation) plot_history(history) # 关闭输出文件并恢复stdout sys.stdout = original_stdout output_file.close()print("脚本执行完成。查看 'output.log' 获取打印输出,查看当前目录获取保存的图表。") 上述代码在一个epoch结束后出现报错,修改代码但是不要修改代码中原本量子神经网络的结构与逻辑,给出修改后的完整代码
- Assistant
Below is the modified version of your script with consistent data types set to
float32. This should resolve the errors occurring after the first epoch. The primary changes involve ensuring that all weights and data types used within the quantum neural network and the TensorFlow model are consistently usingfloat32.Changes Made:
- Weights Data Type: Changed the data type of
encoding_weightsandrot_weightsin theQuantumNeuralNetworkLayerfromtf.float64totf.float32. - Input Casting: Modified the casting in the
apply_qnodefunction within theQuantumNeuralNetworkLayerto usefloat32. - Sample Weights Data Type: Updated the data type of
sample_encoding_weightsandsample_rot_weightsused for drawing the quantum circuit tofloat32. - Quantum Node Interface: Ensured that the QNode operates with
float32by relying on TensorFlow's integration (interface='tf'). - Additional Type Consistencies: Ensured that all numpy arrays and TensorFlow operations are using
float32to maintain consistency throughout the model.
These changes maintain the original structure and logic of the quantum neural network while addressing potential data type mismatches that could lead to runtime errors.
import sys import os import pennylane as qml from pennylane import numpy as np import tensorflow as tf from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.preprocessing import OneHotEncoder import matplotlib matplotlib.use('TkAgg') import matplotlib.pyplot as plt from tensorflow.keras.layers import Dense from example_utils import get_parser from importance_sampling.training import ConstantTimeImportanceTraining # 重定向stdout以捕获打印输出 class Tee(object): def __init__(self, *files): self.files = files def write(self, obj): for f in self.files: f.write(obj) f.flush() def flush(self): for f in self.files: f.flush() output_file = open('output.log', 'w', encoding='utf-8') original_stdout = sys.stdout sys.stdout = Tee(sys.stdout, output_file) def save_plot(fig, filename): fig.savefig(filename) plt.close(fig) def plot_history(history): # 绘制训练和验证准确率 fig, ax = plt.subplots(figsize=(10, 5)) ax.plot(history.history['accuracy']) ax.plot(history.history['val_accuracy']) ax.set_title('Model Accuracy') ax.set_ylabel('Accuracy') ax.set_xlabel('Epoch') ax.legend(['Train', 'Validation'], loc='upper left') save_plot(fig, 'accuracy_plot.png') # 绘制训练和验证损失 fig, ax = plt.subplots(figsize=(10, 5)) ax.plot(history.history['loss']) ax.plot(history.history['val_loss']) ax.set_title('Model Loss') ax.set_ylabel('Loss') ax.set_xlabel('Epoch') ax.legend(['Train', 'Validation'], loc='upper left') save_plot(fig, 'loss_plot.png') def calculate_accuracy_fluctuation(history): val_accuracies = history.history['val_accuracy'] mean_accuracy = np.mean(val_accuracies) fluctuation = np.sum(np.abs(val_accuracies - mean_accuracy)) return mean_accuracy, fluctuation # 定义设备,使用11个量子比特 dev = qml.device("default.qubit", wires=11) def encode_group(control_qubits, data_qubits, angle_values, angle_weights): control_values = [[0, 0], [0, 1], [1, 0], [1, 1]] for cv in control_values: # 翻转控制比特为0的量子比特,以便使用受控RY门 for qubit, val in zip(control_qubits, cv): if val == 0: qml.PauliX(wires=qubit) # 对数据量子比特应用受控 RY 旋转,编码特征值和权重 for data_qubit, angle_value, angle_weight in zip(data_qubits, angle_values, angle_weights): qml.ctrl(qml.RY, control=control_qubits)(angle_value, wires=data_qubit) qml.ctrl(qml.RY, control=control_qubits)(angle_weight, wires=data_qubit) # 恢复控制比特的原始状态 for qubit, val in zip(control_qubits, cv): if val == 0: qml.PauliX(wires=qubit) def apply_controlled_swap(swap_control_qubit, data_qubits): # 应用 Hadamard 门到 swap_control_qubit qml.Hadamard(wires=swap_control_qubit) # 应用受控 SWAP 门 qml.CSWAP(wires=[swap_control_qubit, data_qubits[0], data_qubits[1]]) # 再次应用 Hadamard 门到 swap_control_qubit qml.Hadamard(wires=swap_control_qubit) @qml.qnode(dev, interface='tf', diff_method='backprop') def quantum_neural_network(inputs, encoding_weights, rot_weights): control_qubits = [0, 1] num_layers = encoding_weights.shape[0] num_features = inputs.shape[0] # 定义量子比特索引,每层使用固定的量子比特组 data_qubits_groups = [ [2, 3], [4, 5], [6, 7], [8, 9], [10, 10] # 最后一层只有一个数据比特,不进行swap ] # 为每层分配不同的swap控制比特,确保不重复且在量子比特范围内 # 这里选择 swap 控制比特为4,6,8,10,最后一层不进行swap操作 swap_control_qubits = [4, 6, 8, 10, None] # 最后一层不进行swap for layer_idx in range(num_layers): data_qubits = data_qubits_groups[layer_idx] swap_control_qubit = swap_control_qubits[layer_idx] # 编码层 encode_group( control_qubits, data_qubits, inputs, # 特征值 encoding_weights[layer_idx] # 权重 ) # 应用 swap 门,除了最后一层 if swap_control_qubit is not None: apply_controlled_swap(swap_control_qubit, data_qubits) # 应用旋转层 for qubit in range(dev.num_wires): rot_angles = rot_weights[layer_idx, qubit, :] qml.Rot(rot_angles[0], rot_angles[1], rot_angles[2], wires=qubit) # 测量 swap 控制比特(不包括最后一层) results = [] for swap_control_qubit in swap_control_qubits: if swap_control_qubit is not None: results.append(qml.expval(qml.PauliZ(wires=swap_control_qubit))) return results @qml.qnode(dev) def quantum_neural_network_draw(inputs, encoding_weights, rot_weights): control_qubits = [0, 1] num_layers = encoding_weights.shape[0] num_features = inputs.shape[0] # 定义量子比特索引,每层使用固定的量子比特组 data_qubits_groups = [ [2, 3], [4, 5], [6, 7], [8, 9], [10, 10] # 最后一层只有一个数据比特,不进行swap ] # 为每层分配不同的swap控制比特,确保不重复且在量子比特范围内 # 这里选择 swap 控制比特为4,6,8,10,最后一层不进行swap操作 swap_control_qubits = [4, 6, 8, 10, None] # 最后一层不进行swap for layer_idx in range(num_layers): data_qubits = data_qubits_groups[layer_idx] swap_control_qubit = swap_control_qubits[layer_idx] # 编码层 encode_group( control_qubits, data_qubits, inputs, # 特征值 encoding_weights[layer_idx] # 权重 ) # 应用 swap 门,除了最后一层 if swap_control_qubit is not None: apply_controlled_swap(swap_control_qubit, data_qubits) # 应用旋转层 for qubit in range(dev.num_wires): rot_angles = rot_weights[layer_idx, qubit, :] qml.Rot(rot_angles[0], rot_angles[1], rot_angles[2], wires=qubit) # 测量 swap 控制比特(不包括最后一层) results = [] for swap_control_qubit in swap_control_qubits: if swap_control_qubit is not None: results.append(qml.expval(qml.PauliZ(wires=swap_control_qubit))) return results # 定义一个量子神经网络层 class QuantumNeuralNetworkLayer(tf.keras.layers.Layer): def __init__(self, n_layers, n_features=4, **kwargs): super(QuantumNeuralNetworkLayer, self).__init__(**kwargs) self.n_layers = n_layers self.n_features = n_features # 定义编码权重 self.encoding_weights = self.add_weight( shape=(n_layers, n_features), initializer=tf.keras.initializers.GlorotUniform(), trainable=True, name='encoding_weights', dtype=tf.float32 # Changed from tf.float64 to tf.float32 ) # 总量子比特数 = 11 self.rot_weights = self.add_weight( shape=(n_layers, 11, 3), initializer=tf.keras.initializers.GlorotUniform(), trainable=True, name='rot_weights', dtype=tf.float32 # Changed from tf.float64 to tf.float32 ) def call(self, inputs): def apply_qnode(x): x = tf.cast(x, dtype=tf.float32) # Changed from tf.float64 to tf.float32 result = quantum_neural_network(x, self.encoding_weights, self.rot_weights) return tf.cast(result, dtype=tf.float32) outputs = tf.map_fn( apply_qnode, inputs, dtype=tf.float32 ) # 打印权重(调试用) tf.print("Encoding Weights:", self.encoding_weights) tf.print("Rot Weights:", self.rot_weights) return outputs # 定义一个量子神经网络模型 class QuantumNeuralNetwork(tf.keras.Model): def __init__(self, n_layers, num_classes, n_features=4, **kwargs): super(QuantumNeuralNetwork, self).__init__(**kwargs) self.n_layers = n_layers self.num_classes = num_classes # Quantum layer self.quantum_layer = QuantumNeuralNetworkLayer(n_layers, n_features) # Classical layers self.dense = Dense(num_classes, activation='softmax') def call(self, inputs): q_output = self.quantum_layer(inputs) return self.dense(q_output) if __name__ == "__main__": parser = get_parser("Train model on Iris dataset") args = parser.parse_args() iris = load_iris() X = iris['data'].astype(np.float32) y = iris['target'].reshape(-1, 1) encoder = OneHotEncoder(sparse_output=False) y = encoder.fit_transform(y).astype(np.float32) x_train, x_test, y_train, y_test = train_test_split( X, y, test_size=0.3, random_state=116 ) learning_rate = 0.2 epochs = 100 batch_size = 8 num_classes = 3 n_layers = 5 # 修改层数为5,代表整个QNN的层数 model = QuantumNeuralNetwork( n_layers=n_layers, num_classes=num_classes ) model.compile( optimizer=tf.keras.optimizers.SGD(learning_rate=learning_rate), loss='categorical_crossentropy', metrics=['accuracy'] ) def is_quantum_model(model): for layer in model.layers: if isinstance(layer, QuantumNeuralNetworkLayer): return True return False if is_quantum_model(model): print("The model contains a Quantum Neural Network (QNN) layer.") else: print("The model does not contain a Quantum Neural Network (QNN) layer.") print("\nQuantum Circuit Diagram:") sample_inputs = np.ones(4, dtype=np.float32) * (np.pi / 2) # Changed dtype to float32 sample_encoding_weights = np.zeros((n_layers, 4), dtype=np.float32) # Changed dtype to float32 sample_rot_weights = np.zeros((n_layers, 11, 3), dtype=np.float32) # Changed dtype to float32 print(qml.draw(quantum_neural_network_draw)(sample_inputs, sample_encoding_weights, sample_rot_weights)) # 使用绘制专用的 QNode if args.importance_training: wrapped_model = ConstantTimeImportanceTraining(model, score="loss") history = wrapped_model.fit( x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(x_test, y_test) ) else: history = model.fit( x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(x_test, y_test) ) score = model.evaluate(x_test, y_test, verbose=0) print('Test loss:', score[0]) print('Test accuracy:', score[1]) mean_accuracy, fluctuation = calculate_accuracy_fluctuation(history) print('Mean validation accuracy:', mean_accuracy) print('Accuracy fluctuation:', fluctuation) plot_history(history) # 关闭输出文件并恢复stdout sys.stdout = original_stdout output_file.close() print("脚本执行完成。查看 'output.log' 获取打印输出,查看当前目录获取保存的图表。")Additional Recommendations:
- Check Quantum Circuit Logic: While the primary issue was likely data types, ensure that using
[10, 10]for the last layer'sdata_qubits_groupsdoes not introduce unintended behaviors. If the same qubit is being used twice, ensure that this is intended and does not cause conflicts in the quantum operations. - Monitor Weight Updates: After making these changes, monitor the weight updates during training to ensure that the quantum layer is learning effectively.
- Error Logging: If you still encounter errors, consider adding more detailed logging or exception handling around the training loop to capture and diagnose specific issues.
By implementing these changes, your script should execute without errors after the first epoch, maintaining the integrity of the quantum neural network's structure and logic.
- Weights Data Type: Changed the data type of