Quantum-Enhanced Multi-Head Attention for Hybrid Classical-Quantum Neural Networks
收藏NIAID Data Ecosystem2026-05-02 收录
下载链接:
https://data.mendeley.com/datasets/z5wcdpc45g
下载链接
链接失效反馈官方服务:
资源简介:
This code integrates quantum-inspired computations into a deep learning pipeline to classify images from the CIFAR-10 dataset. It combines traditional neural network layers with a quantum multi-head attention mechanism, leveraging the capabilities of both classical and quantum approaches. The model utilizes PyTorch for defining and training the network, while Qiskit and Qiskit Aer simulate quantum circuits for the quantum-inspired components. The CIFAR-10 dataset is preprocessed using torchvision, with subsets created for efficient training and validation.
The quantum multi-head attention mechanism is implemented as a custom PyTorch autograd function, QuantumMultiHeadAttentionFunction. This function defines the forward and backward passes, where data is encoded into quantum circuits and parameter updates are computed using finite-difference approximation. The function relies on Z-expectation values of qubits, simulated using Qiskit Aer's statevector simulator. These computations are encapsulated in the QuantumMultiHeadAttentionLayer, which integrates seamlessly with PyTorch's module structure.
The core model, HybridAttentionCIFAR, consists of a combination of convolutional layers, a linear embedding layer, the quantum attention layer, and a fully connected output layer. The convolutional layers extract features from input images, which are then transformed into embeddings compatible with the quantum attention layer. The quantum attention mechanism processes these embeddings, enhancing feature representation, and the final fully connected layer maps the processed embeddings to class logits for classification.
The get_cifar_subset function prepares the CIFAR-10 data, creating small subsets for training and validation. These subsets are batched using PyTorch's DataLoader. The train_cifar_qattn function orchestrates the training process, configuring the model, optimizer, and loss function. Over multiple epochs, it computes training and validation losses and accuracies, updating model parameters using the Adam optimizer.
本代码将类量子计算集成至深度学习流水线中,用于对CIFAR-10数据集(CIFAR-10)中的图像进行分类。该方案将传统神经网络层与量子多头注意力机制相结合,充分利用经典与量子两种计算范式的优势。模型依托PyTorch完成网络的定义与训练,同时使用Qiskit与Qiskit Aer对类量子模块的量子线路进行仿真。CIFAR-10数据集通过torchvision进行预处理,并构建其子集以实现高效的训练与验证流程。
量子多头注意力机制被实现为自定义PyTorch自动求导函数QuantumMultiHeadAttentionFunction。该函数定义了前向与反向传播流程:数据被编码至量子线路中,参数更新则通过有限差分近似法计算。函数依赖量子比特的Z方向期望值,并借助Qiskit Aer的态矢量模拟器完成仿真。上述计算逻辑被封装至QuantumMultiHeadAttentionLayer中,该模块可与PyTorch的模块结构无缝集成。
核心模型HybridAttentionCIFAR由卷积层、线性嵌入层、量子注意力层与全连接输出层组合而成。卷积层从输入图像中提取特征,随后将其转换为可适配量子注意力层的嵌入向量。量子注意力机制对这些嵌入向量进行处理,以强化特征表达能力;最终的全连接层则将处理后的嵌入向量映射至分类所需的类别对数似然(logits)。
get_cifar_subset函数用于预处理CIFAR-10数据集,构建用于训练与验证的小型子集。这些子集通过PyTorch的DataLoader进行批处理。train_cifar_qattn函数统筹整个训练流程,完成模型、优化器与损失函数的配置。在多轮训练周期(epoch)中,该函数计算训练与验证集的损失及准确率,并借助Adam优化器更新模型参数。
创建时间:
2025-02-11



