Devel swap fusion#798
Open
VinaYrdx wants to merge 6 commits into
Open
Conversation
Member
|
Hi there, |
Author
|
@TysonRayJones Hello, yes this is Vinay, i understood completely, I'll remove out the Kahan changes and keep just the SWAP fusion. Thanks you. And you noticed :) Actually I'm very new to the stuff so I did take help from platforms, my bad. Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
unitaryHACK26 Submission: QuEST Core Optimizations
This pull request resolves two open bounties for the QuEST (Quantum Exact Simulation Toolkit) framework: the implementation of an$\mathcal{O}(k)$ graph routing algorithm for SWAP fusion, and the integration of Neumaier compensated summation for numerically stable dense matrix operations.
1. SWAP Fusion:$\mathcal{O}(1)$ Fused State Vector Permutation
The Bottleneck
Applying$k$ sequential SWAP operations to a quantum state requires $k$ independent passes over the $\mathcal{O}(2^n)$ state vector array. For large systems, this severely bottlenecks on memory bandwidth and destroys CPU cache locality.
The Implementation
We introduce a new public API function:
applyMultiSwap(Qureg qureg, const int* targs1, const int* targs2, int numSwaps).Instead of executing the SWAPs sequentially, the algorithm processes the execution queue to form a bipartite graph of logical-to-physical memory maps. The bit-permutation is calculated in$\mathcal{O}(k)$ time. The final amplitudes are then moved to their precise memory addresses in a single, cache-friendly $\mathcal{O}(2^n)$ pass.
Rigorous Correctness Proof
Theorem: Given a set of $k$ disjoint qubit-index transpositions, the induced state vector permutation can be fully resolved in-place in a single $\mathcal{O}(2^n)$ iteration using the guard $\pi(i) > i$.
Proof:$i \in {0, 1, \dots, 2^n - 1}$ . Let $T = { \tau_1, \tau_2, \dots, \tau_k }$ be a set of disjoint transpositions acting on the $n$ qubit indices, where $\tau_m = (a_m, b_m)$ and ${a_m, b_m} \cap {a_{m'}, b_{m'}} = \emptyset$ for $m \neq m'$ .
Let the state vector amplitudes be indexed by
The induced permutation$\pi$ on the amplitude index $i$ flips the $a_m$ -th and $b_m$ -th bits of $i$ if and only if those bits differ. Because the transpositions are disjoint, their bit-flips are completely orthogonal. An index $i$ may have its bit-pairs flipped by multiple independent transpositions simultaneously, but because these flips commute, applying $\pi$ twice restores all original bit values:
Because$\pi^2 = \text{id}$ , the permutation $\pi$ is an involution. The disjoint cycle decomposition of an involution consists exclusively of fixed points and 2-cycles.
false. No memory swap occurs.true, and the amplitudes are swapped. When the iterator reaches the larger indexfalse, strictly preventing the reversion of the swap.Every 2-cycle is processed exactly once. The complete multi-qubit permutation is applied accurately in-place, requiring only$\mathcal{O}(1)$ auxiliary memory. $\blacksquare$
2. Numerical Stability: Neumaier Compensated Summation
The Bottleneck
In$N$ -target dense complex matrix requires iterating $2^N$ times, linearly combining dynamic amplitudes via the standard $\mathcal{O}(n \varepsilon)$ , destroying the strict unitarity ($\text{Tr}(\rho) = 1$ ) of the density matrix for massive state vectors.
cpu_statevec_anyCtrlAnyTargDenseMatr_sub, the application of an+=accumulation. In IEEE 754 arithmetic, this standard addition suffers from catastrophic cancellation when summing highly oscillatory complex amplitudes. The arithmetic error scales asThe Implementation
We replaced the naive accumulation inner loops with a Neumaier Summation algorithm (an improvement over Kahan summation that covers instances where the next term is larger than the running sum). This isolates the low-order bits lost during floating-point rounding into an independent error accumulator, reducing the overall algorithmic error bound to effectively$\mathcal{O}(\varepsilon)$ .
Compiler Flag Override
Compiling QuEST with
-Ofastor-ffast-mathforces the compiler to reassociate floating-point operations, which mathematically annihilates the Neumaier error compensation logic. To prevent this without disabling global optimization, the specific inner loop function is safeguarded using a function-specific compiler attribute: