Manuscript Offprint
arXiv:2610.38189Ancilla-free randomized Toffoli gates and the T-count of Boolean oracles
Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.
Abstract
In fault-tolerant Clifford+ circuits, a Boolean oracle costs gates and extra qubits, and on four or more qubits every exact Toffoli gate needs an extra qubit. Gosset, Kothari and Zhang showed that sampling a fresh random circuit for each call makes the -count of the Toffoli gate independent of its size, but a direct implementation of their sampler uses about one extra qubit per bit of precision. We show that computing the sampled parities in place, on the gate's own controls, removes every extra qubit once the gate is large enough: each random circuit then uses about 8 gates per bit of precision and borrows only the gate's own idle inputs. This is one gate more per circuit than the best measurement-free variant that keeps one clean ancilla. The same holds for exclusive-ors of indicators of affine subspaces, such as equality tests and checks of linear equations. For randomized circuits that do not measure, this is within a factor of eight of optimal up to an additive constant, even with extra qubits, and when each random circuit computes a Boolean function exactly, closing the factor asymptotically is equivalent to determining the leading constant in the -count of exact multi-controlled Toffoli gates. Finally, the oracle of the indicator of a random set of fixed density needs exponentially many gates at constant error, while that of an affine subspace of the same density needs a constant number, and extra qubits reduce the exponential cost only quadratically.
Contents
1 Introduction 2
1.1 Structured oracles 3
1.2 Lower bounds and exact gates 4
1.3 Distance from affine and random sets 4
2 Technical overview 5
2.1 Warm-up: one branch 5
2.2 Structured oracles 6
2.3 Lower bounds for the Toffoli gate 7
2.4 Distance from affine and random sets 7
3 Related work 8
4 Discussion and open problems 9
A Preliminaries 10
A.1 Distances and synthesis models 10
A.2 Multi-controlled NOTs with borrowed qubits 11
*Imperial College London. rhik.mazumder26@imperial.ac.uk
B Constructions without an ancilla 11 B.1 The randomized Toffoli gate ..... 12 B.2 Affine decompositions ..... 14 B.2.1 Every Boolean function has such a decomposition ..... 15
C Lower bounds 15 C.1 The Toffoli gate ..... 15 C.2 Oracle branches and exact multi-controlled NOTs ..... 17 C.3 The distance from affine ..... 19 C.4 Random exceptional sets ..... 20
The Toffoli gate has controls and one target, and on a computational-basis input it flips the target exactly when the control string is all ones. For this gate the two costs pull against each other. Once , no Clifford+ circuit on its qubits implements it exactly [5, 6], so an exact circuit needs an extra qubit. A clean ancilla, which starts and ends in , brings the cost to about gates, with or without a mid-circuit measurement [7, 8]. A borrowed qubit, which may be in any state but must be restored, costs about [7, 9]. Neither count can drop below linear in , since an exact circuit needs at least gates even with measurement and feedforward [10, Proposition 4.1]. Approximation alone does not help: for large , a unitary circuit within diamond distance less than of still needs gates even with clean ancillae [11, Theorem 2], where diamond distance is the worst-case distance between quantum channels.
Gosset, Kothari and Zhang (GKZ) showed that randomness removes this dependence on [11]. If each call runs a freshly sampled Clifford+T circuit and only the averaged channel has to be within diamond distance of , then gates suffice. Their construction, which we call GKZ's sampler, first flips every control, so that the target must flip exactly when the control string is all zeros. A parity of the controls is the sum modulo 2 of a subset of them. Each sampled circuit checks about parities with uniformly random subsets and flips the target only if all of them are 0. The all-zeros string passes every check, and any other string fails each check with probability , so it passes them all with probability about . Once the parities are computed, the only non-Clifford gate is a multi-controlled NOT on their values. A direct implementation computes them into fresh qubits (Figure 1) and so uses about ancillae, a number GKZ do not optimize.

