# A Reconfigurable Framework for Hybrid Quantum-Classical Computing
## 3 Proposed Framework
### Quantum Machine Learning Task
#### 3.1.4 Training: Gradient Computation and Loss Function
#pragma HLS INTERFACE s_axilite port=return bund[e=control] #pragma HLS INTERFACE s_axilite port=learning_rate bundle=control #pragma HLS INTERFACE s_axilite port=epsilon bundle=control #pragma HLS INTERFACE s_axilite port=n_qubits bundle=control #pragma HLS INTERFACE s_axilite port=n_layers bundle=control
#pragma HLS INTERFACE m_axil port=y_pred offset=slave #pragma HLS INTERFACE m_axil port=y_pred_plus_batch offset=slave #pragma HLS INTERFACE m_axil port=y_true offset=slave #pragma HLS INTERFACE m_axil port=grades offset=slave #pragma HLS INTERFACE m_axil port=updated_weights offset=slave #pragma HLS INTERFACE m_axil port=loss_out offsets=slave #pragma HLS INTERFACE s_axilite port=y_pred bund[e=control] #pragma HLS INTERFACE s_axilite port=y_pred_plus_batch bundle=control #pragma HLS INTERFACE s_axilite port=y_true bund[e=control] #pragma HLS INTERFACE s_axilite port=grades bundle=control #pragma HLS INTERFACE s_axilite port=updated_weights bundle=control #pragma HLS INTERFACE s_axilite port=loss_out bundle=control #pragma HLS INTERFACE s_axilite port=current_weights bundle=control  
The processing pipeline block is the computational core, where the three critical computations (loss calculation, gradient computation, and weight updates) occur. The initial loss calculation block takes the predicted probability vector _y_pred_local_ and the true label vector _y_true_local_ as input. It calculates the baseline cross-entropy loss using the negative log-likelihood function \(-log(y\_pred)\), where _y_pred_ is the predicted probability, with hardware-optimized logarithmic units and #pragma HLS UNROLL for parallel computation. This hardware optimization, achieved by translating hls:log() into dedicated FPGA logic using techniques like CORDIC and lookup tables, significantly reduces latency and increases throughput compared to CPU-based methods, which is crucial for iterative gradient calculations. The output is the original_loss value. The perturbed loss is calculated by first slightly altering each weight in the model, generating a new set of predicted outputs stored in \(y\_pred\_plus\_batch\). Within the kernel's perturbed loss compute block, these perturbed predictions are loaded into \(pred\_plus\_buffer\), and the loss is then computed using the same negative log-likelihood formula as the original loss: \(-\log(pred\_plus\_buffer[][i]+1e^{-8})\). This process leverages #pragma HLS ARRAY_PARTITION and #pragma HLS UNROLL directives for efficient data handling. It determines how changes in individual weights influence the model's loss. This provides the necessary information for gradient computation and subsequent weight updates during the optimization process. In the processing pipeline, the gradient computation (accelerating stochastic gradient descent calculations through block processing) and weights update (performing parallel parameter updates using computed gradients and configurable learning rates) stages perform critical calculations. Gradient descent updates the weights by first calculating the gradients using (perturbed_loss \(-\) original_loss)/epsilon, see (2) and (3), where original_loss and perturbed_loss are loss values before and after weight perturbation, and epsilon is a small perturbation value. Each gradient, representing the loss function's slope with respect to the corresponding weight, is then used to update the weights according to (6). \[updated\_weights[idx]=weights\_local[i]-learning\_rate*grad[idx] \tag{6}\]  
where \(learning\_rate\) controls the step size. This pipeline leverages #pragma HLS UNROLL and #pragma HLS ARRAY_PARTITION on weights_local and pred_plus_buffer to paral  
Figure 5: HLS pragma interface. -leize computations across blocks of 16 elements (BLOCK_SIZE) and HLS_PIPELINE II=1 directives to maximize throughput. The hyper-parameters block supplies the necessary scalar values learning_rate and epsilon, which are interfaced via AXI-Lite.