---
title: Modular Addition (mod 97)
url: https://www.emergentmind.com/topics/modular-addition-mod-97
type: topic
---

# Modular Addition (mod 97)

Modular addition modulo \(97\) is the operation that maps an integer sum to its unique residue in \(\{0,\dots,96\}\). For integers \(a,b\), the quantity \(a+b \bmod 97\) is the unique \(r\in\{0,\dots,96\}\) such that \(r\equiv a+b \pmod{97}\). Although elementary as a definition, modular addition mod \(97\) occupies several technically distinct roles in current research: it is a basic operation in the ring \(\mathbb{Z}_{97}\), a nontrivial target function for neural sequence models, and a core arithmetic primitive in reversible and quantum circuit synthesis [2605.07648], [2102.03615], [2010.00255].

## 1. Algebraic definition and elementary arithmetic

For modulus \(97\), each residue class has a unique representative in \(\{0,\dots,96\}\). Accordingly,
\[
a+b \bmod 97 := \text{the unique } r\in\{0,\dots,96\}\text{ such that } r\equiv a+b \pmod{97}.
\]
This is the standard modular-addition rule specialized to \(q=97\) [2605.07648].

The operation can be read informally as ordinary addition followed by reduction by \(97\). If the ordinary sum already lies in \(\{0,\dots,96\}\), nothing further happens. Thus \(50+30=80\), so \(50+30 \bmod 97 = 80\). If the sum exceeds \(96\), one subtracts a multiple of \(97\) until the result returns to the residue range. Since \(60+60=120\) and \(120=97\cdot 1+23\), one obtains \(60+60 \bmod 97 = 23\). The same rule applies to longer sums: \(10+90+25=125\), and \(125=97\cdot 1+28\), hence
\[
10+90+25 \bmod 97 = 28.
\]

In the sequence-based formalism used in recent learning work, modular addition is the function
\[
f_q(\mathbf{x}) := \sum_{i=1}^N x_i \bmod q,
\qquad
\mathbf{x}=[x_1,\dots,x_N],\; x_i\in\{0,\dots,q-1\}.
\]
For mod \(97\), this becomes
\[
f_{97}(\mathbf{x}) = \left(\sum_{i=1}^N x_i\right)\bmod 97,
\qquad x_i\in\{0,\dots,96\}.
\]
The case \(q=2\) is the parity function, so parity is a special case of modular addition rather than a distinct problem class [2605.07648].

## 2. Wrap-around, carries, and digit-system structure

A central structural feature of modular addition is wrap-around: the sum crosses a multiple of the modulus and must be reduced back into the residue set. In the learning-theoretic treatment, the number of wraps around modulus \(m\) for input \(\mathbf{x}\) is
\[
D_m(\mathbf{x}) := \left\lfloor \frac{\sum_{i=1}^N x_i}{m} \right\rfloor.
\]
For mod \(97\), \(D_{97}(\mathbf{x})\) records how many times the unreduced sum crosses multiples of \(97\). The cited work treats wrap frequency as a proxy for task difficulty: more wraps correspond to a more sensitive input-output mapping [2605.07648].

A distinct but related perspective comes from additive combinatorics, where one studies carries induced by a digit system. For a base \(m\), a digital set \(A\) is a complete set of residues modulo \(m\). If \(a_1,a_2\in A\), a carry occurs when the sum does not remain inside the chosen digit set. In the modular setting of digital sets \(A\subseteq \mathbb{Z}_q\), the paper formulates carry occurrence through the condition \(a_1+a_2\notin A\), and studies the frequency
\[
C_2(A)=\frac{|\{(a_1,a_2)\in A\times A:\; a_1+a_2\notin A\}|}{|A|^2}.
\]
Under the paper’s hypotheses, asymptotically every such digital system has carry frequency at least \(1/4\), and balanced digit sets are extremal or near-extremal [1511.02404].

For modulus \(97\), the digital-set framework has a subtle specialization. Since \(97\) is prime, the only nontrivial divisor choice in the strict \(m\mid q\) setup is \(m=97\), and then a digital set of size \(97\) inside \(\mathbb{Z}_{97}\) must be all of \(\mathbb{Z}_{97}\), making carries trivial. Nontrivial carry-minimization phenomena relevant to mod \(97\) therefore arise either in ordinary base-\(m\) representations whose total sums are later reduced mod \(97\), or in higher-lifted settings such as base-\(97\) digits inside \(\mathbb{Z}_{97^2}\) [1511.02404]. A common misconception is that carry-minimization results “for mod \(97\)” directly concern arithmetic internal to \(\mathbb{Z}_{97}\); in the strict digital-set model, they do not.

## 3. Modular addition mod 97 as a learning problem

