Integration Guide: Quantum Attention Head
1. Prerequisites
torch$\ge 2.0$numpy$\ge 1.21$cupy(Optional, for GPU acceleration)
2. Implementation Steps
Step 1: QPU Initialization
Initialize the HyperspaceQPU with the desired qubit count and bond dimension.
from core.mini_qpu import HyperspaceQPU
qpu = HyperspaceQPU(n_qubits=4, bond_dimension=16)
Step 2: Projection Layer
Create a linear layer to map the model dimension $d_{\text{model}}$ to the QPU parameter space.
self.proj = nn.Linear(d_model, n_qubits * 2)
Step 3: The Forward Pass
Integrate the QPU call within the attention mechanism:
- Project input to rotation angles.
- Apply gates to the QPU state.
- Extract the expectation value as a scalar.
- Scale the attention output by this scalar.
Step 4: Block Integration
Wrap the QuantumAttentionHead within a standard Transformer block, ensuring a LayerNorm follows the quantum path to prevent gradient explosion.
3. Verification
Run the sanity test provided in core/sovereign_transformer.py. A successful integration is confirmed when the output shape matches the input shape and the expectation value is non-zero.