Figure 1: A direct implementation of one branch of GKZ's sampler. A slash marks a bundle of qubits, and flips every control. The CNOT circuit writes the sampled parities into clean ancillae, and the open circle is a control active on 0. The multi-controlled NOT is the only non-Clifford gate, and returns the ancillae to .
We work in GKZ's mixed model (Definition 6). A circuit implements a unitary if it applies up to a global phase. A mixture is a probability distribution over unitary Clifford+T circuits, called branches, which do not measure or adapt. Its error is the diamond distance of the averaged channel from the target, its cost is the largest -count of a branch, and is the least cost at error . The oracle of acts on qubits, and acts on . Our lower bounds allow any number of ancillae in any initial state, unless we say that the ancillae are clean. Throughout, is the number of parities a branch checks, and logarithms are to base 2.
Our results concern whether a mixture for still needs extra qubits (Section 1.1), how close it comes to the lower bounds (Section 1.2), and which property of determines its cost (Section 1.3).
Theorem 1 (the randomized Toffoli gate (informal version of Theorem 11)). If , then has a mixture at diamond error whose branches use exactly gates, no ancilla and no measurement. If only , the branches still use no ancilla and at most gates.
The extra qubit is forced by a determinant. Every Clifford+ circuit on four or more qubits implements a unitary of determinant 1, while an oracle that flips the target on control strings has determinant . An exact Toffoli gate flips it on one string. A branch that checks linearly independent parities also flips the target on every other string whose parities are all 0. These strings form a subspace of dimension , so there are of them. This number is even when , so the unitary such a branch must implement has determinant 1 and the obstruction does not apply. The parities depend only on the span of the sampled subsets, so a CNOT change of basis can overwrite at most of the controls with them, and the branch runs on the gate's own qubits. We draw the subsets linearly independent, which keeps GKZ's error bound and makes the count above exact (Construction 8). The checks then become a multi-controlled NOT on these controls, which can borrow the other controls as work qubits.
Against a direct implementation of GKZ's sampler, the branches save about ancillae. Computing the parities in place still leaves one clean ancilla if the multi-controlled NOT is built on it, as in the cheapest exact constructions. Once , the branches remove that qubit by borrowing idle controls instead, at the cost of one to five gates (Table 1), which is worthwhile when a computation is limited by its number of logical qubits. With clean ancillae and measurement, the sampler can instead use a ladder of temporary logical-AND gates [12, 13] and needs gates [14], about half of . We know of no other Clifford+ implementation of that uses no qubit beyond its own and a number of gates independent of . Once , the branches also use fewer gates than every exact construction without a clean ancilla, and they need no measurement.
1.1 Structured oracles
A branch of GKZ's sampler flips the target exactly on the solutions of linear equations over , so it is itself a test of linear equations. Many useful oracles have the same form. The inputs that satisfy independent equations form an affine subspace of dimension , that is, of codimension . CNOT gates can write the value of each equation onto one of the inputs, so the oracle of such a test is up to Clifford gates, and our branches extend to it. They also extend to an exclusive-or of such oracles, built term by term at error each. We write for the indicator function of a set .
Theorem 2 (affine decompositions (informal version of Theorem 12)). Let with of codimension , and put . If for every , then has a mixture at diamond error with no ancilla, no measurement and at most gates per branch, independently of .
With , Theorem 2 covers the reflection about , which is up to Clifford gates and is used in amplitude amplification [3] and in the quantum singular value transform [4]. It also covers equality tests between two registers or against a fixed string. Membership in the solution set of a linear system, or in a linear code, is another example. For these oracles GKZ's construction already gives gates [11], and Theorem 2 removes its ancillae.
Larger covers functions that split into disjoint pieces. The inputs that reach a leaf of a decision tree satisfy the conditions along its path, so they form an affine subspace. Different leaves share no input, so the function is an exclusive-or of one indicator per accepting leaf. The same holds for parity decision trees, which query parities of the input instead of single bits, and for disjunctive normal forms with disjoint terms. For these functions Theorem 2 matches, up to constants, the bound that GKZ obtain with ancillae [11, Theorem 5].
These savings shrink when the oracle is called many times. If a computation makes calls, each call needs error about , because diamond errors add up over a sequence of calls. So grows like , and the branches pay off only on oracles with somewhat more than qubits (Figure 3).
1.2 Lower bounds and exact gates
GKZ proved that mixtures for need gates, even with measurement and adaptivity [11]. They also showed that a mixture with gates can be simulated by a random choice of parity decision trees, each querying about parities of the input [11, Theorem 23]. With its controls flipped, computes the negation of the OR function, and a standard calculation gives the exact number of parity queries a random decision tree needs for OR (Proposition 13). The following explicit bound for unitary mixtures is a corollary of their reduction, with leading constant 1, where the argument of their Theorem 3 gives about but also covers measurement and adaptivity.
Theorem 3 (lower bound for the Toffoli gate (informal version of Theorem 14)). For , every mixture of unitary Clifford+ circuits that approximates to diamond error has a branch with at least gates.
By Theorem 3, the branches of Theorem 1 are within a factor of eight of optimal up to an additive constant (Corollary 15). The same holds for GKZ's sampler built on the best known clean-ancilla multi-controlled NOTs. In both constructions every branch is the exact oracle of some Boolean function, though not the one computes. For mixtures of this kind, Theorem 4 shows that closing the factor of eight is equivalent to finding exact multi-controlled NOTs with fewer gates. Let be the least -count of an exact -controlled NOT with clean ancillae and no measurement. The stabilizer-nullity bound of Beverland et al. and Maslov's construction give for [10, Proposition 4.1], [6, Proposition 4], and we know of no better bounds.
Theorem 4 (oracle branches and exact multi-controlled NOTs (informal version of Theorem 18)). Let and . Among mixtures of circuits that each implement some Boolean oracle exactly, with any clean ancillae, the least branch -count for at diamond error is between and , where .
GKZ's sampler gives the upper bound [11, Theorem 1], and the lower bound is new. So without a better exact multi-controlled NOT, closing the factor of eight needs branches that are not the exact oracle of any Boolean function, for example circuits that add input-dependent phases (Theorem 18).
1.3 Distance from affine and random sets
An affine function is a parity of some of the inputs or its negation, and its oracle needs no gates, since CNOT and gates implement it [15]. The function computed by differs from the affine function 0 on a single input, and by the intuition behind Theorem 3 that input is what costs gates. This suggests measuring a general by its distance from affine. Let be the smallest fraction of inputs on which a non-affine differs from an affine function, and call those inputs exceptional. Through GKZ's reduction to decision trees, needs at least gates (Theorem 20). When the exceptional inputs form an affine subspace, this is tight up to a factor of eight and an additive constant (Corollary 21). At constant error the bound is only a constant, and the cost then depends on how the exceptional inputs are arranged. Theorem 5 compares the two extremes, a subspace and a set scattered at random.
Theorem 5 (distance does not determine the -count (informal version of Theorem 28)). Let . Let be an affine subspace of codimension , and let be a random set that contains each input independently with probability . With probability at least over , the following hold at .
(1) Both and are at distance between and from affine.
(2) The oracle of costs at most 24 T gates.
(3) The oracle of costs T gates with clean ancillae and without ancillae.
Both functions are 0 on almost every input, so their exceptional inputs are and themselves, each about a fraction of the inputs. They differ only in structure. Membership in is decided by parities, and at a branch of Theorem 2 checks just four random combinations of them. A random set has no such description: it meets every large subspace in about its expected fraction, so no few parities single it out. Counting makes this precise, since mixtures with few gates can approximate far fewer functions than there are sets .
The upper bounds hash the input to about bits and implement the hashed set exactly (Proposition 27), and the lower bounds count the functions that cheap mixtures can approximate (Theorem 26). With clean ancillae the count adapts that of Gosset, Kothari and Wu [16, Lemma 4.8], and without ancillae a finer count gives the larger bound. That count extends the exact no-ancilla counting bound of Low, Kliuchnikov and Schaeffer [17, Section 5] to mixtures at constant error.
Some sets of this density were already known to be expensive. Gosset, Kothari and Wu’s counting gives a function on bits that needs T gates even with measurement, and requiring the other inputs to be 0 gives one of density about . Earlier counting arguments for sparse sets apply only to exact circuits. Theorem 5 shows that a typical set of this density is expensive even at diamond error and that matching upper bounds exist, so distance from affine does not determine the cost. For an oracle with no structure, randomness cannot remove the exponential cost, and clean ancillae reduce it only from about to about . An oracle whose exceptional inputs form an affine subspace stays cheap and needs no ancillae, by Theorem 2.
Section 2 goes through one branch of the Toffoli construction and sketches the proofs, Section 3 discusses related work, and Section 4 the open problems. Appendix A fixes notation and the model and reviews the multi-controlled NOTs we build on, Appendix B proves the constructions, and Appendix C proves the lower bounds. Python scripts that check the constructions and the numerical claims are available at https://github.com/arulrhikm/randomized-oracle-checks.
2 Technical overview
2.1 Warm-up: one branch
We first make the branch from Section 1 precise. Write for the control string with every bit flipped, so that flips the target exactly when . A branch picks subsets of the controls whose indicator vectors are linearly independent over , uniformly at random among such choices. It flips the target when has even parity on every . A nonzero passes all tests with probability at most (Construction 8), as it does for GKZ's uniformly random subsets. A mixture of oracles that errs on each input with probability at most is within diamond distance of the target (Lemma 7), so gives error at most . When , the determinant obstruction of Section 1 does not apply (Proposition 9).
Completing the subsets to a basis gives an invertible CNOT circuit whose first outputs are the parities of on . Applying , then an -controlled NOT with controls active on 0, then implements the branch with no extra qubit (Figure 2). The multi-controlled NOT does not depend on the other qubits. A circuit for it can therefore use them as borrowed work qubits, which may be in any state and are returned unchanged, as in the constructions of Barenco et al. [18, Lemma 7.2]. Maslov's construction uses gates once such qubits are available, that is, once [6, Proposition 5]. It is built from relative-phase Toffoli gates, whose phases cancel in pairs (Section A.2).
Table 1 and Figure 3 compare the T-counts of the branches of Theorem 1 with those of GKZ's sampler built on three other multi-controlled NOTs and of exact constructions. Measurement and reset are counted as free. Every other construction in the table uses an extra qubit. Those without a clean ancilla need at least 384 T gates, against 88 to 352 for the branches. For between and , the branches use Zindorf and Bose's construction instead, which borrows the idle controls [9]. Even there they can beat the

Figure 2: One branch of Theorem 1, drawn as in Figure 1 but with no ancilla. The CNOT circuit moves the sampled parities onto the first controls. The multi-controlled NOT uses the other qubits as borrowed work qubits and returns them unchanged, and it is the only non-Clifford gate in the branch.
| Construction | Source | Ancillae | Meas. | T-count | T-count at | ||
|---|---|---|---|---|---|---|---|
| () | () | () | |||||
| Randomized, diamond error | |||||||
| This paper | Theorem 1 | none (idle controls) | no | 88 | 168 | ||
| GKZ + Khattar–Gidney | [7, 11] | 1 clean | no | 87 | 167 | 279 | |
| GKZ + Kole et al. | [8, 11] | 1 clean | yes | 83 | 163 | 275 | |
| GKZ + AND ladder | [11–13] | clean | yes | 47 | 87 | 143 | |
| Exact | |||||||
| Zindorf–Bose | [9, Theorem 2], [7, Table 1] | 1 borrowed | no | 752 | |||
| Maslov | [6, Proposition 5] | borrowed | no | 384 | |||
| Khattar–Gidney | [7], [8, Eq. (12)] | 1 clean | no | 383 | |||
| Kole et al. | [8, Eq. (8)] | 1 clean | yes | 379 | |||
| AND ladder | [12, 13], [14, Eqs. (3)–(4)] | clean | yes | 195 | |||
Table 1: -counts for with controls. The branches are in bold. GKZ + X is GKZ's sampler with its parities computed in place as in Construction 8 and its multi-controlled NOT built as in X. Computing them into fresh qubits instead, as in GKZ's direct circuit, adds ancillae. The Khattar–Gidney rows use the count without measurement of [8, Eq. (12)]; with the measurement-based uncomputation of [7] the count is . At the entry marked , and the count is with [9, Theorems 2 and 10].
exact circuits with one clean ancilla, for example with 352 against 379 gates at and . From the branches need no ancilla and use fewer gates than exact circuits with one borrowed qubit. They beat exact circuits with one clean ancilla once , and those with clean ancillae once .
2.2 Structured oracles
Theorem 2 applies the branch of Section 2.1 to each term of an affine decomposition, so the only new questions are which terms to randomize and how their errors add. A term of codimension at most is implemented exactly, as in the synthesis of exclusive-or sums of affine products [19]. A term of larger codimension is replaced by a branch that tests random combinations of its equations. Such a term misses an input with probability at most . The misses of the terms add, so each input is missed with probability at most , and Lemma 7 turns this into diamond error at most . Every Boolean function has such a decomposition, for instance with one term per accepting input, but can then be exponential in . A parity decision tree of depth with accepting leaves gives a decomposition that costs at most gates when (Section B.2.1).
For example, the membership oracle of Grover search with marked strings is an exclusive-or of indicators of single points, so when it uses gates and no ancilla. Grover search makes about