Recent work studies modular addition as a supervised learning task for Transformers and related architectures. In that setting the model receives \(\mathbf{x}\in\{0,\dots,96\}^N\) and must predict \(f_{97}(\mathbf{x})\). The cited paper emphasizes that such tasks are hard despite their algorithmic simplicity, and that difficulty increases with the number of summands \(N\), the modulus size \(q\), and especially the amount of wrap-around [2605.07648].

The paper’s main proposal is an auxiliary-modulus training scheme. Instead of always training on the target \(f_q(\mathbf{x})\), one mixes labels from modulus \(q\) and modulus \(Kq\), where \(K>1\):
\[
y =
\begin{cases}
f_q(\mathbf{x}) & \text{with probability } 1-r,\\[4pt]
f_{Kq}(\mathbf{x}) & \text{with probability } r.
\end{cases}
\]
For mod \(97\), one may take \(q=97\) and, for example, \(K=4\), so that the auxiliary target is mod \(388\). The inputs remain \(\mathbf{x}\in\{0,\dots,96\}^N\); only the training label changes [2605.07648].

The theoretical motivation is that enlarging the modulus reduces wrap frequency. If \(x_i\) are i.i.d. uniform on \(\{0,\dots,q-1\}\), the paper shows that the expected wrap proxy under auxiliary-modulus mixing is scaled by
\[
(1-r)+\frac{r}{K}.
\]
Thus the method reduces effective difficulty while leaving the input distribution unchanged. This is its key contrast with the earlier sparse method, which makes training inputs contain more zeros, thereby reducing the effective number of summands but introducing a covariate shift between training and test data [2605.07648].

For mod \(97\), the paper reports successful hyperparameter choices including \(K=5,r=0.4\) for \(N=16\) with angular embedding, \(K=4,r=0.1\) for \(N=32\), and \(K=4,r=0.3\) for \(N=64\). This suggests that modest \(K\) and moderate \(r\) are already effective in the small-prime-modulus regime, not only at very large moduli. A second misconception is therefore that a modulus as small as \(97\) makes the problem trivial for generic neural architectures; the reported experiments do not support that view.

## 4. Empirical behavior at \(q=97\)

The paper evaluates models using two metrics. Match accuracy is exact correctness on the residue class. For large moduli and continuous angular outputs, it also uses relaxed \(\tau\)-accuracy, where a prediction is counted as correct if its circular distance from the true label is at most \(\tau q\). For \(q=97\) and \(\tau=0.05\), the tolerance is approximately \(4.85\), so predictions within about \(4\) to \(5\) positions on the residue circle are accepted [2605.07648].

For token embedding at \(q=97\) with \(1\)M samples, the auxiliary-modulus method substantially outperforms the sparse baseline across sequence lengths:

| \(N\) | Auxiliary modulus method | Sparse method |
|---|---:|---:|
| 8 | 90.1% match accuracy | 74.9% match accuracy |
| 16 | 81.9% match accuracy | 1.1% match accuracy |
| 32 | 62.2% match accuracy | 1.0% match accuracy |

The same study states that angular embeddings perform even better for periodic tasks, with match accuracies near \(99\%\) for moderate \(N\) at \(q=97\) [2605.07648]. The empirical pattern is consistent with the paper’s theoretical emphasis on wrap reduction and with the representation advantage of circular outputs for periodic labels.

The paper also places the \(q=97\) results inside a broader scalability narrative. At \(N=64\) and \(q=974269\), training on \(100\)K samples yields \(97.0\%\) \(\tau\)-accuracy at \(\tau=0.05\) for the auxiliary-modulus method, whereas the sparse method achieves \(9.5\%\) on the same data size and \(93.9\%\) even when extended to \(1\)M samples [2605.07648]. Although these figures are not specific to mod \(97\), they indicate that the same training principle remains effective as both sequence length and modulus grow.

## 5. Reversible and quantum realizations for modulus \(97\)

In reversible and quantum arithmetic, modular addition typically appears as a map of the form
\[
|a\rangle \longmapsto |(a+c)\bmod N\rangle,
\]
where \(a\) is quantum, while \(c\) and \(N\) are classical constants. For modulus \(97\), one has
\[
97_{10}=1100001_2,
\]
so \(97\) fits in \(7\) bits. The width-optimized Toffoli-based construction therefore takes \(n=7\), uses an \(n+1=8\)-qubit data register to hold intermediate carries, and requires a total of \(n+3=10\) qubits: \(8\) data qubits, one garbage qubit, and one flag qubit [2102.03615].

The construction combines a recursive constant adder with the Vedral–Barenco–Ekert modular-addition scheme. Operationally, it adds the classical constant \(c\), subtracts \(97\), tests the sign of the result through the most significant bit, conditionally re-adds \(97\) if the subtraction went negative, and then performs a cleanup sequence that restores all ancillas to \(|0\rangle\). The paper emphasizes that this realizes modular reduction without a separate comparator and uses only Toffoli, CNOT, and NOT gates before Clifford+\(T\) decomposition [2102.03615].

