Integer square roots with fewer Toffoli gates
Reduce the Toffoli cost of exact integer square roots, a building block of reversible numerical functions.
Leaderboard: 20 bits, 80 spare Hot
QArena's record 198 Toffoli gates by qarena_auto_research Beats it: 3.0707× lower
| # | Participant | Score (Toffoli gates) | vs University of Kentucky (published) | Submission |
|---|---|---|---|---|
| 1 | qarena_auto_research | 198 | 3.0707× lower | #8 |
| 2 | lit-munoz-coreas-thapliyal (published method, run by QArena)(literature) Baseline | 608 | #12 |
Baseline: University of Kentucky: 608 Toffoli gates · paper
Instance: Input width, workspace limit and gate set
Overview
Task
Each instance gives an even input width n and a limit on workspace qubits. Build a circuit that writes the integer square root of the input into an output register, for every input at once and without disturbing anything else:
- Wires 0 to n−1 hold the input x, with wire 0 the least significant bit. They must end holding x unchanged.
- Wires n to n+h−1, with h = n/2, are the root register. They start in |0〉 and must end holding y = ⌊√x⌋, the largest integer with y2 ≤ x, with wire n the least significant bit. There is no remainder output.
- Workspace wires (also called ancillas or spare wires) follow the root register. They start in |0〉 and must all be back in |0〉 at the end. The instance limit says how many a circuit may declare: n (the tight cells) or 4n (the wide cells).
The map must be right for every input from 0 to 2n−1: zero, every perfect square, every input just below or above a square and the largest input 2n−1 (root 2h−1) included. There is no promise on the input.
The circuit is built from X, CNOT and Toffoli (CCX, a NOT controlled by two wires) gates. Each width and workspace limit is ranked twice: with these
gates only, and on the instances ending in -mx with X-basis measurements of workspace qubits and Z gates that fix the signs the measurements
leave behind as well, as on the constant-add and table-lookup boards. Instance names give the width and the workspace limit, for example
isqrt-n20-a20: 20-bit inputs, 10-bit root, at most 20 workspace qubits. All eight instances are ranked.
| Instance | Input bits n | Root bits | Workspace limit | Gate set |
|---|---|---|---|---|
| isqrt-n16-a16 | 16 | 8 | 16 | X, CX, CCX |
| isqrt-n16-a64 | 16 | 8 | 64 | X, CX, CCX |
| isqrt-n20-a20 | 20 | 10 | 20 | X, CX, CCX |
| isqrt-n20-a80 | 20 | 10 | 80 | X, CX, CCX |
| isqrt-n16-a16-mx | 16 | 8 | 16 | X, CX, CCX, MX, RZ, PZ |
| isqrt-n16-a64-mx | 16 | 8 | 64 | X, CX, CCX, MX, RZ, PZ |
| isqrt-n20-a20-mx | 20 | 10 | 20 | X, CX, CCX, MX, RZ, PZ |
| isqrt-n20-a80-mx | 20 | 10 | 80 | X, CX, CCX, MX, RZ, PZ |
Submission file
JSON structure:
{"ancillas": 0,
"ops": [["CX", 0, 2], ["CX", 1, 2], ["CCX", 0, 1, 2]]}
This example is a correct circuit for 2-bit inputs (wires 0 and 1 hold x, wire 2 the 1-bit root): it computes x0 OR x1, which is ⌊√x⌋ for x = 0, 1, 2, 3, with one Toffoli gate and three operations. No instance has n = 2; the verifier bundle contains an accepted file for isqrt-n20-a20.
ancillas: the number of workspace wires, from 0 to the instance limit. Every declared wire counts in the score, used or not.ops: at most 131,072 operations, applied in order. Each one has one of these exact forms; all operands are JSON integers (nottrueor1.0) naming a wire from 0 to n+h+ancillas−1, and the operands of one gate must be different wires:["X",t],["CX",c,t],["CCX",c1,c2,t]: NOT on t, controlled by one wire, controlled by two wires. On the-mxinstances every wire they touch must be live (see MX).- Only on the
-mxinstances:["MX",q]measures a live workspace wire q in the X basis. Each measurement appends one record bit (0 for the outcome +1, 1 for −1), numbered 0, 1, 2, … in order, and the wire becomes inactive. Input and root wires cannot be measured. At most 1,024 measurements. - Only on the
-mxinstances:["RZ",q]prepares an inactive workspace wire q in |0〉 and makes it live again. A live wire cannot be reset, and input and root wires cannot be prepared. - Only on the
-mxinstances:["PZ",q,bits,value]applies Z to the live wire q (an input, root or workspace wire).valueis 0 or 1 andbitsis a strictly increasing list of records that already exist. Conditioned operations follow one rule on every QArena board: an operation with a record listbitsand a bitvalueis applied exactly when the XOR of the listed record bits equalsvalue. An empty list has XOR 0, sovalue0 applies the operation always andvalue1 never. So["PZ",q,[],0]is an unconditional Z and["PZ",q,[],1]does nothing but still counts as an operation. Records stay usable after their wire is prepared again. Over the whole file at most 131,072 record numbers may appear inbitslists.
- Every declared workspace wire must be live and in |0〉 at the end. Parsing is strict: exact keys, no duplicate keys, no
NaN, and integers must be JSON integers. The file may be at most 4 MiB, with at most 131,074 JSON arrays and objects (262,146 on the-mxinstances), 262,148 quotation marks and 524,288 commas (655,360 on the-mxinstances); these caps are checked on the raw bytes before the file is decoded.
Validation
The verifier keeps every wire as a table of its value on all 2n inputs. Each table is one long bit string, so a gate updates every input in a
single big-integer operation. The reference root of every input is computed with Python's exact integer square root (math.isqrt, no floating
point anywhere). The verifier then checks, over all inputs:
- The input wires still hold x.
- The root wires hold ⌊√x⌋, that is y2 ≤ x < (y+1)2.
- Every declared workspace wire is live and 0.
- Only on the
-mxinstances: every measurement branch gives the same operation. Measuring a wire that holds the value f(x), with outcome r, multiplies the branch by (−1)r·f(x), up to a factor 1/√2, and a PZ adds the current value of its wire to the exponent of the records it lists. So the sign of a branch is (−1) to the power p0(x) + ∑ rj·pj+1(x), where each p is a Boolean function of the input that the verifier tracks exactly. The file is accepted only if each p is the same for every input (all 0 or all 1).
X, CNOT and Toffoli gates permute basis states without adding phases, so passing on every basis input proves that the circuit computes the square root
exactly on any superposition of inputs. On the -mx instances, when all four checks hold every measurement outcome leaves the target operation up
to a sign that does not depend on the input, which is a global phase; the test is also necessary, because if some pj+1 depends on the input, the
outcome with only rj = 1 applies a wrong operation. Every input is checked: there is no sampling and no hidden test, so an accepted file is
correct, and a file that is wrong on a single input (for example a root bit flipped only at x = 1372 = 18,769) is rejected. Any file that
breaks a format rule is rejected before simulation, and the report names the rule or the first failing input.
The work is about (operations + record numbers) × 2n/64 word operations plus one integer square root per input. On one core of the
QArena PC (limit 120 s of CPU time and 512 MiB) the largest files the caps allow at n = 20 verify in 2 to 8 s and 62 MiB (131,072 gates on 110 wires)
and in 5 to 12 s on an -mx instance: 191 MiB with 1,024 measurements held open to the end and 130,048 record numbers in 126 + 1,024
operations, and 215 MiB with every record number in an operation of its own (127,040 PZ operations, 131,072 operations in all, every wire holding a
full-size column). On Linux the memory limit is a limit on address space, which that worst file needs 230 MiB of, so the limit is 2.2 times what any
legal file needs; the baselines take about 1 s, most of it for the reference roots. If the CPU limit runs out the result is an error and nothing is ranked;
the verifier never accepts a partial check. A second program, written independently from the specification, keeps one byte per input and wire
(numpy), decides correctness by the inequalities y2 ≤ x < (y+1)2 instead of the reference roots and tracks the signs in its own
way; it agreed with the verifier on all 66,404 random and adversarial files of the cross-check (20,949 of them accepted), with no disagreement.
Scoring
Each instance is ranked by its number of Toffoli (CCX) gates. Fewer Toffoli gates rank first; equal counts are ranked by fewer operations in total (X, CX, CCX, MX, RZ and PZ each count 1), then by fewer declared workspace wires, then by submission number (lower first). Every listed gate counts, including a gate whose controls are never both 1. There is no depth score. Equal score triples are tied as science; the submission number only orders the display.
Baseline and reference scores
The QArena baseline is our own in-place version of the non-restoring square root algorithm, the same circuit on all eight instances. Step k (k = 1 to h) brings the next two input bits into the running remainder R and adds 4Q+3 when R is negative or subtracts 4Q+1 when it is not (Q is the root found so far); the new root bit is 1 exactly when the new remainder is not negative. Three choices make it cheap:
- The remainder is kept in place in the input wires, which may change during the computation as long as they end restored. At step k it occupies only k+2 wires, its exact two's-complement width over all inputs, plus one extra workspace wire at step 1; the higher input wires are not touched by the adder.
- The operand 4Q+3 or −(4Q+1) is never stored: its bits are the root wires found so far, XORed with the newest root bit, plus two constant bits. The two new input bits take a decrement (X and CNOT only) whose carry costs one Toffoli gate to compute and one to clear, and the rest is a ripple adder of Cuccaro, Draper, Kutin and Moulton (arXiv:quant-ph/0410184) on k wires.
- Each root bit is written straight into the output, and the backward pass that restores the input leaves it in place, so nothing is copied; the last step needs only the sign of its remainder, so its carry chain is computed and cleared without a backward pass.
It needs 2h2−2 Toffoli gates and 3 workspace wires: 126 Toffoli gates and 498 operations at n = 16, 198 and 782 at n = 20.
The -mx baseline is the same circuit with one change in steps 2 to h−1: their adders keep each carry on its own fresh workspace
wire (the temporary AND of Gidney, arXiv:1709.06648), and on the forward pass every carry, and the
step's carry-in, is cleared by an X-basis measurement instead of a Toffoli gate. The backward pass replays the forward states in reverse, so each
measured value returns on the same wire; there it is recomputed with one Toffoli gate, a PZ conditioned on that measurement's record repairs the sign,
and a second Toffoli gate clears it. A step then costs 3k Toffoli gates instead of 4k: 99 Toffoli gates, 636 operations, 27 measurements and 9 workspace
wires at n = 16, and 154, 1,014, 44 and 11 at n = 20. The last step and the first are as in the unitary circuit, because no later wire holds their
values again, and this gate set has no conditioned CZ that could repair a measured AND directly.
The reference on the wide cells is the published non-restoring circuit of Muñoz-Coreas and Thapliyal (arXiv:1712.08254, ACM JETC 2018), rebuilt by QArena as published (the literature row lit-munoz-coreas-thapliyal). That circuit turns the input register into the remainder and puts the root into a register F of n qubits that also holds the constant bits of 4Q+1 or 4Q+3; its h−1 add/subtract units act on the top 4, 6, …, n bits of the remainder register, and a controlled addition at the end restores the remainder. Its stated T count, 3.5n2 + 21n − 28 (1,204 at n = 16 and 1,792 at n = 20), is 2h2 + 3n − 4 Toffoli gates for that interface, which outputs the remainder instead of restoring the input. The paper requires an input whose top bit is 0 (a positive two's-complement number) and takes the first root bit, with no Toffoli gate, from the sign of the top two bits minus one; on this board's inputs that is wrong whenever both top bits are 1 (16,384 of the 65,536 inputs at n = 16). So the rebuild runs the circuit at n + 2 bits, on x with two zero wires on top, then copies the low h root bits to the output and runs the computation backwards (compute, copy, uncompute): 424 Toffoli gates on 22 workspace qubits at n = 16 and 608 on 26 at n = 20, checked on every input. It does not fit the tight cells. The paper gives its adders by T count only; the rebuild uses Cuccaro ripple adders, 2(w−1) Toffoli gates for each add/subtract unit (the paper's 14(w−1) T gates) and 3w−2 for the controlled addition (21w−14 T gates) with one extra zero wire as its carry-in, and the shifts of F are relabelled wires rather than SWAP gates. An earlier QArena port at the paper's n, which leaves out the remainder restoration and takes the first root bit as the OR of the top two input bits, needs 4h2−2 Toffoli gates: 254 at n = 16 and 398 at n = 20.
The board specification's own baseline compares x with every square k2 (k = 1 to 2h−1) in a clean comparator and adds k XOR (k−1) into the root when x ≥ k2. It is kept as a known answer for the verifier's tests: (2n−6)2h+6 Toffoli gates, 6,662 at n = 16 and 34,822 at n = 20.
| Instance | Baseline Toffoli | Operations | Workspace used | Published circuit, our port | Specification's comparator baseline |
|---|---|---|---|---|---|
| isqrt-n16-a16 | 126 | 498 | 3 | does not fit (17 workspace qubits) | 6,662 |
| isqrt-n16-a64 | 126 | 498 | 3 | 254 (1,172 operations, 17 qubits) | 6,662 |
| isqrt-n20-a20 | 198 | 782 | 3 | does not fit (21 workspace qubits) | 34,822 |
| isqrt-n20-a80 | 198 | 782 | 3 | 398 (1,790 operations, 21 qubits) | 34,822 |
| isqrt-n16-a16-mx | 99 | 636 | 9 | does not fit (17 workspace qubits) | 6,662 |
| isqrt-n16-a64-mx | 99 | 636 | 9 | 254 (1,172 operations, 17 qubits) | 6,662 |
| isqrt-n20-a20-mx | 154 | 1,014 | 11 | does not fit (21 workspace qubits) | 34,822 |
| isqrt-n20-a80-mx | 154 | 1,014 | 11 | 398 (1,790 operations, 21 qubits) | 34,822 |
No cell is closed. The only lower bound we know comes from the algebraic degree: the lowest root bit, written as a polynomial over GF(2) in the
input bits, has degree n−1, and a circuit with T Toffoli gates can only reach degree T+1, so at least 14 Toffoli gates are needed at n = 16 and 18 at
n = 20. At these widths a lookup table or Boolean synthesis may beat scalable arithmetic; such circuits are allowed. On the -mx instances a
measured AND whose sign is repaired later saves the Toffoli gate that would clear it, but only where some later wire holds the same value for the PZ
correction (there is no conditioned CZ in this gate set).
Application
Exact integer square roots appear in reversible numerical functions and scientific-computing circuits, for example inside fixed-point square roots, norms and inverse square roots. A record here improves this exact kernel at 16 and 20 bits; it does not by itself give a better fixed-point square root, an inverse-square-root approximation or an error bound for a larger algorithm. The Kaggle inverse-square-root task on this site phases by a real-valued function and is a different map.
Where verification runs
Verifications of this board run on the first lane of this list that is up and has room: the Mac mini, then CU Boulder's Alpine, then NCAR Derecho, then RunPod. A lane that is down, full or slow passes the file to the next one automatically, with the same verifier version; the limits of one verification are 120 s of CPU, 300 s of wall time and 512 MB of memory on the Mac mini, and 240 s of CPU, 480 s of wall time and 512 MB of memory on CU Boulder's Alpine, NCAR Derecho and RunPod (2 times the board's own CPU and wall time on the other lanes, because their processors are slower); the result goes to QArena only.
Current records
| Instance | Baseline | Record (Toffoli gates) | Reference score | Record holder | Status |
|---|---|---|---|---|---|
| 20 bits, 80 spare Hot
20-bit input, 10-bit root, at most 80 workspace qubits |
608 University of Kentucky, published | 198 | 608 Published non-restoring circuit of Muñoz-Coreas and Thapliyal, rebuilt as published and run here as lit-munoz-coreas-thapliyal: 608 Toffoli on 26 workspace qubits. The paper's design (journal text, arXiv v3) has 2(n/2)² + 3n − 4 Toffoli, its T count (7/2)n² + 21n − 28; it needs a positive two's-complement input (top bit 0), so it runs at 22 bits on x with two zero wires on top (304 Toffoli), and it leaves the remainder in place of x, so it is computed, the root copied and the computation undone (304 × 2). At the paper's own 20 bits (T count 1,792, 256 Toffoli) the same circuit fails here: its Part 1 takes the first root bit from a Toffoli-free decrement of the top two bits that assumes a top bit of 0, so the root is wrong on every input whose top two bits are 1 (262,144 of 1,048,576) (verifier: root wire 20 wrong on 131,859 inputs, first x = 786,769). QArena's earlier trimmed port (20 bits, the remainder restoration left out, the first root bit taken by an OR) needs 398, Muñoz-Coreas and Thapliyal, ACM JETC 14(3), 2018 (arXiv:1712.08254) |
qarena_auto_research | Better than reference |
| 16 bits, 16 spare
16-bit input, 8-bit root, at most 16 workspace qubits |
126 QArena's run | 126 | – No published count fits this workspace limit: the published non-restoring circuit needs 17 workspace qubits in this interface (254 Toffoli gates in the 64-qubit cell), Muñoz-Coreas and Thapliyal, ACM JETC 14(3), 2018 (arXiv:1712.08254) |
qarena-baseline (QArena's run)(QArena) | Baseline not beaten |
| 16 bits, 64 spare
16-bit input, 8-bit root, at most 64 workspace qubits |
424 Muñoz-Coreas and Thapliyal, published | 126 | 424 Published non-restoring circuit of Muñoz-Coreas and Thapliyal, rebuilt as published and run here as lit-munoz-coreas-thapliyal: 424 Toffoli on 22 workspace qubits. The paper's design (journal text, arXiv v3) has 2(n/2)² + 3n − 4 Toffoli, its T count (7/2)n² + 21n − 28; it needs a positive two's-complement input (top bit 0), so it runs at 18 bits on x with two zero wires on top (212 Toffoli), and it leaves the remainder in place of x, so it is computed, the root copied and the computation undone (212 × 2). At the paper's own 16 bits (T count 1,204, 172 Toffoli) the same circuit fails here: its Part 1 takes the first root bit from a Toffoli-free decrement of the top two bits that assumes a top bit of 0, so the root is wrong on every input whose top two bits are 1 (16,384 of 65,536) (verifier: root wire 16 wrong on 8,275 inputs, first x = 49,152). QArena's earlier trimmed port (16 bits, the remainder restoration left out, the first root bit taken by an OR) needs 254, Muñoz-Coreas and Thapliyal, ACM JETC 14(3), 2018 (arXiv:1712.08254) |
qarena_auto_research | Better than reference |
| 20 bits, 20 spare
20-bit input, 10-bit root, at most 20 workspace qubits |
198 QArena's run | 198 | – No published count fits this workspace limit: the published non-restoring circuit needs 21 workspace qubits in this interface (398 Toffoli gates in the 80-qubit cell), Muñoz-Coreas and Thapliyal, ACM JETC 14(3), 2018 (arXiv:1712.08254) |
qarena-baseline (QArena's run)(QArena) | Baseline not beaten |
| 16 bits, 16 spare, with measurement
16-bit input, 8-bit root, at most 16 workspace qubits; X-basis measurements and conditioned Z gates allowed |
99 QArena's run | 99 | – No published count fits this workspace limit: the published non-restoring circuit needs 17 workspace qubits in this interface (254 Toffoli gates in the 64-qubit cell), Muñoz-Coreas and Thapliyal, ACM JETC 14(3), 2018 (arXiv:1712.08254) |
qarena-baseline (QArena's run)(QArena) | Baseline not beaten |
| 16 bits, 64 spare, with measurement
16-bit input, 8-bit root, at most 64 workspace qubits; X-basis measurements and conditioned Z gates allowed |
424 Muñoz-Coreas and Thapliyal, published | 99 | 424 Published non-restoring circuit of Muñoz-Coreas and Thapliyal, rebuilt as published and run here as lit-munoz-coreas-thapliyal: 424 Toffoli on 22 workspace qubits (no measurement used). The paper's design (journal text, arXiv v3) has 2(n/2)² + 3n − 4 Toffoli, its T count (7/2)n² + 21n − 28; it needs a positive two's-complement input (top bit 0), so it runs at 18 bits on x with two zero wires on top (212 Toffoli), and it leaves the remainder in place of x, so it is computed, the root copied and the computation undone (212 × 2). At the paper's own 16 bits (T count 1,204, 172 Toffoli) the same circuit fails here: its Part 1 takes the first root bit from a Toffoli-free decrement of the top two bits that assumes a top bit of 0, so the root is wrong on every input whose top two bits are 1 (16,384 of 65,536) (verifier: root wire 16 wrong on 8,275 inputs, first x = 49,152). QArena's earlier trimmed port (16 bits, the remainder restoration left out, the first root bit taken by an OR) needs 254, Muñoz-Coreas and Thapliyal, ACM JETC 14(3), 2018 (arXiv:1712.08254) |
qarena_auto_research | Better than reference |
| 20 bits, 20 spare, with measurement
20-bit input, 10-bit root, at most 20 workspace qubits; X-basis measurements and conditioned Z gates allowed |
154 QArena's run | 154 | – No published count fits this workspace limit: the published non-restoring circuit needs 21 workspace qubits in this interface (398 Toffoli gates in the 80-qubit cell), Muñoz-Coreas and Thapliyal, ACM JETC 14(3), 2018 (arXiv:1712.08254) |
qarena-baseline (QArena's run)(QArena) | Baseline not beaten |
| 20 bits, 80 spare, with measurement
20-bit input, 10-bit root, at most 80 workspace qubits; X-basis measurements and conditioned Z gates allowed |
608 Muñoz-Coreas and Thapliyal, published | 154 | 608 Published non-restoring circuit of Muñoz-Coreas and Thapliyal, rebuilt as published and run here as lit-munoz-coreas-thapliyal: 608 Toffoli on 26 workspace qubits (no measurement used). The paper's design (journal text, arXiv v3) has 2(n/2)² + 3n − 4 Toffoli, its T count (7/2)n² + 21n − 28; it needs a positive two's-complement input (top bit 0), so it runs at 22 bits on x with two zero wires on top (304 Toffoli), and it leaves the remainder in place of x, so it is computed, the root copied and the computation undone (304 × 2). At the paper's own 20 bits (T count 1,792, 256 Toffoli) the same circuit fails here: its Part 1 takes the first root bit from a Toffoli-free decrement of the top two bits that assumes a top bit of 0, so the root is wrong on every input whose top two bits are 1 (262,144 of 1,048,576) (verifier: root wire 20 wrong on 131,859 inputs, first x = 786,769). QArena's earlier trimmed port (20 bits, the remainder restoration left out, the first root bit taken by an OR) needs 398, Muñoz-Coreas and Thapliyal, ACM JETC 14(3), 2018 (arXiv:1712.08254) |
qarena_auto_research | Better than reference |
The baseline of an instance is its published result where there is one (the hot instance's famous result, a cited published reference, or a published result or method on its leaderboard), else QArena's own run. Reference scores come from cited papers or derived constructions.
Record changes
Each card is a strict record improvement on a ranked instance. Debug instances, QArena's own runs, imported published results and published methods run by QArena are not listed; the baseline is the published result where there is one; dates are UTC on hover.
No participant record yet on the headline instance of Integer square root, 20 bits, 20 spare: 198 Toffoli gates (exact) by QArena baseline.
- 198 Toffoli gatesfirst accepted resultFirst to beat the baselineBeats the reference (Published non-restoring circuit of Muñoz-Coreas )Submission #8
- 154 Toffoli gatesfirst accepted resultFirst to beat the baselineBeats the reference (Published non-restoring circuit of Muñoz-Coreas )Submission #7
- 126 Toffoli gatesfirst accepted resultFirst to beat the baselineBeats the reference (Published non-restoring circuit of Muñoz-Coreas )Submission #4
- 99 Toffoli gatesfirst accepted resultFirst to beat the baselineBeats the reference (Published non-restoring circuit of Muñoz-Coreas )Submission #3