| This paper (no ancilla) | Kole et al. (1 clean, measured) | -- -- Zindorf-Bose (1 borrowed) |
|---|---|---|
| AND ladder (n - 3 clean, measured) | GKZ + AND ladder ( - 2 clean, measured) |
Figure 3: T-count against at , where , for the constructions of Table 1. Dotted lines mark and . Below the branches need a clean ancilla and are not drawn. Between the lines they cost with idle controls, and from on they cost 168. The exact circuits with at most one ancilla leave the frame at and , and GKZ + Khattar-Gidney, which is not drawn, lies one gate below the branches.
calls, so the error per call must be about . At , and , this gives , and each call costs 464 gates on the oracle's own 101 qubits. The oracle is up to Clifford gates, so Table 1 applies with . An exact circuit needs an extra qubit. Without measurement it uses 1568 gates with a borrowed qubit [7, 9] or 791 with a clean ancilla [7], [8, Eq. (12)], which is 3.4 and 1.7 times as many. Only constructions with clean ancillae do better: GKZ's sampler uses 463 with one, and the AND ladder uses 399 with 98 and measurement. So even at the error that calls demand, the branches free a qubit for one gate per call.
2.3 Lower bounds for the Toffoli gate
The proof of Theorem 3 goes through parity decision trees. GKZ show that a mixture whose branches use gates can be simulated by a random choice of decision trees, each asking parities of the input and then outputting any function of the answers [11, Theorem 23]. It therefore suffices to show that random trees with few queries cannot compute , which in the coordinates of Section 2.1 is the negation of OR on bits. Fix one tree that asks parities. All in the kernel of these parities give the same answers, so the tree treats like the nonzero points of this kernel and errs either at 0 or on all of them. Put half the weight on and spread the other half over the nonzero inputs. Every such tree then errs with probability about , and by Yao's principle so does every random choice of trees. Error thus needs , as in the standard argument for one-sided Equality [20, 21]. The bound is tight: a mixture of GKZ's test with a tree of constant output meets it exactly (Proposition 13), and with the reduction this gives Theorem 3.
Theorem 4 needs a different argument. Decision-tree bounds stay below , so they cannot see the constant in front of . The proof instead works with a single branch. For mixtures of oracles, diamond error bounds the error probability at every input by (Lemma 17), so some branch is correct at y = 0 and wrong on few other inputs. A random subspace of dimension about that contains y = 0 then avoids all of those inputs with positive probability, and we restrict the branch to it. What remains is an exact multi-controlled NOT on about controls.
2.4 Distance from affine and random sets
Theorem 20 extends the argument for Theorem 3 from to any . Let be the affine function closest to , so that and differ on a fraction of the inputs. Since the oracle of needs no gates, implementing
is as hard as implementing the oracle of , which is 1 exactly on the exceptional inputs. Put half the weight on the exceptional inputs and spread the other half over the rest. A tree that asks parities gives the same answers on each coset of their kernel, which holds a fraction of the inputs. It therefore errs with probability at least , and by Yao's principle so does every random choice of trees. Error thus needs about queries.
When the exceptional inputs form an affine subspace of codimension , so that and , Theorem 2 builds the oracle from a single term with at most about 8 min gates. Against the lower bound above, this gives the factor of eight in Corollary 21. An exact implementation needs at least gates, so randomization helps only when , that is, roughly when .
For Theorem 5, a Chernoff bound puts within a factor of 6/5 of . Every affine function other than 0 differs from on about half the inputs, so the distance of from affine is about . Decision-tree bounds stay below here, so the exponential bounds use counting instead. Averaging sampled branches and rounding recovers exactly, so a mixture with few gates would give a short description of . Circuits with gates have only possible acceptance-probability functions, or without ancillae (Lemma 25). There are possible sets , so must be about with clean ancillae and about without.
The upper bounds hash the input to about bits with a random CNOT circuit and implement the oracle of the hashed set exactly. An input outside lands in the hashed set only with small probability, and this is the error of the mixture. With clean ancillae the hashed oracle follows the randomized construction of Li et al. for sparse lookup tables [22, Theorem 3.4]. Without ancillae it is built point by point from Zindorf and Bose's multi-controlled NOT [9], which borrows one of the unused inputs.
3 Related work
Campbell and Hastings showed that a random mixture of circuits can approximate a single-qubit rotation more cheaply than any one circuit, because the errors of the branches average out [23, 24]. Gosset, Kothari and Zhang carried the idea to multi-qubit Boolean oracles [11]. They gave gates for and for an arbitrary , where is the sum of the absolute Fourier coefficients of . Theorem 1 computes their parities in place and borrows the idle controls, so each branch runs without ancillae, and Theorem 2 does the same for affine decompositions.
GKZ also proved a matching lower bound by generalizing stabilizer nullity, and bounded -counts below by randomized parity decision trees. For mixtures of oracle branches, Theorem 4 shows that their construction, built on an optimal exact multi-controlled NOT, is optimal up to an additive constant. Girish, May, Parham and Yuen proved -count lower bounds for mixtures through communication complexity, including with advice states [25]. Randomized non-adaptive parity decision trees are randomized linear sketches over , studied by Kannan et al. and by Montanaro and Osborne [20, 21], and Propositions 13 and 19 use the standard averaging arguments from that setting.
Counting arguments give lower bounds for unstructured oracles. Low, Kliuchnikov and Schaeffer implemented the oracle of an arbitrary -bit function with gates using ancillae, and proved a counting lower bound when the number of ancillae is restricted [17]. Gosset, Kothari and Wu settled the -count of state preparation and diagonal unitaries by counting canonical forms whose size does not depend on the ancillae [16]. As GKZ note, this gives a Boolean oracle that needs gates at constant error, even with measurement.
Sparse oracles have been studied separately. Li et al. determined the -count of sparse classical data [22], Luo and Li synthesized sparse Boolean functions with Toffoli gates for a large support of size [26], and Nie and Zi counted the total gates of sparse Boolean oracles [27]. For one-bit oracles of sparse sets, the counting lower bounds of Li et al. and of Nie and Zi need exact implementations. Average-case error cannot capture these oracles, because such an oracle agrees with the identity on all but a small fraction of inputs. The approximate bounds of Li et al. assume at least two output bits [22, Theorem 5.1], although they allow measurement and feedforward. Theorem 26 instead holds at constant diamond error for mixtures of unitary circuits, with clean ancillae or without, and for a random set with high probability.
Exact constructions differ in the number and kind of extra qubits they use. For exact Clifford+T circuits one ancilla always suffices [5], as it does for reversible circuits of NOT, CNOT and Toffoli gates [28]. The determinant decides which oracles need that ancilla, namely those of full degree over (Remark 10), and so rules out ancilla-free exact Toffoli gates on four or more qubits [6]. Avoiding the ancilla exactly would also need gates of exponential precision [29]. Randomized branches escape both obstructions, because no branch has to implement exactly. The exact constructions of Table 1 fall into three groups. Some borrow idle qubits [6, 9, 18]. Others use one clean ancilla through conditionally clean ancillae [7, 8, 29]. The AND ladder uses many clean ancillae as temporary logical-AND gates [12–14].
Reversible-logic synthesis builds the oracle of a Boolean function from an exclusive-or of products of literals, one multi-controlled NOT per term. Fazel, Thornton and Rice gave an early cascade construction [30], and Meuli et al. compiled low-T oracles from such decompositions and from the multiplicative complexity of [31]. Schaeffer and Perkowski allowed each factor to be a parity, so that each term is the indicator of an affine subspace [19]. Theorem 2 implements those terms without ancillae and randomizes the terms of large codimension, as GKZ's sampler does for a single term.
4 Discussion and open problems
We do not know the constant in the -count of randomized Toffoli gates. The branches use about eight gates per bit of precision, and the best lower bound is about one. Algorithms such as Grover search call the oracle many times, so this constant multiplies their total -count. Is at least for some when ? For mixtures of oracle branches, Theorem 4 reduces this to whether , a question about exact multi-controlled NOTs.
Which property of the exceptional set determines the cost of with affine? Theorem 5 settles the two extremes at constant error. Subspaces cost , while random sets of density cost about with clean ancillae, so distance from affine alone does not decide the cost. Oracles in applications rarely sit at either extreme, and a compiler would want to know which of them are cheap without ancillae. A natural candidate measure is the least number of affine subspaces whose indicators have exclusive-or . Theorem 2 bounds the cost above by about . Is it also bounded below by a function of ? Our counting argument gives lower bounds only for random sets, not for a given . It also fails with advice states, which can encode , and we do not know whether random sets stay expensive with measurement and feedforward.
The branches matter most when logical qubits, rather than T gates, limit a computation. Magic-state cultivation may push computations toward that regime by making T states much cheaper [32, 33], though its cost under realistic postselection is still open [34], and early fault-tolerant devices will have few logical qubits in any case [35].
Amplitude estimation without ancillae, for example, reflects about on its whole register at every step, so no qubit is free to serve as an ancilla [36]. Compilers also treat clean qubits as scarce and borrow idle ones where they can [37]. GKZ's sampler, as written, computes its parities into clean ancillae, while the branches borrow only their own idle controls. Being unitary, the branches also avoid the mid-circuit measurements and classically controlled corrections of the measured constructions in Table 1.
Like GKZ's sampler, the branches are accurate only on average, so the control system must draw a fresh branch for every call, using about random bits. Each branch applies and its inverse with CNOT gates, about as many as GKZ's direct circuit uses to compute its parities. We have not estimated space-time volume on a specific architecture.
The branches cost more when is just above . For they can borrow only idle controls and use gates, up to twice the they use with more idle controls. We do not know how to reach with idle controls. The same question is open for exact multi-controlled
NOTs with a few borrowed qubits, and an answer there would carry over to the branches directly.
A Preliminaries
All logarithms are base 2 unless written as , and . We identify a subset with its characteristic vector in , so is also its Hamming weight. We write for the all-ones vector and for the indicator function of a set .
For , the parity is the mod-2 sum of the bits of that selects. The affine functions are the maps .
The oracle of a Boolean function acts on input qubits and one target qubit as . The Toffoli gate on qubits is the oracle of the conjunction of bits, which are its controls.
A.1 Distances and synthesis models
For states , the trace distance is , where is the trace norm. We write for the operator norm. For channels , the diamond distance is
where ranges over states of the channels' input system and a reference system on which both act as the identity. We abbreviate , where .
Definition 6 (mixed implementations). A mixed implementation of is a probability distribution over unitary Clifford+ circuits, called branches. Each branch acts on the qubits of and on ancillae that start in and are discarded at the end. The error of the mixed implementation is the diamond distance of the averaged channel from , and its cost is the largest number of gates on any branch. The minimum cost at diamond error is . Most lower bounds in Section C also hold when the ancillae start in an arbitrary advice state, as stated there.
An ancilla is clean if it starts in and must be returned to . It is borrowed (often called dirty) if it may start in any state, even one entangled with the rest of the computation, and must be returned unchanged. A branch with no ancilla acts only on the qubits of U, but it may borrow some of them as work qubits.
The next lemma, from GKZ [11, Theorem 12], bounds the diamond error of a mixture by the largest probability, over all inputs, that a random branch computes that input wrongly. We include its short proof.
Lemma 7 (error from pointwise miss probabilities). Let U be a unitary, and for each value of a random variable a let be a projector. If
then the average of the channels is within diamond distance 4p of U. In particular, let f be Boolean and let be a random Boolean function. If for every x, then
Proof. Each branch applies and then , and the final does not change trace distances. So it suffices to compare the average of with . By convexity, the maximum in the diamond distance may be taken over pure inputs, so let be a pure state of the system and the reference. Write for the averaged projector. Expanding the product gives
Since and has trace norm 1, each of the first two terms has trace norm at most . The last term is positive semidefinite, so its trace norm equals its trace. Since , this trace is
The difference therefore has trace norm at most , so the two states are at trace distance at most 4p. This holds for every pure input , which proves the first statement.
For the second statement, take and let project onto the inputs where and differ, with the target in :
An oracle applies to the target on input . Since , it is diagonal in the basis : it fixes and multiplies by . Hence is diagonal too, with entry -1 on the range of and 1 elsewhere, so . The averaged projector is diagonal, with entry at and 0 elsewhere, so its operator norm is at most . The first statement then gives the second.
A.2 Multi-controlled NOTs with borrowed qubits
Write for the m-controlled NOT, which flips its target when all m controls are 1. The constructions in Section B use three known circuits for it.
A relative-phase Toffoli gate is a Toffoli gate followed by a diagonal unitary on its three qubits. Maslov gives one with 4 T gates, against 7 for the Toffoli gate itself [6, Fig. 3]. Its inverse is the diagonal followed by the Toffoli gate. So when a relative-phase Toffoli gate and its inverse enclose gates that touch its three qubits only as controls, the two diagonals cancel and the pair acts as two exact Toffoli gates.
Borrowed qubits enter through the identity of Figure 4. A borrowed qubit holds an unknown bit. The target is toggled by , then is toggled by the AND of other controls, and then the target is toggled by again. The two toggles of the target differ by , so the target flips by whatever held, and a final toggle of by restores it. Applied recursively along a chain, this builds from Toffoli gates and borrowed qubits [18, Lemma 7.2]. Splitting the controls into two halves, each borrowing the other's qubits, brings this down to one borrowed qubit and Toffoli gates [18, Lemma 7.3 and Corollary 7.4]. Only the Toffoli gates that act on the target must be exact, and the others may be relative-phase gates. Maslov also uses relative-phase gates with three controls, so that each borrowed qubit absorbs two controls. For this gives with borrowed qubits and gates [6, Proposition 5]. For his circuit uses one borrowed qubit and gates [6, Eq. (5)].
Zindorf and Bose use a borrowed qubit differently. A rotation by multiplies every state by -1, so a rotation of the borrowed qubit, controlled by the controls and the target, is a multi-controlled whatever the borrowed qubit holds. Hadamard gates on the target turn it into . For they implement it with gates, and each further borrowed qubit, up to of them, saves 16 gates [9, Theorems 2 and 10].
B Constructions without an ancilla
This appendix gives the constructions without an ancilla: the randomized Toffoli gate in Section B.1 and its extension to exclusive-ors of affine subspaces in Section B.2. Both test a few random parities of the input with one multi-controlled NOT. Inputs that the gate does not use serve as its borrowed work qubits.