For modulus \(97\), the paper works through the example \((x+25)\bmod 97\) with \(x=80\). Since \(80+25=105\) and \(105-97=8\), the desired output is \(8\). In the circuit logic, the register first reaches the binary encoding of \(105\), then the encoding of \(8\) after subtraction of \(97\), the sign flag remains zero because the subtraction is nonnegative, and the cleanup sequence restores the ancillas while leaving the main register in state \(|8\rangle\) [2102.03615].

The same paper contrasts its approach with Fourier-basis adders. Its stated advantage is that it does not require small-angle rotations and their Clifford+\(T\) decomposition. For mod \(97\), this yields an exact Toffoli-based constant modular adder with \(10\) logical qubits and linear depth in \(n\) [2102.03615].

## 6. Controlled modular addition, carry-lookahead synthesis, and resource trade-offs

A second quantum-circuit line of work studies the controlled modular adder
\[
\ket{x}\ket{b}\longmapsto \ket{x}\ket{b+xa \bmod N},
\]
where \(x\in\{0,1\}\) is a control qubit and \(a,N\) are classical constants. This is the primitive needed in modular multiplication and modular exponentiation. For \(N=97\), one again sets \(n=7\), since \(97<2^7\), and encodes all residues on \(7\) bits [2010.00255].

The architecture in the cited paper is built from two comparators and one controlled-controlled adder on top of a Draper–Kutin–Rains–Svore carry-lookahead adder. Its total logical width is \(4n+2\), so specialization to modulus \(97\) gives \(30\) qubits: one control qubit, a \(7\)-qubit data register \(b\), a \(7\)-qubit ancilla data register \(d\), one comparator flag, and \(14\) carry/propagate ancillas [2010.00255].

The paper optimizes with respect to \( \mathrm{KQ} \), defined as the product of qubit count and circuit depth. In the fault-tolerant setting it focuses on \( \mathrm{KQ}_T \), and in the NISQ setting on \( \mathrm{KQ}_{\mathrm{CX}} \). Using relative-phase Toffoli gates, it reports a control modular adder that uses only \(20\%\) of the number of \(T\) gates of the original and only \(35\%\) of the number of CNOT gates of the original; it further states that \( \mathrm{KQ}_{\mathrm{CX}} \) is \(38\%\) of the original [2010.00255].

At the asymptotic resource level, the same paper gives \(43n\) \(T\) gates for its \(T\)-optimal control modular adder, \(64.75n\) CNOT gates for the NISQ-oriented construction, CNOT depth \(30\log n\), and \( \mathrm{KQ}_{\mathrm{CX}} = 120n\log n \) [2010.00255]. For \(n=7\), the paper’s specialization yields rough figures of \(301\) \(T\) gates, approximately \(453\) CNOTs, CNOT depth approximately \(84\), and \( \mathrm{KQ}_{\mathrm{CX}} \approx 2520 \). These numbers are substantially larger than the \(10\)-qubit constant-adder construction because the objectives differ: one design minimizes width for constant modular addition, while the other optimizes controlled modular addition under carry-lookahead and relative-phase-Toffoli cost models. This suggests complementarity rather than contradiction between the two circuit families.

## 7. Conceptual synthesis for modulus \(97\)

Across algebra, machine learning, additive combinatorics, and quantum computing, modular addition mod \(97\) is the same formal map but not the same technical object. In bare arithmetic it is reduction of a sum to the residue set \(\{0,\dots,96\}\). In sequence learning it is the target function
\[
f_{97}(\mathbf{x})=\left(\sum_i x_i\right)\bmod 97,
\]
whose difficulty is governed in part by wrap frequency and whose learnability can be improved by auxiliary-modulus training without inducing input-side covariate shift [2605.07648]. In additive combinatorics it is connected to carry phenomena through digit systems, although the strict digital-set model is degenerate at \(q=97\) itself and becomes nontrivial only in lifted or external-base representations [1511.02404]. In reversible and quantum circuit design it appears both as a width-sensitive constant-adder problem with \(10\) qubits for modulus \(97\) and as a control-sensitive carry-lookahead problem with \(30\) qubits and explicit \(T\)-count/CNOT-depth trade-offs [2102.03615], [2010.00255].

Taken together, these results make clear that “modular addition mod \(97\)” is not merely a textbook residue calculation. It is also a benchmark for sensitivity in learned arithmetic, a vehicle for carry-minimization theorems, and a standard primitive for exact fault-tolerant arithmetic synthesis.

Source: https://www.emergentmind.com/topics/modular-addition-mod-97