Figure 4: Toggle detection. The target is toggled by before and after b is toggled by y, so it flips by whatever b held. The last gate restores b.
B.1 The randomized Toffoli gate
Construction 8 (the sampled branch). Put and assume . The case is the third part of Theorem 11. Sample linearly independent by drawing uniformly random tuples until one is independent. Each draw succeeds with probability
so fewer than four draws are needed on average. The branch then acts as follows.
-
Apply X to all n - 1 controls.
-
Row-reduce , which keeps their common kernel, and complete them to a basis of with unit vectors on the non-pivot coordinates. Apply a CNOT circuit for the invertible linear map , which needs CNOT gates. We call the qubits that hold the data coordinates.
-
Apply with the first data coordinates as controls, active on 0.
-
Apply and then the X layer again.
On a basis state, with and , the four steps give
where . Step 3 flips the target exactly when every sampled parity of y vanishes, so the branch is the oracle . It is GKZ's branch, drawn in Figure 2. GKZ draw the uniformly, possibly dependent; the branch depends only on their span, so it can be computed in place either way, and we draw independent vectors only so that the counts below are exact.
Since acts in place, no extra register holds the sampled parities. The other data coordinates are idle during step 3, so can borrow them as work qubits.
Proposition 9 (determinants of the Toffoli gate and its branches). Let .
-
No Clifford+T circuit on the n qubits of implements it, even up to a global phase.
-
The oracle of a branch of Construction 8 has determinant 1 if and -1 if .
Proof. Both parts rest on one fact: every Clifford+T circuit on qubits has determinant 1 [5]. The determinant is multiplicative, so it suffices to check each gate. A gate on of the qubits acts on the whole register as , whose determinant is . The exponent is for a one-qubit gate, a multiple of 8, and for CNOT, a multiple of 4. With , the generators have
and . So every gate, and hence every circuit, has determinant 1.
For the first part, suppose a circuit implements . The gate swaps and and fixes all other basis states, so and the circuit's determinant is . This must equal 1, so is a root of unity. It is also the circuit's matrix entry at , which lies in [5], and the only roots of unity in are the powers of . Hence and , since 8 divides , a contradiction.
For the second part, swaps and for each with and fixes all other basis states. It is a product of disjoint transpositions, so . It remains to count these . By Construction 8, exactly when for , where . These are independent linear equations, so exactly vectors satisfy them. Since is a bijection, . This count is even when and equals 1 when .
The first part is the standard obstruction [5, 6, 28].
Remark 10 (which oracles need an ancilla). The same computation applies to any oracle. As in the second part, . In the algebraic normal form of , the coefficient of a monomial is the exclusive-or of over all whose ones lie in (Möbius inversion). For this is the exclusive-or of all values of , so is odd exactly when . For the converse also holds, so has an exact Clifford+ circuit on its qubits if and only if [28, Lemma 11, Theorem 12 and Corollary 37][5, Corollary 2].
Theorem 11 (the randomized Toffoli gate). Let and , so . If , the branches of Construction 8 form a mixed implementation of at diamond error . The cost depends on the number of idle data coordinates:
-
if , each branch has a circuit with gates, no ancilla and no measurement;
-
if , each branch has a circuit with gates, no ancilla and no measurement;
-
if , has a mixed implementation at diamond error with one clean ancilla and gates per branch.
Figure 3 plots the three regimes at .
Proof of Theorem 11. For the error, fix an input and let . By Construction 8, the branch flips the target exactly when for every . At this always holds, and flips the target too, so the branch is correct. At the gate does not flip the target, so the branch errs exactly when every lies in the hyperplane , of dimension . A space of dimension contains independent ordered -tuples, since the th vector must avoid the span of the earlier ones. Comparing with gives
since . So every input is computed wrongly with probability at most , and Lemma 7 gives diamond error at most .
For the first part, the number of idle data coordinates is . By Section A.2, Maslov's circuit for needs this many borrowed qubits and uses gates, including , where . As noted after Construction 8, the idle coordinates can serve as these borrowed qubits. The X layers and are Clifford, so the branch uses gates, no ancilla and no measurement.
For the second part, gives , and gives . In particular this range is nonempty only when . Zindorf and Bose's circuit for (Section A.2) can use the d idle coordinates as borrowed qubits, since by the bound above. It uses
T gates and is unitary, so the branch needs no ancilla and no measurement. For the count is , which Khattar and Gidney also give [7, Table 1].
For the third part, suppose first that . No data coordinate is then idle, and for each branch has determinant -1 by Proposition 9, so it cannot be built on its own qubits. We implement its gate by Barenco et al.'s construction with one borrowed qubit (Section A.2), which uses gates. One clean ancilla serves as that qubit. If , the same construction implements exactly with one clean ancilla and gates.
B.2 Affine decompositions
This subsection extends the branches from the Toffoli gate to exclusive-ors of indicators of affine subspaces. Each term reduces to a multi-controlled NOT, so we first fix its cost. Let be the -count of Maslov's circuit for from Section A.2, which borrows idle qubits. For the gate is a NOT, a CNOT or a Toffoli gate, so and , and for .
Theorem 12 (affine decompositions). Let and , where each is an affine subspace cut out by independent affine equations. Put . If for every , then has a mixed Clifford+T implementation at diamond error with no ancilla, no measurement and
T gates per branch. The terms with are implemented exactly.
The hypothesis matches the first part of Theorem 11, shifted by one because has inputs while has controls. For it forces . So every exact or sampled term is the oracle of an affine subspace of codimension less than , which has an even number of points. By Remark 10 each term then has determinant 1, which is why no ancilla is needed even when .
Proof of Theorem 12. Write . Since , we have , so it suffices to implement each . Fix , put , and write with of full row rank . As in Construction 8, complete the rows of to a basis in the same way and apply the CNOT circuit of the resulting invertible map, so that the first data coordinates hold . An gate on each coordinate with then makes exactly when these coordinates are all 0.
Suppose first that . Then on these coordinates, with controls active on 0, flips the target exactly when . Undoing the gates and gives exactly with gates. The circuit for borrows of the other data coordinates, or none when . There are enough, because the hypothesis gives .
Suppose next that . Write for the values of the coordinates, so that exactly when . We run the branch of Construction 8 on , with in place of and in place of . It samples independent and moves the parities onto of the coordinates by a CNOT circuit. It then applies with controls active on 0 and undoes the CNOT circuit, the gates and . The error computation in the proof of Theorem 11 applies unchanged. The branch is correct when and errs with probability at most at each . By Lemma 7, the averaged term is within of . Its gate costs gates and borrows of the other data coordinates. There are enough, because gives .
Choose the randomness of the terms independently. A branch for is then one branch per term, applied in sequence, and the averaged channel is the composition of the averaged term channels. Each is within of its
. The diamond distance is subadditive under composition, , so the averaged channel is within of . Since , every branch uses at most gates and makes no measurement. It borrows only data coordinates and returns them unchanged, so it needs no ancilla.
B.2.1 Every Boolean function has such a decomposition
Every Boolean function has such decompositions, and Theorem 12 applies to each of them.
- The algebraic normal form writes as an exclusive-or of monomials . Each monomial is the indicator of the affine subspace , of codimension .
• Any partition of into affine subspaces gives one, because the exclusive-or of the indicators of disjoint sets is the indicator of their union. Single points give terms of codimension n.
The decomposition should be chosen with care. For example, the algebraic normal form of has all nonempty monomials, but is a single term of codimension followed by an gate on the target.
The pieces need not be disjoint. The intersection of two affine subspaces is empty or an affine subspace, so is again such a decomposition. By Theorem 12, few terms of small codimension give a cheap oracle. Finding the cheapest decomposition is the minimization of exclusive-or sums of affine products, for which reversible-logic synthesis has heuristics [19, 30].
A parity decision tree queries one parity of the input at each internal node and outputs a bit at each leaf. A tree of depth with accepting leaves gives such a decomposition. The inputs that reach a leaf satisfy the at most equations along its path, so each nonempty accepting leaf is an affine subspace of codimension at most . Distinct leaves are disjoint, so the second item above applies. Since is nondecreasing, Theorem 12 implements the oracle of the tree's function with at most gates and no ancilla when . With workspace, GKZ's bound in terms of randomized parity decision trees gives [11, Theorem 5].
C Lower bounds
Every bound in this appendix holds for mixtures of unitary Clifford+T circuits with any number of ancillae in any advice state. The exceptions are Theorem 18, Theorem 26 and the bounds with clean ancillae or none in Theorem 28, which need ancillae that start in .
Most of the bounds go through randomized parity decision trees. A non-adaptive randomized parity decision tree of depth is a parity decision tree, as in Section B.2.1, whose queries are drawn at random and fixed in advance. Formally, it is a probability distribution over pairs consisting of parities of the input and a rule that maps their values to an output bit. It computes with error if its output equals with probability at least for every input . Write for the least depth of such a tree. These trees are randomized linear sketches over [20], and the averaging arguments below are the standard ones from that setting [20, 21].
GKZ show that , even when the ancillae start in an arbitrary advice state [11, Theorem 23 and its proof]. Girish, May, Parham and Yuen recover this reduction by a different technique [25, Eq. (21)].
C.1 The Toffoli gate
Proposition 13 (randomized parity decision trees for OR). For , and , let , the probability that a fixed nonzero vector lies in a uniformly random subspace of of codimension . Then:
-
GKZ's test of depth computes with error , and this depth exceeds the exact value by at most one when ;
-
Proof of Proposition 13. For the second part, let satisfy . The test of Construction 8 chooses linearly independent parities uniformly at random and outputs 0 exactly when all of them vanish. Their common kernel is a uniformly random subspace of codimension . The output is correct at , and at it is wrong exactly when . Invertible linear maps send any nonzero vector to any other and leave the distribution of unchanged, so is the same for every nonzero . Averaging over the nonzero vectors gives
For the first part, take a randomized tree of depth with error at most . We show that . Let give weight to 0 and spread the remaining weight uniformly over the nonzero inputs. The tree errs with probability at most on every input, hence also on an input drawn from . That error is an average over the deterministic trees in its support, so one of them errs with -probability at most . Its queries have a common kernel of codimension at most . Every input in gives the same answers, so the tree's output is constant on . If that output is 1, the tree errs at 0, with -probability . If it is 0, the tree errs on the at least nonzero inputs of , with -probability at least . The choice of makes these equal,
so either way , which rearranges to . Since decreases in , the depth is at least the least with this property.
To see that this depth is attained, let be least with and put , so that . Consider the tree that outputs 1 with probability and otherwise runs GKZ's test of depth . At 0 it errs only when it outputs 1, with probability . At a nonzero input it errs only when the test misses, with probability . This tree has depth and error , which proves the first part.
For the gap in the second part, gives , and gives . So if is the exact value, then , and GKZ's test needs depth at most .
For the third part, put . The condition is equivalent to , and the right side is at most . Taking logarithms, every with , in particular the exact value, satisfies
Since , the second part gives . So no lower bound for obtained through the GKZ reduction can exceed .
Theorem 14 (lower bound for the Toffoli gate). For and , every mixed Clifford+T implementation of Toff at diamond error , with any number of ancillae in any advice state, has a branch with at least gates.
Proof of Theorem 14. The Toffoli gate is an OR oracle up to X gates,
since the target gains . The gates are Clifford, so adding them turns a mixed implementation of either gate into one of the other with the same -count and error. The GKZ reduction and the third part of Proposition 13 with then give
□
Corollary 15 (within a factor of eight). Let and , and suppose . Let be the least branch -count of any mixed implementation of at diamond error , with any ancillae in any advice state. The branches of Theorem 11 use no ancilla and at most gates.
Proof of Corollary 15. We compare the lower bound of Theorem 14 with the count of Theorem 11. The hypothesis on n gives
so the minimum in Theorem 14 is . Using , we get
Since , the branches use
T gates.
For example, at we have , and the least with is 54. There the GKZ reduction with the exact value from Proposition 13 gives a lower bound of 33, while the branches use 280 gates, a ratio of 8.5. The ratio tends to 8 as .
C.2 Oracle branches and exact multi-controlled NOTs
The branches of Construction 8, like those of GKZ's sampler, each implement an exact oracle. This subsection shows that for such mixtures the cost of is pinned to the cost of exact multi-controlled NOTs. Let be the least -count of a unitary Clifford+ circuit that implements exactly, with any number of clean ancillae and no measurement. Known bounds give for [10, Proposition 4.1], [6, Proposition 4].
Lemma 16 (steps of ). For every , .
Proof. For the first inequality, is with one control fixed to 1. So run an optimal circuit for with that control on a clean ancilla, set to by an gate before the circuit and reset afterwards. This implements with gates.
For the second inequality, the case holds because , as is a CNOT. For , merge two controls of into one: write the AND of and into a clean ancilla with a relative-phase Toffoli gate, which uses 4 gates (Section A.2). Then apply an optimal circuit for with in place of and , and undo the relative-phase gate with 4 more gates. In between, touches only as controls, so the two relative-phase gates act as exact Toffoli gates and returns to . The result implements with gates.
A mixed implementation has oracle branches if each branch implements an oracle exactly and returns its ancillae to , where the Boolean function may depend on the branch. Write for the least cost of such an implementation.
Lemma 17 (pointwise errors of oracle branches). If the oracle branches average to within diamond distance of , then for every x.
Proof. Fix . Each branch returns its ancillae to , so we may ignore them. Take a reference qubit and the input
As in the proof of Lemma 7, an oracle multiplies by and fixes , so the branch maps to
Write for the ideal output and for the averaged output. If then . Otherwise the two differ by a relative sign and are orthogonal. Hence . For any projector , and taking gives
Together, Lemmas 7 and 17 show that for oracle branches the diamond error is the largest pointwise error up to a factor of 4:
Theorem 18 (oracle branches and exact multi-controlled NOTs). Let and . Then
With , this gives , so has the same and as when with .
Proof. As in the proof of Theorem 14, conjugating by gates replaces by with and . This keeps oracle branches oracle branches and does not change -counts.
For the upper bound, put and take GKZ's sampler [11, Theorem 1], that is, the branch of Construction 8, with its gate implemented by an optimal exact circuit with clean ancillae. Each branch is an oracle and uses gates. By the error computation in the proof of Theorem 11, every input is wrong with probability at most , so Lemma 7 gives error at most .
For the lower bound, take oracle branches with error and put . We find a branch that computes OR exactly on a subspace of dimension about , and turn it into an exact . Let be the set of inputs that branch rejects. The ideal rejects only 0, so Lemma 17 at 0 and summed over the nonzero inputs gives
Since , conditioning on the event , which has probability at least , raises its expectation by at most a factor . Some branch therefore has
We now look for a subspace of dimension that avoids every nonzero input the branch rejects, that is, with . On such a the branch rejects only 0, so in coordinates of it computes . Note that , since .
To find , choose it uniformly at random among subspaces of dimension . By the symmetry argument in the proof of Proposition 13, each nonzero vector lies in with probability . The set has at most nonzero vectors, so the expected number of them in is at most , where the last step is the choice of . A count whose expectation is below 1 is 0 for some choice, so some contains no nonzero vector of . Since , this has .
It remains to turn this branch into an exact circuit for . Let be an invertible linear map with . On controls , a target and clean ancillae, apply the CNOT circuit of , then the branch, then the CNOT circuit of . The input becomes . The branch adds to the target and leaves the input register unchanged, and restores . So the circuit implements exactly, returns its ancillae to , and uses the branch's gates, which are at most the cost of the mixture. With gates as in the proof of Theorem 14 it becomes . Hence with .
For the second statement, gives
Since t is nondecreasing, the first statement and Lemma 16 give
As , runs through all large integers and differs from by less than one. Dividing by therefore gives the statement about and .
C.3 The distance from affine
The bounds so far concern the Toffoli gate. This subsection bounds every oracle in terms of one number, its distance from the nearest affine function. For an affine , write for the fraction of inputs on which and differ. The distance from affine is the least such . It is at most , because and are both affine and one of them agrees with on at least half of the inputs.
Proposition 19 (decision trees and the distance from affine). Let be non-affine, let be affine with , and let . Then
Proof of Proposition 19. We reduce to the function , which is 1 only on the small set , of size . Since is not affine, is neither 0 nor 1, so both and its complement are nonempty. A randomized tree for of depth , plus one query of the parity that defines , is a randomized tree for of depth with the same error. Hence
and it suffices to show that every randomized tree for with error at most has depth .
Take such a tree, and let put weight uniformly on and weight uniformly on its complement. As in the proof of Proposition 13, some deterministic tree in its support errs with -probability at most . Its queries have a common kernel of codimension at most , so its output is constant on each coset of . These cosets are large but meet in few points, so the tree cannot isolate :
- If it outputs 1 on some coset , it errs on the at least points of , each of -weight at least . Its error is then at least
- If it outputs 0 everywhere, it errs on all of , with -probability .
Either way , which rearranges to
Combining this with proves the proposition.
Theorem 20 (distance from affine). For every non-affine and , every mixture of unitary Clifford+T circuits that approximates to diamond error , with any number of ancillae in any advice state, has a branch with at least gates.
Proof of Theorem 20. Take an affine attaining , which is at most . The GKZ reduction and Proposition 19 give
For the Toffoli gate, the AND of bits differs from the constant 0 only at the all-ones input, so and Theorem 20 recovers Theorem 14 up to an additive constant. The bound is tight up to a factor of eight whenever differs from an affine function on a single affine subspace, as the next corollary shows.
Corollary 21 (one exceptional subspace). Let , where is affine and is an affine subspace of codimension . Let and , and let be the least branch -count of any mixture of unitary circuits that approximates to diamond error , with any ancillae in any advice state. Then:
-
-
when , the upper bound is attained with no ancilla and no measurement, and this construction uses at most gates;
-
as .
Proof of Corollary 21. For the lower bound in the first part, differs from exactly on , so . Also is not affine: two distinct affine functions differ on half of the inputs or on all of them, while differs from on a fraction . Theorem 20 then gives the lower bound, since .
For the upper bound and the second part, , where is a CNOT circuit, possibly with an on the target. It is Clifford, so only costs gates. When , Theorem 12 with implements with no ancilla and no measurement, using
T gates, since for . For smaller the same circuits work with clean ancillae as their borrowed qubits, which allows.
For the factor of eight in the second part, write for the lower bound. Since ,
and the construction uses at most gates. For the third part, both sides of the first part are , and .
Toffoli gates, membership oracles of linear codes and all their affine shifts have this form. Each such equals up to Clifford gates, so an exact implementation needs at least gates [10], while the exact constructions of Section 3 use about .
C.4 Random exceptional sets
Two oracles at the same distance from affine can have very different costs. At constant error, the indicator of an affine subspace of codimension k is at distance about from affine and costs T gates (Corollary 21). A random set R of the same density is at the same distance, but we show that it costs far more, in two steps.
-
Decision trees. A random set meets every coset of every subspace of small codimension in about its expected proportion. A shallow tree, whose output is constant on such cosets, then cannot separate from its complement, so it needs depth close to (Theorem 24). This adapts the affine-extractor argument of Kannan et al. [20, Lemmas B.2 and B.6]. As in Proposition 19, giving half the weight keeps the error constant at every density.
-
Counting. Circuits with few gates have few possible acceptance probabilities, too few to single out most sets of density . So random sets need exponentially many gates (Theorem 26), far more than decision trees can certify.
For with and its complement nonempty, let give weight uniformly to and weight uniformly to its complement.
Lemma 22 (equidistribution on cosets). Let be a subspace of codimension , let and , and suppose for every coset of . Then every rule that is constant on the cosets of disagrees with with -probability at least .
Proof. Each coset receives nearly the same share of as of its complement, so a rule that is constant on cosets cannot tell the two halves of apart.
First, is well defined. Summing the hypothesis over the cosets gives , so is nonempty. Each coset has , so the complement of is nonempty too.
Let
be the fractions of and of its complement that lie in . The hypothesis and the bound on give . The same holds for , because and are within relative error of and . Both lie in the same interval of length , and hence
On a coset the rule errs with -probability if it outputs 0 there and if it outputs 1. Using and summing over the cosets, the rule errs with probability at least
Lemma 23 (from cosets to decision trees). If the hypothesis of Lemma 22 holds with for every subspace of codimension exactly , then and for every , with any ancillae in any advice state.
Proof. Suppose a randomized tree of depth at most has error at most . Averaging over , some deterministic tree in its support errs with -probability at most . Its queries have a common kernel of codimension at most , and its output is constant on the cosets of . It is then also constant on the cosets of any subspace of codimension exactly . The bound of Lemma 22 decreases in , so applying it to with bounds the error below by
a contradiction. Hence , and the GKZ reduction gives .
Theorem 24 (random exceptional sets). Let and , and let be an integer with and
Let contain each point independently with probability , or be a uniformly random set of size . With probability at least , every coset of every subspace of codimension at most satisfies . On this event, for every ,
the latter for mixtures of unitary Clifford+T circuits with any ancillae in any advice state. If moreover and , then also . Condition (1) holds for , and also for some .
Proof. Fix a subspace of codimension exactly and a coset of . Then , and has mean . For independent inclusion, is binomial. For a uniformly random set of fixed size it is hypergeometric, which satisfies the same Chernoff bounds [38, Theorem 4]. In both cases
The number of subspaces of codimension is a Gaussian binomial coefficient, at most , and each has cosets. The probability that some coset fails is therefore at most
The last inequality is (1) after taking logarithms, since and . Every coset of a subspace of codimension is a disjoint union of cosets of a subspace of codimension contained in it. The estimate therefore extends to all codimensions at most . Lemma 23 with and gives the bounds on and .
For the distance, the case gives , which is the distance to the constant 0. A non-constant affine splits into the hyperplane cosets and . The case gives . When , it follows that
so a is farther from than the constant 0. The constant 1 is farther still.
For the choices of , since , we have . For and ,
Since , (1) also holds once , which is satisfied by some .
Like the canonical-form count of Gosset, Kothari and Wu [16, Lemma 4.8], the first bound in the following count does not depend on the number of ancillae.
Lemma 25 (few acceptance functions). Let be a unitary Clifford+T circuit with at most gates on input qubits, a target qubit and a ancillae, and let be the probability that measuring the target of gives 1. As ranges over all such circuits, there are at most distinct functions .
Proof. The function depends on only through a little combinatorial data about the Pauli rotations in , and we count that data. Up to a global phase, with a Clifford , Hermitian Paulis and [39], [11, proof of Lemma 22]. A circuit with fewer gates is padded with . Let project the target onto , put , a Hermitian Pauli, and let . Then with .
Conjugation by fixes a Pauli that commutes with and sends one that anticommutes with to . Expanding term by term, over , where is the product of and the with in decreasing order of , and each depends only on which pairs among anticommute. Write with and . Since , each is times , with the sign fixed by the bits , and . On the basis state , is if and 0 otherwise, and depends only on the coordinates of on the input qubits.
Hence is determined by four pieces of data:
-
the phases , with choices,
-
the bits for , which also fix which pairs anticommute, with choices,
-
the subspace of indicator vectors with , with at most choices,
-
the input coordinates of the , with choices.
The product is . Alternatively, is determined by themselves, each one of Hermitian Paulis, which gives .
Theorem 26 (counting bound for random sets). Let , and , put , and let be random as in Theorem 24. With probability at least , every mixture of unitary Clifford+T circuits with clean ancillae that approximates to diamond error has cost with
and every such mixture without ancillae has cost with . At , where , this gives
with clean ancillae and without ancillae, respectively.
Proof. We show that a few branches of a cheap mixture already determine , and that few sets arise this way. Fix a mixture with clean ancillae, cost and diamond error , with branches drawn from , and put in the notation of Lemma 25. On input , measuring the target gives 1 with probability for the mixture and for . The diamond error bounds the trace distance between these outputs, so .
Let be the average of over independent branches . By Hoeffding's inequality [38], for each ,
A union bound over the inputs leaves some sample with for all , and then .
Hence belongs to the family of sets , where range over circuits with at most gates. Lemma 25 gives with .
A random rarely lands in . A uniformly random set of size is one of equally likely sets. Under independent inclusion with probability , no set has probability above . In both models .
Take the largest with . With probability above , the set is not in , so every mixture for it costs more than . As increases with , the first inequality follows. Without ancillae, the second bound of Lemma 25 with replaces by and gives the second inequality. A union bound over the two cases gives probability . At , the bound yields the two closed forms.
The first bound is positive once . The proof uses only that the ancillae start in , so it also covers branches that leave them in other states.
Proposition 27 (upper bounds for sparse sets). Let and . If , then with clean ancillae . If , then without ancillae .
Proof. Hash by a uniformly random full-rank linear map and accept when . This never errs on . For and , the nonzero vector lies in the kernel of with probability at most , so the error at is at most . As a parity decision tree this gives (compare [20, Fact B.7]).
The same hash gives circuits. A CNOT circuit brings to of the input qubits, the oracle of the -bit function acts there exactly, and the CNOT circuit is undone. Lemma 7 turns the miss probability into diamond error at most .
With clean ancillae the oracle of costs gates [17, Theorem 2], [16, Lemma 2.1], as in the randomized sparse QROM of Li et al. [22]. Without ancillae this oracle is an exclusive-or of at most point indicators on bits. Up to gates each is a gate , which borrows one of the other inputs and costs at most gates (Section A.2). This gives .
Theorem 28 (distance does not determine the T-count). Let and . Let be an affine subspace of codimension , and let be random as in Theorem 24. Then and . With probability at least , also and is at least (1) with any ancillae in any advice state,
(2) with clean ancillae, and
(3) without ancillae.
If also , then with the same probability is with clean ancillae and without ancillae.
Proof. For , Theorem 12 with and has . Since , it gives gates. The distance holds because .
For , we apply Theorem 24 with , which satisfies (1). The upper bound on gives , so the theorem bounds and gives . The bounds with clean ancillae and without ancillae are Theorem 26, and a union bound over the two theorems gives the probability.
For the upper bounds, the same event gives , so at we have . When this is at most , and Proposition 27 gives the two bounds.
Together, the -count of a random set is with clean ancillae and without, both up to factors polynomial in . Decision trees cannot show this, since .
Acknowledgments
The author thanks Ryan O'Donnell, who introduced the author to this topic, engaged in helpful discussions, and suggested the one-clean-ancilla construction in part 3 of Theorem 11. The author also thanks Junseo Lee for detailed feedback on earlier drafts.
AI Disclosure
Generative artificial intelligence tools (Claude) assisted the author in deriving and refining some of the proofs, in writing the verification code, and in drafting and revising the manuscript, including its structure, cross-references and comparisons with cited sources. The author reviewed and verified all AI-assisted arguments and text and takes full responsibility for the content of this work.
References
[1] Austin G. Fowler and Craig Gidney. Low overhead quantum computation using lattice surgery. 2018. arXiv:1808.06709.
[2] Daniel Litinski. A Game of Surface Codes: Large-Scale Quantum Computing with Lattice Surgery. Quantum, 3:128, 2019. arXiv:1808.02892.
[3] Lov K. Grover. A fast quantum mechanical algorithm for database search. In Proceedings of the 28th Annual ACM Symposium on Theory of Computing, pages 212–219, 1996.
[4] András Gilyén, Yuan Su, Guang Hao Low, and Nathan Wiebe. Quantum singular value transformation and beyond: exponential improvements for quantum matrix arithmetics. In Proceedings of the 51st Annual ACM SIGACT Symposium on Theory of Computing, pages 193–204, 2019.
[5] Brett Giles and Peter Selinger. Exact synthesis of multiqubit Clifford+T circuits. Physical Review A, 87(3):032332, 2013. arXiv:1212.0506.
[6] Dmitri Maslov. Advantages of using relative-phase Toffoli gates with an application to multiple control Toffoli optimization. Physical Review A, 93(2):022311, 2016.
[7] Tanuj Khattar and Craig Gidney. Rise of conditionally clean ancillae for efficient quantum circuit constructions, 2024. Quantum 9, 1752 (2025).
[8] Abhoy Kole, Majd Assaad, Till Schnittka, and Rolf Drechsler. Adaptive Clifford+T decomposition of large Toffoli gates with one clean ancilla. arXiv:2605.18169, 2026.
[9] Ben Zindorf and Sougato Bose. Efficient implementation of multi-controlled quantum gates, 2024. arXiv:2404.02279.
[10] Michael Beverland, Earl Campbell, Mark Howard, and Vadym Kliuchnikov. Lower bounds on the non-Clifford resources for quantum computations. Quantum Science and Technology, 5(3):035009, May 2020.
[11] David Gosset, Robin Kothari, and Chenyi Zhang. Multi-qubit Toffoli with exponentially fewer T gates. arXiv:2510.07223v1; theorem numbers refer to v1, 2025.
[12] Cody Jones. Low-overhead constructions for the fault-tolerant Toffoli gate. Physical Review A, 87(2):022328, 2013. arXiv:1212.5069.
[13] Craig Gidney. Halving the cost of quantum addition. Quantum, 2:74, 2018. arXiv:1709.06648.
[14] Abhoy Kole, Till Schnittka, and Rolf Drechsler. Measurement-driven adaptive low-overhead implementation of multi-controlled Toffoli gates. arXiv:2605.18159, 2026.
[15] Guangyi Li, Yu Gan, Zeguan Wu, Xueyue Zhang, Zheshen Zhang, and Junyu Liu. Stab-QRAM: A Clifford-only quantum oracle for affine Boolean data. arXiv:2509.26494, 2025.
[16] David Gosset, Robin Kothari, and Kewen Wu. Quantum state preparation with optimal T-count. arXiv:2411.04790, 2024.
[17] Guang Hao Low, Vadym Kliuchnikov, and Luke Schaeffer. Trading T gates for dirty qubits in state preparation and unitary synthesis. Quantum, 8:1375, 2024. arXiv:1812.00954.
[18] Adriano Barenco, Charles H. Bennett, Richard Cleve, David P. DiVincenzo, Norman Margolus, Peter Shor, Tycho Sleator, John A. Smolin, and Harald Weinfurter. Elementary gates for quantum computation. Physical Review A, 52(5):3457–3467, 1995. arXiv:quant-ph/9503016.
[19] Ben Schaeffer and Marek Perkowski. Synthesis of reversible circuits based on EXORs of products of EXORs. In 2013 IEEE 43rd International Symposium on Multiple-Valued Logic (ISMVL), 2013.
[20] Sampath Kannan, Elchanan Mossel, Swagato Sanyal, and Grigory Yaroslavtsev. Linear sketching over . In 33rd Computational Complexity Conference (CCC 2018), volume 102 of LIPIcs, pages 8:1–8:37, 2018. arXiv:1611.01879.
[21] Ashley Montanaro and Tobias Osborne. On the communication complexity of XOR functions. arXiv:0909.3392, 2009.
[22] Tongyang Li, Fengning Ou, Xinzhao Wang, Penghui Yao, Pei Yuan, and Shengyu Zhang. Optimal T counts under sparsity: from QROM to state preparation and block encoding. arXiv:2607.28260, 2026.
[23] Earl Campbell. Shorter gate sequences for quantum computing by mixing unitaries. Physical Review A, 95:042306, 2017.
[24] Matthew B. Hastings. Turning gate synthesis errors into incoherent errors. Quantum Information and Computation, 17:488–494, 2017.
[25] Uma Girish, Alex May, Natalie Parham, and Henry Yuen. Magic and communication complexity. arXiv:2510.07246, 2025.
[26] Jingquan Luo and Lvzhou Li. Sparse quantum state preparation with sublinear T-count. arXiv:2608.00414, 2026.
[27] Junhong Nie and Wei Zi. Nearly optimal quantum circuits for Boolean oracles. arXiv:2607.28402, 2026.
[28] Vivek V. Shende, Aditya K. Prasad, Igor L. Markov, and John P. Hayes. Synthesis of reversible logic circuits. IEEE Transactions on Computer-Aided Design of Integrated Circuits and Systems, 22(6):710–722, 2003. arXiv:quant-ph/0207001; theorem numbers refer to arXiv v4.
[29] Junhong Nie, Wei Zi, and Xiaoming Sun. Quantum circuit for multi-qubit Toffoli gate with optimal resource. arXiv:2402.05053, 2024.
[30] Kamalika Fazel, Mitchell A. Thornton, and Jacqueline E. Rice. ESOP-based Toffoli gate cascade generation. In 2007 IEEE Pacific Rim Conference on Communications, Computers and Signal Processing, pages 206–209, 2007.
[31] Giulia Meuli, Mathias Soeken, Earl Campbell, Martin Roetteler, and Giovanni De Micheli. The role of multiplicative complexity in compiling low T-count oracle circuits. In 2019 IEEE/ACM International Conference on Computer-Aided Design (ICCAD), pages 1–8, 2019. arXiv:1908.01609.
[32] Craig Gidney, Noah Shutty, and Cody Jones. Magic state cultivation: growing T states as cheap as CNOT gates. arXiv:2409.17595, 2024.
[33] E. Rosenfeld, C. Gidney, et al. Magic state cultivation on a superconducting quantum processor. arXiv:2512.13908, 2025.
[34] Rohan Mehta, Varun Menon, Hengyun Zhou, Mikhail D. Lukin, and J. Pablo Bonilla Ataide. The practical cost of magic state cultivation. arXiv:2610.06828, 2026.
[35] Amara Katabarwa, Katerina Gratsea, Athena Caesura, and Peter D. Johnson. Early fault-tolerant quantum computing. PRX Quantum, 5:020101, 2024.
[36] Jona Erle and Bálint Koczor. Nearly optimal amplitude estimation at any depth. arXiv:2608.24434, 2026.
[37] Bonan Su, Li Zhou, Yuan Feng, and Mingsheng Ying. Borrowing dirty qubits in quantum programs. arXiv:2508.17190, 2025.
[38] Wassily Hoeffding. Probability inequalities for sums of bounded random variables. Journal of the American Statistical Association, 58(301):13–30, 1963.
[39] David Gosset, Vadym Kliuchnikov, Michele Mosca, and Vincent Russo. An algorithm for the T-count. Quantum Information and Computation, 14(15–16):1261–1276, 2014. arXiv:1308.4134.