Intro & Main Results
Consider an injective function f : Z 2 n → Z 2 m f : \mathbb{Z}_{2^n} \to\mathbb{Z}_{2^m} f : Z 2 n → Z 2 m . A fundamental task across quantum algorithms research is to determine whether various injective functions like f f f can be computed efficiently using quantum circuits, and if they can, at what resource cost. Gate, depth, and space requirements are the principal resources of interest and are often traded for one another. For well-studied functions, such as certain arithmetic operations, asymptotic performance improvements are highly sought after and heavily researched, due to the widespread use of these operations as subroutines in larger algorithms [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] .
David Halpenny: david.halpenny1@outlook.com
Adithya Sireesh: s1616497@ed.ac.uk
Two natural approaches for computing these functions exist, typically referred to as in-place and out-of-place , illustrated (without considering potential ancillas) below:
out-of-place : ∣ x ⟩ n ∣ 0 ⟩ m ⟼ ∣ x ⟩ n ∣ f ( x ) ⟩ m in-place : ∣ x ⟩ n ∣ 0 ⟩ m − n ⟼ ∣ f ( x ) ⟩ m . (1) \begin{aligned}
\text{out-of-place}: \lvert x\rangle_n\lvert0\rangle_m &\longmapsto\lvert x\rangle_n\lvert f(x)\rangle_m \\
\text{in-place}: \lvert x\rangle_n\lvert0\rangle_{m-n} &\longmapsto\lvert f(x)\rangle_m . \tag*{(1)}
\end{aligned} out-of-place : ∣ x ⟩ n ∣ 0 ⟩ m in-place : ∣ x ⟩ n ∣ 0 ⟩ m − n ⟼ ∣ x ⟩ n ∣ f ( x ) ⟩ m ⟼ ∣ f ( x ) ⟩ m . (1)
In the context of a larger algorithm, each approach offers a distinct benefit. If the function input needs to be reused later, retaining it with an out-of-place approach is desirable. Otherwise, an in-place approach, defined by its uncomputation of the function input is preferable. In many algorithms, uncomputation is an essential component [7 , 8 , 9 , 10 , 11 , 12 , 13 ] .
The standard approach for out-of-place integer multiplication in both classical and quantum settings is the schoolbook method, which can be implemented by repeatedly using an addition circuit, requiring a gate count that scales quadratically with respect to the length of the input. Fast constructions achieving subquadratic gate counts have also been developed, based on recursive multiplication algorithms from classical computing [14 , 15 , 16 , 17 , 18 , 19 , 20 ] . Until recently, it was a major open question whether the recursive nature of these constructions could be reconciled with quantum reversibility without incurring a space overhead, for the storage of intermediate computations, scaling linearly in the length of the input. This question was resolved in [20 ] , when it was proven that out-of-place quantum integer multiplication is possible using O ( n 1 + ϵ ) O(n^{1+\epsilon}) O ( n 1 + ϵ ) gates and no ancilla qubits, for any ϵ > 0 \epsilon> 0 ϵ > 0 .
It is a well-known fact that two unitaries U f U_f U f and U g U_g U g computing f f f and its left-inverse g g g out-of-place can be used to build an in-place algorithm for f f f [21 ] . This is clear by considering the following sequence of operations:
∣ x ⟩ n ∣ 0 ⟩ m → U f ∣ x ⟩ n ∣ f ( x ) ⟩ m → S W A P ∣ f ( x ) ⟩ m ∣ x ⟩ n → U g − 1 ∣ f ( x ) ⟩ m ∣ 0 ⟩ n . (2) \lvert x\rangle_n\lvert0\rangle_m \xrightarrow{U_f} \lvert x\rangle_n\lvert f(x)\rangle_m \xrightarrow{\mathrm{SWAP}} \lvert f(x)\rangle_m\lvert x\rangle_n \xrightarrow{U_g^{-1}} \lvert f(x)\rangle_m\lvert0\rangle_n . \tag*{(2)} ∣ x ⟩ n ∣ 0 ⟩ m U f ∣ x ⟩ n ∣ f ( x ) ⟩ m SWAP ∣ f ( x ) ⟩ m ∣ x ⟩ n U g − 1 ∣ f ( x ) ⟩ m ∣ 0 ⟩ n . (2)
However, the space efficiency of this in-place algorithm is clearly sub-optimal, involving n n n ancilla qubits for the simultaneous storage of both the function input and output, in addition to any workspace required. In contrast, an ancilla-free , in-place algorithm for computing f f f is optimally space-efficient, directly overwriting the input with the function output. In-place and ancilla-free constructions for standard multiplication and modular multiplication modulo a power of two exist [22 , 23 , 24 , 25 , 26 , 27 ] ([24] is nearly ancilla-free), all scaling quadratically with respect to the length of the input in terms of gate count.
The history of quantum instantiations for fast multiplication and the evident tension between gate and space cost motivate the following question: since in-place and ancilla-free algorithms reduce space cost maximally, might they be fundamentally prevented from being made fast?
Our Contributions. In this paper, we show that this is not the case. We present in-place and ancilla-free (in all but one of the cases we highlight) algorithms achieving subquadratic gate count for a number of quantum arithmetic operations (see Table 1 ).
Table 1. Main arithmetic results. The results hold for any ϵ > 0 \epsilon> 0 ϵ > 0 as long as m = O ( n ) m = O(n) m = O ( n ) . The first two results are for multiplication (modular and standard) of a quantum register by a classical multiplier. The next two are for multiplication (modular and standard) between two quantum registers. The fifth result is for modular inversion of a quantum register. The ancilla count s s s is a parameter in our construction that determines the exact cost of the algorithm. For all s s s in the range 0 ≤ s ≤ n − 1 0 \leq s \leq n - 1 0 ≤ s ≤ n − 1 the claimed asymptotic gate count holds. We work in the abstract quantum circuit model.
In more detail, we show that integer multiplication modulo a power of two can be performed in-place on a quantum register between two O ( n ) O(n) O ( n ) -bit integers using O ( n 1 + ϵ ) O(n^{1+\epsilon}) O ( n 1 + ϵ ) gates and no ancilla qubits, for any ϵ > 0 \epsilon> 0 ϵ > 0 . The multiplier can be either a classically chosen value a ∈ Z 2 n a \in\mathbb{Z}_{2^n} a ∈ Z 2 n (Theorem 3.1 ) or a quantum state ∣ x ⟩ m \lvert x\rangle_m ∣ x ⟩ m for x ∈ Z 2 m x \in\mathbb{Z}_{2^m} x ∈ Z 2 m and m = O ( n ) m = O(n) m = O ( n ) (Theorem 4.3 ).
We then show that standard integer multiplication can be performed in-place on a quantum register between two O ( n ) O(n) O ( n ) -bit integers using O ( n 1 + ϵ ) O(n^{1+\epsilon}) O ( n 1 + ϵ ) gates for any ϵ > 0 \epsilon> 0 ϵ > 0 , and with a multiplier-dependent ancilla count (Theorems 3.3 and 4.5 ). When the multiplier is a classically chosen value a ∈ Z 2 m ∖ { 0 } a \in\mathbb{Z}_{2^m} \setminus\{0\} a ∈ Z 2 m ∖ { 0 } or a quantum state ∣ x ⟩ m \lvert x\rangle_m ∣ x ⟩ m with x ∈ Z 2 m × x \in\mathbb{Z}_{2^m}^{\times} x ∈ Z 2 m × and m = O ( n ) m = O(n) m = O ( n ) , no ancillas are required, and when x ∈ Z 2 m ∖ { 0 } x \in\mathbb{Z}_{2^m} \setminus\{0\} x ∈ Z 2 m ∖ { 0 } , just ⌈ log 2 m ⌉ \lceil\log_2 m\rceil ⌈ log 2 m ⌉ ancillas are required. Note that in-place multiplication by zero is not a unitary operation (see Remark 4.9 for details on handling this case). We also prove that our multiplication algorithms involving a classical value have O ( n log n ) O(n \log n) O ( n log n ) circuit depth.
Finally, we show that modular inversion of x ∈ Z 2 n × x \in\mathbb{Z}_{2^n}^{\times} x ∈ Z 2 n × can be performed in-place on a quantum register using O ( n 1 + ϵ ) O(n^{1+\epsilon}) O ( n 1 + ϵ ) gates and s s s ancilla qubits, for any ϵ > 0 \epsilon> 0 ϵ > 0 and 0 ≤ s ≤ n − 1 0 \leq s \leq n - 1 0 ≤ s ≤ n − 1 (Theorem 5.4 ). The ancilla count s s s is a parameter affecting the true gate count of the algorithm and does not impact the stated asymptotic upper bound, meaning that our fast modular inversion algorithm can be ancilla-free.
To the best of our knowledge, these are the first known quantum algorithms achieving both subquadratic gate counts and sublinear ancilla counts for all of the stated in-place operations. With the exception of our algorithm for standard multiplication by ∣ x ⟩ m |x\rangle_m ∣ x ⟩ m for x ∈ Z 2 m ∖ { 0 } x \in\mathbb{Z}_{2^m} \setminus\{0\} x ∈ Z 2 m ∖ { 0 } , all of these results are optimal in terms of space cost.
We unify our multiplication algorithms using a single construction (Theorem 2.5 ) that treats quantum versus classical and modular versus standard multiplication as parameters that can be turned on and off before running the algorithm. The results that we highlight in Table 1 are therefore a selection of well-motivated operations in the context of quantum arithmetic following from this main theorem, and not an exhaustive list. We believe this main theorem of our work may help to build other fast and space-efficient arithmetic operations.
Central to our results are two subroutines: the phase-encoded fast integer multiplication operation introduced in [20 ] , which we extend (Lemmas 2.2 and 2.3 ), and a fast controlled and parametrised version of the quantum Fourier transform, which to our knowledge we introduce here for the first time, and which we expect to be of independent interest (Lemma 2.4 ).
Motivated by highly space-constrained settings where temporarily idle qubits may be repurposed [13 , 28 , 29 , 30 ] , we develop variants of our standard integer multiplication algorithms which encode the ( m + n ) (m+n) ( m + n ) -bit product on an idle quantum register in a potentially unknown state (Theorems 3.6 and 4.8 ). If used within a larger algorithm, these constructions may be even more space-efficient than the main results of this work, as they avoid initialising an m m m qubit register ∣ 0 ⟩ m |0\rangle_m ∣0 ⟩ m to store the ( m + n ) (m+n) ( m + n ) -bit product (see Remark 3.5 for more details).
Paper Outline
Our work is structured into four main sections. In Section 2, we introduce the fast and ancilla-free subroutines critical for this work and we present our main theorem that unifies standard and modular multiplication with classical and quantum multipliers in a single construction. We prove the cost of these subroutines in Appendices A and B, and an extension of our main theorem in Appendix E. In Section 3, we present our fast, in-place, and ancilla-free algorithms for integer multiplication with a classical multiplier, and in Section 4, we present our algorithms for multiplication with a quantum multiplier. In Section 5, we use our algorithm for modular multiplication, modulo a power of two, to construct a fast algorithm for inversion modulo a power of two. We leave the proof of some additional subroutines used in our multiplication and inversion algorithms for Appendices C and D. We conclude with a discussion in Section 6.
Conventions
Throughout this work we adopt the following conventions. A length n n n bitstring is denoted x n − 1 ⋯ x 0 x_{n-1}\cdots x_0 x n − 1 ⋯ x 0 and identified with the integer x = ∑ i = 0 n − 1 2 i x i x = \sum_{i=0}^{n-1} 2^i x_i x = ∑ i = 0 n − 1 2 i x i in Z 2 n \mathbb{Z}_{2^n} Z 2 n . The bit-reversal x 0 ⋯ x n − 1 x_0\cdots x_{n-1} x 0 ⋯ x n − 1 is denoted x ˉ \bar{x} x ˉ and identified with ∑ i = 0 n − 1 2 n − i − 1 x i \sum_{i=0}^{n-1} 2^{n-i-1}x_i ∑ i = 0 n − 1 2 n − i − 1 x i . The variables w , x , y , z w, x, y, z w , x , y , z will always refer to integers that are stored within a quantum register. We refer to these as quantum integers. The variables a , b , c a, b, c a , b , c will always refer to an integer that is a classical parameter of an algorithm, chosen before the algorithm starts. We refer to this as a classical integer. We use Z 2 n × \mathbb{Z}_{2^n}^{\times} Z 2 n × to refer to the multiplicative group of Z 2 n \mathbb{Z}_{2^n} Z 2 n , whose elements we identify with odd integers.
We use subscripts to denote the number of qubits contained within a quantum register and to denote the number of qubits a unitary operation is being applied to. For example, ∣ x ⟩ n |x\rangle_n ∣ x ⟩ n refers to an n n n qubit quantum register storing the integer x x x and U m , n U_{m,n} U m , n refers to a unitary transformation applied to two registers of sizes m m m and n n n . We use I m I_m I m for the identity on m m m qubits. If during an algorithm, we apply an operation to just a subregister of the total register of qubits, it is understood that the rest of the qubits are receiving the identity.
Unless stated otherwise logarithms are taken base 2. We define the function t z : Z 2 n → Z n + 1 t_z : \mathbb{Z}_{2^n} \to\mathbb{Z}_{n+1} t z : Z 2 n → Z n + 1 by t z ( x ) = log ( gcd ( x , 2 n ) ) t_z(x) = \log(\gcd(x, 2^n)) t z ( x ) = log ( g cd( x , 2 n )) . This function counts the number of trailing zeroes of the bitstring x x x , evaluating to n n n for the zero bitstring. We use G ( ⋅ ) G(\cdot) G ( ⋅ ) and D ( ⋅ ) D(\cdot) D ( ⋅ ) to denote the gate count and depth of an operation. When solving gate and depth recurrence relations, we often implicitly apply the master theorem for divide and conquer algorithms. We use 1 ( P ) \mathbb{1}(P) 1 ( P ) to denote an indicator function which takes the value 1 when the predicate P P P is true and 0 when it is not.
We work in the abstract quantum circuit model. We assign equal cost to all elementary gates and assume that arbitrary rotation gates can be implemented exactly. We assume qubits have all-to-all connectivity and that gates acting on disjoint sets of qubits can be executed in parallel.
Main Theorem
We now develop the subroutines used in our main theorem. We present algorithms for phase-encoded integer multiplication between two or three differently sized registers and establish their gate, depth, and ancilla costs. We use these operations to develop a fast controlled and parametrised quantum Fourier transform that requires no ancilla qubits. Together, these results provide the tools needed to treat multiplication by both classical and quantum integers within a single construction, culminating in our main theorem.
Subroutines
We start by defining the operations that we need for our main theorem, Theorem 2.5 . First, we consider the parametrised operations P t , m , n ( ϕ , α ) \mathbf{P}_{t,m,n}(\phi,\alpha) P t , m , n ( ϕ , α ) , P ‾ t , m , n ( ϕ , α ) \overline{\mathbf{P}}_{t,m,n}(\phi,\alpha) P t , m , n ( ϕ , α ) acting on t + m + n t+m+n t + m + n qubits defined as follows. Let x ∈ Z 2 t x \in\mathbb{Z}_{2^t} x ∈ Z 2 t , y ∈ Z 2 m y \in\mathbb{Z}_{2^m} y ∈ Z 2 m , z ∈ Z 2 n z \in\mathbb{Z}_{2^n} z ∈ Z 2 n , ϕ ∈ Q \phi\in\mathbb{Q} ϕ ∈ Q and α ∈ { 0 , 1 } \alpha\in\{0,1\} α ∈ { 0 , 1 } . Then
P t , m , n ( ϕ , α ) : ∣ x ⟩ t ∣ y ⟩ m ∣ z ⟩ n ⟼ e 2 π i ϕ y z x α ∣ x ⟩ t ∣ y ⟩ m ∣ z ⟩ n , (3) \mathbf{P}_{t,m,n}(\phi,\alpha): |x\rangle_t |y\rangle_m |z\rangle_n \longmapsto e^{2\pi i\phi yzx^\alpha}|x\rangle_t |y\rangle_m |z\rangle_n, \tag*{(3)} P t , m , n ( ϕ , α ) : ∣ x ⟩ t ∣ y ⟩ m ∣ z ⟩ n ⟼ e 2 π i ϕ y z x α ∣ x ⟩ t ∣ y ⟩ m ∣ z ⟩ n , (3)
P ‾ t , m , n ( ϕ , α ) : ∣ x ⟩ t ∣ y ⟩ m ∣ z ⟩ n ⟼ e 2 π i ϕ ‾ y z x α ∣ x ⟩ t ∣ y ⟩ m ∣ z ⟩ n . (4) \overline{\mathbf{P}}_{t,m,n}(\phi,\alpha): |x\rangle_t |y\rangle_m |z\rangle_n \longmapsto e^{2\pi i\overline{\phi}yzx^\alpha}|x\rangle_t |y\rangle_m |z\rangle_n. \tag*{(4)} P t , m , n ( ϕ , α ) : ∣ x ⟩ t ∣ y ⟩ m ∣ z ⟩ n ⟼ e 2 π i ϕ y z x α ∣ x ⟩ t ∣ y ⟩ m ∣ z ⟩ n . (4)
Both gates perform a multiplication of integers in the phase. When α = 1 \alpha= 1 α = 1 there are three integers involved and when α = 0 \alpha= 0 α = 0 there are just two. We use the subscripts t t t , m m m and n n n to indicate the sizes of the registers that we are applying the gate to. The first of the subscripts indicates which register is optional, i.e. controlled by α \alpha α . The parameter ϕ \phi ϕ determines the fraction that appears in the phase of the complex exponential. The gate is well-defined as long as t , m , n ≥ 0 t,m,n \ge0 t , m , n ≥ 0 and at least one of these is non-zero. Since it serves no purpose, we drop the parameter α \alpha α from the definition if t = 0 t = 0 t = 0 . It is worth observing that
I t ⊗ P 0 , m , n ( ϕ ) = P t , m , n ( ϕ , 0 ) . I_t \otimes\mathbf{P}_{0,m,n}(\phi) = \mathbf{P}_{t,m,n}(\phi,0). I t ⊗ P 0 , m , n ( ϕ ) = P t , m , n ( ϕ , 0 ) .
It is proven in [20 ] that, for any ϵ > 0 \epsilon> 0 ϵ > 0 , the operations P 0 , n , n ( ϕ ) \mathbf{P}_{0,n,n}(\phi) P 0 , n , n ( ϕ ) and P n , n , n ( ϕ , 1 ) \mathbf{P}_{n,n,n}(\phi,1) P n , n , n ( ϕ , 1 ) can be performed using just O ( n 1 + ϵ ) O(n^{1+\epsilon}) O ( n 1 + ϵ ) gates without requiring any ancilla qubits. These results are reviewed in Appendix § 8 . It is also shown in [20 ] how to improve the circuit depth of these operations using ancillas. The authors observed that, without the use of ancillas, the algorithm for P 0 , n , n ( ϕ ) \mathbf{P}_{0,n,n}(\phi) P 0 , n , n ( ϕ ) seems to require a circuit of depth Ω ( n ) \Omega(n) Ω ( n ) .
In the following lemma, we show that P 0 , n , n ( ϕ ) \mathbf{P}_{0,n,n}(\phi) P 0 , n , n ( ϕ ) can be performed in O ( n ) O(n) O ( n ) depth without the use of ancillas, matching this suggested lower bound.
Lemma 2.1 (Linear-depth phase multiplication). Let n > 0 n > 0 n > 0 . For any ϵ > 0 \epsilon> 0 ϵ > 0 , there exists a quantum circuit for P 0 , n , n ( ϕ ) \mathbf{P}_{0,n,n}(\phi) P 0 , n , n ( ϕ ) that uses O ( n 1 + ϵ ) O(n^{1+\epsilon}) O ( n 1 + ϵ ) gates, O ( n ) O(n) O ( n ) depth, and no ancilla qubits.
Proof sketch. The full proof of this lemma is left for Appendix § 8 . For those familiar with [20 ] , the key observation is that we can choose the ( 2 k − 1 ) × k (2k - 1) \times k ( 2 k − 1 ) × k matrix A A A so that, when we divide the input registers ∣ y ⟩ n |y\rangle_n ∣ y ⟩ n and ∣ z ⟩ n |z\rangle_n ∣ z ⟩ n into k k k subregisters, at least two linear combinations of these k k k subregisters can always be prepared simultaneously, using a sequence of out-of-place quantum additions. This roughly halves the number of sequential subproblems at each level of recursion in the algorithm for P 0 , n , n ( ϕ ) \mathbf{P}_{0,n,n}(\phi) P 0 , n , n ( ϕ ) . As a result, the O ( n ) O(n) O ( n ) depth requirement at the top level of recursion dominates the total algorithm depth. The gate and ancilla count costs are retained. The matrix A A A that we choose uses evaluation points { ∞ , 0 , 1 , 2 , … , 2 k − 3 } \{\infty,0,1,2,\ldots,2k-3\} { ∞ , 0 , 1 , 2 , … , 2 k − 3 } .
The following two lemmas show how to handle phase-encoded multiplication between registers of different sizes, extending the results of [20 ] . We show that the multiplication costs no more than in the equal-size case, with the register size set to the bit length of the largest input.
Lemma 2.2 (Phase multiplication with two differently sized inputs). Let n > 0 n > 0 n > 0 and 0 ≤ m ≤ n 0 \le m \le n 0 ≤ m ≤ n . For any ϵ > 0 \epsilon> 0 ϵ > 0 , there exists a quantum circuit for P 0 , m , n ( ϕ ) \mathbf{P}_{0,m,n}(\phi) P 0 , m , n ( ϕ ) that uses O ( n 1 + ϵ ) O(n^{1+\epsilon}) O ( n 1 + ϵ ) gates, O ( n ) O(n) O ( n ) depth, and no ancilla qubits.
Proof sketch. The full proof of this lemma is left for Appendix § 8 . We give a brief sketch of the proof here. Consider q > 0 q > 0 q > 0 and 0 ≤ r < m 0 \le r < m 0 ≤ r < m such that n = q m + r n = qm + r n = q m + r . Dividing ∣ z ⟩ n |z\rangle_n ∣ z ⟩ n into q q q subregisters Z q − 1 , … , Z 0 \mathbb{Z}_{q-1},\ldots,\mathbb{Z}_0 Z q − 1 , … , Z 0 of size m m m , and 1 1 1 subregister of size r r r , we apply P 0 , m , m ( 2 q m ϕ ) \mathbf{P}_{0,m,m}(2^{qm}\phi) P 0 , m , m ( 2 q m ϕ ) to ∣ y ⟩ m |y\rangle_m ∣ y ⟩ m and Z i Z_i Z i . This reduces the problem to performing P 0 , m , r ( 2 q m ϕ ) \mathbf{P}_{0,m,r}(2^{qm}\phi) P 0 , m , r ( 2 q m ϕ ) . This construction can be applied recursively, with the register sizes determined by the Euclidean algorithm for finding the greatest common divisor of n n n and m m m . The claimed asymptotic costs follow.
Lemma 2.3 (Phase multiplication with up to three differently sized inputs). Let n > 0 n>0 n > 0 and 0 ≤ t ≤ m ≤ n 0\leq t\leq m\leq n 0 ≤ t ≤ m ≤ n . For any ϵ > 0 \epsilon>0 ϵ > 0 , there exist quantum circuits for P t , m , n ( ϕ , α ) P_{t,m,n}(\phi,\alpha) P t , m , n ( ϕ , α ) and P ‾ t , m , n ( ϕ , α ) \overline{P}_{t,m,n}(\phi,\alpha) P t , m , n ( ϕ , α ) that use O ( n 1 + ϵ ) O(n^{1+\epsilon}) O ( n 1 + ϵ ) gates and no ancilla qubits. For the special case t = 0 t=0 t = 0 , these circuits have O ( n ) O(n) O ( n ) depth.
Proof sketch. The full proof of this lemma is left for Appendix § 8 . For those familiar with [20 ] , or for those who have read the review we provide in Appendix § 8 , we give a brief sketch of the proof here. The key is to apply three recursions. The first recursion creates k k k subproblems using the decomposition of ϕ z \phi z ϕ z from Equations A.6 and is applied to depth s = ⌊ log k ( n / m ) ⌋ s=\lfloor\log_k(n/m)\rfloor s = ⌊ log k ( n / m )⌋ , until the subproblems are between registers of size n / k s n/k^s n / k s , m m m and t t t . The second recursion creates 2 k − 1 2k-1 2 k − 1 subproblems using the decomposition of ϕ y z \phi yz ϕ y z from Equations A.6 and is applied to depth r = ⌊ log k ( n / t k s ) ⌋ r=\lfloor\log_k(n/tk^s)\rfloor r = ⌊ log k ( n / t k s )⌋ , until the subproblems are between registers of size n / k s + r n/k^{s+r} n / k s + r , n / k s + r n/k^{s+r} n / k s + r and t t t . The final recursion creates 3 k − 2 3k-2 3 k − 2 subproblems using the decomposition of ϕ x y z \phi xyz ϕ x y z and is applied until the subproblems are between registers of size O ( 1 ) O(1) O ( 1 ) . A key observation is that at the boundary of the first recursion and at the boundary of the second recursion, the register of size m m m and the register of size t t t , is treated as though it has been padded with zeroes. This virtual padding ensures that we can view the integers that we recurse on next as if they are sitting on registers of equal-size, making the decompositions of Equations A.6 applicable. The gate count can be upper bounded by G ( P n , n , n ( ϕ , 1 ) ) G(P_{n,n,n}(\phi,1)) G ( P n , n , n ( ϕ , 1 )) and the algorithms require no ancillas.
Next, we consider the operation Q F T m , n ( ϕ , α ) \mathbf{QFT}_{m,n}(\phi,\alpha) QFT m , n ( ϕ , α ) acting on m + n m+n m + n qubits defined as follows. Let x ∈ Z 2 m x\in\mathbb{Z}_{2^m} x ∈ Z 2 m , y ∈ Z 2 n y\in\mathbb{Z}_{2^n} y ∈ Z 2 n and ϕ ∈ Z 2 n \phi\in\mathbb{Z}_{2^n} ϕ ∈ Z 2 n and let α ∈ { 0 , 1 } \alpha\in\{0,1\} α ∈ { 0 , 1 } . Then
Q F T m , n ( ϕ , α ) : ∣ x ⟩ m ∣ y ⟩ n ⟼ ∣ x ⟩ m 1 2 n ∑ z ∈ Z 2 n e 2 π i ϕ y z x α / 2 n ∣ z ⟩ n . (5) \mathbf{QFT}_{m,n}(\phi,\alpha): |x\rangle_m|y\rangle_n\longmapsto|x\rangle_m\frac{1}{\sqrt{2^n}}\sum_{z\in\mathbb{Z}_{2^n}}e^{2\pi i\phi yz x^\alpha/2^n}|z\rangle_n. \tag*{(5)} QFT m , n ( ϕ , α ) : ∣ x ⟩ m ∣ y ⟩ n ⟼ ∣ x ⟩ m 2 n 1 z ∈ Z 2 n ∑ e 2 π i ϕ y z x α / 2 n ∣ z ⟩ n . (5)
This operation is a controlled and parametrised version of the standard quantum Fourier transform on Z 2 n \mathbb{Z}_{2^n} Z 2 n which leaves bitstrings in the output superposition in reverse order. When α = 1 \alpha=1 α = 1 the parametrised QFT is controlled on the register of m m m qubits and when α = 0 \alpha=0 α = 0 it is not. As before, we use the subscripts m m m and n n n to indicate the size of the registers that we are applying the gate to. The first subscript always refers to the optional control register. The gate is well-defined as long as m ≥ 0 m\geq0 m ≥ 0 and n > 0 n>0 n > 0 . For m = 0 m=0 m = 0 , we can drop the parameter α \alpha α , as it serves no purpose. It is worth observing that Q F T 0 , n ( 1 ) \mathbf{QFT}_{0,n}(1) QFT 0 , n ( 1 ) is the standard QFT on Z 2 n \mathbb{Z}_{2^n} Z 2 n and that Q F T m , n ( ϕ , 0 ) = I m ⊗ Q F T 0 , n ( ϕ ) \mathbf{QFT}_{m,n}(\phi,0)=I_m\otimes\mathbf{QFT}_{0,n}(\phi) QFT m , n ( ϕ , 0 ) = I m ⊗ QFT 0 , n ( ϕ ) . We note that the parametrised QFT on Z 2 n \mathbb{Z}_{2^n} Z 2 n , Q F T 0 , n ( ϕ ) \mathbf{QFT}_{0,n}(\phi) QFT 0 , n ( ϕ ) in our notation, was also considered in the context of quantum arithmetic in [23 , 25 ] .
Lemma 2.4 (Fast controlled and parametrised QFT). Let n > 0 n>0 n > 0 and m = O ( n ) m=O(n) m = O ( n ) . For any ϵ > 0 \epsilon>0 ϵ > 0 , there exists a quantum circuit for Q F T m , n ( ϕ , α ) \mathbf{QFT}_{m,n}(\phi,\alpha) QFT m , n ( ϕ , α ) that uses O ( n 1 + ϵ ) O(n^{1+\epsilon}) O ( n 1 + ϵ ) gates and no ancilla qubits. For the special case m = 0 m=0 m = 0 , the circuit has O ( n log n ) O(n\log n) O ( n log n ) depth.
Proof sketch. We outline the proof of this lemma, which is left for Appendix § 9 . We give a recursive definition for Q F T 0 , n ( ϕ ) \mathbf{QFT}_{0,n}(\phi) QFT 0 , n ( ϕ ) , resembling the recursive definition for the standard QFT on Z 2 n \mathbb{Z}_{2^n} Z 2 n found in [31 ] . Using Lemma 2.2 , we prove the claim for m = 0 m=0 m = 0 . We then consider the control register and give a similar recursive definition for Q F T m , n ( ϕ , 1 ) \mathbf{QFT}_{m,n}(\phi,1) QFT m , n ( ϕ , 1 ) . Using this definition and Lemma 2.3 , we prove the claim for m = O ( n ) m=O(n) m = O ( n ) .
Our discovery that a controlled and parametrised version of the QFT can be made fast, without requiring any ancilla qubits, is fundamental, directly enabling in-place multiplication algorithms that are fast and ancilla-free. We note that, in [20 ] , the standard QFT on Z 2 n \mathbb{Z}_{2^n} Z 2 n , Q F T 0 , n ( 1 ) \mathrm{QFT}_{0,n}(1) QFT 0 , n ( 1 ) in our notation, was shown to require O ( n 1 + ϵ ) O(n^{1+\epsilon}) O ( n 1 + ϵ ) gates and no ancilla qubits, for any ϵ > 0 \epsilon> 0 ϵ > 0 , using the recursive definition of the QFT from [31 ] .
Now that we have introduced these two operations, P t , m , n ( ϕ , α ) P_{t,m,n}(\phi,\alpha) P t , m , n ( ϕ , α ) and Q F T m , n ( ϕ , α ) \mathrm{QFT}_{m,n}(\phi,\alpha) QFT m , n ( ϕ , α ) , which are of critical importance to the algorithms in this paper, we proceed to the main theorem of this work, from which many of our results will follow.
Main Theorem
Theorem 2.5 (Main Theorem). Let m > 0 m > 0 m > 0 , s = O ( m ) s = O(m) s = O ( m ) and t = O ( m ) t = O(m) t = O ( m ) . Let w ∈ Z 2 s × w \in\mathbb{Z}_{2^s}^{\times} w ∈ Z 2 s × , x ∈ Z 2 t x \in\mathbb{Z}_{2^t} x ∈ Z 2 t , y ∈ Z 2 m y \in\mathbb{Z}_{2^m} y ∈ Z 2 m , b , c ∈ Z ∖ 2 Z b,c \in\mathbb{Z} \setminus2\mathbb{Z} b , c ∈ Z ∖ 2 Z , and α , β ∈ { 0 , 1 } \alpha,\beta\in\{0,1\} α , β ∈ { 0 , 1 } . For any ϵ > 0 \epsilon> 0 ϵ > 0 , there exists a quantum circuit for performing the operation
∣ w ⟩ s ∣ x ⟩ t ∣ y ⟩ m ⟼ ∣ w ⟩ s ∣ 2 m b w α x + c w β y m o d 2 t + m ⟩ t + m (6) \lvert w\rangle_s\lvert x\rangle_t\lvert y\rangle_m \longmapsto\lvert w\rangle_s\lvert2^m b w^\alpha x + c w^\beta y \bmod2^{t+m}\rangle_{t+m} \tag*{(6)} ∣ w ⟩ s ∣ x ⟩ t ∣ y ⟩ m ⟼ ∣ w ⟩ s ∣ 2 m b w α x + c w β y mod 2 t + m ⟩ t + m (6) that uses O ( m 1 + ϵ ) O(m^{1+\epsilon}) O ( m 1 + ϵ ) gates and no ancilla qubits. For the special case s = 0 s = 0 s = 0 , the circuit has O ( m log m ) O(m\log m) O ( m log m ) depth. In this case, we adopt the convention w α = w β = 1 w^\alpha= w^\beta= 1 w α = w β = 1 .
Proof. We provide an algorithm that proves this claim. On input ∣ w ⟩ s ∣ x ⟩ t ∣ y ⟩ m \lvert w\rangle_s\lvert x\rangle_t\lvert y\rangle_m ∣ w ⟩ s ∣ x ⟩ t ∣ y ⟩ m apply the following operations.
⟶ Q F T ‾ s , t ( b , α ) ⊗ I m ∣ w ⟩ s 1 2 t ∑ v ∈ Z 2 t e 2 π i b x v w α / 2 t ∣ v ‾ ⟩ t ∣ y ⟩ m (7) \mathop{\longrightarrow}^{\overline{\mathrm{QFT}}_{s,t}(b,\alpha)\otimes I_m}
\lvert w\rangle_s \frac{1}{\sqrt{2^t}}\sum_{v\in\mathbb{Z}_{2^t}}e^{2\pi i b x v w^\alpha/2^t}\lvert\overline{v}\rangle_t\lvert y\rangle_m \tag*{(7)} ⟶ QFT s , t ( b , α ) ⊗ I m ∣ w ⟩ s 2 t 1 v ∈ Z 2 t ∑ e 2 π ib xv w α / 2 t ∣ v ⟩ t ∣ y ⟩ m (7)
⟶ P ‾ s , t , m ( c / 2 t + m , β ) ∣ w ⟩ s 1 2 t ∑ v ∈ Z 2 t e 2 π i ( b x v w α / 2 t + c y w β / 2 t + m ) ∣ v ‾ ⟩ t ∣ y ⟩ m (8) \mathop{\longrightarrow}^{\overline{P}_{s,t,m}(c/2^{t+m},\beta)}
\lvert w\rangle_s \frac{1}{\sqrt{2^t}}\sum_{v\in\mathbb{Z}_{2^t}}e^{2\pi i(bxvw^\alpha/2^t+cyw^\beta/2^{t+m})}\lvert\overline{v}\rangle_t\lvert y\rangle_m \tag*{(8)} ⟶ P s , t , m ( c / 2 t + m , β ) ∣ w ⟩ s 2 t 1 v ∈ Z 2 t ∑ e 2 π i ( b xv w α / 2 t + cy w β / 2 t + m ) ∣ v ⟩ t ∣ y ⟩ m (8)
⟶ I t ⊗ Q F T s , m ( c , β ) ∣ w ⟩ s 1 2 t + m ∑ v ∈ Z 2 t e 2 π i ( b x v w α / 2 t + c y w β / 2 t + m ) ∣ v ‾ ⟩ t ∑ z ∈ Z 2 m e 2 π i ( c y z w β / 2 m ) ∣ z ‾ ⟩ m (9) \mathop{\longrightarrow}^{I_t\otimes\mathrm{QFT}_{s,m}(c,\beta)}
\lvert w\rangle_s \frac{1}{\sqrt{2^{t+m}}}\sum_{v\in\mathbb{Z}_{2^t}}e^{2\pi i(bxvw^\alpha/2^t+cyw^\beta/2^{t+m})}\lvert\overline{v}\rangle_t\sum_{z\in\mathbb{Z}_{2^m}}e^{2\pi i(cyzw^\beta/2^m)}\lvert\overline{z}\rangle_m \tag*{(9)} ⟶ I t ⊗ QFT s , m ( c , β ) ∣ w ⟩ s 2 t + m 1 v ∈ Z 2 t ∑ e 2 π i ( b xv w α / 2 t + cy w β / 2 t + m ) ∣ v ⟩ t z ∈ Z 2 m ∑ e 2 π i ( cy z w β / 2 m ) ∣ z ⟩ m (9)
Note that ∣ v ‾ ⟩ t ∣ z ‾ ⟩ m = ∣ 2 t z + v ⟩ t + m \lvert\overline{v}\rangle_t\lvert\overline{z}\rangle_m=\lvert2^t z+v\rangle_{t+m} ∣ v ⟩ t ∣ z ⟩ m = ∣ 2 t z + v ⟩ t + m and
e 2 π i ( b x v w α / 2 t + c y w β / 2 t + m + c y z w β / 2 m ) = e 2 π i ( 2 m b x v w α + 2 t m b x z w α + c y w β + 2 t c y z w β ) / 2 t + m = e 2 π i ( 2 t z + v ) ( 2 m b x w α + c y w β ) / 2 t + m . (10) \begin{aligned}
e^{2\pi i(bxvw^\alpha/2^t+cyw^\beta/2^{t+m}+cyzw^\beta/2^m)}
=e^{2\pi i(2^m b x v w^\alpha+2^t m b x z w^\alpha+c y w^\beta+2^t c y z w^\beta)/2^{t+m}} \\
&=e^{2\pi i(2^t z+v)(2^m b x w^\alpha+c y w^\beta)/2^{t+m}}. \tag*{(10)}
\end{aligned} e 2 π i ( b xv w α / 2 t + cy w β / 2 t + m + cy z w β / 2 m ) = e 2 π i ( 2 m b xv w α + 2 t mb x z w α + cy w β + 2 t cy z w β ) / 2 t + m = e 2 π i ( 2 t z + v ) ( 2 m b x w α + cy w β ) / 2 t + m . (10)
Therefore, we can rewrite Equation (9) as
= ∣ w ⟩ s 1 2 t + m ∑ v ∈ Z 2 t ∑ z ∈ Z 2 m e 2 π i ( 2 t z + v ) ( 2 m b x w α + c y w β ) / 2 t + m ∣ 2 t z + v ⟩ t + m . (11) =\lvert w\rangle_s\frac{1}{\sqrt{2^{t+m}}}\sum_{v\in\mathbb{Z}_{2^t}}\sum_{z\in\mathbb{Z}_{2^m}}e^{2\pi i(2^t z+v)(2^m b x w^\alpha+c y w^\beta)/2^{t+m}}\lvert2^t z+v\rangle_{t+m}. \tag*{(11)} = ∣ w ⟩ s 2 t + m 1 v ∈ Z 2 t ∑ z ∈ Z 2 m ∑ e 2 π i ( 2 t z + v ) ( 2 m b x w α + cy w β ) / 2 t + m ∣ 2 t z + v ⟩ t + m . (11)
Since ( z , v ) ↦ 2 t z + v (z,v)\mapsto2^t z+v ( z , v ) ↦ 2 t z + v describes a one-to-one correspondence between Z 2 m × Z 2 t \mathbb{Z}_{2^m}\times\mathbb{Z}_{2^t} Z 2 m × Z 2 t and Z 2 t + m \mathbb{Z}_{2^{t+m}} Z 2 t + m , this equals
= ∣ w ⟩ s 1 2 t + m ∑ q ∈ Z 2 t + m e 2 π i q ( 2 m b x w α + c y w β ) / 2 t + m ∣ q ‾ ⟩ t + m . (12) =\lvert w\rangle_s\frac{1}{\sqrt{2^{t+m}}}\sum_{q\in\mathbb{Z}_{2^{t+m}}}e^{2\pi i q(2^m b x w^\alpha+c y w^\beta)/2^{t+m}}\lvert\overline{q}\rangle_{t+m}. \tag*{(12)} = ∣ w ⟩ s 2 t + m 1 q ∈ Z 2 t + m ∑ e 2 π i q ( 2 m b x w α + cy w β ) / 2 t + m ∣ q ⟩ t + m . (12)
Then finally apply,
⟶ I s ⊗ Q F T 0 , t + m ( 1 ) − 1 ∣ w ⟩ s ∣ 2 m b w α x + c w β y m o d 2 t + m ⟩ t + m . (13) \mathop{\longrightarrow}^{I_s\otimes\mathrm{QFT}_{0,t+m}(1)^{-1}}
\lvert w\rangle_s\lvert2^m b w^\alpha x+c w^\beta y\bmod2^{t+m}\rangle_{t+m}. \tag*{(13)} ⟶ I s ⊗ QFT 0 , t + m ( 1 ) − 1 ∣ w ⟩ s ∣ 2 m b w α x + c w β y mod 2 t + m ⟩ t + m . (13)
By Lemmas 2.3 and 2.4 , this algorithm can be performed using O ( m 1 + ϵ ) O(m^{1+\epsilon}) O ( m 1 + ϵ ) gates and no ancilla qubits, for any ϵ > 0 \epsilon> 0 ϵ > 0 . For the special case s = 0 s=0 s = 0 , by Lemmas 2.2 and 2.4 , the circuit has depth O ( m log m ) O(m\log m) O ( m log m ) .
Figure 1. Illustration of the algorithm in Theorem 2.5 . Dashed brown boxes refer to classical input parameters. Solid blue boxes refer to optional quantum control.
Remark 2.6. In Theorem E.1 of Appendix § 12 , we extend the domain of the operation in Theorem 2.5 to arbitrary quantum multipliers w ∈ Z 2 s w \in\mathbb{Z}_{2^s} w ∈ Z 2 s . This extension turns out to be useful in the proof of Lemma 5.3 in Appendix § 11 and may also be of independent interest.
We show in the next section that Theorem 2.5 immediately implies the existence of fast, in-place, and ancilla-free quantum algorithms for integer multiplication between a quantum register and a fixed classical constant.
Multiplication with a classical integer
Theorem 3.1 (Modular multiplication by a classical integer mod 2 n 2^n 2 n ). Let z ∈ Z 2 n z \in\mathbb{Z}_{2^n} z ∈ Z 2 n and a ∈ Z 2 n ∗ a \in\mathbb{Z}_{2^n}^{*} a ∈ Z 2 n ∗ . For any ϵ > 0 \epsilon> 0 ϵ > 0 , there exists a quantum circuit for performing the operation
∣ z ⟩ n ↦ ∣ a z mod 2 n ⟩ n (14) |z\rangle_n \mapsto|az \mathbin{\operatorname{mod}} 2^n\rangle_n \tag*{(14)} ∣ z ⟩ n ↦ ∣ a z mod 2 n ⟩ n (14) that uses O ( n 1 + ϵ ) O(n^{1+\epsilon}) O ( n 1 + ϵ ) gates, has depth O ( n log n ) O(n \log n) O ( n log n ) , and requires no ancilla qubits.
Proof. Let s = 0 s = 0 s = 0 (this implies α \alpha α and β \beta β are undefined and we use the convention w α = w β = 1 w^\alpha= w^\beta= 1 w α = w β = 1 ). Let t = ⌈ n / 2 ⌉ t = \lceil n/2 \rceil t = ⌈ n /2 ⌉ , m = ⌊ n / 2 ⌋ m = \lfloor n/2 \rfloor m = ⌊ n /2 ⌋ , $2^m x + y = z$, b = c = a b = c = a b = c = a and apply Theorem 2.5 . □
Before considering multiplication by an arbitrary classical integer, we introduce two new operations. For a ∈ Z 2 m a \in\mathbb{Z}_{2^m} a ∈ Z 2 m , let
tz ( a ) = log ( gcd ( a , 2 m ) ) , (15) \operatorname{tz}(a) = \log(\gcd(a,2^m)), \tag*{(15)} tz ( a ) = log ( g cd( a , 2 m )) , (15)
denote the number of trailing zeroes in the binary representation of a a a . Note that tz ( 0 ) = m \operatorname{tz}(0) = m tz ( 0 ) = m . For a classically known integer 0 ≤ j < n 0 \le j < n 0 ≤ j < n , let CS n ( j ) \operatorname{CS}_n(j) CS n ( j ) denote the cyclic shift
CS n ( j ) : ∣ w ⟩ n − j ∣ x ⟩ j ↦ ∣ x ⟩ j ∣ w ⟩ n − j . (16) \operatorname{CS}_n(j): |w\rangle_{n-j}|x\rangle_j \mapsto|x\rangle_j|w\rangle_{n-j}. \tag*{(16)} CS n ( j ) : ∣ w ⟩ n − j ∣ x ⟩ j ↦ ∣ x ⟩ j ∣ w ⟩ n − j . (16)
This moves the j j j least significant qubits to the most significant position.
Remark 3.2 (Implementation of classical cyclic shifts). The operation CS n ( j ) \operatorname{CS}_n(j) CS n ( j ) permutes n n n qubits. Any permutation of n n n positions can be written as a product of at most n − 1 n - 1 n − 1 transpositions [32 § 26.13] . Therefore, this operation can be implemented using at most n − 1 n - 1 n − 1 SWAP gates, without ancilla qubits. Since j j j is known classically, we can also perform this operation without applying any gates by simply relabelling qubits, provided subsequent operations use this new labelling.
Theorem 3.3 (Multiplication by a classical integer). Let z ∈ Z 2 n z \in\mathbb{Z}_{2n} z ∈ Z 2 n , m = O ( n ) m = O(n) m = O ( n ) and a ∈ Z 2 m ∖ { 0 } a \in\mathbb{Z}_{2m} \setminus\{0\} a ∈ Z 2 m ∖ { 0 } . For any ϵ > 0 \epsilon> 0 ϵ > 0 , there exists a quantum circuit for performing the operation
∣ 0 ⟩ m ∣ z ⟩ n ⟼ ∣ a z ⟩ m + n (17) \lvert0\rangle_m \lvert z\rangle_n \longmapsto\lvert az\rangle_{m+n} \tag*{(17)} ∣ 0 ⟩ m ∣ z ⟩ n ⟼ ∣ a z ⟩ m + n (17) that uses O ( n 1 + ϵ ) O(n^{1+\epsilon}) O ( n 1 + ϵ ) gates, has depth O ( n log n ) O(n \log n) O ( n log n ) , and requires no ancilla qubits.
Proof. Let j = tz ( a ) j = \operatorname{tz}(a) j = tz ( a ) , which we compute classically, and let a ′ = a / 2 j a' = a/2^j a ′ = a / 2 j . Since a ′ a' a ′ is odd, we can apply Theorem 2.5 to ∣ 0 ⟩ m − j ∣ z ⟩ n \lvert0\rangle_{m-j}\lvert z\rangle_n ∣ 0 ⟩ m − j ∣ z ⟩ n , setting the parameters of the theorem as follows: s = 0 s = 0 s = 0 , t = m − j t = m-j t = m − j , m = n m = n m = n , x = 0 x = 0 x = 0 , y = z y = z y = z , b = 1 b = 1 b = 1 , and c = a ′ c = a' c = a ′ . This prepares ∣ a ′ z ⟩ m + n − j \lvert a'z\rangle_{m+n-j} ∣ a ′ z ⟩ m + n − j . We now apply CS m + n ( j ) − 1 \operatorname{CS}_{m+n}(j)^{-1} CS m + n ( j ) − 1 to obtain
CS m + n ( j ) − 1 ⟶ ∣ a ′ z ⟩ m + n − j ∣ 0 ⟩ j = ∣ a z ⟩ m + n . (18) \operatorname{CS}_{m+n}(j)^{-1} \longrightarrow\lvert a'z\rangle_{m+n-j}\lvert0\rangle_j = \lvert az\rangle_{m+n}. \tag*{(18)} CS m + n ( j ) − 1 ⟶ ∣ a ′ z ⟩ m + n − j ∣ 0 ⟩ j = ∣ a z ⟩ m + n . (18)
Theorem 3.6 (Multiplication by a classical integer on mostly dirty qubits). Let z ∈ Z 2 n z \in\mathbb{Z}_{2n} z ∈ Z 2 n , m = O ( n ) m = O(n) m = O ( n ) and a ∈ Z 2 m ∖ { 0 } a \in\mathbb{Z}_{2m} \setminus\{0\} a ∈ Z 2 m ∖ { 0 } . Let j = tz ( a ) j = \operatorname{tz}(a) j = tz ( a ) . Let x ∈ Z 2 j x \in\mathbb{Z}_{2^j} x ∈ Z 2 j and y ∈ Z 2 m − j y \in\mathbb{Z}_{2^{m-j}} y ∈ Z 2 m − j . For any ϵ > 0 \epsilon> 0 ϵ > 0 , there exists a quantum circuit for performing the operation
∣ 0 ⟩ 1 ∣ x ⟩ j ∣ y ⟩ m − j ∣ z ⟩ n ⟼ ∣ 2 n y + x + a z ⟩ m + n + 1 (19) \lvert0\rangle_1 \lvert x\rangle_j \lvert y\rangle_{m-j} \lvert z\rangle_n \longmapsto\lvert2^n y + x + az\rangle_{m+n+1} \tag*{(19)} ∣ 0 ⟩ 1 ∣ x ⟩ j ∣ y ⟩ m − j ∣ z ⟩ n ⟼ ∣ 2 n y + x + a z ⟩ m + n + 1 (19) that uses O ( n 1 + ϵ ) O(n^{1+\epsilon}) O ( n 1 + ϵ ) gates, has depth O ( n log n ) O(n \log n) O ( n log n ) , and requires no ancilla qubits.
Proof. As in Theorem 3.3, compute j = tz ( a ) j = \operatorname{tz}(a) j = tz ( a ) classically and let a ′ = a / 2 j a' = a/2^j a ′ = a / 2 j . Both 2 n y 2^n y 2 n y and a ′ z a'z a ′ z can be represented using m + n − j m+n-j m + n − j bits. However, their sum may require one extra bit. We therefore move the clean qubit ∣ 0 ⟩ 1 \lvert0\rangle_1 ∣ 0 ⟩ 1 to be between the x x x - and y y y -registers, so that the multiplication we are trying to perform acts on ∣ 0 ⟩ 1 ∣ y ⟩ m − j ∣ z ⟩ n \lvert0\rangle_1\lvert y\rangle_{m-j}\lvert z\rangle_n ∣ 0 ⟩ 1 ∣ y ⟩ m − j ∣ z ⟩ n , and leaves ∣ x ⟩ j \lvert x\rangle_j ∣ x ⟩ j unchanged. To do this, apply CS m ( j ) − 1 \operatorname{CS}_{m}(j)^{-1} CS m ( j ) − 1 to the x x x - and y y y -registers, followed by CS m + 1 ( j ) \operatorname{CS}_{m+1}(j) CS m + 1 ( j ) to the first m + 1 m+1 m + 1 qubits:
CS m ( j ) − 1 ⟶ ∣ 0 ⟩ 1 ∣ y ⟩ m − j ∣ x ⟩ j ∣ z ⟩ n , (20) \operatorname{CS}_{m}(j)^{-1} \longrightarrow\lvert0\rangle_1\lvert y\rangle_{m-j}\lvert x\rangle_j\lvert z\rangle_n, \tag*{(20)} CS m ( j ) − 1 ⟶ ∣ 0 ⟩ 1 ∣ y ⟩ m − j ∣ x ⟩ j ∣ z ⟩ n , (20)
CS m + 1 ( j ) ⟶ ∣ x ⟩ j ∣ 0 ⟩ 1 ∣ y ⟩ m − j ∣ z ⟩ n . (21) \operatorname{CS}_{m+1}(j) \longrightarrow\lvert x\rangle_j\lvert0\rangle_1\lvert y\rangle_{m-j}\lvert z\rangle_n. \tag*{(21)} CS m + 1 ( j ) ⟶ ∣ x ⟩ j ∣ 0 ⟩ 1 ∣ y ⟩ m − j ∣ z ⟩ n . (21)
Since j j j is known classically, the same operation can be performed by C S j + 1 ( j ) \mathrm{CS}_{j+1}(j) CS j + 1 ( j ) on the leading clean qubit and the x x x -register. We retain the two shifts here to make the parallel with the quantum-integer construction in Theorem 4.8 explicit.
Let s = 0 s=0 s = 0 , t = m − j + 1 t=m-j+1 t = m − j + 1 , m = n m=n m = n , x = y x=y x = y , y = z y=z y = z , b = 1 b=1 b = 1 , c = a ′ c=a' c = a ′ and apply the circuit from Theorem 2.5 to ∣ 0 ⟩ 1 ∣ y ⟩ m − j ∣ z ⟩ n \lvert0\rangle_1\lvert y\rangle_{m-j}\lvert z\rangle_n ∣ 0 ⟩ 1 ∣ y ⟩ m − j ∣ z ⟩ n . Since 2 n y + a ′ z < 2 m + n − j + 1 2^n y+a'z<2^{m+n-j+1} 2 n y + a ′ z < 2 m + n − j + 1 , this gives
∣ x ⟩ j ∣ 2 n y + a ′ z ⟩ m + n − j + 1 (22) \lvert x\rangle_j\lvert2^n y+a'z\rangle_{m+n-j+1} \tag*{(22)} ∣ x ⟩ j ∣ 2 n y + a ′ z ⟩ m + n − j + 1 (22)
We now apply C S m + n + 1 ( j ) − 1 \mathrm{CS}_{m+n+1}(j)^{-1} CS m + n + 1 ( j ) − 1 to obtain
∣ 2 n y + a ′ z ⟩ m + n − j + 1 ∣ x ⟩ j = ∣ 2 n + j y + x + a z ⟩ m + n + 1 (23) \lvert2^n y+a'z\rangle_{m+n-j+1}\lvert x\rangle_j=\lvert2^{n+j}y+x+az\rangle_{m+n+1} \tag*{(23)} ∣ 2 n y + a ′ z ⟩ m + n − j + 1 ∣ x ⟩ j = ∣ 2 n + j y + x + a z ⟩ m + n + 1 (23)
It should be clear that Theorem 3.3 is the special case of Theorem 3.6 where x = y = 0 x=y=0 x = y = 0 and the overflow qubit ∣ 0 ⟩ 1 \lvert0\rangle_1 ∣ 0 ⟩ 1 is omitted.
We show in the next section that our main theorem, Theorem 2.5 , yields fast, in-place algorithms for multiplying quantum registers. All of these algorithms require at most a logarithmic number of clean ancillas, and some are completely ancilla-free.
Multiplication with a quantum integer
Subroutines
The preceding algorithms compute the trailing-zero count of an integer classically and use it to determine the required cyclic shifts. When the multiplier is a quantum integer, we must perform these operations coherently. We therefore introduce reversible evaluation of the trailing-zero function and cyclic shifts controlled by a quantum register. Quantum circuits for trailing-zero counting and controlled shifts have been studied previously [33 , 34 ] . We provide alternate constructions for these operations below.
Let x ∈ Z 2 m x\in\mathbb{Z}_{2^m} x ∈ Z 2 m and n ≥ ⌈ log ( m + 1 ) ⌉ n\ge\lceil\log(m+1)\rceil n ≥ ⌈ log ( m + 1 )⌉ . Then,
T Z m , n : ∣ x ⟩ m ∣ 0 ⟩ n ⟼ ∣ x ⟩ m ∣ tz ( x ) ⟩ n (24) \mathrm{TZ}_{m,n}:\lvert x\rangle_m\lvert0\rangle_n\longmapsto\lvert x\rangle_m\lvert\operatorname{tz}(x)\rangle_n \tag*{(24)} TZ m , n : ∣ x ⟩ m ∣ 0 ⟩ n ⟼ ∣ x ⟩ m ∣ tz ( x ) ⟩ n (24)
This computes the trailing-zero count while preserving the input register. As usual, we use subscripts to indicate the sizes of the registers that we are applying the gate to. The first subscript always refers to the control register and the second to the target. For this operation to be well-defined, n ≥ ⌈ log ( m + 1 ) ⌉ n\ge\lceil\log(m+1)\rceil n ≥ ⌈ log ( m + 1 )⌉ .
Lemma 4.1 (Trailing zero count). Let m ≥ 1 m\ge1 m ≥ 1 . There exists a quantum circuit for T Z m , ⌈ log ( m + 1 ) ⌉ \mathrm{TZ}_{m,\lceil\log(m+1)\rceil} TZ m , ⌈ l o g ( m + 1 )⌉ that uses O ( m ) O(m) O ( m ) gates and no ancillas.
Proof. The proof of this lemma is left for Appendix § 10 .
Next, we consider the operation C S m , n \mathrm{CS}_{m,n} CS m , n acting on m + n m+n m + n qubits, defined as follows. Let z ∈ Z 2 n z\in\mathbb{Z}_{2^n} z ∈ Z 2 n and let y ∈ Z 2 m y\in\mathbb{Z}_{2^m} y ∈ Z 2 m such that 0 ≤ y < n 0\le y<n 0 ≤ y < n . Consider x ∈ Z 2 y x\in\mathbb{Z}_{2^y} x ∈ Z 2 y and w ∈ Z 2 n − y w\in\mathbb{Z}_{2^{n-y}} w ∈ Z 2 n − y such that z = 2 y w + x z=2^y w+x z = 2 y w + x . Then,
C S m , n : ∣ y ⟩ m ∣ z ⟩ n ⟼ ∣ y ⟩ m ( C S n ( y ) ∣ z ⟩ n ) = ∣ y ⟩ m ∣ x ⟩ y ∣ w ⟩ n − y . (25) \begin{aligned}
\mathrm{CS}_{m,n}:\lvert y\rangle_m\lvert z\rangle_n&\longmapsto\lvert y\rangle_m\left(\mathrm{CS}_n(y)\lvert z\rangle_n\right)\\
&=\lvert y\rangle_m\lvert x\rangle_y\lvert w\rangle_{n-y}. \tag*{(25)}
\end{aligned} CS m , n : ∣ y ⟩ m ∣ z ⟩ n ⟼ ∣ y ⟩ m ( CS n ( y ) ∣ z ⟩ n ) = ∣ y ⟩ m ∣ x ⟩ y ∣ w ⟩ n − y . (25)
Controlled on the y y y -register, C S m , n \mathrm{CS}_{m,n} CS m , n cyclically shifts the y y y least significant qubits of the z z z -register, making them the most significant qubits. As with T Z m , n \mathrm{TZ}_{m,n} TZ m , n , the first subscript of CS m , n \operatorname{CS}_{m,n} CS m , n always refers to the control register and the second to the target. Observe that the action of CS ⌈ log m ⌉ , m ∘ ( I 1 ⊗ TZ m − 1 , ⌈ log m ⌉ ) \operatorname{CS}_{\lceil\log m\rceil,m} \circ(I_1 \otimes\operatorname{TZ}_{m-1,\lceil\log m\rceil}) CS ⌈ l o g m ⌉ , m ∘ ( I 1 ⊗ TZ m − 1 , ⌈ l o g m ⌉ ) is well-defined on ∣ x ⟩ m ∣ 0 ⟩ ⌈ log m ⌉ |x\rangle_m |0\rangle_{\lceil\log m\rceil} ∣ x ⟩ m ∣0 ⟩ ⌈ l o g m ⌉ for x ∈ Z 2 m ∖ { 0 } x \in\mathbb{Z}_{2^m} \setminus\{0\} x ∈ Z 2 m ∖ { 0 } . The composition of these operations prepares ∣ 0 ⟩ tz ( x ) ∣ x / 2 tz ( x ) ⟩ m − tz ( x ) ∣ tz ( x ) ⟩ ⌈ log m ⌉ |0\rangle_{\operatorname{tz}(x)} |x/2^{\operatorname{tz}(x)}\rangle_{m-\operatorname{tz}(x)} |\operatorname{tz}(x)\rangle_{\lceil\log m\rceil} ∣0 ⟩ tz ( x ) ∣ x / 2 tz ( x ) ⟩ m − tz ( x ) ∣ tz ( x ) ⟩ ⌈ l o g m ⌉ since tz ( x ) = tz ( x m − 2 ⋯ x 0 ) \operatorname{tz}(x) = \operatorname{tz}(x_{m-2}\cdots x_0) tz ( x ) = tz ( x m − 2 ⋯ x 0 ) .
Lemma 4.2 (Cyclic shift). Let n ≥ 2 n \ge2 n ≥ 2 and 1 ≤ m ≤ ⌈ log n ⌉ 1 \le m \le\lceil\log n\rceil 1 ≤ m ≤ ⌈ log n ⌉ . There exists a quantum circuit for CS m , n \operatorname{CS}_{m,n} CS m , n that uses O ( n log n ) O(n\log n) O ( n log n ) gates and no ancillas.
Proof. Let y = ∑ i = 0 m − 1 2 i y i y = \sum_{i=0}^{m-1} 2^i y_i y = ∑ i = 0 m − 1 2 i y i . We iterate over the bits of y y y and apply CS n ( 2 i ) \operatorname{CS}_n(2^i) CS n ( 2 i ) to the target register, controlled on y i y_i y i , giving the required total shift by y y y . By Remark 3.2, each iteration uses at most n − 1 n-1 n − 1 controlled SWAP gates. Therefore, we use at most m ( n − 1 ) ≤ n m m(n-1) \le nm m ( n − 1 ) ≤ nm controlled SWAPs in total. Each controlled SWAP uses one Toffoli and two CNOTs, giving O ( n m ) = O ( n log n ) O(nm) = O(n\log n) O ( nm ) = O ( n log n ) gates.
With these operations and our main theorem in hand, we prove a number of results.
Main Results
Theorem 4.3 (Modular multiplication by a quantum integer mod 2 n 2^n 2 n ). Let z ∈ Z 2 n z \in\mathbb{Z}_{2^n} z ∈ Z 2 n . Let m = O ( n ) m = O(n) m = O ( n ) and let x ∈ Z 2 m x \in\mathbb{Z}_{2^m} x ∈ Z 2 m . For any ϵ > 0 \epsilon> 0 ϵ > 0 , there exists a quantum circuit for performing the operation
∣ x ⟩ m ∣ z ⟩ n ⟼ ∣ x ⟩ m ∣ x z m o d 2 n ⟩ n (26) |x\rangle_m |z\rangle_n \longmapsto|x\rangle_m |xz \bmod2^n\rangle_n \tag*{(26)} ∣ x ⟩ m ∣ z ⟩ n ⟼ ∣ x ⟩ m ∣ x z mod 2 n ⟩ n (26) that uses O ( n 1 + ϵ ) O(n^{1+\epsilon}) O ( n 1 + ϵ ) gates and no ancilla qubits.
Proof. Let s = m s=m s = m , t = ⌈ n / 2 ⌉ t=\lceil n/2\rceil t = ⌈ n /2 ⌉ , m = ⌊ n / 2 ⌋ m=\lfloor n/2\rfloor m = ⌊ n /2 ⌋ , α = β = 1 \alpha=\beta=1 α = β = 1 , w = x w=x w = x , 2 m x + y = z 2^m x+y=z 2 m x + y = z , b = c = 1 b=c=1 b = c = 1 and apply Theorem 2.5 .
Remark 4.4. It should be clear how to achieve ∣ x ⟩ m ∣ z ⟩ n ⟼ ∣ x ⟩ m ∣ a x z m o d 2 n ⟩ n |x\rangle_m |z\rangle_n \longmapsto|x\rangle_m |axz \bmod2^n\rangle_n ∣ x ⟩ m ∣ z ⟩ n ⟼ ∣ x ⟩ m ∣ a x z mod 2 n ⟩ n for some a ∈ Z ∖ 2 Z a \in\mathbb{Z}\setminus2\mathbb{Z} a ∈ Z ∖ 2 Z . Simply let b = c = a b=c=a b = c = a in the algorithm of Theorem 2.5 instead. This shows that performing classical and quantum modular multiplication is possible within a single construction.
Theorem 4.5 (Multiplication by a quantum integer). Let z ∈ Z 2 n z \in\mathbb{Z}_{2^n} z ∈ Z 2 n , m = O ( n ) m=O(n) m = O ( n ) and x ∈ Z 2 m ∖ { 0 } x \in\mathbb{Z}_{2^m}\setminus\{0\} x ∈ Z 2 m ∖ { 0 } . For any ϵ > 0 \epsilon>0 ϵ > 0 , there exists a quantum circuit for performing the operation
∣ x ⟩ m ∣ 0 ⟩ m ∣ z ⟩ n ⟼ ∣ x ⟩ m ∣ x z ⟩ m + n (27) |x\rangle_m |0\rangle_m |z\rangle_n \longmapsto|x\rangle_m |xz\rangle_{m+n} \tag*{(27)} ∣ x ⟩ m ∣0 ⟩ m ∣ z ⟩ n ⟼ ∣ x ⟩ m ∣ x z ⟩ m + n (27) that uses O ( n 1 + ϵ ) O(n^{1+\epsilon}) O ( n 1 + ϵ ) gates and requires ⌈ log m ⌉ \lceil\log m\rceil ⌈ log m ⌉ ancilla qubits. For the special case x ∈ Z 2 m × x \in\mathbb{Z}_{2^m}^{\times} x ∈ Z 2 m × no ancilla qubits are required.
Proof. We provide an algorithm proving the claim. The main idea is the same as in Theorem 3.3 . We separate the odd factor x / 2 tz ( x ) x/2^{\operatorname{tz}(x)} x / 2 tz ( x ) , perform multiplication with this factor, and then rescale the result by 2 tz ( x ) 2^{\operatorname{tz}(x)} 2 tz ( x ) .
Since, x ∈ Z 2 m ∖ { 0 } x \in\mathbb{Z}_{2^m}\setminus\{0\} x ∈ Z 2 m ∖ { 0 } , 0 ≤ tz ( x ) ≤ m − 1 0 \le\operatorname{tz}(x) \le m-1 0 ≤ tz ( x ) ≤ m − 1 . Note also that tz ( x ) = tz ( x m − 2 ⋯ x 0 ) \operatorname{tz}(x)=\operatorname{tz}(x_{m-2}\cdots x_0) tz ( x ) = tz ( x m − 2 ⋯ x 0 ) . On input ∣ x ⟩ m ∣ 0 ⟩ ⌈ log m ⌉ ∣ 0 ⟩ m ∣ z ⟩ n |x\rangle_m |0\rangle_{\lceil\log m\rceil}|0\rangle_m |z\rangle_n ∣ x ⟩ m ∣0 ⟩ ⌈ l o g m ⌉ ∣0 ⟩ m ∣ z ⟩ n , apply the following operations
I 1 ⊗ TZ m − 1 , ⌈ log m ⌉ ⟶ ∣ x m − 1 / 1 ⟩ ∣ x m − 2 ⋯ x 0 ⟩ m − 1 ∣ tz ( x ) ⟩ ⌈ log m ⌉ ∣ 0 ⟩ m ∣ z ⟩ n (28) \begin{aligned}
&\operatorname{I}_1\otimes\operatorname{TZ}_{m-1,\lceil\log m\rceil} \\
&\qquad\longrightarrow|x_{m-1}/1\rangle|x_{m-2}\cdots x_0\rangle_{m-1}|\operatorname{tz}(x)\rangle_{\lceil\log m\rceil}|0\rangle_m|z\rangle_n \tag*{(28)}
\end{aligned} I 1 ⊗ TZ m − 1 , ⌈ l o g m ⌉ ⟶ ∣ x m − 1 /1 ⟩ ∣ x m − 2 ⋯ x 0 ⟩ m − 1 ∣ tz ( x ) ⟩ ⌈ l o g m ⌉ ∣0 ⟩ m ∣ z ⟩ n (28)
CS ⌈ log m ⌉ , m ⟶ ∣ x / 2 tz ( x ) ⟩ m ∣ tz ( x ) ⟩ ⌈ log m ⌉ ∣ 0 ⟩ m ∣ z ⟩ n (29) \operatorname{CS}_{\lceil\log m\rceil,m}\longrightarrow|x/2^{\operatorname{tz}(x)}\rangle_m|\operatorname{tz}(x)\rangle_{\lceil\log m\rceil}|0\rangle_m|z\rangle_n \tag*{(29)} CS ⌈ l o g m ⌉ , m ⟶ ∣ x / 2 tz ( x ) ⟩ m ∣ tz ( x ) ⟩ ⌈ l o g m ⌉ ∣0 ⟩ m ∣ z ⟩ n (29)
Let s = m , t = m , m = n , α = 0 , β = 1 , w = x / 2 tz ( x ) , x = 0 , y = z , b = c = 1 s=m,t=m,m=n,\alpha=0,\beta=1,w=x/2^{\operatorname{tz}(x)},x=0,y=z,b=c=1 s = m , t = m , m = n , α = 0 , β = 1 , w = x / 2 tz ( x ) , x = 0 , y = z , b = c = 1 and apply Theorem 2.5 to ∣ x / 2 tz ( x ) ⟩ m ∣ 0 ⟩ m ∣ z ⟩ n \lvert x/2^{\operatorname{tz}(x)}\rangle_m\lvert0\rangle_m\lvert z\rangle_n ∣ x / 2 tz ( x ) ⟩ m ∣ 0 ⟩ m ∣ z ⟩ n
∣ x / 2 tz ( x ) ⟩ m ∣ tz ( x ) ⟩ ⌈ log m ⌉ ∣ x z / 2 tz ( x ) ⟩ m + n (30) \lvert x/2^{\operatorname{tz}(x)}\rangle_m\lvert\operatorname{tz}(x)\rangle_{\lceil\log m\rceil}\lvert xz/2^{\operatorname{tz}(x)}\rangle_{m+n} \tag*{(30)} ∣ x / 2 tz ( x ) ⟩ m ∣ tz ( x ) ⟩ ⌈ l o g m ⌉ ∣ x z / 2 tz ( x ) ⟩ m + n (30)
∣ x / 2 tz ( x ) ⟩ m ∣ tz ( x ) ⟩ ⌈ log m ⌉ ∣ x z ⟩ m + n (31) \lvert x/2^{\operatorname{tz}(x)}\rangle_m\lvert\operatorname{tz}(x)\rangle_{\lceil\log m\rceil}\lvert xz\rangle_{m+n} \tag*{(31)} ∣ x / 2 tz ( x ) ⟩ m ∣ tz ( x ) ⟩ ⌈ l o g m ⌉ ∣ x z ⟩ m + n (31)
∣ x ⟩ m ∣ tz ( x ) ⟩ ⌈ log m ⌉ ∣ x z ⟩ m + n (32) \lvert x\rangle_m\lvert\operatorname{tz}(x)\rangle_{\lceil\log m\rceil}\lvert xz\rangle_{m+n} \tag*{(32)} ∣ x ⟩ m ∣ tz ( x ) ⟩ ⌈ l o g m ⌉ ∣ x z ⟩ m + n (32)
∣ x ⟩ m ∣ 0 ⟩ ⌈ log m ⌉ ∣ x z ⟩ m + n (33) \lvert x\rangle_m\lvert0\rangle_{\lceil\log m\rceil}\lvert xz\rangle_{m+n} \tag*{(33)} ∣ x ⟩ m ∣ 0 ⟩ ⌈ l o g m ⌉ ∣ x z ⟩ m + n (33)
Since m = O ( n ) m=O(n) m = O ( n ) , by Theorem 2.5 and Lemmas 4.1 and 4.2 this algorithm uses O ( n 1 + ϵ ) O(n^{1+\epsilon}) O ( n 1 + ϵ ) gates and ⌈ log m ⌉ \lceil\log m\rceil ⌈ log m ⌉ ancillas, for any ϵ > 0 \epsilon>0 ϵ > 0 . For the special case x ∈ Z 2 m × x\in\mathbb{Z}_{2^m}^{\times} x ∈ Z 2 m × the cyclic shifts and trailing zero computations are unnecessary, since tz ( x ) = 0 \operatorname{tz}(x)=0 tz ( x ) = 0 . Theorem 2.5 can be applied directly, requiring no ancilla qubits.
Remark 4.6. We can use a cheaper algorithm than the algorithm of Theorem 2.5 to implement the multiplication taking place in Equation (30) . Namely, we can replace the first parametrised QFT in the algorithm of Theorem 2.5 with a Hadamard transform, since QFT s , t ( b , α ) ∣ w ⟩ s ∣ 0 ⟩ t = ∣ w ⟩ s H ⊗ t ∣ 0 ⟩ t \operatorname{QFT}_{s,t}(b,\alpha)\lvert w\rangle_s\lvert0\rangle_t=\lvert w\rangle_sH^{\otimes t}\lvert0\rangle_t QFT s , t ( b , α ) ∣ w ⟩ s ∣ 0 ⟩ t = ∣ w ⟩ s H ⊗ t ∣ 0 ⟩ t . This change does not affect the asymptotic cost.
Remark 4.7. For a ∈ Z 2 t ∖ { 0 } a\in\mathbb{Z}_{2^t}\setminus\{0\} a ∈ Z 2 t ∖ { 0 } , t = O ( n ) t=O(n) t = O ( n ) , combining Theorems 3.3 and 4.5 shows that the transformation ∣ x ⟩ m ∣ 0 ⟩ t + m ∣ z ⟩ n ↦ ∣ x ⟩ m ∣ a x z ⟩ t + m + n \lvert x\rangle_m\lvert0\rangle_{t+m}\lvert z\rangle_n\mapsto\lvert x\rangle_m\lvert axz\rangle_{t+m+n} ∣ x ⟩ m ∣ 0 ⟩ t + m ∣ z ⟩ n ↦ ∣ x ⟩ m ∣ a x z ⟩ t + m + n can be performed using O ( n 1 + ϵ ) O(n^{1+\epsilon}) O ( n 1 + ϵ ) gates and ⌈ log m ⌉ \lceil\log m\rceil ⌈ log m ⌉ ancillas. However, it should be clear that a cheaper algorithm, albeit with the same asymptotic cost, is available. First, compute tz ( a ) \operatorname{tz}(a) tz ( a ) classically. Then, let a ′ = a / 2 tz ( a ) a'=a/2^{\operatorname{tz}(a)} a ′ = a / 2 tz ( a ) , and apply Theorem 4.5 to ∣ x ⟩ m ∣ 0 ⟩ t + m − tz ( a ) ∣ z ⟩ n \lvert x\rangle_m\lvert0\rangle_{t+m-\operatorname{tz}(a)}\lvert z\rangle_n ∣ x ⟩ m ∣ 0 ⟩ t + m − tz ( a ) ∣ z ⟩ n setting c = a ′ c=a' c = a ′ instead. This will prepare ∣ x ⟩ m ∣ 0 ⟩ tz ( a ) ∣ x a ′ z ⟩ t + m + n − tz ( a ) \lvert x\rangle_m\lvert0\rangle_{\operatorname{tz}(a)}\lvert xa'z\rangle_{t+m+n-\operatorname{tz}(a)} ∣ x ⟩ m ∣ 0 ⟩ tz ( a ) ∣ x a ′ z ⟩ t + m + n − tz ( a ) . Applying CS t + m + n − 1 ( tz ( a ) ) \operatorname{CS}^{-1}_{t+m+n}(\operatorname{tz}(a)) CS t + m + n − 1 ( tz ( a )) , which by Remark 3.2 requires no additional gates, prepares ∣ x ⟩ m ∣ x a z ⟩ t + m + n \lvert x\rangle_m\lvert xaz\rangle_{t+m+n} ∣ x ⟩ m ∣ x a z ⟩ t + m + n . This shows that classical and quantum multiplication can be implemented in a single construction.
Finally, we consider the scenario where we want to compute the product x z ∈ Z 2 m + n xz\in\mathbb{Z}_{2^{m+n}} x z ∈ Z 2 m + n and we must work on top of dirty qubits. This provides a primitive for settings where qubits are available but initialising a large output register with clean ancillas is undesirable. See Remark 3.5 for further motivation.
Theorem 4.8 (Multiplication by a quantum integer on mostly dirty qubits). Let z ∈ Z 2 n z\in\mathbb{Z}_{2^n} z ∈ Z 2 n , m = O ( n ) m=O(n) m = O ( n ) and x ∈ Z 2 m ∖ { 0 } x\in\mathbb{Z}_{2^m}\setminus\{0\} x ∈ Z 2 m ∖ { 0 } . For any ϵ > 0 \epsilon>0 ϵ > 0 , there exists a quantum circuit for performing the operation
∣ x ⟩ m ∣ 0 ⟩ 1 ∣ y ⟩ m − tz ( x ) ∣ z ⟩ n ↦ ∣ x ⟩ m ∣ 2 n + tz ( x ) y + w + x z ⟩ m + n + 1 (34) \lvert x\rangle_m\lvert0\rangle_1\lvert y\rangle_{m-\operatorname{tz}(x)}\lvert z\rangle_n\mapsto\lvert x\rangle_m\lvert2^{n+\operatorname{tz}(x)}y+w+xz\rangle_{m+n+1} \tag*{(34)} ∣ x ⟩ m ∣ 0 ⟩ 1 ∣ y ⟩ m − tz ( x ) ∣ z ⟩ n ↦ ∣ x ⟩ m ∣ 2 n + tz ( x ) y + w + x z ⟩ m + n + 1 (34) that uses O ( n 1 + ϵ ) O(n^{1+\epsilon}) O ( n 1 + ϵ ) gates and requires ⌈ log m ⌉ \lceil\log m\rceil ⌈ log m ⌉ ancilla qubits. For the special case x ∈ Z 2 m × x\in\mathbb{Z}_{2^m}^{\times} x ∈ Z 2 m × no ancilla qubits are required. The corresponding operation in this case is
∣ x ⟩ m ∣ 0 ⟩ 1 ∣ y ⟩ m ∣ z ⟩ n ↦ ∣ x ⟩ m ∣ 2 n y + x z ⟩ m + n + 1 . (35) \lvert x\rangle_m\lvert0\rangle_1\lvert y\rangle_m\lvert z\rangle_n\mapsto\lvert x\rangle_m\lvert2^ny+xz\rangle_{m+n+1}. \tag*{(35)} ∣ x ⟩ m ∣ 0 ⟩ 1 ∣ y ⟩ m ∣ z ⟩ n ↦ ∣ x ⟩ m ∣ 2 n y + x z ⟩ m + n + 1 . (35)
Proof. We provide an algorithm similar to that of Theorem 4.5 . We first transform the x x x register and rearrange the target register so that we can perform the multiplication using the odd factor x / 2 tz ( x ) x/2^{\operatorname{tz}(x)} x / 2 tz ( x ) of x x x . After this multiplication, we rescale the result by 2 tz ( x ) 2^{\operatorname{tz}(x)} 2 tz ( x ) and restore x x x .
On input ∣ x ⟩ m ∣ 0 ⟩ ⌈ log m ⌉ ∣ 0 ⟩ 1 ∣ w ⟩ tz ( x ) ∣ y ⟩ m − tz ( x ) ∣ z ⟩ n \lvert x\rangle_m\lvert0\rangle_{\lceil\log m\rceil}\lvert0\rangle_1\lvert w\rangle_{\operatorname{tz}(x)}\lvert y\rangle_{m-\operatorname{tz}(x)}\lvert z\rangle_n ∣ x ⟩ m ∣ 0 ⟩ ⌈ l o g m ⌉ ∣ 0 ⟩ 1 ∣ w ⟩ tz ( x ) ∣ y ⟩ m − tz ( x ) ∣ z ⟩ n , compute tz ( x ) \operatorname{tz}(x) tz ( x ) from the least significant m − 1 m-1 m − 1 qubits of x x x ,
∣ x ⟩ m ∣ tz ( x ) ⟩ ⌈ log m ⌉ ∣ 0 ⟩ 1 ∣ w ⟩ tz ( x ) ∣ y ⟩ m − tz ( x ) ∣ z ⟩ n (36) \lvert x\rangle_m\lvert\operatorname{tz}(x)\rangle_{\lceil\log m\rceil}\lvert0\rangle_1\lvert w\rangle_{\operatorname{tz}(x)}\lvert y\rangle_{m-\operatorname{tz}(x)}\lvert z\rangle_n \tag*{(36)} ∣ x ⟩ m ∣ tz ( x ) ⟩ ⌈ l o g m ⌉ ∣ 0 ⟩ 1 ∣ w ⟩ tz ( x ) ∣ y ⟩ m − tz ( x ) ∣ z ⟩ n (36)
Next, apply CS ⌈ log m ⌉ , m \operatorname{CS}_{\lceil\log m\rceil,m} CS ⌈ l o g m ⌉ , m to the x x x -register, controlled on tz ( x ) \operatorname{tz}(x) tz ( x ) . All applications of CS in this algorithm are controlled on tz ( x ) \operatorname{tz}(x) tz ( x ) .
∣ x / 2 tz ( x ) ⟩ m ∣ tz ( x ) ⟩ ⌈ log m ⌉ ∣ 0 ⟩ 1 ∣ w ⟩ tz ( x ) ∣ y ⟩ m − tz ( x ) ∣ z ⟩ n (37) \lvert x/2^{\operatorname{tz}(x)}\rangle_m\lvert\operatorname{tz}(x)\rangle_{\lceil\log m\rceil}\lvert0\rangle_1\lvert w\rangle_{\operatorname{tz}(x)}\lvert y\rangle_{m-\operatorname{tz}(x)}\lvert z\rangle_n \tag*{(37)} ∣ x / 2 tz ( x ) ⟩ m ∣ tz ( x ) ⟩ ⌈ l o g m ⌉ ∣ 0 ⟩ 1 ∣ w ⟩ tz ( x ) ∣ y ⟩ m − tz ( x ) ∣ z ⟩ n (37)
We now need to place the clean qubit between w w w and y y y , to accommodate the carry from 2 n y + x z / 2 tz ( x ) 2^n y+xz/2^{\operatorname{tz}(x)} 2 n y + x z / 2 tz ( x ) . To do this, first apply CS ⌈ log m ⌉ , m − 1 \operatorname{CS}^{-1}_{\lceil\log m\rceil,m} CS ⌈ l o g m ⌉ , m − 1 to the m m m qubits ∣ w ⟩ tz ( x ) ∣ y ⟩ m − tz ( x ) \lvert w\rangle_{\operatorname{tz}(x)}\lvert y\rangle_{m-\operatorname{tz}(x)} ∣ w ⟩ tz ( x ) ∣ y ⟩ m − tz ( x ) ,
∣ x / 2 tz ( x ) ⟩ m ∣ tz ( x ) ⟩ ⌈ log m ⌉ ∣ 0 ⟩ 1 ∣ y ⟩ m − tz ( x ) ∣ w ⟩ tz ( x ) ∣ z ⟩ n (38) \lvert x/2^{\operatorname{tz}(x)}\rangle_m\lvert\operatorname{tz}(x)\rangle_{\lceil\log m\rceil}\lvert0\rangle_1\lvert y\rangle_{m-\operatorname{tz}(x)}\lvert w\rangle_{\operatorname{tz}(x)}\lvert z\rangle_n \tag*{(38)} ∣ x / 2 tz ( x ) ⟩ m ∣ tz ( x ) ⟩ ⌈ l o g m ⌉ ∣ 0 ⟩ 1 ∣ y ⟩ m − tz ( x ) ∣ w ⟩ tz ( x ) ∣ z ⟩ n (38)
Then, apply CS ⌈ log m ⌉ , m + 1 \operatorname{CS}_{\lceil\log m\rceil,m+1} CS ⌈ l o g m ⌉ , m + 1 to the m + 1 m+1 m + 1 qubits ∣ 0 ⟩ 1 ∣ y ⟩ m − tz ( x ) ∣ w ⟩ tz ( x ) \lvert0\rangle_1\lvert y\rangle_{m-\operatorname{tz}(x)}\lvert w\rangle_{\operatorname{tz}(x)} ∣ 0 ⟩ 1 ∣ y ⟩ m − tz ( x ) ∣ w ⟩ tz ( x ) ,
∣ x / 2 tz ( x ) ⟩ m ∣ tz ( x ) ⟩ ⌈ log m ⌉ ∣ w ⟩ tz ( x ) ∣ 0 ⟩ 1 ∣ y ⟩ m − tz ( x ) ∣ z ⟩ n (39) \lvert x/2^{\operatorname{tz}(x)}\rangle_m\lvert\operatorname{tz}(x)\rangle_{\lceil\log m\rceil}\lvert w\rangle_{\operatorname{tz}(x)}\lvert0\rangle_1\lvert y\rangle_{m-\operatorname{tz}(x)}\lvert z\rangle_n \tag*{(39)} ∣ x / 2 tz ( x ) ⟩ m ∣ tz ( x ) ⟩ ⌈ l o g m ⌉ ∣ w ⟩ tz ( x ) ∣ 0 ⟩ 1 ∣ y ⟩ m − tz ( x ) ∣ z ⟩ n (39)
Note, for tz ( x ) = 0 \operatorname{tz}(x)=0 tz ( x ) = 0 , both shifts are the identity and the register w w w has zero qubits. We can write this state as
∣ x / 2 tz ( x ) ⟩ m ∣ tz ( x ) ⟩ ⌈ log m ⌉ ∣ 2 m − tz ( x ) + 1 w + y ⟩ m + 1 ∣ z ⟩ n (40) \lvert x/2^{\operatorname{tz}(x)}\rangle_m\lvert\operatorname{tz}(x)\rangle_{\lceil\log m\rceil}\lvert2^{m-\operatorname{tz}(x)+1}w+y\rangle_{m+1}\lvert z\rangle_n \tag*{(40)} ∣ x / 2 tz ( x ) ⟩ m ∣ tz ( x ) ⟩ ⌈ l o g m ⌉ ∣ 2 m − tz ( x ) + 1 w + y ⟩ m + 1 ∣ z ⟩ n (40)
Since x / 2 tz ( x ) x/2^{\operatorname{tz}(x)} x / 2 tz ( x ) is odd, we can now apply Theorem 2.5 (with s = m s=m s = m , t = m + 1 t=m+1 t = m + 1 , m = n m=n m = n , α = 0 \alpha=0 α = 0 , β = 1 \beta=1 β = 1 and b = c = 1 b=c=1 b = c = 1 ).
∣ x / 2 tz ( x ) ⟩ m ∣ tz ( x ) ⟩ ⌈ log m ⌉ ∣ 2 m + n − tz ( x ) + 1 w + 2 n y + x z / 2 tz ( x ) ⟩ m + n + 1 (41) \lvert x/2^{\operatorname{tz}(x)}\rangle_m\lvert\operatorname{tz}(x)\rangle_{\lceil\log m\rceil}\lvert2^{m+n-\operatorname{tz}(x)+1}w+2^n y+xz/2^{\operatorname{tz}(x)}\rangle_{m+n+1} \tag*{(41)} ∣ x / 2 tz ( x ) ⟩ m ∣ tz ( x ) ⟩ ⌈ l o g m ⌉ ∣ 2 m + n − tz ( x ) + 1 w + 2 n y + x z / 2 tz ( x ) ⟩ m + n + 1 (41)
Since y , x / 2 tz ( x ) < 2 m − tz ( x ) y,x/2^{\operatorname{tz}(x)}<2^{m-\operatorname{tz}(x)} y , x / 2 tz ( x ) < 2 m − tz ( x ) and z < 2 n z<2^n z < 2 n , we have
0 ≤ 2 n y + x z / 2 tz ( x ) < 2 m + n − tz ( x ) + 1 . (42) 0\le2^n y+xz/2^{\operatorname{tz}(x)}<2^{m+n-\operatorname{tz}(x)+1}. \tag*{(42)} 0 ≤ 2 n y + x z / 2 tz ( x ) < 2 m + n − tz ( x ) + 1 . (42)
Therefore, no carry propagates to w w w , and the state is
∣ x / 2 tz ( x ) ⟩ m ∣ tz ( x ) ⟩ ⌈ log m ⌉ ∣ w ⟩ tz ( x ) ∣ 2 n y + x z / 2 tz ( x ) ⟩ m + n − tz ( x ) + 1 . (43) \lvert x/2^{\operatorname{tz}(x)}\rangle_m\lvert\operatorname{tz}(x)\rangle_{\lceil\log m\rceil}\lvert w\rangle_{\operatorname{tz}(x)}\lvert2^n y+xz/2^{\operatorname{tz}(x)}\rangle_{m+n-\operatorname{tz}(x)+1}. \tag*{(43)} ∣ x / 2 tz ( x ) ⟩ m ∣ tz ( x ) ⟩ ⌈ l o g m ⌉ ∣ w ⟩ tz ( x ) ∣ 2 n y + x z / 2 tz ( x ) ⟩ m + n − tz ( x ) + 1 . (43)
We now apply CS ⌈ log m ⌉ , m + n + 1 − 1 \operatorname{CS}^{-1}_{\lceil\log m\rceil,m+n+1} CS ⌈ l o g m ⌉ , m + n + 1 − 1 to the last m + n + 1 m+n+1 m + n + 1 qubits, moving w w w to the least significant positions.
∣ x / 2 tz ( x ) ⟩ m ∣ tz ( x ) ⟩ ⌈ log m ⌉ ∣ 2 n y + x z / 2 tz ( x ) ⟩ m + n − tz ( x ) + 1 ∣ w ⟩ tz ( x ) (44) \lvert x/2^{\operatorname{tz}(x)}\rangle_m\lvert\operatorname{tz}(x)\rangle_{\lceil\log m\rceil}\lvert2^n y+xz/2^{\operatorname{tz}(x)}\rangle_{m+n-\operatorname{tz}(x)+1}\lvert w\rangle_{\operatorname{tz}(x)} \tag*{(44)} ∣ x / 2 tz ( x ) ⟩ m ∣ tz ( x ) ⟩ ⌈ l o g m ⌉ ∣ 2 n y + x z / 2 tz ( x ) ⟩ m + n − tz ( x ) + 1 ∣ w ⟩ tz ( x ) (44)
∣ x / 2 tz ( x ) ⟩ m ∣ tz ( x ) ⟩ ⌈ log m ⌉ ∣ 2 n + tz ( x ) y + x z + w ⟩ m + n + 1 . (45) \lvert x/2^{\operatorname{tz}(x)}\rangle_m\lvert\operatorname{tz}(x)\rangle_{\lceil\log m\rceil}\lvert2^{n+\operatorname{tz}(x)}y+xz+w\rangle_{m+n+1}. \tag*{(45)} ∣ x / 2 tz ( x ) ⟩ m ∣ tz ( x ) ⟩ ⌈ l o g m ⌉ ∣ 2 n + tz ( x ) y + x z + w ⟩ m + n + 1 . (45)
Finally, we restore x x x by applying CS ⌈ log m ⌉ , m − 1 \operatorname{CS}^{-1}_{\lceil\log m\rceil,m} CS ⌈ l o g m ⌉ , m − 1 to the first m m m qubits, and invert the trailing-zero computation.
∣ x ⟩ m ∣ tz ( x ) ⟩ ⌈ log m ⌉ ∣ 2 n + tz ( x ) y + x z + w ⟩ m + n + 1 , (46) \lvert x\rangle_m\lvert\operatorname{tz}(x)\rangle_{\lceil\log m\rceil}\lvert2^{n+\operatorname{tz}(x)}y+xz+w\rangle_{m+n+1}, \tag*{(46)} ∣ x ⟩ m ∣ tz ( x ) ⟩ ⌈ l o g m ⌉ ∣ 2 n + tz ( x ) y + x z + w ⟩ m + n + 1 , (46)
I 1 ⊗ T Z − 1 m − 1 , ⌈ log m ⌉ ⟶ ∣ x ⟩ m ∣ 0 ⟩ ⌈ log m ⌉ ∣ 2 n + tz ( x ) y + x z + w ⟩ m + n + 1 (47) \frac{I_{1}\otimes T Z^{-1}}{m-1,\lceil\log m\rceil}\longrightarrow|x\rangle_m |0\rangle_{\lceil\log m\rceil} |2^n+\operatorname{tz}(x)y+xz+w\rangle_{m+n+1} \tag*{(47)} m − 1 , ⌈ log m ⌉ I 1 ⊗ T Z − 1 ⟶ ∣ x ⟩ m ∣0 ⟩ ⌈ l o g m ⌉ ∣ 2 n + tz ( x ) y + x z + w ⟩ m + n + 1 (47)
Since m = O ( n ) m=O(n) m = O ( n ) , the trailing-zero computations and cyclic shifts use O ( n log n ) O(n\log n) O ( n log n ) gates by Lemmas 4.1 and 4.2. Together with Theorem 2.5 , this gives O ( n 1 + ϵ ) O(n^{1+\epsilon}) O ( n 1 + ϵ ) gates and ⌈ log m ⌉ \lceil\log m\rceil ⌈ log m ⌉ ancilla qubits. For the special case x ∈ Z 2 m × x\in\mathbb{Z}_{2^m}^{\times} x ∈ Z 2 m × the cyclic shifts and trailing zero computations are unnecessary, since tz ( x ) = 0 \operatorname{tz}(x)=0 tz ( x ) = 0 and there is no w w w -register. In this case, Theorem 2.5 can be applied directly, requiring no ancilla qubits.
Remark 4.9. In Theorems 4.5 and 4.8 , we restrict x x x to be non-zero so that the stated transformations are unitary. If x = 0 x=0 x = 0 is allowed, the appropriate behaviour depends on the surrounding algorithm that the multiplication sits within. As an example, replacing Theorem 2.5 with Theorem E.1 in the above proofs gives an extension to all x x x , with the same gate and ancilla bounds and unchanged behaviour for x ≠ 0 x\ne0 x = 0 .
In the next section, we use our modular multiplication circuit to develop in-place algorithms for computing the multiplicative inverse of a quantum integer modulo a power of two. We prove that these algorithms attain the same asymptotic upper bound on gate count as quantum modular multiplication, even without ancilla qubits.
Multiplicative inverse
In this section, we use the symbols × \times × and ÷ \div ÷ to represent quantum circuits that perform modular multiplication with a quantum multiplier. In more detail, these operations are defined for x ∈ Z 2 m × x\in\mathbb{Z}_{2^m}^{\times} x ∈ Z 2 m × and y ∈ Z 2 n y\in\mathbb{Z}_{2^n} y ∈ Z 2 n as follows,
× ( x ) : ∣ x ⟩ m ∣ y ⟩ n ⟼ ∣ x ⟩ m ∣ y x m o d 2 n ⟩ n , ÷ ( x ) : ∣ x ⟩ m ∣ y ⟩ n ⟼ ∣ x ⟩ m ∣ y ( x − 1 ) m o d 2 n ⟩ n . \times(x): |x\rangle_m |y\rangle_n\longmapsto|x\rangle_m |yx\bmod2^n\rangle_n,\qquad\div(x): |x\rangle_m |y\rangle_n\longmapsto|x\rangle_m |y(x^{-1})\bmod2^n\rangle_n. × ( x ) : ∣ x ⟩ m ∣ y ⟩ n ⟼ ∣ x ⟩ m ∣ y x mod 2 n ⟩ n , ÷ ( x ) : ∣ x ⟩ m ∣ y ⟩ n ⟼ ∣ x ⟩ m ∣ y ( x − 1 ) mod 2 n ⟩ n .
All arithmetic on n n n qubit registers is understood to be mod 2 n 2^n 2 n , so from now on, we drop the modulus for ease of notation.
For m = O ( n ) m=O(n) m = O ( n ) , by Theorem 4.3, the operation × \times × can be performed using O ( n 1 + ϵ ) O(n^{1+\epsilon}) O ( n 1 + ϵ ) gates and no ancilla qubits, for any ϵ > 0 \epsilon>0 ϵ > 0 . ÷ \div ÷ is the inverse operation. We use these costs when justifying the main results of this section.
Before proceeding to these results, we prove the following lemma.
Lemma 5.1 (Computing a partial inverse). Let 1 ≤ n ≤ m 1\le n\le m 1 ≤ n ≤ m . Let x ∈ Z 2 m × x\in\mathbb{Z}_{2^m}^{\times} x ∈ Z 2 m × and let x − 1 x^{-1} x − 1 denote the multiplicative inverse of x x x in Z 2 m × \mathbb{Z}_{2^m}^{\times} Z 2 m × . For any ϵ > 0 \epsilon>0 ϵ > 0 , there exists a quantum circuit for performing the operation
∣ x ⟩ m ∣ 0 ⟩ n ⟼ ∣ x ⟩ m ∣ x − 1 m o d 2 n ⟩ n (48) |x\rangle_m |0\rangle_n\longmapsto|x\rangle_m |x^{-1}\bmod2^n\rangle_n \tag*{(48)} ∣ x ⟩ m ∣0 ⟩ n ⟼ ∣ x ⟩ m ∣ x − 1 mod 2 n ⟩ n (48) that uses O ( n 1 + ϵ ) O(n^{1+\epsilon}) O ( n 1 + ϵ ) gates and no ancilla qubits.
Proof. Consider x l o , ( x − 1 ) l o ∈ Z 2 n x_{\mathrm{lo}},(x^{-1})_{\mathrm{lo}}\in\mathbb{Z}_{2^n} x lo , ( x − 1 ) lo ∈ Z 2 n and x h i , ( x − 1 ) h i ∈ Z 2 m − n x_{\mathrm{hi}},(x^{-1})_{\mathrm{hi}}\in\mathbb{Z}_{2^{m-n}} x hi , ( x − 1 ) hi ∈ Z 2 m − n such that
x = 2 n x h i + x l o , x − 1 = 2 n ( x − 1 ) h i + ( x − 1 ) l o . (49) x=2^n x_{\mathrm{hi}}+x_{\mathrm{lo}},\qquad x^{-1}=2^n(x^{-1})_{\mathrm{hi}}+(x^{-1})_{\mathrm{lo}}. \tag*{(49)} x = 2 n x hi + x lo , x − 1 = 2 n ( x − 1 ) hi + ( x − 1 ) lo . (49) Expanding the product x x − 1 xx^{-1} x x − 1 gives
2 2 n x h i ( x − 1 ) h i + 2 n ( x h i ( x − 1 ) l o + x l o ( x − 1 ) h i ) + x l o ( x − 1 ) l o ≡ 1 ( m o d 2 m ) . (50) 2^{2n}x_{\mathrm{hi}}(x^{-1})_{\mathrm{hi}}+2^n\bigl(x_{\mathrm{hi}}(x^{-1})_{\mathrm{lo}}+x_{\mathrm{lo}}(x^{-1})_{\mathrm{hi}}\bigr)+x_{\mathrm{lo}}(x^{-1})_{\mathrm{lo}}\equiv1\pmod{2^m}. \tag*{(50)} 2 2 n x hi ( x − 1 ) hi + 2 n ( x hi ( x − 1 ) lo + x lo ( x − 1 ) hi ) + x lo ( x − 1 ) lo ≡ 1 ( mod 2 m ) . (50) Reducing modulo 2 n 2^n 2 n gives x l o ( x − 1 ) l o ≡ 1 ( m o d 2 n ) x_{\mathrm{lo}}(x^{-1})_{\mathrm{lo}}\equiv1\pmod{2^n} x lo ( x − 1 ) lo ≡ 1 ( mod 2 n ) . Since x l o x_{\mathrm{lo}} x lo has a unique inverse in Z 2 n × \mathbb{Z}_{2^n}^{\times} Z 2 n × and ( x − 1 ) l o ∈ Z 2 n (x^{-1})_{\mathrm{lo}}\in\mathbb{Z}_{2^n} ( x − 1 ) lo ∈ Z 2 n we know that ( x l o ) − 1 = ( x − 1 ) l o (x_{\mathrm{lo}})^{-1}=(x^{-1})_{\mathrm{lo}} ( x lo ) − 1 = ( x − 1 ) lo . This observation reveals an algorithm proving the claim. On input ∣ x ⟩ m ∣ 0 ⟩ n |x\rangle_m|0\rangle_n ∣ x ⟩ m ∣0 ⟩ n , apply
I m + n − 1 ⊗ X ⟶ ∣ x h i ⟩ m − n ∣ x l o ⟩ n ∣ 1 ⟩ n (51) I_{m+n-1}\otimes X
\longrightarrow|x_{\mathrm{hi}}\rangle_{m-n}|x_{\mathrm{lo}}\rangle_n|1\rangle_n \tag*{(51)} I m + n − 1 ⊗ X ⟶ ∣ x hi ⟩ m − n ∣ x lo ⟩ n ∣1 ⟩ n (51)
⟶ ÷ ( x l o ) ∣ x h i ⟩ m − n ∣ x l o ⟩ n ∣ ( x − 1 ) l o ⟩ n . (52) \overset{\div(x_{\mathrm{lo}})}{\longrightarrow} |x_{\mathrm{hi}}\rangle_{m-n}|x_{\mathrm{lo}}\rangle_n|(x^{-1})_{\mathrm{lo}}\rangle_n . \tag*{(52)} ⟶ ÷ ( x lo ) ∣ x hi ⟩ m − n ∣ x lo ⟩ n ∣ ( x − 1 ) lo ⟩ n . (52)
Theorem 5.2 (In-place modular inversion). Let m ≥ 2 m \ge2 m ≥ 2 and n = ⌈ m / 2 ⌉ n = \lceil m/2\rceil n = ⌈ m /2 ⌉ . Let x ∈ Z 2 m × x \in\mathbb{Z}_{2^m}^{\times} x ∈ Z 2 m × and let x − 1 x^{-1} x − 1 denote the multiplicative inverse of x x x in Z 2 m × \mathbb{Z}_{2^m}^{\times} Z 2 m × . For any ϵ > 0 \epsilon> 0 ϵ > 0 , there exists a quantum circuit for performing the operation
∣ x ⟩ m ⟼ ∣ x − 1 ⟩ m (53) |x\rangle_m \longmapsto|x^{-1}\rangle_m \tag*{(53)} ∣ x ⟩ m ⟼ ∣ x − 1 ⟩ m (53) that uses O ( m 1 + ϵ ) O(m^{1+\epsilon}) O ( m 1 + ϵ ) gates and requires n n n ancilla qubits.
Proof. As in Lemma 5.1 , consider x l o , ( x − 1 ) l o ∈ Z 2 n x_{\mathrm{lo},}(x^{-1})_{\mathrm{lo}} \in\mathbb{Z}_{2^n} x lo , ( x − 1 ) lo ∈ Z 2 n and x h i , ( x − 1 ) h i ∈ Z 2 m − n x_{\mathrm{hi}},(x^{-1})_{\mathrm{hi}} \in\mathbb{Z}_{2^{m-n}} x hi , ( x − 1 ) hi ∈ Z 2 m − n such that
x = 2 n x h i + x l o , x − 1 = 2 n ( x − 1 ) h i + ( x − 1 ) l o . (54) x = 2^n x_{\mathrm{hi}} + x_{\mathrm{lo}}, \qquad x^{-1} = 2^n(x^{-1})_{\mathrm{hi}} + (x^{-1})_{\mathrm{lo}} . \tag*{(54)} x = 2 n x hi + x lo , x − 1 = 2 n ( x − 1 ) hi + ( x − 1 ) lo . (54)
We showed in Lemma 5.1 that ( x − 1 ) l o (x^{-1})_{\mathrm{lo}} ( x − 1 ) lo equals ( x l o ) − 1 (x_{\mathrm{lo}})^{-1} ( x lo ) − 1 , the multiplicative inverse of x l o x_{\mathrm{lo}} x lo in Z 2 n \mathbb{Z}_{2^n} Z 2 n . In particular, this implies that x l o ( x − 1 ) l o = 2 n c + 1 x_{\mathrm{lo}}(x^{-1})_{\mathrm{lo}} = 2^n c + 1 x lo ( x − 1 ) lo = 2 n c + 1 for some c ∈ Z 2 n c \in\mathbb{Z}_{2^n} c ∈ Z 2 n . With these observations in hand, we provide an algorithm proving the claim. On input ∣ x ⟩ m ∣ 0 ⟩ n |x\rangle_m|0\rangle_n ∣ x ⟩ m ∣0 ⟩ n , apply
⟶ Lemma 5.1 ∣ x ⟩ m ∣ ( x − 1 ) l o ⟩ n . (55) \overset{\text{Lemma 5.1}}{\longrightarrow} |x\rangle_m|(x^{-1})_{\mathrm{lo}}\rangle_n . \tag*{(55)} ⟶ Lemma 5.1 ∣ x ⟩ m ∣ ( x − 1 ) lo ⟩ n . (55)
Then multiply this partial inverse into ∣ x ⟩ m |x\rangle_m ∣ x ⟩ m
⟶ × ( ( x − 1 ) l o ) ∣ x ( x − 1 ) l o ⟩ m ∣ ( x − 1 ) l o ⟩ n = ∣ x h i ( x − 1 ) l o + c ⟩ m − n ∣ 1 ⟩ n ∣ ( x − 1 ) l o ⟩ n , (56) \overset{\times((x^{-1})_{\mathrm{lo}})}{\longrightarrow} |x(x^{-1})_{\mathrm{lo}}\rangle_m|(x^{-1})_{\mathrm{lo}}\rangle_n = |x_{\mathrm{hi}}(x^{-1})_{\mathrm{lo}} + c\rangle_{m-n}|1\rangle_n|(x^{-1})_{\mathrm{lo}}\rangle_n , \tag*{(56)} ⟶ × (( x − 1 ) lo ) ∣ x ( x − 1 ) lo ⟩ m ∣ ( x − 1 ) lo ⟩ n = ∣ x hi ( x − 1 ) lo + c ⟩ m − n ∣1 ⟩ n ∣ ( x − 1 ) lo ⟩ n , (56)
We can reset the middle n n n qubit register to ∣ 0 ⟩ n |0\rangle_n ∣0 ⟩ n with an X X X gate and ignore it for the rest of the algorithm. The state is thus,
∣ x h i ( x − 1 ) l o + c ⟩ m − n ∣ ( x − 1 ) l o ⟩ n . (57) |x_{\mathrm{hi}}(x^{-1})_{\mathrm{lo}} + c\rangle_{m-n}|(x^{-1})_{\mathrm{lo}}\rangle_n . \tag*{(57)} ∣ x hi ( x − 1 ) lo + c ⟩ m − n ∣ ( x − 1 ) lo ⟩ n . (57)
Finally, multiply the m − n m-n m − n qubit register with − ( x − 1 ) l o -(x^{-1})_{\mathrm{lo}} − ( x − 1 ) lo ,
⟶ × ( − ( x − 1 ) l o ) ∣ − 2 n x h i ( x − 1 ) l o 2 − 2 n c ( x − 1 ) l o + ( x − 1 ) l o ⟩ m , (58) \overset{\times(-(x^{-1})_{\mathrm{lo}})}{\longrightarrow} |-2^n x_{\mathrm{hi}}(x^{-1})_{\mathrm{lo}}^2 - 2^n c(x^{-1})_{\mathrm{lo}} + (x^{-1})_{\mathrm{lo}}\rangle_m , \tag*{(58)} ⟶ × ( − ( x − 1 ) lo ) ∣ − 2 n x hi ( x − 1 ) lo 2 − 2 n c ( x − 1 ) lo + ( x − 1 ) lo ⟩ m , (58)
since 2 n ( y m o d 2 m − n ) = 2 n y m o d 2 m 2^n(y \bmod2^{m-n}) = 2^n y \bmod2^m 2 n ( y mod 2 m − n ) = 2 n y mod 2 m . The result follows from
x ( − 2 n x h i ( x − 1 ) l o 2 − 2 n c ( x − 1 ) l o + ( x − 1 ) l o ) = ( 2 n x h i + x l o ) ( − 2 n x h i ( x − 1 ) l o 2 − 2 n c ( x − 1 ) l o + ( x − 1 ) l o ) ≡ 2 n x h i ( x − 1 ) l o − 2 n x h i ( x − 1 ) l o + ( − 2 n c + 1 ) x l o ( x − 1 ) l o ( m o d 2 m ) ≡ ( − 2 n c + 1 ) ( 2 n c + 1 ) ≡ 1 ( m o d 2 m ) . (59) \begin{aligned}
x\bigl(-2^n x_{\mathrm{hi}}(x^{-1})_{\mathrm{lo}}^2 - 2^n c(x^{-1})_{\mathrm{lo}} + (x^{-1})_{\mathrm{lo}}\bigr)
&= (2^n x_{\mathrm{hi}} + x_{\mathrm{lo}})\bigl(-2^n x_{\mathrm{hi}}(x^{-1})_{\mathrm{lo}}^2 - 2^n c(x^{-1})_{\mathrm{lo}} + (x^{-1})_{\mathrm{lo}}\bigr) \\
&\equiv2^n x_{\mathrm{hi}}(x^{-1})_{\mathrm{lo}} - 2^n x_{\mathrm{hi}}(x^{-1})_{\mathrm{lo}} + (-2^n c + 1)x_{\mathrm{lo}}(x^{-1})_{\mathrm{lo}} \pmod{2^m} \\
&\equiv(-2^n c + 1)(2^n c + 1) \equiv1 \pmod{2^m}. \tag*{(59)}
\end{aligned} x ( − 2 n x hi ( x − 1 ) lo 2 − 2 n c ( x − 1 ) lo + ( x − 1 ) lo ) = ( 2 n x hi + x lo ) ( − 2 n x hi ( x − 1 ) lo 2 − 2 n c ( x − 1 ) lo + ( x − 1 ) lo ) ≡ 2 n x hi ( x − 1 ) lo − 2 n x hi ( x − 1 ) lo + ( − 2 n c + 1 ) x lo ( x − 1 ) lo ( mod 2 m ) ≡ ( − 2 n c + 1 ) ( 2 n c + 1 ) ≡ 1 ( mod 2 m ) . (59)
Ancilla-efficient inversion
Finally, we present an in-place modular inversion algorithm for the more general setting where we have 0 ≤ s ≤ m − 1 0 \le s \le m - 1 0 ≤ s ≤ m − 1 ancillas at our disposal. We show that, with access to any number of ancillas in this range, our algorithm retains the fast asymptotic cost of quantum modular multiplication. This remains true even in the absence of ancillas.
In Theorem 5.2 , notice that the state of the algorithm at Equation (57) is ∣ z ⟩ m − n ∣ y ⟩ n |z\rangle_{m-n}|y\rangle_n ∣ z ⟩ m − n ∣ y ⟩ n for integers y , z y,z y , z satisfying the relation x y ≡ 1 + 2 n z ( m o d 2 m ) xy \equiv1+2^n z \pmod{2^m} x y ≡ 1 + 2 n z ( mod 2 m ) . We showed in this theorem that, if n = ⌈ m / 2 ⌉ n=\lceil m/2\rceil n = ⌈ m /2 ⌉ , the only operation that remains to prepare ∣ x − 1 ⟩ m |x^{-1}\rangle_m ∣ x − 1 ⟩ m from ∣ z ⟩ m − n ∣ y ⟩ n |z\rangle_{m-n}|y\rangle_n ∣ z ⟩ m − n ∣ y ⟩ n is a multiplication by − y -y − y on the z z z -register.
In Theorem 5.4 below, we will see that, for any n ≥ ⌈ m / 2 ⌉ n\ge\lceil m/2\rceil n ≥ ⌈ m /2 ⌉ , multiplying the z z z -register by − y -y − y always prepares ∣ x − 1 ⟩ m |x^{-1}\rangle_m ∣ x − 1 ⟩ m , as long as the integers y y y and z z z satisfy the previous relation. This motivates the following lemma.
Lemma 5.3 (Doubling the number of inverse bits). Let 2 ≤ 2 h < m 2\le2h<m 2 ≤ 2 h < m . Let x ∈ Z 2 m × x\in\mathbb{Z}_{2^m}^{\times} x ∈ Z 2 m × , y ∈ Z 2 h × y\in\mathbb{Z}_{2^h}^{\times} y ∈ Z 2 h × and z ∈ Z 2 m − h z\in\mathbb{Z}_{2^{m-h}} z ∈ Z 2 m − h such that x y ≡ 1 + 2 h z ( m o d 2 m ) xy\equiv1+2^h z\pmod{2^m} x y ≡ 1 + 2 h z ( mod 2 m ) . There exists a quantum circuit for performing the operation
∣ z ⟩ m − h ∣ y ⟩ h ⟼ ∣ z ′ ⟩ m − 2 h ∣ y ′ ⟩ 2 h , (60) |z\rangle_{m-h}|y\rangle_h \longmapsto|z'\rangle_{m-2h}|y'\rangle_{2h}, \tag*{(60)} ∣ z ⟩ m − h ∣ y ⟩ h ⟼ ∣ z ′ ⟩ m − 2 h ∣ y ′ ⟩ 2 h , (60) with y ′ ∈ Z 2 2 h y'\in\mathbb{Z}_{2^{2h}} y ′ ∈ Z 2 2 h , z ′ ∈ Z 2 m − 2 h z'\in\mathbb{Z}_{2^{m-2h}} z ′ ∈ Z 2 m − 2 h satisfying the relation x y ′ ≡ 1 + 2 2 h z ′ ( m o d 2 m ) xy'\equiv1+2^{2h}z'\pmod{2^m} x y ′ ≡ 1 + 2 2 h z ′ ( mod 2 m ) . The circuit uses six modular multiplications, four modular additions and no ancilla qubits.
Proof. Our algorithm for performing this inverse-doubling operation is derived from the classical Newton–Hensel iteration [35 Section 2.5] . The proof is left for Appendix § 11 . □
With this lemma in hand, we prove our ancilla-efficient modular inversion algorithm.
Theorem 5.4 (Ancilla-efficient in-place modular inversion). Let m ≥ 2 m\ge2 m ≥ 2 , and 0 ≤ s ≤ m − 1 0\le s\le m-1 0 ≤ s ≤ m − 1 . Let x ∈ Z 2 m × x\in\mathbb{Z}_{2^m}^{\times} x ∈ Z 2 m × and let x − 1 x^{-1} x − 1 denote the multiplicative inverse of x x x in Z 2 m \mathbb{Z}_{2^m} Z 2 m . For any ϵ > 0 \epsilon>0 ϵ > 0 , there exists a quantum circuit for performing the operation
∣ x ⟩ m ⟼ ∣ x − 1 ⟩ m (61) |x\rangle_m \longmapsto|x^{-1}\rangle_m \tag*{(61)} ∣ x ⟩ m ⟼ ∣ x − 1 ⟩ m (61) that uses O ( m 1 + ϵ ) O(m^{1+\epsilon}) O ( m 1 + ϵ ) gates and s s s ancilla qubits. The circuit uses O ( 1 + log ( m / ( s + 1 ) ) ) O(1+\log(m/(s+1))) O ( 1 + log ( m / ( s + 1 ))) modular multiplications and additions.
Proof. We present an algorithm proving the claim. The first step of the algorithm is to transform the input register ∣ x ⟩ m |x\rangle_m ∣ x ⟩ m to ∣ z ⟩ m − h ∣ y ⟩ h |z\rangle_{m-h}|y\rangle_h ∣ z ⟩ m − h ∣ y ⟩ h for some z ∈ Z 2 m − h z\in\mathbb{Z}_{2^{m-h}} z ∈ Z 2 m − h , y ∈ Z 2 h y\in\mathbb{Z}_{2^h} y ∈ Z 2 h satisfying x y ≡ 1 + 2 h z ( m o d 2 m ) xy\equiv1+2^h z\pmod{2^m} x y ≡ 1 + 2 h z ( mod 2 m ) . After this is achieved, we can iteratively apply Lemma 5.3 . We stop when the size of the register that contains the partial inverse, denoted h ′ h' h ′ , satisfies 2 h ′ ≥ m 2h'\ge m 2 h ′ ≥ m . At this point we have ∣ z ′ ⟩ m − h ′ ∣ y ′ ⟩ h ′ |z'\rangle_{m-h'}|y'\rangle_{h'} ∣ z ′ ⟩ m − h ′ ∣ y ′ ⟩ h ′ for some z ′ ∈ Z 2 m − h ′ z'\in\mathbb{Z}_{2^{m-h'}} z ′ ∈ Z 2 m − h ′ , y ′ ∈ Z 2 h ′ y'\in\mathbb{Z}_{2^{h'}} y ′ ∈ Z 2 h ′ satisfying x y ′ ≡ 1 + 2 h ′ z ′ ( m o d 2 m ) xy'\equiv1+2^{h'}z'\pmod{2^m} x y ′ ≡ 1 + 2 h ′ z ′ ( mod 2 m ) . We then apply,
⟶ × ( − y ′ ) ∣ − y ′ z ′ ⟩ m − h ′ ∣ y ′ ⟩ h ′ . (62) \underset{\times(-y')}{\longrightarrow}|-y'z'\rangle_{m-h'}|y'\rangle_{h'}. \tag*{(62)} × ( − y ′ ) ⟶ ∣ − y ′ z ′ ⟩ m − h ′ ∣ y ′ ⟩ h ′ . (62)
It follows that we have prepared ∣ x − 1 ⟩ m |x^{-1}\rangle_m ∣ x − 1 ⟩ m since
x ( − 2 h ′ z ′ + y ′ ) = x y ′ ( − 2 h ′ z ′ + 1 ) ≡ ( 2 h ′ z ′ + 1 ) ( − 2 h ′ z ′ + 1 ) ≡ 1 ( m o d 2 m ) . (63) x(-2^{h'}z'+y')=xy'(-2^{h'}z'+1)\equiv(2^{h'}z'+1)(-2^{h'}z'+1)\equiv1\pmod{2^m}. \tag*{(63)} x ( − 2 h ′ z ′ + y ′ ) = x y ′ ( − 2 h ′ z ′ + 1 ) ≡ ( 2 h ′ z ′ + 1 ) ( − 2 h ′ z ′ + 1 ) ≡ 1 ( mod 2 m ) . (63)
All that remains is to show that we can transform ∣ x ⟩ m |x\rangle_m ∣ x ⟩ m to ∣ z ⟩ m − h ∣ y ⟩ h |z\rangle_{m-h}|y\rangle_h ∣ z ⟩ m − h ∣ y ⟩ h with x y ≡ 1 + 2 h z ( m o d 2 m ) xy\equiv1+2^h z\pmod{2^m} x y ≡ 1 + 2 h z ( mod 2 m ) . For s ≥ 1 s\ge1 s ≥ 1 , we repeat the approach presented in Theorem 5.2 . On input ∣ x ⟩ m ∣ 0 ⟩ s |x\rangle_m|0\rangle_s ∣ x ⟩ m ∣0 ⟩ s , apply
⟶ Lemma 5.1 ∣ x ⟩ m ∣ x − 1 m o d 2 s ⟩ s . (64) \underset{\text{Lemma }5.1}{\longrightarrow}|x\rangle_m|x^{-1}\bmod2^s\rangle_s. \tag*{(64)} Lemma 5.1 ⟶ ∣ x ⟩ m ∣ x − 1 mod 2 s ⟩ s . (64)
⟶ × ( x − 1 m o d 2 s ) ∣ z ⟩ m − s ∣ 1 ⟩ s ∣ x − 1 m o d 2 s ⟩ s . (65) \underset{\times(x^{-1}\bmod2^s)}{\longrightarrow}|z\rangle_{m-s}|1\rangle_s|x^{-1}\bmod2^s\rangle_s. \tag*{(65)} × ( x − 1 mod 2 s ) ⟶ ∣ z ⟩ m − s ∣1 ⟩ s ∣ x − 1 mod 2 s ⟩ s . (65)
Here the integer z ∈ Z 2 m − s z \in\mathbb{Z}_{2^{m-s}} z ∈ Z 2 m − s clearly satisfies the relation x y ≡ 1 + 2 s z ( m o d 2 m ) xy \equiv1 + 2^{s}z \pmod{2^{m}} x y ≡ 1 + 2 s z ( mod 2 m ) with y = x − 1 m o d 2 s y = x^{-1} \bmod2^{s} y = x − 1 mod 2 s . Thus, we have the conditions necessary to apply Lemma 5.3 , with h = s h=s h = s to ∣ z ⟩ m − s ∣ x − 1 m o d 2 s ⟩ |z\rangle_{m-s}|x^{-1} \bmod2^{s}\rangle ∣ z ⟩ m − s ∣ x − 1 mod 2 s ⟩ . For s = 0 s=0 s = 0 , the original input register ∣ x ⟩ m |x\rangle_m ∣ x ⟩ m already has the desired form with h = 1 h=1 h = 1 , y = 1 y=1 y = 1 and z = ( x − 1 ) / 2 z=(x-1)/2 z = ( x − 1 ) /2 .
Since h h h initially equals max { 1 , s } ≥ ( s + 1 ) / 2 \max\{1,s\} \ge(s+1)/2 max { 1 , s } ≥ ( s + 1 ) /2 , the total number of multiplications and additions is O ( 1 + log ( m / ( s + 1 ) ) ) O(1+\log(m/(s+1))) O ( 1 + log ( m / ( s + 1 ))) . For any ϵ > 0 \epsilon>0 ϵ > 0 , we can pick 0 < δ < ϵ 0<\delta<\epsilon 0 < δ < ϵ . Each modular multiplication in the above algorithm can then be performed using O ( m 1 + δ ) O(m^{1+\delta}) O ( m 1 + δ ) gates. Thus, for any 0 ≤ s ≤ m − 1 0 \le s \le m-1 0 ≤ s ≤ m − 1 and any ϵ > 0 \epsilon>0 ϵ > 0 , the total algorithm requires O ( m 1 + δ ( 1 + log m ) ) = O ( m 1 + ϵ ) O(m^{1+\delta}(1+\log m))=O(m^{1+\epsilon}) O ( m 1 + δ ( 1 + log m )) = O ( m 1 + ϵ ) gates and s s s ancilla qubits.
Figure 2 illustrates the relationship between clean ancillas and modular multiplications for the algorithm proposed in Theorem 5.4 . As an example, at m = 256 m=256 m = 256 , allowing at most 0, 8, 32, and 128 clean ancillas gives 43, 27, 15, and 3 modular multiplications, respectively.
Figure 2. Number of modular multiplications required for in-place inversion mod 2 m 2^m 2 m as a function of number of clean ancillas available (s s s ). Horizontal spacing is logarithmic in s + 1 s+1 s + 1 . The multiplications do not take place on registers of the same size, so are not equivalent in terms of true gate count. Note: We do not take into account the optimizations from Remark 5.5 in this plot.
Discussion
In this work, we have presented several quantum algorithms for in-place multiplication and inversion that simultaneously achieve subquadratic gate counts and zero ancilla qubits (with at most a logarithmic number of ancillas required for multiplication by a general non-zero quantum integer). Our constructions build on the phase-encoded multiplication algorithms of Kahanamoku-Meyer and Yao [20 ] and the fast quantum Fourier transform construction of Cleve and Watrous [31 ] . We show that a fast controlled and parametrised quantum Fourier transform leads to several in-place and ancilla-free multiplication algorithms arising naturally from a single unified theorem (Theorem 2.5 ). We envision that this unified construction may also make our algorithms easier to code up, with the different variants obtained by toggling relevant parameters.
The gate counts we report are based on a family of recursive phase-encoded multiplication algorithms, inspired by classical k k k -way Toom–Cook multiplication, whose asymptotic gate counts can be made arbitrarily close to linear. However, the constants hidden in these bounds depend on the chosen recursion parameters. Identifying practical fault-tolerant instantiations therefore requires explicit resource estimates and a suitable choice of recursive decompositions and base cases. Our results also assume the exact implementation of arbitrary rotation gates. Resource estimates for a finite gate set must additionally account for the required approximation precision and the cost of synthesising these rotations.
Our constructions provide flexibility when arithmetic is used within a larger algorithm. The dirty register variants reduce the need for initially clean qubits, provided that the surrounding computation supports the required correction and restoration of the borrowed register. For modular inversion, additional clean ancillas reduce the number of multiplication and addition operations while retaining the same asymptotic gate costs. Since these operations act on registers of different sizes, their count alone does not determine the total gate cost. These choices should therefore be assessed in the context of a full algorithm and its available workspace.
Several future directions and open questions remain. An important next step would be to try and improve the depth bounds for multiplication by a quantum integer while retaining the gate and ancilla bounds obtained here. Another direction is to determine whether our modulo powers of two constructions can be extended to arbitrary moduli while retaining the same resource costs. Together with explicit finite-size resource estimates, these developments would clarify the range of applications for the methods presented in this work.
Acknowledgements
We thank Alex Cojocaru for helpful discussions and feedback on this work. D. H and P. W. were supported by the Ofgem Strategic Innovation Fund (SIF), delivered in partnership with Innovate UK (UKRI), under the ‘Network Security in a Quantum Future (NSiaQF)’ project (Grant Reference: 10166255) and by the EPSRC-funded ‘Quantum Advantage Pathfinder (QAP)’ programme (Grant Reference: EP/X026167/1). A. S. was supported by the Engineering and Physical Sciences Research Council (EPSRC) doctoral studentship (Grant Reference: EP/W524384/1). P. W. was also supported by the EPSRC-funded ‘Quantum Advantage Turbocharger (QATCH)’ programme (Grant Reference: UKRI14257) and the QCi3 Hub (Grant Reference: EP/Z53318X/1).
We used GPT-6 Astra and GPT-6.1 Sol to search the literature, proofread earlier drafts, and suggest minor textual revisions. We also used it to suggest proof techniques for Lemmas 2.1 and 5.3 . The authors reviewed and revised the AI-assisted material and take full responsibility for the content of this work.
Integer multiplication in the phase
In this section, we review some of the main results from Kahanamoku-Meyer & Yao [20 ] . We introduce new notation for expressing these results, notation which we reuse in the subsequent three lemmas.
In Lemma Lemma 2.1 , we show how to perform phase-encoded integer multiplication between two equal-size registers in linear depth. In Lemmas Lemmas 2.2 and 2.3 , we show how to perform phase-encoded integer multiplication between differently sized registers, first considering two registers and then three. We give algorithms that are no more expensive than the equal-size case.
To recap, P t , m , n ( ϕ , α ) \mathrm{P}_{t,m,n}(\phi,\alpha) P t , m , n ( ϕ , α ) and P ‾ t , m , n ( ϕ , α ) \overline{\mathrm{P}}_{t,m,n}(\phi,\alpha) P t , m , n ( ϕ , α ) are defined as follows. Let x ∈ Z 2 t x\in\mathbb{Z}_{2^{t}} x ∈ Z 2 t , y ∈ Z 2 m y\in\mathbb{Z}_{2^{m}} y ∈ Z 2 m , z ∈ Z 2 n z\in\mathbb{Z}_{2^{n}} z ∈ Z 2 n , ϕ ∈ Q \phi\in\mathbb{Q} ϕ ∈ Q and α ∈ { 0 , 1 } \alpha\in\{0,1\} α ∈ { 0 , 1 } . Then
P t , m , n ( ϕ , α ) : ∣ x ⟩ t ∣ y ⟩ m ∣ z ⟩ n ⟼ e 2 π i ϕ y z x α ∣ x ⟩ t ∣ y ⟩ m ∣ z ⟩ n , (66) \mathrm{P}_{t,m,n}(\phi,\alpha):|x\rangle_t|y\rangle_m|z\rangle_n\longmapsto e^{2\pi i\phi yzx^{\alpha}}|x\rangle_t|y\rangle_m|z\rangle_n, \tag*{(66)} P t , m , n ( ϕ , α ) : ∣ x ⟩ t ∣ y ⟩ m ∣ z ⟩ n ⟼ e 2 π i ϕ y z x α ∣ x ⟩ t ∣ y ⟩ m ∣ z ⟩ n , (66)
P ‾ t , m , n ( ϕ , α ) : ∣ x ⟩ t ∣ y ⟩ m ∣ z ⟩ n ⟼ e 2 π i ϕ y z x α ∣ x ⟩ t ∣ y ⟩ m ∣ z ⟩ n . (67) \overline{\mathrm{P}}_{t,m,n}(\phi,\alpha):|x\rangle_t|y\rangle_m|z\rangle_n\longmapsto e^{2\pi i\phi yzx^{\alpha}}|x\rangle_t|y\rangle_m|z\rangle_n. \tag*{(67)} P t , m , n ( ϕ , α ) : ∣ x ⟩ t ∣ y ⟩ m ∣ z ⟩ n ⟼ e 2 π i ϕ y z x α ∣ x ⟩ t ∣ y ⟩ m ∣ z ⟩ n . (67)
It was shown in [20 ] that, for any integer k ≥ 2 k\ge2 k ≥ 2 of size O ( 1 ) O(1) O ( 1 ) with respect to n n n , the operation P 0 , n , n ( ϕ ) \mathrm{P}_{0,n,n}(\phi) P 0 , n , n ( ϕ ) requires O ( n log k ( 2 k − 1 ) ) O(n^{\log_k(2k-1)}) O ( n l o g k ( 2 k − 1 ) ) gates and no ancilla qubits, and the operation P n , n , n ( ϕ , 1 ) \mathrm{P}_{n,n,n}(\phi,1) P n , n , n ( ϕ , 1 ) requires O ( n log k ( 3 k − 2 ) ) O(n^{\log_k(3k-2)}) O ( n l o g k ( 3 k − 2 ) ) gates and no ancilla qubits. As a result, for any ϵ > 0 \epsilon>0 ϵ > 0 , we can choose integers k , k ′ k,k' k , k ′ that suppress the asymptotic gate count of both operations to O ( n 1 + ϵ ) O(n^{1+\epsilon}) O ( n 1 + ϵ ) .
The algorithms rely on a recursive decomposition of integer multiplication, based on the Toom–Cook method, which we present now.
Fast integer multiplication. Let A ( ℓ , g ) A(\ell,g) A ( ℓ , g ) be a ℓ × k \ell\times k ℓ × k matrix with entries
A ( ℓ , g ) i , j = { w i k − 1 − j if i < g v i j if i ≥ g , (68) A(\ell,g)_{i,j}=
\begin{cases}
w_i^{k-1-j} & \text{if } i<g\\
v_i^j & \text{if } i\ge g,
\end{cases} \tag*{(68)} A ( ℓ , g ) i , j = { w i k − 1 − j v i j if i < g if i ≥ g , (68)
for integers w 0 , … , w g − 1 , v g , … , v ℓ − 1 w_0,\ldots,w_{g-1},v_g,\ldots,v_{\ell-1} w 0 , … , w g − 1 , v g , … , v ℓ − 1 of size O ( 1 ) O(1) O ( 1 ) with respect to n n n . In other words, A ( ℓ , g ) A(\ell,g) A ( ℓ , g ) is a block matrix formed by vertically stacking two Vandermonde matrices, with the upper block ordered by decreasing powers. No two rows of the matrix are allowed to be the same. Additionally, if w i 1 = − 1 w_{i_1}=-1 w i 1 = − 1 for some i 1 i_1 i 1 , v i 2 ≠ − 1 v_{i_2}\ne-1 v i 2 = − 1 for all i 2 i_2 i 2 . For the remainder of this recap, we ignore the dependence of this matrix on g g g , since the results hold for any choice of g g g . However, for the subsequent three lemmas, our choice of g g g becomes relevant.
Consider the function
f k , d : Z 2 d ⟶ Z 2 ⌊ d / k ⌋ + d m o d k × ( Z 2 ⌊ d / k ⌋ ) k − 1 (69) f_{k,d}:\mathbb{Z}_{2^d}\longrightarrow\mathbb{Z}_{2^{\lfloor d/k\rfloor+d\bmod k}}\times\left(\mathbb{Z}_{2^{\lfloor d/k\rfloor}}\right)^{k-1} \tag*{(69)} f k , d : Z 2 d ⟶ Z 2 ⌊ d / k ⌋ + d mod k × ( Z 2 ⌊ d / k ⌋ ) k − 1 (69)
mapping integers c ∈ Z 2 d c\in\mathbb{Z}_{2^d} c ∈ Z 2 d to vectors ( c k − 1 , … , c 0 ) (c_{k-1},\ldots,c_0) ( c k − 1 , … , c 0 ) where c = ∑ j = 0 k − 1 ( 2 ⌊ d / k ⌋ ) c j c=\sum_{j=0}^{k-1}(2^{\lfloor d/k\rfloor})c_j c = ∑ j = 0 k − 1 ( 2 ⌊ d / k ⌋ ) c j . We define the operation
A ℓ , i ( d ) ( c ) = ( A ( ℓ ) f k , d ( c ) ) i , (70) \mathcal{A}^{(d)}_{\ell,i}(c)=\left(A(\ell)f_{k,d}(c)\right)_i, \tag*{(70)} A ℓ , i ( d ) ( c ) = ( A ( ℓ ) f k , d ( c ) ) i , (70)
which on input c ∈ Z 2 d c\in\mathbb{Z}_{2^d} c ∈ Z 2 d returns the i th i^{\text{th}} i th entry of the matrix-vector product A ( ℓ ) f k , d ( c ) A(\ell)f_{k,d}(c) A ( ℓ ) f k , d ( c ) . We will often write A ℓ , i ( c ) A_{\ell,i}(c) A ℓ , i ( c ) when the size of the register the input sits within is clear. Let ℓ 1 = k , ℓ 2 = 2 k − 1 , ℓ 3 = 3 k − 2 \ell_1=k,\ell_2=2k-1,\ell_3=3k-2 ℓ 1 = k , ℓ 2 = 2 k − 1 , ℓ 3 = 3 k − 2 . It was observed in [20 ] that for ϕ ∈ Q \phi\in\mathbb{Q} ϕ ∈ Q , and x , y , z ∈ Z 2 n x,y,z\in\mathbb{Z}_{2^n} x , y , z ∈ Z 2 n ,
ϕ z = ∑ i = 0 ℓ 1 − 1 ϕ ℓ 1 , i ( n ) ⋅ A ℓ 1 , i ( n ) ( z ) , ϕ y z = ∑ i = 0 ℓ 2 − 1 ϕ ℓ 2 , i ( n ) ⋅ A ℓ 2 , i ( n ) ( y ) ⋅ A ℓ 2 , i ( n ) ( z ) , \phi z=\sum_{i=0}^{\ell_1-1}\phi_{\ell_1,i}^{(n)}\cdot A_{\ell_1,i}^{(n)}(z),\qquad
\phi yz=\sum_{i=0}^{\ell_2-1}\phi_{\ell_2,i}^{(n)}\cdot A_{\ell_2,i}^{(n)}(y)\cdot A_{\ell_2,i}^{(n)}(z), ϕ z = i = 0 ∑ ℓ 1 − 1 ϕ ℓ 1 , i ( n ) ⋅ A ℓ 1 , i ( n ) ( z ) , ϕ y z = i = 0 ∑ ℓ 2 − 1 ϕ ℓ 2 , i ( n ) ⋅ A ℓ 2 , i ( n ) ( y ) ⋅ A ℓ 2 , i ( n ) ( z ) ,
ϕ x y z = ∑ i = 0 ℓ 3 − 1 ϕ ℓ 3 , i ( n ) ⋅ A ℓ 3 , i ( n ) ( x ) ⋅ A ℓ 3 , i ( n ) ( y ) ⋅ A ℓ 3 , i ( n ) ( z ) . (71) \phi xyz=\sum_{i=0}^{\ell_3-1}\phi_{\ell_3,i}^{(n)}\cdot A_{\ell_3,i}^{(n)}(x)\cdot A_{\ell_3,i}^{(n)}(y)\cdot A_{\ell_3,i}^{(n)}(z). \tag*{(71)} ϕ x y z = i = 0 ∑ ℓ 3 − 1 ϕ ℓ 3 , i ( n ) ⋅ A ℓ 3 , i ( n ) ( x ) ⋅ A ℓ 3 , i ( n ) ( y ) ⋅ A ℓ 3 , i ( n ) ( z ) . (71)
where the rational numbers φ ℓ 1 , i ( n ) , φ ℓ 2 , i ( n ) , φ ℓ 3 , i ( n ) \varphi_{\ell_{1},i}^{(n)}, \varphi_{\ell_{2},i}^{(n)}, \varphi_{\ell_{3},i}^{(n)} φ ℓ 1 , i ( n ) , φ ℓ 2 , i ( n ) , φ ℓ 3 , i ( n ) depend only on φ \varphi φ , the initial register width n n n , and the matrices A ( ℓ 1 ) , A ( ℓ 2 ) , A ( ℓ 3 ) A(\ell_{1}), A(\ell_{2}), A(\ell_{3}) A ( ℓ 1 ) , A ( ℓ 2 ) , A ( ℓ 3 ) respectively. Crucially, this makes them classically pre-computable. For each of the equations, the terms on the right-hand side are of the same form as the initial multiplication on the left-hand side, instead involving integers of size at most 2 ⌊ n / k ⌋ + O ( 1 ) 2^{\lfloor n/k\rfloor+O(1)} 2 ⌊ n / k ⌋ + O ( 1 ) . Therefore, we can apply each decomposition recursively. Applying the recursion to depth O ( log k n ) O(\log_k n) O ( log k n ) gives integers of size O ( 1 ) O(1) O ( 1 ) .
Recursive algorithms. These observations reveal recursive algorithms for P 0 , n , n ( φ ) P_{0,n,n}(\varphi) P 0 , n , n ( φ ) and P n , n , n ( φ , 1 ) P_{n,n,n}(\varphi,1) P n , n , n ( φ , 1 ) . Suppose for all i ∈ Z ℓ 2 i\in\mathbb{Z}_{\ell_2} i ∈ Z ℓ 2 , the integers A ℓ 2 , i ( n ) ( y ) , A ℓ 2 , i ( n ) ( z ) < 2 d 2 \mathcal{A}_{\ell_2,i}^{(n)}(y),\mathcal{A}_{\ell_2,i}^{(n)}(z)<2^{d_2} A ℓ 2 , i ( n ) ( y ) , A ℓ 2 , i ( n ) ( z ) < 2 d 2 and for all i ∈ Z ℓ 3 i\in\mathbb{Z}_{\ell_3} i ∈ Z ℓ 3 the integers A ℓ 3 , i ( n ) ( x ) , A ℓ 3 , i ( n ) ( y ) , A ℓ 3 , i ( n ) ( z ) < 2 d 3 \mathcal{A}_{\ell_3,i}^{(n)}(x),\mathcal{A}_{\ell_3,i}^{(n)}(y),\mathcal{A}_{\ell_3,i}^{(n)}(z)<2^{d_3} A ℓ 3 , i ( n ) ( x ) , A ℓ 3 , i ( n ) ( y ) , A ℓ 3 , i ( n ) ( z ) < 2 d 3 . Then, to perform P 0 , n , n ( φ ) P_{0,n,n}(\varphi) P 0 , n , n ( φ ) we simply apply P 0 , d 2 , d 2 ( φ ℓ 2 , i ( n ) ) P_{0,d_2,d_2}(\varphi_{\ell_2,i}^{(n)}) P 0 , d 2 , d 2 ( φ ℓ 2 , i ( n ) ) to ∣ A ℓ 2 , i ( n ) ( y ) ⟩ d 2 ∣ A ℓ 2 , i ( n ) ( z ) ⟩ d 2 \left|\mathcal{A}_{\ell_2,i}^{(n)}(y)\right\rangle_{d_2}\left|\mathcal{A}_{\ell_2,i}^{(n)}(z)\right\rangle_{d_2} A ℓ 2 , i ( n ) ( y ) ⟩ d 2 A ℓ 2 , i ( n ) ( z ) ⟩ d 2 for all i ∈ Z ℓ 2 i\in\mathbb{Z}_{\ell_2} i ∈ Z ℓ 2 , and to perform P n , n , n ( φ , 1 ) P_{n,n,n}(\varphi,1) P n , n , n ( φ , 1 ) we apply P d 3 , d 3 , d 3 ( φ ℓ 3 , i ( n ) , 1 ) P_{d_3,d_3,d_3}(\varphi_{\ell_3,i}^{(n)},1) P d 3 , d 3 , d 3 ( φ ℓ 3 , i ( n ) , 1 ) to ∣ A ℓ 3 , i ( n ) ( x ) ⟩ d 3 ∣ A ℓ 3 , i ( n ) ( y ) ⟩ d 3 ∣ A ℓ 3 , i ( n ) ( z ) ⟩ d 3 \left|\mathcal{A}_{\ell_3,i}^{(n)}(x)\right\rangle_{d_3}\left|\mathcal{A}_{\ell_3,i}^{(n)}(y)\right\rangle_{d_3}\left|\mathcal{A}_{\ell_3,i}^{(n)}(z)\right\rangle_{d_3} A ℓ 3 , i ( n ) ( x ) ⟩ d 3 A ℓ 3 , i ( n ) ( y ) ⟩ d 3 A ℓ 3 , i ( n ) ( z ) ⟩ d 3 for all i ∈ Z ℓ 3 i\in\mathbb{Z}_{\ell_3} i ∈ Z ℓ 3 . The operations P 0 , d 2 , d 2 ( φ ℓ 2 , i ( n ) ) P_{0,d_2,d_2}(\varphi_{\ell_2,i}^{(n)}) P 0 , d 2 , d 2 ( φ ℓ 2 , i ( n ) ) and P d 3 , d 3 , d 3 ( φ ℓ 3 , i ( n ) , 1 ) P_{d_3,d_3,d_3}(\varphi_{\ell_3,i}^{(n)},1) P d 3 , d 3 , d 3 ( φ ℓ 3 , i ( n ) , 1 ) can be implemented in the same way, using A ℓ 2 , i ( d 2 ) ( ⋅ ) \mathcal{A}_{\ell_2,i}^{(d_2)}(\cdot) A ℓ 2 , i ( d 2 ) ( ⋅ ) and A ℓ 3 , i ( d 3 ) ( ⋅ ) \mathcal{A}_{\ell_3,i}^{(d_3)}(\cdot) A ℓ 3 , i ( d 3 ) ( ⋅ ) with newly defined rationals.
This reduces the task of performing P 0 , n , n ( φ ) P_{0,n,n}(\varphi) P 0 , n , n ( φ ) and P n , n , n ( φ , 1 ) P_{n,n,n}(\varphi,1) P n , n , n ( φ , 1 ) to the task of computing linear combinations of quantum registers, according to the definition of A ℓ , i ( n ) ( ⋅ ) \mathcal{A}_{\ell,i}^{(n)}(\cdot) A ℓ , i ( n ) ( ⋅ ) . Since every row of A ( ℓ ) A(\ell) A ( ℓ ) includes at least a single 1 1 1 , there is always one integer in a linear combination defined by A ℓ , i ( n ) ( ⋅ ) \mathcal{A}_{\ell,i}^{(n)}(\cdot) A ℓ , i ( n ) ( ⋅ ) that does not need to be scaled. Therefore, we can build A ℓ , i ( n ) ( z ) \mathcal{A}_{\ell,i}^{(n)}(z) A ℓ , i ( n ) ( z ) on the register containing this integer extended with O ( 1 ) O(1) O ( 1 ) clean ancilla qubits, using only out-of-place scaled additions. The ancilla qubit count is clear since we know that d 2 , d 3 = ⌊ n / k ⌋ + O ( 1 ) d_2,d_3=\lfloor n/k\rfloor+O(1) d 2 , d 3 = ⌊ n / k ⌋ + O ( 1 ) . An addition scaled by λ \lambda λ can be performed with at most log ( λ ) + 1 \log(\lambda)+1 log ( λ ) + 1 shifted additions. The cost of this sequence of scaled additions on ∣ z ⟩ n \lvert z\rangle_n ∣ z ⟩ n , for scalars of size O ( 1 ) O(1) O ( 1 ) with respect to n n n , is O ( n ) O(n) O ( n ) [36 ] . Nothing changes at the lower levels of recursion.
Putting all this together, the algorithm for P 0 , n , n ( φ ) P_{0,n,n}(\varphi) P 0 , n , n ( φ ) starts by partitioning ∣ y ⟩ n \lvert y\rangle_n ∣ y ⟩ n and ∣ z ⟩ n \lvert z\rangle_n ∣ z ⟩ n into k k k subregisters according to the definition of f k , n f_{k,n} f k , n . Then it prepares a linear combination of the y y y subregisters and the z z z subregisters according to A ℓ 2 , i ( n ) ( ⋅ ) \mathcal{A}_{\ell_2,i}^{(n)}(\cdot) A ℓ 2 , i ( n ) ( ⋅ ) , applies P 0 , d 2 , d 2 ( φ ℓ 2 , i ( n ) ) P_{0,d_2,d_2}(\varphi_{\ell_2,i}^{(n)}) P 0 , d 2 , d 2 ( φ ℓ 2 , i ( n ) ) to the linear combination pair, and finally reverses the operations returning to the original ∣ y ⟩ n ∣ z ⟩ n \lvert y\rangle_n\lvert z\rangle_n ∣ y ⟩ n ∣ z ⟩ n . It does this sequentially for each i ∈ Z ℓ 2 i\in\mathbb{Z}_{\ell_2} i ∈ Z ℓ 2 . The algorithm for P n , n , n ( φ , 1 ) P_{n,n,n}(\varphi,1) P n , n , n ( φ , 1 ) is the same, just involving three linear combinations, each sitting on d 3 d_3 d 3 qubits instead. Both algorithms are applied recursively to solve the subproblems, requiring O ( 1 ) O(1) O ( 1 ) ancilla qubits at every level of recursion to store each linear combination in a register.
Avoiding ancillas. However, notice that these algorithms do not actually need to store a linear combination of the form A ℓ , i ( n ) ( z ) \mathcal{A}_{\ell,i}^{(n)}(z) A ℓ , i ( n ) ( z ) in the basis. They just require that the product associated with A ℓ , i ( n ) ( z ) \mathcal{A}_{\ell,i}^{(n)}(z) A ℓ , i ( n ) ( z ) reaches the phase. Exploiting this fact, it is shown in Appendix § 10 of [20 ] how to get away with only storing A ℓ , i ( n ) ( z ) m o d 2 ⌊ n / k ⌋ \mathcal{A}_{\ell,i}^{(n)}(z)\bmod2^{\lfloor n/k\rfloor} A ℓ , i ( n ) ( z ) mod 2 ⌊ n / k ⌋ , thereby avoiding the requirement for ancillas at each level of recursion. We review this argument now since we will make a similar one in the proof of Lemma 2.3 .
Suppose our goal is to push the product ψ A ℓ , i ( y ) A ℓ , i ( z ) \psi\mathcal{A}_{\ell,i}(y)\mathcal{A}_{\ell,i}(z) ψ A ℓ , i ( y ) A ℓ , i ( z ) into the phase of our quantum state, for A ℓ , i ( y ) , A ℓ , i ( z ) ∈ Z 2 d \mathcal{A}_{\ell,i}(y),\mathcal{A}_{\ell,i}(z)\in\mathbb{Z}_{2^d} A ℓ , i ( y ) , A ℓ , i ( z ) ∈ Z 2 d with d = ⌊ n / k ⌋ + O ( 1 ) d=\lfloor n/k\rfloor+O(1) d = ⌊ n / k ⌋ + O ( 1 ) . Instead of applying P 0 , d , d ( ψ ) P_{0,d,d}(\psi) P 0 , d , d ( ψ ) to ∣ A ℓ , i ( y ) ⟩ d ∣ A ℓ , i ( z ) ⟩ d \lvert\mathcal{A}_{\ell,i}(y)\rangle_d\lvert\mathcal{A}_{\ell,i}(z)\rangle_d ∣ A ℓ , i ( y ) ⟩ d ∣ A ℓ , i ( z ) ⟩ d , we can do the following. Consider ( A ℓ , i ( y ) ) h i , ( A ℓ , i ( z ) ) h i ∈ Z 2 d − ⌊ n / k ⌋ (\mathcal{A}_{\ell,i}(y))_{\mathrm{hi}},(\mathcal{A}_{\ell,i}(z))_{\mathrm{hi}}\in\mathbb{Z}_{2^{d-\lfloor n/k\rfloor}} ( A ℓ , i ( y ) ) hi , ( A ℓ , i ( z ) ) hi ∈ Z 2 d − ⌊ n / k ⌋ , and ( A ℓ , i ( y ) ) l o , ( A ℓ , i ( z ) ) l o ∈ Z 2 ⌊ n / k ⌋ (\mathcal{A}_{\ell,i}(y))_{\mathrm{lo}},(\mathcal{A}_{\ell,i}(z))_{\mathrm{lo}}\in\mathbb{Z}_{2^{\lfloor n/k\rfloor}} ( A ℓ , i ( y ) ) lo , ( A ℓ , i ( z ) ) lo ∈ Z 2 ⌊ n / k ⌋ , such that
A ℓ , i ( y ) = 2 ⌊ n / k ⌋ ( A ℓ , i ( y ) ) h i + ( A ℓ , i ( y ) ) l o , (72) \mathcal{A}_{\ell,i}(y)=2^{\lfloor n/k\rfloor}(\mathcal{A}_{\ell,i}(y))_{\mathrm{hi}}+(\mathcal{A}_{\ell,i}(y))_{\mathrm{lo}}, \tag*{(72)} A ℓ , i ( y ) = 2 ⌊ n / k ⌋ ( A ℓ , i ( y ) ) hi + ( A ℓ , i ( y ) ) lo , (72)
A ℓ , i ( z ) = 2 ⌊ n / k ⌋ ( A ℓ , i ( z ) ) h i + ( A ℓ , i ( z ) ) l o . (73) \mathcal{A}_{\ell,i}(z)=2^{\lfloor n/k\rfloor}(\mathcal{A}_{\ell,i}(z))_{\mathrm{hi}}+(\mathcal{A}_{\ell,i}(z))_{\mathrm{lo}}. \tag*{(73)} A ℓ , i ( z ) = 2 ⌊ n / k ⌋ ( A ℓ , i ( z ) ) hi + ( A ℓ , i ( z ) ) lo . (73)
The product can therefore be decomposed as ψ A ℓ , i ( y ) A ℓ , i ( z ) = \psi\mathcal{A}_{\ell,i}(y)\mathcal{A}_{\ell,i}(z)= ψ A ℓ , i ( y ) A ℓ , i ( z ) =
= ψ 2 ⌊ n / k ⌋ ( A ℓ , i ( y ) ) h i A ℓ , i ( z ) + ψ 2 ⌊ n / k ⌋ ( A ℓ , i ( y ) ) l o ( A ℓ , i ( z ) ) h i + ψ ( A ℓ , i ( y ) ) l o ( A ℓ , i ( z ) ) l o . (74) =\psi2^{\lfloor n/k\rfloor}(\mathcal{A}_{\ell,i}(y))_{\mathrm{hi}}\mathcal{A}_{\ell,i}(z)+\psi2^{\lfloor n/k\rfloor}(\mathcal{A}_{\ell,i}(y))_{\mathrm{lo}}(\mathcal{A}_{\ell,i}(z))_{\mathrm{hi}}+\psi(\mathcal{A}_{\ell,i}(y))_{\mathrm{lo}}(\mathcal{A}_{\ell,i}(z))_{\mathrm{lo}}. \tag*{(74)} = ψ 2 ⌊ n / k ⌋ ( A ℓ , i ( y ) ) hi A ℓ , i ( z ) + ψ 2 ⌊ n / k ⌋ ( A ℓ , i ( y ) ) lo ( A ℓ , i ( z ) ) hi + ψ ( A ℓ , i ( y ) ) lo ( A ℓ , i ( z ) ) lo . (74)
We show how to push each term in Equation A.9 to the phase, effectively pushing the desired product.
First, we deal with the term ψ 2 ⌊ n / k ⌋ ( A ℓ , i ( y ) ) h i A ℓ , i ( z ) \psi2^{\lfloor n/k\rfloor}(\mathcal{A}_{\ell,i}(y))_{\mathrm{hi}}\mathcal{A}_{\ell,i}(z) ψ 2 ⌊ n / k ⌋ ( A ℓ , i ( y ) ) hi A ℓ , i ( z ) . The value ( A ℓ , i ( y ) ) h i (\mathcal{A}_{\ell,i}(y))_{\mathrm{hi}} ( A ℓ , i ( y ) ) hi represents the overflow that occurs when we replace each scaled addition used in the preparation of A ℓ , i ( y ) \mathcal{A}_{\ell,i}(y) A ℓ , i ( y ) with a scaled modular addition, modulo 2 ⌊ n / k ⌋ 2^{\lfloor n/k\rfloor} 2 ⌊ n / k ⌋ . We focus on a single scaled addition of the form y 0 ↦ y 0 + 2 λ j y 1 y_0 \mapsto y_0+2^{\lambda_j}y_1 y 0 ↦ y 0 + 2 λ j y 1 since each scaled addition of the form y 0 ↦ y 0 + λ y 1 y_0 \mapsto y_0+\lambda y_1 y 0 ↦ y 0 + λ y 1 can be implemented with at most log ( λ ) + 1 = O ( 1 ) \log(\lambda)+1=O(1) log ( λ ) + 1 = O ( 1 ) additions or subtractions of this form. The overflow contribution of this addition can be placed in two categories: overflow from reducing 2 λ j y 1 2^{\lambda_j}y_1 2 λ j y 1 modulo 2 ⌊ n / k ⌋ 2^{\lfloor n/k\rfloor} 2 ⌊ n / k ⌋ and overflow from reducing y 0 + ( 2 λ j y 1 m o d 2 ⌊ n / k ⌋ ) y_0+(2^{\lambda_j}y_1\bmod2^{\lfloor n/k\rfloor}) y 0 + ( 2 λ j y 1 mod 2 ⌊ n / k ⌋ ) modulo 2 ⌊ n / k ⌋ 2^{\lfloor n/k\rfloor} 2 ⌊ n / k ⌋ . This first type of overflow is determined by the shift 2 λ j 2^{\lambda_j} 2 λ j and the most significant bits of y 1 y_1 y 1 . The second type of overflow is the final carry in an addition between two ⌊ n / k ⌋ \lfloor n/k\rfloor ⌊ n / k ⌋ -bit numbers. In the ripple carry addition circuit of [36 ] , this final carry is actually stored at the halfway point of the circuit.1 These facts mean that, using modular additions instead of plain additions does not mean we lose track of the overflow; all of the bits determining this overflow are stored in the basis at some stage regardless, they just may not be stored when the modular addition is complete. Instead of using new ancillas, we can simply control on the relevant bits before (for the first type of overflow) or during (for the second type of overflow) each modular addition, and apply phase rotations to the z z z -register. The angles used for these rotations are determined exclusively by A ℓ , i ( ⋅ ) \mathcal{A}_{\ell,i}(\cdot) A ℓ , i ( ⋅ ) and ψ \psi ψ , thus are classically pre-computable. Since each addition can only have O ( 1 ) O(1) O ( 1 ) overflow bits, we only require O ( n ) O(n) O ( n ) controlled phase rotations (for the n n n qubits of the z z z register). Letting y = ∑ j = 0 k − 1 ( 2 ⌊ n / k ⌋ ) j y j y=\sum_{j=0}^{k-1}(2^{\lfloor n/k\rfloor})^j y_j y = ∑ j = 0 k − 1 ( 2 ⌊ n / k ⌋ ) j y j and assuming that we build the desired linear combination on ∣ y 0 ⟩ ⌊ n / k ⌋ |y_0\rangle_{\lfloor n/k\rfloor} ∣ y 0 ⟩ ⌊ n / k ⌋ , this proves that it is possible to map
∣ y ⟩ n ∣ z ⟩ n ↦ e 2 π i ψ 2 ⌊ n / k ⌋ ( A ℓ , i ( y ) ) h i A ℓ , i ( z ) ∣ y k − 1 ⋯ y 1 ⟩ n − ⌊ n / k ⌋ ∣ ( A ℓ , i ( y ) ) l o ⟩ ⌊ n / k ⌋ ∣ z ⟩ n , (75) |y\rangle_n|z\rangle_n\mapsto e^{2\pi i\psi2^{\lfloor n/k\rfloor}(\mathcal{A}_{\ell,i}(y))_{\mathrm{hi}}\mathcal{A}_{\ell,i}(z)}|y_{k-1}\cdots y_1\rangle_{n-\lfloor n/k\rfloor}|(\mathcal{A}_{\ell,i}(y))_{\mathrm{lo}}\rangle_{\lfloor n/k\rfloor}|z\rangle_n, \tag*{(75)} ∣ y ⟩ n ∣ z ⟩ n ↦ e 2 π i ψ 2 ⌊ n / k ⌋ ( A ℓ , i ( y ) ) hi A ℓ , i ( z ) ∣ y k − 1 ⋯ y 1 ⟩ n − ⌊ n / k ⌋ ∣ ( A ℓ , i ( y ) ) lo ⟩ ⌊ n / k ⌋ ∣ z ⟩ n , (75)
using O ( n ) O(n) O ( n ) gates and no ancillas. We note, since it will be relevant in Lemma 2.3 , that clearly if z z z is on a m m m qubit register, we require O ( m ) O(m) O ( m ) phase rotations to avoid using ancillas and O ( n ) O(n) O ( n ) gates for the sequence of modular additions.
By all of the same reasoning as above, the sequence of additions for A ℓ , i ( z ) \mathcal{A}_{\ell,i}(z) A ℓ , i ( z ) can be replaced with modular additions modulo 2 ⌊ n / k ⌋ 2^{\lfloor n/k\rfloor} 2 ⌊ n / k ⌋ and the overflow ( A ℓ , i ( z ) ) h i (\mathcal{A}_{\ell,i}(z))_{\mathrm{hi}} ( A ℓ , i ( z ) ) hi controlled on. Thus, to push the second term in Equation A.9 to the phase we require O ( n / k ) O(n/k) O ( n / k ) controlled phase rotations and O ( n ) O(n) O ( n ) gates for the sequence of modular additions. This leaves ∣ ( A ℓ , i ( y ) ) l o ⟩ ⌊ n / k ⌋ |(\mathcal{A}_{\ell,i}(y))_{\mathrm{lo}}\rangle_{\lfloor n/k\rfloor} ∣ ( A ℓ , i ( y ) ) lo ⟩ ⌊ n / k ⌋ on one of the k k k subregisters of ∣ y ⟩ n |y\rangle_n ∣ y ⟩ n and ∣ ( A ℓ , i ( z ) ) l o ⟩ ⌊ n / k ⌋ |(\mathcal{A}_{\ell,i}(z))_{\mathrm{lo}}\rangle_{\lfloor n/k\rfloor} ∣ ( A ℓ , i ( z ) ) lo ⟩ ⌊ n / k ⌋ on one of the k k k subregisters of ∣ z ⟩ n |z\rangle_n ∣ z ⟩ n . Applying P 0 , ⌊ n / k ⌋ , ⌊ n / k ⌋ ( ψ ) \mathrm{P}_{0,\lfloor n/k\rfloor,\lfloor n/k\rfloor}(\psi) P 0 , ⌊ n / k ⌋ , ⌊ n / k ⌋ ( ψ ) deals with the third term in Equation A.9. This proves that P 0 , n , n ( ϕ ) \mathrm{P}_{0,n,n}(\phi) P 0 , n , n ( ϕ ) can be implemented with modular additions and careful accounting of overflow, requiring O ( n ) O(n) O ( n ) gates, and with P 0 , ⌊ n / k ⌋ , ⌊ n / k ⌋ ( ϕ L 2 , i ( n ) ) \mathrm{P}_{0,\lfloor n/k\rfloor,\lfloor n/k\rfloor}(\phi_{L_{2,i}}^{(n)}) P 0 , ⌊ n / k ⌋ , ⌊ n / k ⌋ ( ϕ L 2 , i ( n ) ) for i ∈ Z ℓ 2 i\in\mathbb{Z}_{\ell_2} i ∈ Z ℓ 2 . By the master theorem for divide and conquer algorithms, we require O ( n log k ( 2 k − 1 ) ) O(n^{\log_k(2k-1)}) O ( n l o g k ( 2 k − 1 ) ) gates and no ancilla qubits.
For P n , n , n ( ϕ , 1 ) \mathrm{P}_{n,n,n}(\phi,1) P n , n , n ( ϕ , 1 ) , we can avoid ancillas by accounting for the overflow of modular additions using exactly the same techniques. The only difference is that, since the phase-encoded multiplication is between three registers, when we control on ( A ℓ , i ( y ) ) h i (\mathcal{A}_{\ell,i}(y))_{\mathrm{hi}} ( A ℓ , i ( y ) ) hi , we must apply an operation to ∣ x ⟩ n ∣ z ⟩ n |x\rangle_n|z\rangle_n ∣ x ⟩ n ∣ z ⟩ n that pushes the product ψ 2 ⌊ n / k ⌋ A ℓ , i ( x ) A ℓ , i ( z ) \psi2^{\lfloor n/k\rfloor}\mathcal{A}_{\ell,i}(x)\mathcal{A}_{\ell,i}(z) ψ 2 ⌊ n / k ⌋ A ℓ , i ( x ) A ℓ , i ( z ) to the phase instead. The desired operation is simply k 2 = O ( 1 ) k^2=O(1) k 2 = O ( 1 ) gates of the form P 0 , ⌊ n / k ⌋ , ⌊ n / k ⌋ ( ψ ′ ) \mathrm{P}_{0,\lfloor n/k\rfloor,\lfloor n/k\rfloor}(\psi') P 0 , ⌊ n / k ⌋ , ⌊ n / k ⌋ ( ψ ′ ) where ψ ′ \psi' ψ ′ is determined by ψ 2 ⌊ n / k ⌋ \psi2^{\lfloor n/k\rfloor} ψ 2 ⌊ n / k ⌋ and A ℓ , i ( ⋅ ) \mathcal{A}_{\ell,i}(\cdot) A ℓ , i ( ⋅ ) . Therefore, the cost of avoiding ancillas at the top-level of recursion is G ( P 0 , n , n ( ϕ ) ) G(\mathrm{P}_{0,n,n}(\phi)) G ( P 0 , n , n ( ϕ )) . It follows from the master theorem for divide and conquer algorithms that the total recursive algorithm requires O ( n log k ( 3 k − 2 ) ) O(n^{\log_k(3k-2)}) O ( n l o g k ( 3 k − 2 ) ) gates and no ancilla qubits.
Lemma conventions. We briefly mention some notational conventions we will use in the following three lemmas.
It is observed in [20 ] that using the larger register of ⌊ n / k ⌋ + ( n m o d k ) \lfloor n/k\rfloor+(n\bmod k) ⌊ n / k ⌋ + ( n mod k ) qubits for storing linear combinations can be advantageous. Despite this, to simplify our analysis, we will avoid making a register of this form entirely. Instead, at every level of recursion, after we partition a register according to f k , d f_{k,d} f k , d , we will push the product associated with the overhanging d m o d k d\bmod k d mod k qubits into the phase immediately, using the same technique as we used for overflow. By the above analysis, this does not affect the algorithm’s asymptotic cost, since k = O ( 1 ) k=O(1) k = O ( 1 ) . This allows us to make the simplifying assumption when performing phase-encoded multiplication between two equal-size registers, that the register size is a power of k k k .
We drop the modulus when writing a state of the form ∣ A ℓ , i ( z ) m o d 2 n / k ⟩ n / k |\mathcal{A}_{\ell,i}(z)\bmod2^{n/k}\rangle_{n/k} ∣ A ℓ , i ( z ) mod 2 n / k ⟩ n / k since it is understood that the integer A ℓ , i ( z ) \mathcal{A}_{\ell,i}(z) A ℓ , i ( z ) must be taken mod 2 n / k 2^{n/k} 2 n / k to sit on n / k n/k n / k qubits.
Since the parameters ϕ \phi ϕ have no impact on the cost of phase-encoded multiplication (in our circuit model; in practice, this is not the case) we omit them from statements involving G ( ⋅ ) G(\cdot) G ( ⋅ ) and D ( ⋅ ) D(\cdot) D ( ⋅ ) .
Lemma A.1 (Linear-depth phase multiplication). Let n > 0 n>0 n > 0 . For any ϵ > 0 \epsilon>0 ϵ > 0 , there exists a quantum circuit for P 0 , n , n ( ϕ ) \mathrm{P}_{0,n,n}(\phi) P 0 , n , n ( ϕ ) that uses O ( n 1 + ϵ ) O(n^{1+\epsilon}) O ( n 1 + ϵ ) gates, O ( n ) O(n) O ( n ) depth, and no ancilla qubits.
Proof. The gate and ancilla count claims follow from [20 ] . As described above, the algorithm for P 0 , n , n ( ϕ ) \mathrm{P}_{0,n,n}(\phi) P 0 , n , n ( ϕ ) involves sequences of out-of-place modular additions, computing integers of the form A ℓ 2 , i ( n ) ( z ) m o d 2 n / k \mathcal{A}_{\ell_2,i}^{(n)}(z)\bmod2^{n/k} A ℓ 2 , i ( n ) ( z ) mod 2 n / k . For i ∈ Z ℓ 2 i\in\mathbb{Z}_{\ell_2} i ∈ Z ℓ 2 , we compute (and later uncompute) each integer sequentially.
Here we will show that it is always possible to parallelise at least two of these computations for k ≥ 3 k\ge3 k ≥ 3 . This allows us to perform ⌊ ℓ 2 / 2 ⌋ \lfloor\ell_2/2\rfloor ⌊ ℓ 2 /2 ⌋ layers of modular addition sequences at each level of the recursion, instead of ℓ 2 \ell_2 ℓ 2 , implying the result for P 0 , n , n ( ϕ ) \mathrm{P}_{0,n,n}(\phi) P 0 , n , n ( ϕ ) .
Partition the input ∣ z ⟩ n |z\rangle_n ∣ z ⟩ n into k k k subregisters ∣ z k − 1 ⟩ n / k , … , ∣ z 0 ⟩ n / k |z_{k-1}\rangle_{n/k},\ldots,|z_0\rangle_{n/k} ∣ z k − 1 ⟩ n / k , … , ∣ z 0 ⟩ n / k according to f k , n f_{k,n} f k , n and label the register containing z j z_j z j as Z j Z_j Z j . In the definition of ( A ℓ 2 , g ) (\mathcal{A}_{\ell_2,g}) ( A ℓ 2 , g ) from Equation A.3, set g = ℓ 2 − 1 , w i = i g=\ell_2-1,w_i=i g = ℓ 2 − 1 , w i = i for all i = 0 , … , ℓ 2 − 2 i=0,\ldots,\ell_2-2 i = 0 , … , ℓ 2 − 2 and w ℓ 2 − 1 = 0 w_{\ell_2-1}=0 w ℓ 2 − 1 = 0 . Observe that this implies A ℓ 2 , 0 ( z ) = z 0 \mathcal{A}_{\ell_2,0}(z)=z_0 A ℓ 2 , 0 ( z ) = z 0 , A ℓ 2 , ℓ 2 − 1 ( z ) = z k − 1 \mathcal{A}_{\ell_2,\ell_2-1}(z)=z_{k-1} A ℓ 2 , ℓ 2 − 1 ( z ) = z k − 1 and A ℓ 2 , 1 ( z ) = ∑ j = 0 k − 1 z j \mathcal{A}_{\ell_2,1}(z)=\sum_{j=0}^{k-1}z_j A ℓ 2 , 1 ( z ) = ∑ j = 0 k − 1 z j . Thus, these three linear combinations can be computed simultaneously; simply do nothing to ∣ z 0 ⟩ n / k , ∣ z k − 1 ⟩ n / k |z_0\rangle_{n/k},|z_{k-1}\rangle_{n/k} ∣ z 0 ⟩ n / k , ∣ z k − 1 ⟩ n / k and pick any other register to build A ℓ 2 , 1 ( z ) m o d 2 n / k \mathcal{A}_{\ell_2,1}(z)\bmod2^{n/k} A ℓ 2 , 1 ( z ) mod 2 n / k on. Next, consider A ℓ 2 , i 1 ( z ) \mathcal{A}_{\ell_2,i_1}(z) A ℓ 2 , i 1 ( z ) and A ℓ 2 , i 2 ( z ) \mathcal{A}_{\ell_2,i_2}(z) A ℓ 2 , i 2 ( z ) for i 2 = i 1 + 1 < g i_2=i_1+1<g i 2 = i 1 + 1 < g . Then
A ℓ 2 , i 1 ( z ) = ∑ j = 0 k − 1 i 1 j z j , A ℓ 2 , i 2 ( z ) = ∑ j = 0 k − 1 ( i 1 + 1 ) j z j . (76) \mathcal{A}_{\ell_2,i_1}(z)=\sum_{j=0}^{k-1}i_1^jz_j,\qquad\mathcal{A}_{\ell_2,i_2}(z)=\sum_{j=0}^{k-1}(i_1+1)^jz_j. \tag*{(76)} A ℓ 2 , i 1 ( z ) = j = 0 ∑ k − 1 i 1 j z j , A ℓ 2 , i 2 ( z ) = j = 0 ∑ k − 1 ( i 1 + 1 ) j z j . (76)
We show how to prepare ∣ A ℓ 2 , i 1 ( z ) ⟩ n / k |\mathcal{A}_{\ell_2,i_1}(z)\rangle_{n/k} ∣ A ℓ 2 , i 1 ( z ) ⟩ n / k and ∣ A ℓ 2 , i 2 ( z ) ⟩ n / k |\mathcal{A}_{\ell_2,i_2}(z)\rangle_{n/k} ∣ A ℓ 2 , i 2 ( z ) ⟩ n / k simultaneously. Add i 1 Z 1 i_1Z_1 i 1 Z 1 to Z 0 Z_0 Z 0 . Then add Z 0 Z_0 Z 0 to Z 1 Z_1 Z 1 . This maps
∣ z 1 ⟩ n / k ∣ z 0 ⟩ n / k ⟼ ∣ ( i 1 + 1 ) z 1 + z 0 ⟩ n / k ∣ i 1 z 1 + z 0 ⟩ n / k . (77) \lvert z_1\rangle_{n/k}\lvert z_0\rangle_{n/k} \longmapsto\lvert(i_1+1)z_1+z_0\rangle_{n/k}\lvert i_1z_1+z_0\rangle_{n/k}. \tag*{(77)} ∣ z 1 ⟩ n / k ∣ z 0 ⟩ n / k ⟼ ∣( i 1 + 1 ) z 1 + z 0 ⟩ n / k ∣ i 1 z 1 + z 0 ⟩ n / k . (77)
We note that the overflow from i 1 z 1 + z 0 i_1z_1+z_0 i 1 z 1 + z 0 must be doubly accounted for at the time of this first addition, since it should be part of the overflow on both Z 0 Z_0 Z 0 and Z 1 Z_1 Z 1 . Then for j = 2 , … , k − 1 j=2,\ldots,k-1 j = 2 , … , k − 1 add ( i 1 ) j Z j (i_1)^jZ_j ( i 1 ) j Z j to Z 0 Z_0 Z 0 and add ( i 1 + 1 ) j Z j (i_1+1)^jZ_j ( i 1 + 1 ) j Z j to Z 1 Z_1 Z 1 . This prepares ∣ A ℓ 2 , i 1 ( z ) ⟩ n / k \lvert A_{\ell_2,i_1}(z)\rangle_{n/k} ∣ A ℓ 2 , i 1 ( z ) ⟩ n / k and ∣ A ℓ 2 , i 2 ( z ) ⟩ n / k \lvert A_{\ell_2,i_2}(z)\rangle_{n/k} ∣ A ℓ 2 , i 2 ( z ) ⟩ n / k on Z 0 Z_0 Z 0 and Z 1 Z_1 Z 1 with 2 k − 2 2k-2 2 k − 2 out-of-place scaled modular additions. By our choice of w 2 , … , w ℓ 2 − 2 w_2,\ldots,w_{\ell_2-2} w 2 , … , w ℓ 2 − 2 , this means all of the linear combinations A ℓ 2 , i ( z ) A_{\ell_2,i}(z) A ℓ 2 , i ( z ) for i = 2 , … , ℓ 2 − 2 i=2,\ldots,\ell_2-2 i = 2 , … , ℓ 2 − 2 can be paired up.
This gives an algorithm for P 0 , n , n ( ϕ ) \mathbf{P}_{0,n,n}(\phi) P 0 , n , n ( ϕ ) that has depth O ( n ) O(n) O ( n ) . On input ∣ y ⟩ n ∣ z ⟩ n \lvert y\rangle_n\lvert z\rangle_n ∣ y ⟩ n ∣ z ⟩ n , prepare ∣ A ℓ 2 , i ( y ) ⟩ n / k ∣ A ℓ 2 , i ( z ) ⟩ n / k \lvert A_{\ell_2,i}(y)\rangle_{n/k}\lvert A_{\ell_2,i}(z)\rangle_{n/k} ∣ A ℓ 2 , i ( y ) ⟩ n / k ∣ A ℓ 2 , i ( z ) ⟩ n / k for i = 0 , 1 , ℓ 2 − 1 i=0,1,\ell_2-1 i = 0 , 1 , ℓ 2 − 1 , preparing the y y y register first, then the z z z register. Then apply the gates P 0 , n / k , n / k ( ϕ ℓ 2 , i ( n ) ) \mathbf{P}_{0,n/k,n/k}(\phi_{\ell_2,i}^{(n)}) P 0 , n / k , n / k ( ϕ ℓ 2 , i ( n ) ) to all three pairs of linear combinations simultaneously. Then uncompute the y y y and z z z subregisters, and repeat for new indices i i i which can be paired together as follows: ( 2 , 3 ) , ( 4 , 5 ) , … , ( ℓ 2 − 3 , ℓ 2 − 2 ) (2,3),(4,5),\ldots,(\ell_2-3,\ell_2-2) ( 2 , 3 ) , ( 4 , 5 ) , … , ( ℓ 2 − 3 , ℓ 2 − 2 ) (since ℓ 2 \ell_2 ℓ 2 is odd). The new algorithm involves ( ℓ 2 − 3 ) / 2 + 1 = ⌊ ℓ 2 / 2 ⌋ (\ell_2-3)/2+1=\lfloor\ell_2/2\rfloor ( ℓ 2 − 3 ) /2 + 1 = ⌊ ℓ 2 /2 ⌋ layers of phase-encoded multiplication. Therefore,
D ( P 0 , n , n ) = ⌊ ( 2 k − 1 ) / 2 ⌋ D ( P 0 , n / k , n / k ) + O ( n ) = O ( n ) . (78) D(\mathbf{P}_{0,n,n})=\lfloor(2k-1)/2\rfloor D(\mathbf{P}_{0,n/k,n/k})+O(n)=O(n). \tag*{(78)} D ( P 0 , n , n ) = ⌊( 2 k − 1 ) /2 ⌋ D ( P 0 , n / k , n / k ) + O ( n ) = O ( n ) . (78)
Lemma A.2 (Phase multiplication with two differently sized inputs). Let n > 0 n>0 n > 0 and 0 ≤ m ≤ n 0\le m\le n 0 ≤ m ≤ n . For any ϵ > 0 \epsilon>0 ϵ > 0 , there exists a quantum circuit for P 0 , m , n ( ϕ ) \mathbf{P}_{0,m,n}(\phi) P 0 , m , n ( ϕ ) that uses O ( n 1 + ϵ ) O(n^{1+\epsilon}) O ( n 1 + ϵ ) gates, O ( n ) O(n) O ( n ) depth, and no ancilla qubits.
Proof. For m = n m=n m = n , the result follows from Lemma 2.1 . Assume that n = q m + r n=qm+r n = q m + r for q > 0 q>0 q > 0 and 0 ≤ r < m 0\le r<m 0 ≤ r < m . For r = 0 r=0 r = 0 , to perform P 0 , m , n ( ϕ ) \mathbf{P}_{0,m,n}(\phi) P 0 , m , n ( ϕ ) on ∣ y ⟩ m ∣ z ⟩ n \lvert y\rangle_m\lvert z\rangle_n ∣ y ⟩ m ∣ z ⟩ n we simply partition ∣ z ⟩ n \lvert z\rangle_n ∣ z ⟩ n into q q q subregisters of size m m m , labelled from most to least significant as Z q − 1 , … , Z 0 Z_{q-1},\ldots,Z_0 Z q − 1 , … , Z 0 , and apply P 0 , m , m ( 2 m i ϕ ) \mathbf{P}_{0,m,m}(2^{mi}\phi) P 0 , m , m ( 2 mi ϕ ) to ∣ y ⟩ m \lvert y\rangle_m ∣ y ⟩ m and Z i Z_i Z i for each i ∈ Z q i\in Z_q i ∈ Z q . This requires O ( q ( m ) 1 + ϵ ) O(q(m)^{1+\epsilon}) O ( q ( m ) 1 + ϵ ) gates, O ( q m ) O(qm) O ( q m ) depth and no ancilla qubits, implying the result.
For r ≠ 0 r\ne0 r = 0 , we instead partition ∣ z ⟩ n \lvert z\rangle_n ∣ z ⟩ n into q q q subregisters of size m m m , and one subregister (corresponding to the most significant qubits) of size r r r . We label these subregisters as before, with the extra subregister labelled Z q Z_q Z q , and apply the gates P 0 , m , m ( 2 m i ϕ ) \mathbf{P}_{0,m,m}(2^{mi}\phi) P 0 , m , m ( 2 mi ϕ ) as before. This reduces the original problem, performing P 0 , m , n ( ϕ ) \mathbf{P}_{0,m,n}(\phi) P 0 , m , n ( ϕ ) , to the problem of performing P 0 , m , r ( 2 q m ϕ ) \mathbf{P}_{0,m,r}(2^{qm}\phi) P 0 , m , r ( 2 q m ϕ ) on ∣ y ⟩ m \lvert y\rangle_m ∣ y ⟩ m and Z q Z_q Z q . Since we can express m m m as m = q ′ r + r ′ m=q'r+r' m = q ′ r + r ′ for some q ′ > 0 q'>0 q ′ > 0 and 0 ≤ r ′ < r 0\le r'<r 0 ≤ r ′ < r , we can apply this construction iteratively. The sequence of register sizes generated follows the Euclidean algorithm for finding the greatest common divisor of m m m and n n n , terminating when one of the divisions has a remainder equal to zero.
More formally, let r 0 = n , r 1 = m r_0=n,r_1=m r 0 = n , r 1 = m and define r i + 1 , r i + 2 r_{i+1},r_{i+2} r i + 1 , r i + 2 for all i ≥ 0 i\ge0 i ≥ 0 as the integers satisfying 0 ≤ r i + 2 < r i + 1 0\le r_{i+2}<r_{i+1} 0 ≤ r i + 2 < r i + 1 and r i = q i r i + 1 + r i + 2 r_i=q_ir_{i+1}+r_{i+2} r i = q i r i + 1 + r i + 2 for some q i > 0 q_i>0 q i > 0 . It follows that the depth and gate count of the described algorithm is
D ( P 0 , m , n ) = O ( ∑ i = 0 ℓ q i r i + 1 ) = O ( n + m − gcd ( n , m ) ) = O ( n ) , (79) D(\mathbf{P}_{0,m,n})=O\left(\sum_{i=0}^{\ell}q_ir_{i+1}\right)=O(n+m-\gcd(n,m))=O(n), \tag*{(79)} D ( P 0 , m , n ) = O ( i = 0 ∑ ℓ q i r i + 1 ) = O ( n + m − g cd( n , m )) = O ( n ) , (79)
G ( P 0 , m , n ) = O ( ∑ i = 0 ℓ q i ( r i + 1 ) log k ( 2 k − 1 ) ) = O ( n log k ( 2 k − 1 ) ) , (80) G(\mathbf{P}_{0,m,n})=O\left(\sum_{i=0}^{\ell}q_i(r_{i+1})^{\log_k(2k-1)}\right)=O\left(n^{\log_k(2k-1)}\right), \tag*{(80)} G ( P 0 , m , n ) = O ( i = 0 ∑ ℓ q i ( r i + 1 ) l o g k ( 2 k − 1 ) ) = O ( n l o g k ( 2 k − 1 ) ) , (80)
where r ℓ + 2 = 0 r_{\ell+2}=0 r ℓ + 2 = 0 . To see Equation A.14, observe that q i r i + 1 = r i − r i + 2 q_ir_{i+1}=r_i-r_{i+2} q i r i + 1 = r i − r i + 2 , so the entire summation telescopes giving r 0 + r 1 − r ℓ + 1 − r ℓ + 2 r_0+r_1-r_{\ell+1}-r_{\ell+2} r 0 + r 1 − r ℓ + 1 − r ℓ + 2 . Since r ℓ + 1 r_{\ell+1} r ℓ + 1 is by construction the greatest common divisor of n n n and m m m , the result follows. To see Equation A.15, observe that
∑ i = 0 ℓ q i ( r i + 1 ) log k ( 2 k − 1 ) ≤ n log k ( 2 k − 1 ) − 1 ∑ i = 0 ℓ q i ( r i + 1 ) = O ( n log k ( 2 k − 1 ) ) . (81) \sum_{i=0}^{\ell} q_i(r_{i+1})^{\log_k(2k-1)} \le n^{\log_k(2k-1)-1}\sum_{i=0}^{\ell}q_i(r_{i+1}) = O(n^{\log_k(2k-1)}). \tag*{(81)} i = 0 ∑ ℓ q i ( r i + 1 ) l o g k ( 2 k − 1 ) ≤ n l o g k ( 2 k − 1 ) − 1 i = 0 ∑ ℓ q i ( r i + 1 ) = O ( n l o g k ( 2 k − 1 ) ) . (81)
Thus, for any ϵ > 0 \epsilon> 0 ϵ > 0 , P 0 , m , n ( ϕ ) \mathbf{P}_{0,m,n}(\phi) P 0 , m , n ( ϕ ) can be performed using O ( n 1 + ϵ ) O(n^{1+\epsilon}) O ( n 1 + ϵ ) gates, O ( n ) O(n) O ( n ) depth and no ancilla qubits.
Lemma A.3 (Phase multiplication with up to three differently sized inputs). Let n > 0 n > 0 n > 0 and $0 \le t \le m \le n$. For any ϵ > 0 \epsilon> 0 ϵ > 0 , there exist quantum circuits for P t , m , n ( ϕ , α ) \mathbf{P}_{t,m,n}(\phi,\alpha) P t , m , n ( ϕ , α ) and P ‾ t , m , n ( ϕ , α ) \overline{\mathbf{P}}_{t,m,n}(\phi,\alpha) P t , m , n ( ϕ , α ) that use O ( n 1 + ϵ ) O(n^{1+\epsilon}) O ( n 1 + ϵ ) gates and no ancilla qubits. For the special case t = 0 t = 0 t = 0 , these circuits have O ( n ) O(n) O ( n ) depth.
Proof. First, we note that proving the claim for P t , m , n ( ϕ , α ) \mathbf{P}_{t,m,n}(\phi,\alpha) P t , m , n ( ϕ , α ) immediately implies that it holds for P ‾ t , m , n ( ϕ , α ) \overline{\mathbf{P}}_{t,m,n}(\phi,\alpha) P t , m , n ( ϕ , α ) ; for every operation involving the j j j th qubit of ∣ y ⟩ m |y\rangle_m ∣ y ⟩ m in the quantum circuit for P t , m , n ( ϕ , α ) \mathbf{P}_{t,m,n}(\phi,\alpha) P t , m , n ( ϕ , α ) , perform that operation with the ( m − j − 1 ) (m-j-1) ( m − j − 1 ) th qubit of ∣ y ⟩ m |y\rangle_m ∣ y ⟩ m instead.
For t = 0 t = 0 t = 0 and α = 0 \alpha= 0 α = 0 , the gate count, depth and ancilla count claims follow from Lemma 2.2. Assume that t ≠ 0 t \ne0 t = 0 and α = 1 \alpha= 1 α = 1 . We prove the result by performing three separate recursions on the input ∣ x ⟩ t ∣ y ⟩ m ∣ z ⟩ n |x\rangle_t|y\rangle_m|z\rangle_n ∣ x ⟩ t ∣ y ⟩ m ∣ z ⟩ n , using Equations A.6.
Our goal is to push the product ϕ x y z \phi xyz ϕ x y z into the phase of our input state. For now, assume that n n n is a power of k k k . The first step of the algorithm reduces the problem of performing a phase multiplication between three registers of sizes t t t , m m m and n n n , to the problem of performing a phase multiplication between two equal-size registers, that are close in size to m m m , and a register of size t t t . The second step of the algorithm reduces this further to the problem of performing a multiplication between three equal-size registers, all of which are close in size to t t t . The third step applies the phase multiplication algorithm of [20 ] to these three equal-size registers. We make this argument more precise below.
Recall that ℓ 1 = k \ell_1 = k ℓ 1 = k , ℓ 2 = 2 k − 1 \ell_2 = 2k - 1 ℓ 2 = 2 k − 1 and ℓ 3 = 3 k − 2 \ell_3 = 3k - 2 ℓ 3 = 3 k − 2 . We use the compact notation
[ A ] ℓ , i ( n / k v ) ( z ) = A ℓ , i ( n / k v ) ( z ) m o d 2 n / k v + 1 , (82) [A]_{\ell,i}^{(n/k^v)}(z) = A_{\ell,i}^{(n/k^v)}(z) \bmod2^{n/k^v+1}, \tag*{(82)} [ A ] ℓ , i ( n / k v ) ( z ) = A ℓ , i ( n / k v ) ( z ) mod 2 n / k v + 1 , (82)
for a modulo-reduced linear combination. We start by partitioning ∣ z ⟩ n |z\rangle_n ∣ z ⟩ n into k k k subregisters according to f k , n f_{k,n} f k , n and preparing modulo-reduced linear combinations of these subregisters sequentially. We follow the decomposition
ϕ z = ∑ i = 0 ℓ 1 − 1 ϕ ℓ 1 , i ( n ) A ℓ 1 , i ( n ) ( z ) (83) \phi z = \sum_{i=0}^{\ell_1-1}\phi_{\ell_1,i}^{(n)}A_{\ell_1,i}^{(n)}(z) \tag*{(83)} ϕ z = i = 0 ∑ ℓ 1 − 1 ϕ ℓ 1 , i ( n ) A ℓ 1 , i ( n ) ( z ) (83)
from Equations A.6, accounting for overflow in the phase directly as we are used to. The cost of accounting for the overflow is G ( P 0 , t , m ) G(\mathbf{P}_{0,t,m}) G ( P 0 , t , m ) and the cost to prepare the linear combinations is O ( n ) O(n) O ( n ) .
This reduces the problem of pushing ϕ x y z \phi xyz ϕ x y z to the phase, to the problem of pushing
∑ i ∈ Z ℓ 1 ϕ ℓ 1 , i ( n ) ⋅ [ A ] ℓ 1 , i ( n ) ( z ) ⋅ x y (84) \sum_{i\in\mathbb{Z}_{\ell_1}}\phi_{\ell_1,i}^{(n)}\cdot[A]_{\ell_1,i}^{(n)}(z)\cdot xy \tag*{(84)} i ∈ Z ℓ 1 ∑ ϕ ℓ 1 , i ( n ) ⋅ [ A ] ℓ 1 , i ( n ) ( z ) ⋅ x y (84)
to the phase instead, which can be solved by applying the gates P t , m , n / k ( ϕ ℓ 1 , i ( n ) , 1 ) \mathbf{P}_{t,m,n/k}(\phi_{\ell_1,i}^{(n)},1) P t , m , n / k ( ϕ ℓ 1 , i ( n ) , 1 ) for i ∈ Z ℓ 1 i\in\mathbb{Z}_{\ell_1} i ∈ Z ℓ 1 exactly when each modulo-reduced linear combination is being stored in a register. Each subproblem can be reduced to ℓ 1 \ell_1 ℓ 1 new subproblems involving smaller phase multiplications, via A ℓ 1 , i ( n / k ) A_{\ell_1,i}^{(n/k)} A ℓ 1 , i ( n / k ) . We can repeat this process until we have ℓ 1 s \ell_1^s ℓ 1 s subproblems for s = ⌊ log k ( n / m ) ⌋ s = \lfloor\log_k(n/m)\rfloor s = ⌊ log k ( n / m )⌋ . We describe this new problem more formally next.
Let i = ( i 0 , … , i s − 1 ) ∈ Z ℓ 1 s \mathbf{i}=(i_0,\ldots,i_{s-1})\in\mathbb{Z}_{\ell_1}^{s} i = ( i 0 , … , i s − 1 ) ∈ Z ℓ 1 s . We define
z i = [ A ] ℓ 1 , i s − 1 ( n / k s − 1 ) ∘ [ A ] ℓ 1 , i s − 2 ( n / k s − 2 ) ∘ ⋯ ∘ [ A ] ℓ 1 , i 1 ( n / k ) ∘ [ A ] ℓ 1 , i 0 ( n ) ( z ) . (85) z_{\mathbf{i}}=[A]_{\ell_1,i_{s-1}}^{(n/k^{s-1})}\circ[A]_{\ell_1,i_{s-2}}^{(n/k^{s-2})}\circ\cdots\circ[A]_{\ell_1,i_1}^{(n/k)}\circ[A]_{\ell_1,i_0}^{(n)}(z). \tag*{(85)} z i = [ A ] ℓ 1 , i s − 1 ( n / k s − 1 ) ∘ [ A ] ℓ 1 , i s − 2 ( n / k s − 2 ) ∘ ⋯ ∘ [ A ] ℓ 1 , i 1 ( n / k ) ∘ [ A ] ℓ 1 , i 0 ( n ) ( z ) . (85)
This integer is derived by following the recursion branch i \mathbf{i} i . It sits on n / k s n/k^s n / k s qubits, one of the now k s k^s k s subregisters of the original register ∣ z ⟩ n |z\rangle_n ∣ z ⟩ n . Let ϕ i \phi_{\mathbf{i}} ϕ i denote the corresponding pre-computable rational, determined by the branch, the original register size n n n , the rational ϕ \phi ϕ , and the matrix A ( ℓ 1 ) A(\ell_1) A ( ℓ 1 ) . We have reduced the problem of pushing ϕ x y z \phi xyz ϕ x y z to the phase, to the problem of pushing
∑ i ∈ Z ℓ 1 s ϕ i z i x y . (86) \sum_{\mathbf{i}\in\mathbb{Z}_{\ell_1}^{s}}\phi_{\mathbf{i}}z_{\mathbf{i}}xy. \tag*{(86)} i ∈ Z ℓ 1 s ∑ ϕ i z i x y . (86)
to the phase instead. This problem is solved by applying ℓ 1 s \ell_1^s ℓ 1 s gates P t , m , n / k s ( ϕ i , 1 ) \mathbf{P}_{t,m,n/k^s}(\phi_{\mathbf{i}},1) P t , m , n / k s ( ϕ i , 1 ) to ∣ x ⟩ n ∣ y ⟩ m ∣ z i ⟩ n / k s |x\rangle_n|y\rangle_m|z_{\mathbf{i}}\rangle_{n/k^s} ∣ x ⟩ n ∣ y ⟩ m ∣ z i ⟩ n / k s .
By the definition of s s s , k m > n / k s ≥ m km>n/k^s\geq m k m > n / k s ≥ m . At this point in the algorithm, we would like to apply the decomposition of ϕ y z \phi yz ϕ y z given in Equations A.6 to ϕ i z i y \phi_{\mathbf{i}}z_{\mathbf{i}}y ϕ i z i y . The only trouble is that this decomposition, requires that z i z_{\mathbf{i}} z i and y y y sit on registers of the same size. To get around this fact, we imagine that the bitstring for y y y is padded with n / k s − m n/k^s-m n / k s − m zeroes, allowing us to apply A ℓ 2 , j ( n / k s ) ( ⋅ ) A_{\ell_2,j}^{(n/k^s)}(\cdot) A ℓ 2 , j ( n / k s ) ( ⋅ ) to this new bitstring. It follows that
ϕ i z i y = ∑ j ∈ Z ℓ 2 ( ϕ i ) ℓ 2 , j ( n / k s ) ⋅ A ℓ 2 , j ( n / k s ) ( z i ) ⋅ A ℓ 2 , j ( n / k s ) ( y ) . (87) \phi_{\mathbf{i}}z_{\mathbf{i}}y=\sum_{j\in\mathbb{Z}_{\ell_2}}(\phi_{\mathbf{i}})_{\ell_2,j}^{(n/k^s)}\cdot A_{\ell_2,j}^{(n/k^s)}(z_{\mathbf{i}})\cdot A_{\ell_2,j}^{(n/k^s)}(y). \tag*{(87)} ϕ i z i y = j ∈ Z ℓ 2 ∑ ( ϕ i ) ℓ 2 , j ( n / k s ) ⋅ A ℓ 2 , j ( n / k s ) ( z i ) ⋅ A ℓ 2 , j ( n / k s ) ( y ) . (87)
The challenge, as usual, becomes to reduce this to a problem involving linear combinations of z i z_{\mathbf{i}} z i and y y y that sit on n / k s + 1 n/k^{s+1} n / k s + 1 qubits, accounting for overflow directly. There are two subtleties that make this different from the equal-size register case discussed in the paragraph on Avoiding ancillas.
First, we cannot build the linear combination A ℓ 2 , j ( n / k s ) ( y ) A_{\ell_2,j}^{(n/k^s)}(y) A ℓ 2 , j ( n / k s ) ( y ) on any subregister of the m m m qubit register ∣ y ⟩ m |y\rangle_m ∣ y ⟩ m , since m m m is not being partitioned into k k k equal size subregisters, but into registers of size n / k s + 1 n/k^{s+1} n / k s + 1 and one register of size m m o d n / k s + 1 m\bmod n/k^{s+1} m mod n / k s + 1 . The only guarantee we have is that there exists at least one subregister of size n / k s + 1 n/k^{s+1} n / k s + 1 , the least significant subregister. Therefore, we must build the linear combination on this subregister. Choosing g = ℓ 2 g=\ell_2 g = ℓ 2 in the definition of A ( ℓ 2 , g ) A(\ell_2,g) A ( ℓ 2 , g ) , means that this is always possible using just out-of-place scaled additions.
Second, as we just noted, one of the subregisters of ∣ y ⟩ m |y\rangle_m ∣ y ⟩ m will have size m m o d n / k s + 1 m\bmod n/k^{s+1} m mod n / k s + 1 . Let d = m m o d n / k s + 1 d=m\bmod n/k^{s+1} d = m mod n / k s + 1 . This means A ℓ 2 , j ( n / k s ) ( y ) A_{\ell_2,j}^{(n/k^s)}(y) A ℓ 2 , j ( n / k s ) ( y ) involves a scaled addition of the form y 0 ↦ y 0 + λ y h y_0\mapsto y_0+\lambda y_h y 0 ↦ y 0 + λ y h for y 0 y_0 y 0 sitting on n / k s + 1 n/k^{s+1} n / k s + 1 qubits and y h y_h y h sitting on d d d qubits. We propose to first perform all other scaled modular additions between subregisters of ∣ y ⟩ m |y\rangle_m ∣ y ⟩ m , so that on the least significant n / k s + 1 n/k^{s+1} n / k s + 1 qubits of ∣ y ⟩ m |y\rangle_m ∣ y ⟩ m we have [ A ℓ 2 , j ( n / k s ) ( y ) − λ y h m o d 2 n / k s + 1 [A_{\ell_2,j}^{(n/k^s)}(y)-\lambda y_h\mathbin{\bmod}2^{n/k^{s+1}} [ A ℓ 2 , j ( n / k s ) ( y ) − λ y h mod 2 n / k s + 1 . We use the compact notation
[ A ℓ 2 , j ( n / k s ) ( y , − y h ) = [ A ℓ 2 , j ( n / k s ) ( y ) − λ y h m o d 2 n / k s + 1 (88) [A_{\ell_2,j}^{(n/k^s)}(y,-y_h)=[A_{\ell_2,j}^{(n/k^s)}(y)-\lambda y_h\mathbin{\bmod}2^{n/k^{s+1}} \tag*{(88)} [ A ℓ 2 , j ( n / k s ) ( y , − y h ) = [ A ℓ 2 , j ( n / k s ) ( y ) − λ y h mod 2 n / k s + 1 (88)
to mean the modulo-reduced linear combination [ A ℓ 2 , j ( n / k s ) ( y ) [A_{\ell_2,j}^{(n/k^s)}(y) [ A ℓ 2 , j ( n / k s ) ( y ) minus the term in this linear combination that involves the integer y h y_h y h , with everything taken modulo 2 n / k s + 1 2^{n/k^{s+1}} 2 n / k s + 1 .
We then put λ y h − ( λ y h m o d 2 d ) \lambda y_h-(\lambda y_h\bmod2^d) λ y h − ( λ y h mod 2 d ) into the phase in the usual way requiring G ( P 0 , t , n / k s ) G(\mathbf{P}_{0,t,n/k^s}) G ( P 0 , t , n / k s ) gates, and we add λ y h m o d 2 d \lambda y_h\bmod2^d λ y h mod 2 d to the least significant d d d qubits of the n / k s + 1 n/k^{s+1} n / k s + 1 qubit subregister containing [ A ℓ 2 , j ( n / k s ) ( y , − y h ) [A_{\ell_2,j}^{(n/k^s)}(y,-y_h) [ A ℓ 2 , j ( n / k s ) ( y , − y h ) , using the modular addition circuit of [36 ] . We pause the circuit midway to control on the overflow, which we denote c y h c_{y_h} c y h , of this addition, at a cost of G ( P 0 , t , n / k s ) G(\mathbf{P}_{0,t,n/k^s}) G ( P 0 , t , n / k s ) . Since the overflow we are accounting for is not mod 2 n / k s + 1 2^{n/k^{s+1}} 2 n / k s + 1 rather it is mod 2 d 2^d 2 d , the n / k s + 1 n/k^{s+1} n / k s + 1 qubit subregister that we have been building on now contains [ A ] ℓ 2 , j ( n / k s ) ( y , − y h ) + ( λ y h m o d 2 d ) − 2 d c y h [A]_{\ell_2,j}^{(n/k^{s})}(y,-y_h)+(\lambda y_h \bmod2^d)-2^d c_{y_h} [ A ] ℓ 2 , j ( n / k s ) ( y , − y h ) + ( λ y h mod 2 d ) − 2 d c y h .
Since all overflow can consistently be accounted for directly in the phase, we have successfully reduced the problem of pushing ϕ i z i y \phi_i z_i y ϕ i z i y to the phase to the problem of pushing
∑ j ∈ Z ℓ 2 ( ϕ i ) ℓ 2 , j ( n / k s ) ⋅ [ A ] ℓ 2 , j ( n / k s ) ( z i ) ⋅ ( [ A ] ℓ 2 , j ( n / k s ) ( y , − y h ) + ( λ y h m o d 2 d ) − 2 d c y h ) (89) \sum_{j\in\mathbb{Z}_{\ell_2}}(\phi_i)_{\ell_2,j}^{(n/k^s)}\cdot[A]_{\ell_2,j}^{(n/k^s)}(z_i)\cdot\left([A]_{\ell_2,j}^{(n/k^s)}(y,-y_h)+(\lambda y_h\bmod2^d)-2^d c_{y_h}\right) \tag*{(89)} j ∈ Z ℓ 2 ∑ ( ϕ i ) ℓ 2 , j ( n / k s ) ⋅ [ A ] ℓ 2 , j ( n / k s ) ( z i ) ⋅ ( [ A ] ℓ 2 , j ( n / k s ) ( y , − y h ) + ( λ y h mod 2 d ) − 2 d c y h ) (89)
to the phase, which is possible at this stage in the algorithm with ℓ 1 s ℓ 2 \ell_1^s\ell_2 ℓ 1 s ℓ 2 gates of the form P t , n / k s + 1 , n / k s + 1 ( ( ϕ i ) ℓ 2 , j ( n / k s ) , 1 ) \mathbf{P}_{t,n/k^{s+1},n/k^{s+1}}\left((\phi_i)_{\ell_2,j}^{(n/k^s)},1\right) P t , n / k s + 1 , n / k s + 1 ( ( ϕ i ) ℓ 2 , j ( n / k s ) , 1 ) .
Each term in Equation A.24 can be decomposed using A ℓ 2 , j ( n / k s + 1 ) ( ⋅ ) A_{\ell_2,j}^{(n/k^{s+1})}(\cdot) A ℓ 2 , j ( n / k s + 1 ) ( ⋅ ) , preparing ℓ 1 s ℓ 2 s \ell_1^s\ell_2^s ℓ 1 s ℓ 2 s subproblems, and so on, until we have ℓ 1 s ℓ 2 r \ell_1^s\ell_2^r ℓ 1 s ℓ 2 r subproblems for r = ⌊ log k ( n / t k s ) ⌋ r=\left\lfloor\log_k(n/tk^s)\right\rfloor r = ⌊ log k ( n / t k s ) ⌋ . We formalise this argument next.
Let j = ( j 0 , … , j r − 1 ) ∈ Z ℓ 2 r \mathbf{j}=(j_0,\ldots,j_{r-1})\in\mathbb{Z}_{\ell_2^r} j = ( j 0 , … , j r − 1 ) ∈ Z ℓ 2 r . We define
z i , j = [ A ] ℓ 2 , j r − 1 ( n / k s + r − 1 ) ∘ ⋯ ∘ [ A ] ℓ 2 , j 0 ( n / k s ) ( z i ) (90) z_{\mathbf{i},\mathbf{j}}=[A]_{\ell_2,j_{r-1}}^{(n/k^{s+r-1})}\circ\cdots\circ[A]_{\ell_2,j_0}^{(n/k^s)}(z_i) \tag*{(90)} z i , j = [ A ] ℓ 2 , j r − 1 ( n / k s + r − 1 ) ∘ ⋯ ∘ [ A ] ℓ 2 , j 0 ( n / k s ) ( z i ) (90)
and
y j = [ A ] ℓ 2 , j r − 1 ( n / k s + r − 1 ) ∘ ⋯ ∘ [ A ] ℓ 2 , j 1 ( n / k s + 1 ) ( [ A ] ℓ 2 , j 0 ( n / k s ) ( y , − y h ) + ( λ y h m o d 2 d ) − 2 d c y h ) . (91) y_{\mathbf{j}}=[A]_{\ell_2,j_{r-1}}^{(n/k^{s+r-1})}\circ\cdots\circ[A]_{\ell_2,j_1}^{(n/k^{s+1})}\left([A]_{\ell_2,j_0}^{(n/k^s)}(y,-y_h)+(\lambda y_h\bmod2^d)-2^d c_{y_h}\right). \tag*{(91)} y j = [ A ] ℓ 2 , j r − 1 ( n / k s + r − 1 ) ∘ ⋯ ∘ [ A ] ℓ 2 , j 1 ( n / k s + 1 ) ( [ A ] ℓ 2 , j 0 ( n / k s ) ( y , − y h ) + ( λ y h mod 2 d ) − 2 d c y h ) . (91)
Both integers sit on n / k s + r n/k^{s+r} n / k s + r qubits. z i , j z_{\mathbf{i},\mathbf{j}} z i , j corresponds to the branch i \mathbf{i} i from the first recursion and the branch j \mathbf{j} j from the second recursion while y j y_{\mathbf{j}} y j corresponds to the branch j \mathbf{j} j from the second recursion. Let ϕ i , j \phi_{\mathbf{i},\mathbf{j}} ϕ i , j be the corresponding pre-computable rational, determined by the branches i \mathbf{i} i and j \mathbf{j} j , the original register size n n n , the rational ϕ \phi ϕ , and the matrices A ( ℓ 1 ) A(\ell_1) A ( ℓ 1 ) and A ( ℓ 2 ) A(\ell_2) A ( ℓ 2 ) .
We have reduced the problem of pushing ϕ x y z \phi xyz ϕ x y z to the phase, to the problem of pushing
∑ i ∈ Z ℓ 1 s , j ∈ Z ℓ 2 r ϕ i , j z i , j y j x (92) \sum_{\mathbf{i}\in\mathbb{Z}_{\ell_1^s},\mathbf{j}\in\mathbb{Z}_{\ell_2^r}}\phi_{\mathbf{i},\mathbf{j}}z_{\mathbf{i},\mathbf{j}}y_{\mathbf{j}}x \tag*{(92)} i ∈ Z ℓ 1 s , j ∈ Z ℓ 2 r ∑ ϕ i , j z i , j y j x (92)
to the phase. This problem can be solved by applying ℓ 1 s ℓ 2 r \ell_1^s\ell_2^r ℓ 1 s ℓ 2 r gates P t , n / k s + r , n / k s + r ( ϕ i , j , 1 ) \mathbf{P}_{t,n/k^{s+r},n/k^{s+r}}(\phi_{\mathbf{i},\mathbf{j}},1) P t , n / k s + r , n / k s + r ( ϕ i , j , 1 ) . Again at this point in the algorithm, we would like to apply the decomposition of ϕ x y z \phi xyz ϕ x y z given in Equations A.6, but we cannot directly, since x x x sits on t t t qubits, whereas z i , j z_{\mathbf{i},\mathbf{j}} z i , j and y j y_{\mathbf{j}} y j both sit on n / k s + r n/k^{s+r} n / k s + r qubits.
Since k t > n / k s + r ≥ t kt>n/k^{s+r}\geq t k t > n / k s + r ≥ t , we know that we can consider the padded version of the bitstring x x x with n / k s + r − t n/k^{s+r}-t n / k s + r − t zeroes and apply A ℓ 3 , g ( n / k s + r ) ( ⋅ ) A_{\ell_3,g}^{(n/k^{s+r})}(\cdot) A ℓ 3 , g ( n / k s + r ) ( ⋅ ) to this padded bitstring. For all the same reasons as we gave when applying A ℓ 2 , j ( n / k s ) ( ⋅ ) A_{\ell_2,j}^{(n/k^s)}(\cdot) A ℓ 2 , j ( n / k s ) ( ⋅ ) to the padded version of y y y , the problem of pushing ϕ i , j z i , j y j x \phi_{\mathbf{i},\mathbf{j}}z_{\mathbf{i},\mathbf{j}}y_{\mathbf{j}}x ϕ i , j z i , j y j x to the phase can be reduced to the problem of pushing
∑ g ∈ Z ℓ 3 ( ϕ i , j ) ℓ 3 , g ( n / k s + r ) ⋅ [ A ] ℓ 3 , g ( n / k s + r ) ( z i , j ) ⋅ [ A ] ℓ 3 , g ( n / k s + r ) ( y j ) ⋅ ( [ A ] ℓ 3 , g ( n / k s + r ) ( x , − x h ) + ( λ x h m o d 2 d ) − 2 d c x h ) (93) \sum_{g\in\mathbb{Z}_{\ell_3}}(\phi_{\mathbf{i},\mathbf{j}})_{\ell_3,g}^{(n/k^{s+r})}\cdot[A]_{\ell_3,g}^{(n/k^{s+r})}(z_{\mathbf{i},\mathbf{j}})\cdot[A]_{\ell_3,g}^{(n/k^{s+r})}(y_{\mathbf{j}})
\cdot\left([A]_{\ell_3,g}^{(n/k^{s+r})}(x,-x_h)+(\lambda x_h\bmod2^d)-2^d c_{x_h}\right) \tag*{(93)} g ∈ Z ℓ 3 ∑ ( ϕ i , j ) ℓ 3 , g ( n / k s + r ) ⋅ [ A ] ℓ 3 , g ( n / k s + r ) ( z i , j ) ⋅ [ A ] ℓ 3 , g ( n / k s + r ) ( y j ) ⋅ ( [ A ] ℓ 3 , g ( n / k s + r ) ( x , − x h ) + ( λ x h mod 2 d ) − 2 d c x h ) (93)
to the phase, where here d = t m o d n / k s + r + 1 d=t\bmod n/k^{s+r+1} d = t mod n / k s + r + 1 , [ A ] ℓ 3 , g ( n / k s + r ) ( x , − x h ) [A]_{\ell_3,g}^{(n/k^{s+r})}(x,-x_h) [ A ] ℓ 3 , g ( n / k s + r ) ( x , − x h ) is defined by Equation A.23, x h x_h x h is the integer contained in the register of d d d qubits, λ \lambda λ is the scalar required for the addition involving x h x_h x h , and c x h c_{x_h} c x h is the overflow from adding λ x h m o d 2 d \lambda x_h\bmod2^d λ x h mod 2 d to [ A ] ℓ 3 , g ( n / k s + r ) ( x , − x h ) m o d 2 d [A]_{\ell_3,g}^{(n/k^{s+r})}(x,-x_h)\bmod2^d [ A ] ℓ 3 , g ( n / k s + r ) ( x , − x h ) mod 2 d . This problem can be solved with ℓ 1 s ℓ 2 r ℓ 3 \ell_1^s\ell_2^r\ell_3 ℓ 1 s ℓ 2 r ℓ 3 gates of the form
P n / k s + r + 1 , n / k s + r + 1 , n / k s + r + 1 ( ( ϕ i , j ) ℓ 3 , g ( n / k s + r ) , 1 ) . (94) \mathbf{P}_{n/k^{s+r+1},n/k^{s+r+1},n/k^{s+r+1}}\left((\phi_{\mathbf{i},\mathbf{j}})_{\ell_3,g}^{(n/k^{s+r})},1\right). \tag*{(94)} P n / k s + r + 1 , n / k s + r + 1 , n / k s + r + 1 ( ( ϕ i , j ) ℓ 3 , g ( n / k s + r ) , 1 ) . (94)
Each phase multiplication is between three equal-size registers and can be performed using the method of [20 ] .
If n n n is not a power of k k k (recall that we assumed it was for simplicity at the start), at the top level of recursion we can use k k k subregisters of size ⌊ n / k ⌋ \lfloor n/k\rfloor ⌊ n / k ⌋ instead and push the product that involves the most significant n m o d k n \bmod k n mod k qubits to the phase directly, as discussed in Lemma conventions. We can repeat this at every level of the recursion. This is no more expensive than the cost of accounting for overflow at each level. Thus, whether n n n is a power of k k k or not does not affect the asymptotic cost of the algorithm.
Finally, we analyse this cost. We plug back in the values ℓ 1 = k \ell_1 = k ℓ 1 = k , ℓ 2 = 2 k − 1 \ell_2 = 2k-1 ℓ 2 = 2 k − 1 , ℓ 3 = 3 k − 2 \ell_3 = 3k-2 ℓ 3 = 3 k − 2 , s = ⌊ log k ( n / m ) ⌋ s = \lfloor\log_k(n/m)\rfloor s = ⌊ log k ( n / m )⌋ , and r = ⌊ log k ( n / t k s ) ⌋ r = \lfloor\log_k(n/tk^s)\rfloor r = ⌊ log k ( n / t k s )⌋ . The recurrence relation determining the cost of the first recursion is
G ( P t , m , n ) = k G ( P t , m , n / k ) + O ( n ) + G ( P 0 , t , m ) . (95) G(\mathbf{P}_{t,m,n}) = kG(\mathbf{P}_{t,m,n/k}) + O(n) + G(\mathbf{P}_{0,t,m}). \tag*{(95)} G ( P t , m , n ) = k G ( P t , m , n / k ) + O ( n ) + G ( P 0 , t , m ) . (95)
Recursing until we have ℓ 1 s \ell_1^s ℓ 1 s subproblems means the gate count of this first recursion is
O ( n s ) + O ( k s m log k ( 2 k − 1 ) ) = O ( n log ( n / m ) ) + O ( ( n / m ) m log k ( 2 k − 1 ) ) (96) O(ns) + O(k^s m^{\log_k(2k-1)}) = O(n\log(n/m)) + O((n/m)m^{\log_k(2k-1)}) \tag*{(96)} O ( n s ) + O ( k s m l o g k ( 2 k − 1 ) ) = O ( n log ( n / m )) + O (( n / m ) m l o g k ( 2 k − 1 ) ) (96)
using Lemma 2.2 . We then showed that
G ( P t , m , n / k s ) = ( 2 k − 1 ) G ( P t , n / k s + 1 , n / k s + 1 ) + O ( n / k s ) + G ( P 0 , t , n / k s ) (97) G(\mathbf{P}_{t,m,n/k^s}) = (2k-1)G(\mathbf{P}_{t,n/k^{s+1},n/k^{s+1}}) + O(n/k^s) + G(\mathbf{P}_{0,t,n/k^s}) \tag*{(97)} G ( P t , m , n / k s ) = ( 2 k − 1 ) G ( P t , n / k s + 1 , n / k s + 1 ) + O ( n / k s ) + G ( P 0 , t , n / k s ) (97)
Recursing until we have ℓ 1 s ℓ 2 r \ell_1^s\ell_2^r ℓ 1 s ℓ 2 r subproblems means the gate count of the second recursion is
O ( k s ( n / k s ) log k ( 2 k − 1 ) r ) = O ( ( n / m ) m log k ( 2 k − 1 ) log ( m / t ) ) . (98) O(k^s(n/k^s)^{\log_k(2k-1)r}) = O((n/m)m^{\log_k(2k-1)}\log(m/t)). \tag*{(98)} O ( k s ( n / k s ) l o g k ( 2 k − 1 ) r ) = O (( n / m ) m l o g k ( 2 k − 1 ) log ( m / t )) . (98)
Finally, we showed that G ( P t , n / k s + r , n / k s + r ) = G(\mathbf{P}_{t,n/k^{s+r},n/k^{s+r}}) = G ( P t , n / k s + r , n / k s + r ) =
= ( 3 k − 2 ) G ( P n / k s + r + 1 , n / k s + r + 1 , n / k s + r + 1 ) + O ( n / k s + r ) + G ( P 0 , n / k s + r , n / k s + r ) (99) = (3k-2)G(\mathbf{P}_{n/k^{s+r+1},n/k^{s+r+1},n/k^{s+r+1}}) + O(n/k^{s+r}) + G(\mathbf{P}_{0,n/k^{s+r},n/k^{s+r}}) \tag*{(99)} = ( 3 k − 2 ) G ( P n / k s + r + 1 , n / k s + r + 1 , n / k s + r + 1 ) + O ( n / k s + r ) + G ( P 0 , n / k s + r , n / k s + r ) (99)
Recursing until we have ℓ 1 s ℓ 2 r ℓ 3 q \ell_1^s\ell_2^r\ell_3^q ℓ 1 s ℓ 2 r ℓ 3 q subproblems where q = ⌊ log k ( n / k s + r ) ⌋ q = \lfloor\log_k(n/k^{s+r})\rfloor q = ⌊ log k ( n / k s + r )⌋ means that the gate count of this third recursion is
O ( k s ( 2 k − 1 ) r ( n / k s + r ) log k ( 3 k − 2 ) ) = O ( ( n / m ) ( m / t ) log k ( 2 k − 1 ) t log k ( 3 k − 2 ) ) . (100) O(k^s(2k-1)^r(n/k^{s+r})^{\log_k(3k-2)}) = O((n/m)(m/t)^{\log_k(2k-1)}t^{\log_k(3k-2)}). \tag*{(100)} O ( k s ( 2 k − 1 ) r ( n / k s + r ) l o g k ( 3 k − 2 ) ) = O (( n / m ) ( m / t ) l o g k ( 2 k − 1 ) t l o g k ( 3 k − 2 ) ) . (100)
Putting all of these recursions together, it is clear that P t , m , n ( ϕ , 1 ) \mathbf{P}_{t,m,n}(\phi,1) P t , m , n ( ϕ , 1 ) can be implemented using O ( n log k ( 3 k − 2 ) ) O(n^{\log_k(3k-2)}) O ( n l o g k ( 3 k − 2 ) ) gates. The algorithm also uses no ancillas. It follows that for any ϵ > 0 \epsilon> 0 ϵ > 0 , we can pick an integer k k k such that P t , m , n ( ϕ , α ) \mathbf{P}_{t,m,n}(\phi,\alpha) P t , m , n ( ϕ , α ) can be performed using O ( n 1 + ϵ ) O(n^{1+\epsilon}) O ( n 1 + ϵ ) gates and no ancilla qubits.
Remark A.4. Suppressing the gate count of these algorithms to get arbitrarily close to linear comes at the cost of large, impractical constant factors. It was estimated in [20 ] that, for multiplication at cryptographically relevant integer sizes, choices of k k k leading to O ( n 1.30 … ) O(n^{1.30\ldots}) O ( n 1.30 … ) gates for P 0 , n , n ( ϕ ) \mathbf{P}_{0,n,n}(\phi) P 0 , n , n ( ϕ ) and O ( n 1.46 … ) O(n^{1.46\ldots}) O ( n 1.46 … ) gates for P n , n , n ( ϕ , 1 ) \mathbf{P}_{n,n,n}(\phi,1) P n , n , n ( ϕ , 1 ) would be practical. Determining choices of k k k that lead to practical gate counts for P 0 , m , n ( ϕ ) \mathbf{P}_{0,m,n}(\phi) P 0 , m , n ( ϕ ) and P t , m , n ( ϕ , 1 ) \mathbf{P}_{t,m,n}(\phi,1) P t , m , n ( ϕ , 1 ) requires separate estimates.
The controlled and parametrised quantum Fourier transform
Lemma B.1 (Fast controlled and parametrised QFT). Let n > 0 n > 0 n > 0 and m = O ( n ) m = O(n) m = O ( n ) . For any ϵ > 0 \epsilon> 0 ϵ > 0 , there exists a quantum circuit for Q F T m , n ( ϕ , α ) \mathbf{QFT}_{m,n}(\phi,\alpha) QFT m , n ( ϕ , α ) that uses O ( n 1 + ϵ ) O(n^{1+\epsilon}) O ( n 1 + ϵ ) gates and no ancilla qubits. For the special case m = 0 m = 0 m = 0 , the circuit has O ( n log n ) O(n\log n) O ( n log n ) depth.
Proof. First, we prove the claim for m = 0 m=0 m = 0 . To recap, Q F T 0 , n ( ϕ ) \mathbf{QFT}_{0,n}(\phi) QFT 0 , n ( ϕ ) is defined as follows. Let y ∈ Z 2 n y\in\mathbb{Z}_{2^n} y ∈ Z 2 n and ϕ ∈ Z 2 n \phi\in\mathbb{Z}_{2^n} ϕ ∈ Z 2 n . Then,
Q F T 0 , n ( ϕ ) : ∣ y ⟩ n ↦ 1 2 n ∑ z ∈ Z 2 n e 2 π i ϕ y z / 2 n ∣ z ⟩ n . (101) \mathbf{QFT}_{0,n}(\phi): |y\rangle_n \mapsto\frac{1}{\sqrt{2^n}}\sum_{z\in\mathbb{Z}_{2^n}}e^{2\pi i\phi yz/2^n}|z\rangle_n. \tag*{(101)} QFT 0 , n ( ϕ ) : ∣ y ⟩ n ↦ 2 n 1 z ∈ Z 2 n ∑ e 2 π i ϕ y z / 2 n ∣ z ⟩ n . (101)
We prove that Q F T 0 , n ( ϕ ) \mathbf{QFT}_{0,n}(\phi) QFT 0 , n ( ϕ ) can be defined recursively as
Q F T 0 , n ( ϕ ) = ( I ⌈ n / 2 ⌉ ⊗ Q F T 0 , ⌊ n / 2 ⌋ ( ϕ ) ) ∘ ( P ‾ 0 , ⌈ n / 2 ⌉ , ⌊ n / 2 ⌋ ( ϕ / 2 n ) ) ∘ ( Q F T 0 , ⌈ n / 2 ⌉ ( ϕ ) ⊗ I ⌊ n / 2 ⌋ ) . \mathbf{QFT}_{0,n}(\phi)=\left(I_{\lceil n/2\rceil}\otimes\mathbf{QFT}_{0,\lfloor n/2\rfloor}(\phi)\right)\circ\left(\overline{\mathbf{P}}_{0,\lceil n/2\rceil,\lfloor n/2\rfloor}(\phi/2^n)\right)\circ\left(\mathbf{QFT}_{0,\lceil n/2\rceil}(\phi)\otimes I_{\lfloor n/2\rfloor}\right). QFT 0 , n ( ϕ ) = ( I ⌈ n /2 ⌉ ⊗ QFT 0 , ⌊ n /2 ⌋ ( ϕ ) ) ∘ ( P 0 , ⌈ n /2 ⌉ , ⌊ n /2 ⌋ ( ϕ / 2 n ) ) ∘ ( QFT 0 , ⌈ n /2 ⌉ ( ϕ ) ⊗ I ⌊ n /2 ⌋ ) .
For y ∈ Z 2 n y\in\mathbb{Z}_{2^n} y ∈ Z 2 n , consider y 1 ∈ Z 2 ⌈ n / 2 ⌉ y_1\in\mathbb{Z}_{2^{\lceil n/2\rceil}} y 1 ∈ Z 2 ⌈ n /2 ⌉ , y 2 ∈ Z 2 ⌊ n / 2 ⌋ y_2\in\mathbb{Z}_{2^{\lfloor n/2\rfloor}} y 2 ∈ Z 2 ⌊ n /2 ⌋ such that y = 2 ⌊ n / 2 ⌋ y 1 + y 2 y=2^{\lfloor n/2\rfloor}y_1+y_2 y = 2 ⌊ n /2 ⌋ y 1 + y 2 . Then consider the following algorithm on input ∣ y ⟩ n = ∣ y 1 ⟩ ⌈ n / 2 ⌉ ∣ y 2 ⟩ ⌊ n / 2 ⌋ |y\rangle_n=|y_1\rangle_{\lceil n/2\rceil}|y_2\rangle_{\lfloor n/2\rfloor} ∣ y ⟩ n = ∣ y 1 ⟩ ⌈ n /2 ⌉ ∣ y 2 ⟩ ⌊ n /2 ⌋ ,
⟶ Q F T 0 , ⌈ n / 2 ⌉ ( ϕ ) ⊗ I ⌊ n / 2 ⌋ 1 2 ⌈ n / 2 ⌉ ∑ z 2 ∈ Z 2 ⌈ n / 2 ⌉ e 2 π i ϕ ( 2 ⌊ n / 2 ⌋ y 1 z 2 ) / 2 n ∣ z 2 ⟩ ⌈ n / 2 ⌉ ∣ y 2 ⟩ ⌊ n / 2 ⌋ (102) \underset{\mathbf{QFT}_{0,\lceil n/2\rceil}(\phi)\otimes I_{\lfloor n/2\rfloor}}{\longrightarrow}\frac{1}{\sqrt{2^{\lceil n/2\rceil}}}\sum_{z_2\in\mathbb{Z}_{2^{\lceil n/2\rceil}}}e^{2\pi i\phi(2^{\lfloor n/2\rfloor}y_1z_2)/2^n}|z_2\rangle_{\lceil n/2\rceil}|y_2\rangle_{\lfloor n/2\rfloor} \tag*{(102)} QFT 0 , ⌈ n /2 ⌉ ( ϕ ) ⊗ I ⌊ n /2 ⌋ ⟶ 2 ⌈ n /2 ⌉ 1 z 2 ∈ Z 2 ⌈ n /2 ⌉ ∑ e 2 π i ϕ ( 2 ⌊ n /2 ⌋ y 1 z 2 ) / 2 n ∣ z 2 ⟩ ⌈ n /2 ⌉ ∣ y 2 ⟩ ⌊ n /2 ⌋ (102)
⟶ P ‾ 0 , ⌈ n / 2 ⌉ , ⌊ n / 2 ⌋ ( ϕ / 2 n ) 1 2 ⌈ n / 2 ⌉ ∑ z 2 ∈ Z 2 ⌈ n / 2 ⌉ e 2 π i ϕ ( 2 ⌊ n / 2 ⌋ y 1 z 2 + y 2 z 2 ) / 2 n ∣ z 2 ⟩ ⌈ n / 2 ⌉ ∣ y 2 ⟩ ⌊ n / 2 ⌋ (103) \underset{\overline{\mathbf{P}}_{0,\lceil n/2\rceil,\lfloor n/2\rfloor}(\phi/2^n)}{\longrightarrow}\frac{1}{\sqrt{2^{\lceil n/2\rceil}}}\sum_{z_2\in\mathbb{Z}_{2^{\lceil n/2\rceil}}}e^{2\pi i\phi(2^{\lfloor n/2\rfloor}y_1z_2+y_2z_2)/2^n}|z_2\rangle_{\lceil n/2\rceil}|y_2\rangle_{\lfloor n/2\rfloor} \tag*{(103)} P 0 , ⌈ n /2 ⌉ , ⌊ n /2 ⌋ ( ϕ / 2 n ) ⟶ 2 ⌈ n /2 ⌉ 1 z 2 ∈ Z 2 ⌈ n /2 ⌉ ∑ e 2 π i ϕ ( 2 ⌊ n /2 ⌋ y 1 z 2 + y 2 z 2 ) / 2 n ∣ z 2 ⟩ ⌈ n /2 ⌉ ∣ y 2 ⟩ ⌊ n /2 ⌋ (103)
⟶ I ⌈ n / 2 ⌉ ⊗ Q F T 0 , ⌊ n / 2 ⌋ ( ϕ ) 1 2 n ∑ z 1 ∈ Z 2 ⌊ n / 2 ⌋ z 2 ∈ Z 2 ⌈ n / 2 ⌉ e 2 π i ϕ ( 2 ⌊ n / 2 ⌋ y 1 z 2 + 2 ⌈ n / 2 ⌉ z 1 y 2 + y 2 z 2 ) / 2 n ∣ z 2 ⟩ ⌈ n / 2 ⌉ ∣ z 1 ⟩ ⌊ n / 2 ⌋ (104) \underset{I_{\lceil n/2\rceil}\otimes\mathbf{QFT}_{0,\lfloor n/2\rfloor}(\phi)}{\longrightarrow}\frac{1}{\sqrt{2^n}}\sum_{\substack{z_1\in\mathbb{Z}_{2^{\lfloor n/2\rfloor}}\\ z_2\in\mathbb{Z}_{2^{\lceil n/2\rceil}}}}e^{2\pi i\phi(2^{\lfloor n/2\rfloor}y_1z_2+2^{\lceil n/2\rceil}z_1y_2+y_2z_2)/2^n}|z_2\rangle_{\lceil n/2\rceil}|z_1\rangle_{\lfloor n/2\rfloor} \tag*{(104)} I ⌈ n /2 ⌉ ⊗ QFT 0 , ⌊ n /2 ⌋ ( ϕ ) ⟶ 2 n 1 z 1 ∈ Z 2 ⌊ n /2 ⌋ z 2 ∈ Z 2 ⌈ n /2 ⌉ ∑ e 2 π i ϕ ( 2 ⌊ n /2 ⌋ y 1 z 2 + 2 ⌈ n /2 ⌉ z 1 y 2 + y 2 z 2 ) / 2 n ∣ z 2 ⟩ ⌈ n /2 ⌉ ∣ z 1 ⟩ ⌊ n /2 ⌋ (104)
= 1 2 n ∑ z 1 ∈ Z 2 ⌊ n / 2 ⌋ z 2 ∈ Z 2 ⌈ n / 2 ⌉ e 2 π i ϕ ( 2 ⌊ n / 2 ⌋ y 1 + y 2 ) ( 2 ⌈ n / 2 ⌉ z 1 + z 2 ) / 2 n ∣ z 2 ⟩ ⌈ n / 2 ⌉ ∣ z 1 ⟩ ⌊ n / 2 ⌋ . (105) =\frac{1}{\sqrt{2^n}}\sum_{\substack{z_1\in\mathbb{Z}_{2^{\lfloor n/2\rfloor}}\\ z_2\in\mathbb{Z}_{2^{\lceil n/2\rceil}}}}e^{2\pi i\phi(2^{\lfloor n/2\rfloor}y_1+y_2)(2^{\lceil n/2\rceil}z_1+z_2)/2^n}|z_2\rangle_{\lceil n/2\rceil}|z_1\rangle_{\lfloor n/2\rfloor}. \tag*{(105)} = 2 n 1 z 1 ∈ Z 2 ⌊ n /2 ⌋ z 2 ∈ Z 2 ⌈ n /2 ⌉ ∑ e 2 π i ϕ ( 2 ⌊ n /2 ⌋ y 1 + y 2 ) ( 2 ⌈ n /2 ⌉ z 1 + z 2 ) / 2 n ∣ z 2 ⟩ ⌈ n /2 ⌉ ∣ z 1 ⟩ ⌊ n /2 ⌋ . (105)
Since the map ( z ‾ 2 , z ‾ 1 ) ↦ 2 ⌊ n / 2 ⌋ z ‾ 2 + z ‾ 1 = 2 ⌈ n / 2 ⌉ z 1 + z 2 (\overline{z}_2,\overline{z}_1)\mapsto2^{\lfloor n/2\rfloor}\overline{z}_2+\overline{z}_1=2^{\lceil n/2\rceil}z_1+z_2 ( z 2 , z 1 ) ↦ 2 ⌊ n /2 ⌋ z 2 + z 1 = 2 ⌈ n /2 ⌉ z 1 + z 2 is a one-to-one correspondence between Z 2 ⌈ n / 2 ⌉ × Z 2 ⌊ n / 2 ⌋ \mathbb{Z}_{2^{\lceil n/2\rceil}}\times\mathbb{Z}_{2^{\lfloor n/2\rfloor}} Z 2 ⌈ n /2 ⌉ × Z 2 ⌊ n /2 ⌋ and Z 2 n \mathbb{Z}_{2^n} Z 2 n , we can rewrite this as
= 1 2 n ∑ z ∈ Z 2 n e 2 π i ϕ y z / 2 n ∣ z ⟩ n . (106) =\frac{1}{\sqrt{2^n}}\sum_{z\in\mathbb{Z}_{2^n}}e^{2\pi i\phi yz/2^n}|z\rangle_n. \tag*{(106)} = 2 n 1 z ∈ Z 2 n ∑ e 2 π i ϕ y z / 2 n ∣ z ⟩ n . (106)
Therefore, the problem of implementing Q F T 0 , n ( ϕ ) \mathbf{QFT}_{0,n}(\phi) QFT 0 , n ( ϕ ) reduces to the problem of implementing the base case of this recursion Q F T 0 , 1 ( ϕ ) \mathbf{QFT}_{0,1}(\phi) QFT 0 , 1 ( ϕ ) and P ‾ 0 , ⌈ t / 2 ⌉ , ⌊ t / 2 ⌋ ( ϕ / 2 t ) \overline{\mathbf{P}}_{0,\lceil t/2\rceil,\lfloor t/2\rfloor}(\phi/2^t) P 0 , ⌈ t /2 ⌉ , ⌊ t /2 ⌋ ( ϕ / 2 t ) for any t t t in the range 2 ≤ t ≤ n 2\leq t\leq n 2 ≤ t ≤ n . Since ϕ ∈ Z 2 n \phi\in\mathbb{Z}_{2^n} ϕ ∈ Z 2 n , the least significant bit of ϕ \phi ϕ equals 1. Thus,
Q F T 0 , 1 ( ϕ ) ∣ y k ⟩ 1 = 1 2 ( ∣ 0 ⟩ + e 2 π i ϕ y k / 2 ∣ 1 ⟩ ) = 1 2 ( ∣ 0 ⟩ + e 2 π i y k / 2 ∣ 1 ⟩ ) = H ∣ y k ⟩ 1 . (107) \mathbf{QFT}_{0,1}(\phi)|y_k\rangle_1=\frac{1}{\sqrt{2}}\left(|0\rangle+e^{2\pi i\phi y_k/2}|1\rangle\right)=\frac{1}{\sqrt{2}}\left(|0\rangle+e^{2\pi i y_k/2}|1\rangle\right)=H|y_k\rangle_1. \tag*{(107)} QFT 0 , 1 ( ϕ ) ∣ y k ⟩ 1 = 2 1 ( ∣0 ⟩ + e 2 π i ϕ y k /2 ∣1 ⟩ ) = 2 1 ( ∣0 ⟩ + e 2 π i y k /2 ∣1 ⟩ ) = H ∣ y k ⟩ 1 . (107)
So the base case Q F T 0 , 1 ( ϕ ) \mathbf{QFT}_{0,1}(\phi) QFT 0 , 1 ( ϕ ) is just a Hadamard gate. From Lemma 2.2 we know how to implement each of the gates P ‾ 0 , ⌈ t / 2 ⌉ , ⌊ t / 2 ⌋ ( ϕ / 2 t ) \overline{\mathbf{P}}_{0,\lceil t/2\rceil,\lfloor t/2\rfloor}(\phi/2^t) P 0 , ⌈ t /2 ⌉ , ⌊ t /2 ⌋ ( ϕ / 2 t ) for 2 ≤ t ≤ n 2\leq t\leq n 2 ≤ t ≤ n . Thus, by the recursive definition above and Lemma 2.2 , it follows that, for any ϵ > 0 \epsilon>0 ϵ > 0 ,
G ( Q F T 0 , n ( ϕ ) ) ≤ 2 G ( Q F T 0 , ⌈ n / 2 ⌉ ( ϕ ) ) + O ( ⌈ n / 2 ⌉ 1 + ϵ ) = O ( n 1 + ϵ ) (108) G(\mathbf{QFT}_{0,n}(\phi))\leq2G(\mathbf{QFT}_{0,\lceil n/2\rceil}(\phi))+O(\lceil n/2\rceil^{1+\epsilon})=O(n^{1+\epsilon}) \tag*{(108)} G ( QFT 0 , n ( ϕ )) ≤ 2 G ( QFT 0 , ⌈ n /2 ⌉ ( ϕ )) + O (⌈ n /2 ⌉ 1 + ϵ ) = O ( n 1 + ϵ ) (108)
and
D ( Q F T 0 , n ( ϕ ) ) ≤ 2 D ( Q F T 0 , ⌈ n / 2 ⌉ ( ϕ ) ) + O ( ⌈ n / 2 ⌉ ) = O ( n log n ) . (109) D(\mathbf{QFT}_{0,n}(\phi)) \le2D(\mathbf{QFT}_{0,\lceil n/2\rceil}(\phi)) + O(\lceil n/2\rceil) = O(n\log n). \tag*{(109)} D ( QFT 0 , n ( ϕ )) ≤ 2 D ( QFT 0 , ⌈ n /2 ⌉ ( ϕ )) + O (⌈ n /2 ⌉) = O ( n log n ) . (109)
Next, we prove the claim for arbitrary m m m . Using the recursive definition for Q F T 0 , n ( ϕ ) \mathbf{QFT}_{0,n}(\phi) QFT 0 , n ( ϕ ) , we give a recursive definition for Q F T m , n ( ϕ , α ) \mathbf{QFT}_{m,n}(\phi,\alpha) QFT m , n ( ϕ , α ) . To recap, Q F T m , n ( ϕ , α ) \mathbf{QFT}_{m,n}(\phi,\alpha) QFT m , n ( ϕ , α ) is defined as follows. Let x ∈ Z 2 m x \in\mathbb{Z}_{2^m} x ∈ Z 2 m , y ∈ Z 2 n y \in\mathbb{Z}_{2^n} y ∈ Z 2 n and ϕ ∈ Z 2 n \phi\in\mathbb{Z}_{2^n} ϕ ∈ Z 2 n . Let α ∈ { 0 , 1 } \alpha\in\{0,1\} α ∈ { 0 , 1 } . Then
Q F T m , n ( ϕ , α ) : ∣ x ⟩ m ∣ y ⟩ n ↦ ∣ x ⟩ m 1 2 n ∑ z ∈ Z 2 n e 2 π i ϕ y z x α / 2 n ∣ z ⟩ n . (110) \mathbf{QFT}_{m,n}(\phi,\alpha): |x\rangle_m|y\rangle_n \mapsto|x\rangle_m \frac{1}{\sqrt{2^n}}\sum_{z\in\mathbb{Z}_{2^n}} e^{2\pi i\phi yz x^\alpha/2^n}|z\rangle_n. \tag*{(110)} QFT m , n ( ϕ , α ) : ∣ x ⟩ m ∣ y ⟩ n ↦ ∣ x ⟩ m 2 n 1 z ∈ Z 2 n ∑ e 2 π i ϕ y z x α / 2 n ∣ z ⟩ n . (110)
Q F T m , n ( ϕ , 0 ) \mathbf{QFT}_{m,n}(\phi,0) QFT m , n ( ϕ , 0 ) requires the same gate count and depth as Q F T 0 , n ( ϕ ) \mathbf{QFT}_{0,n}(\phi) QFT 0 , n ( ϕ ) since Q F T m , n ( ϕ , 0 ) = I m ⊗ Q F T 0 , n ( ϕ ) \mathbf{QFT}_{m,n}(\phi,0)=I_m\otimes\mathbf{QFT}_{0,n}(\phi) QFT m , n ( ϕ , 0 ) = I m ⊗ QFT 0 , n ( ϕ ) . As for Q F T m , n ( ϕ , 1 ) \mathbf{QFT}_{m,n}(\phi,1) QFT m , n ( ϕ , 1 ) , controlling on the recursive definition of Q F T 0 , n ( ϕ ) \mathbf{QFT}_{0,n}(\phi) QFT 0 , n ( ϕ ) gives Q F T m , n ( ϕ , 1 ) = \mathbf{QFT}_{m,n}(\phi,1)= QFT m , n ( ϕ , 1 ) =
( I ⌈ n / 2 ⌉ ⊗ Q F T m , ⌊ n / 2 ⌋ ( ϕ , 1 ) ) ∘ ( P ‾ m , ⌈ n / 2 ⌉ , ⌊ n / 2 ⌋ ( ϕ / 2 n , 1 ) ) ∘ ( Q F T m , ⌈ n / 2 ⌉ ( ϕ , 1 ) ⊗ I ⌊ n / 2 ⌋ ) . \left(I_{\lceil n/2\rceil}\otimes\mathbf{QFT}_{m,\lfloor n/2\rfloor}(\phi,1)\right)\circ\left(\overline{\mathbf{P}}_{m,\lceil n/2\rceil,\lfloor n/2\rfloor}(\phi/2^n,1)\right)\circ\left(\mathbf{QFT}_{m,\lceil n/2\rceil}(\phi,1)\otimes I_{\lfloor n/2\rfloor}\right). ( I ⌈ n /2 ⌉ ⊗ QFT m , ⌊ n /2 ⌋ ( ϕ , 1 ) ) ∘ ( P m , ⌈ n /2 ⌉ , ⌊ n /2 ⌋ ( ϕ / 2 n , 1 ) ) ∘ ( QFT m , ⌈ n /2 ⌉ ( ϕ , 1 ) ⊗ I ⌊ n /2 ⌋ ) .
Since e 2 π i ψ x / 2 t = e 2 π i ψ ( x m o d 2 t ) / 2 t e^{2\pi i\psi x/2^t}=e^{2\pi i\psi(x\bmod2^t)/2^t} e 2 π i ψ x / 2 t = e 2 π i ψ ( x mod 2 t ) / 2 t for any integer ψ \psi ψ , it is clear that controlling on all m m m qubits of ∣ x ⟩ m |x\rangle_m ∣ x ⟩ m in the above operations is not always necessary. If m ≥ ⌊ n / 2 ⌋ m\ge\lfloor n/2\rfloor m ≥ ⌊ n /2 ⌋ or m ≥ ⌈ n / 2 ⌉ m\ge\lceil n/2\rceil m ≥ ⌈ n /2 ⌉ , then we just need to control on the least significant ⌊ n / 2 ⌋ \lfloor n/2\rfloor ⌊ n /2 ⌋ or ⌈ n / 2 ⌉ \lceil n/2\rceil ⌈ n /2 ⌉ qubits when performing Q F T m , ⌊ n / 2 ⌋ ( ϕ , 1 ) \mathbf{QFT}_{m,\lfloor n/2\rfloor}(\phi,1) QFT m , ⌊ n /2 ⌋ ( ϕ , 1 ) or Q F T m , ⌈ n / 2 ⌉ ( ϕ , 1 ) \mathbf{QFT}_{m,\lceil n/2\rceil}(\phi,1) QFT m , ⌈ n /2 ⌉ ( ϕ , 1 ) . Similarly if m ≥ n m\ge n m ≥ n , we should control on just the n n n least significant qubits of ∣ x ⟩ m |x\rangle_m ∣ x ⟩ m when performing P ‾ m , ⌈ n / 2 ⌉ , ⌊ n / 2 ⌋ ( ϕ / 2 n , 1 ) \overline{\mathbf{P}}_{m,\lceil n/2\rceil,\lfloor n/2\rfloor}(\phi/2^n,1) P m , ⌈ n /2 ⌉ , ⌊ n /2 ⌋ ( ϕ / 2 n , 1 ) . It follows that,
Q F T m , n ( ϕ , 1 ) = ( I ⌈ n / 2 ⌉ ⊗ Q F T min { m , ⌊ n / 2 ⌋ } , ⌊ n / 2 ⌋ ( ϕ , 1 ) ) ∘ \mathbf{QFT}_{m,n}(\phi,1)=\left(I_{\lceil n/2\rceil}\otimes\mathbf{QFT}_{\min\{m,\lfloor n/2\rfloor\},\lfloor n/2\rfloor}(\phi,1)\right)\circ QFT m , n ( ϕ , 1 ) = ( I ⌈ n /2 ⌉ ⊗ QFT m i n { m , ⌊ n /2 ⌋} , ⌊ n /2 ⌋ ( ϕ , 1 ) ) ∘
( P ‾ min { m , n } , ⌈ n / 2 ⌉ , ⌊ n / 2 ⌋ ( ϕ / 2 n , 1 ) ) ∘ ( Q F T min { m , ⌈ n / 2 ⌉ } , ⌈ n / 2 ⌉ ( ϕ , 1 ) ⊗ I ⌊ n / 2 ⌋ ) . \left(\overline{\mathbf{P}}_{\min\{m,n\},\lceil n/2\rceil,\lfloor n/2\rfloor}(\phi/2^n,1)\right)\circ\left(\mathbf{QFT}_{\min\{m,\lceil n/2\rceil\},\lceil n/2\rceil}(\phi,1)\otimes I_{\lfloor n/2\rfloor}\right). ( P m i n { m , n } , ⌈ n /2 ⌉ , ⌊ n /2 ⌋ ( ϕ / 2 n , 1 ) ) ∘ ( QFT m i n { m , ⌈ n /2 ⌉} , ⌈ n /2 ⌉ ( ϕ , 1 ) ⊗ I ⌊ n /2 ⌋ ) .
Therefore, the problem of implementing Q F T m , n ( ϕ , 1 ) \mathbf{QFT}_{m,n}(\phi,1) QFT m , n ( ϕ , 1 ) reduces to the problem of implementing the base case of this recursion Q F T 1 , 1 ( ϕ , 1 ) \mathbf{QFT}_{1,1}(\phi,1) QFT 1 , 1 ( ϕ , 1 ) and P ‾ s , ⌈ t / 2 ⌉ , ⌊ t / 2 ⌋ ( ϕ / 2 t ) \overline{\mathbf{P}}_{s,\lceil t/2\rceil,\lfloor t/2\rfloor}(\phi/2^t) P s , ⌈ t /2 ⌉ , ⌊ t /2 ⌋ ( ϕ / 2 t ) for any t t t in the range 2 ≤ t ≤ n 2\le t\le n 2 ≤ t ≤ n and any s s s in the range 1 ≤ s ≤ t 1\le s\le t 1 ≤ s ≤ t .
Since x ∈ Z 2 m x\in\mathbb{Z}_{2^m} x ∈ Z 2 m and ϕ ∈ Z 2 n \phi\in\mathbb{Z}_{2^n} ϕ ∈ Z 2 n , Q F T 1 , 1 ( ϕ , 1 ) ∣ x 0 ⟩ 1 ∣ y k ⟩ 1 = \mathbf{QFT}_{1,1}(\phi,1)|x_0\rangle_1|y_k\rangle_1= QFT 1 , 1 ( ϕ , 1 ) ∣ x 0 ⟩ 1 ∣ y k ⟩ 1 =
= ∣ x 0 ⟩ 1 1 2 ( ∣ 0 ⟩ + e 2 π i ϕ x 0 y k / 2 ∣ 1 ⟩ ) = ∣ x 0 ⟩ 1 1 2 ( ∣ 0 ⟩ + e 2 π i y k / 2 ∣ 1 ⟩ ) = ∣ x 0 ⟩ 1 H ∣ y k ⟩ 1 . (111) =|x_0\rangle_1\frac{1}{\sqrt{2}}\left(|0\rangle+e^{2\pi i\phi x_0y_k/2}|1\rangle\right)=|x_0\rangle_1\frac{1}{\sqrt{2}}\left(|0\rangle+e^{2\pi i y_k/2}|1\rangle\right)=|x_0\rangle_1H|y_k\rangle_1. \tag*{(111)} = ∣ x 0 ⟩ 1 2 1 ( ∣0 ⟩ + e 2 π i ϕ x 0 y k /2 ∣1 ⟩ ) = ∣ x 0 ⟩ 1 2 1 ( ∣0 ⟩ + e 2 π i y k /2 ∣1 ⟩ ) = ∣ x 0 ⟩ 1 H ∣ y k ⟩ 1 . (111)
So the base case Q F T 1 , 1 ( ϕ , 1 ) \mathbf{QFT}_{1,1}(\phi,1) QFT 1 , 1 ( ϕ , 1 ) is again just a Hadamard gate. From Lemma 2.3 we know how to implement each of the gates P ‾ s , ⌈ t / 2 ⌉ , ⌊ t / 2 ⌋ ( ϕ / 2 t ) \overline{\mathbf{P}}_{s,\lceil t/2\rceil,\lfloor t/2\rfloor}(\phi/2^t) P s , ⌈ t /2 ⌉ , ⌊ t /2 ⌋ ( ϕ / 2 t ) for 2 ≤ t ≤ n 2\le t\le n 2 ≤ t ≤ n and 1 ≤ s ≤ t 1\le s\le t 1 ≤ s ≤ t . Thus, by the recursive definition above and Lemma 2.3 , it follows that, for any ϵ > 0 \epsilon>0 ϵ > 0 ,
G ( Q F T m , n ( ϕ , 1 ) ) ≤ 2 G ( Q F T ⌈ n / 2 ⌉ , ⌈ n / 2 ⌉ ( ϕ , 1 ) ) + O ( n 1 + ϵ ) = O ( n 1 + ϵ ) . (112) G(\mathbf{QFT}_{m,n}(\phi,1))\le2G(\mathbf{QFT}_{\lceil n/2\rceil,\lceil n/2\rceil}(\phi,1))+O(n^{1+\epsilon})=O(n^{1+\epsilon}). \tag*{(112)} G ( QFT m , n ( ϕ , 1 )) ≤ 2 G ( QFT ⌈ n /2 ⌉ , ⌈ n /2 ⌉ ( ϕ , 1 )) + O ( n 1 + ϵ ) = O ( n 1 + ϵ ) . (112)
Remark B.2. For the same choice of parameter k k k (using the notation of Appendix § 8 ), Q F T 0 , n ( ϕ ) \mathbf{QFT}_{0,n}(\phi) QFT 0 , n ( ϕ ) and Q F T m , n ( ϕ , 1 ) \mathbf{QFT}_{m,n}(\phi,1) QFT m , n ( ϕ , 1 ) have different asymptotic gate counts just like P 0 , m , n ( ϕ ) \mathbf{P}_{0,m,n}(\phi) P 0 , m , n ( ϕ ) and P t , m , n ( ϕ , 1 ) \mathbf{P}_{t,m,n}(\phi,1) P t , m , n ( ϕ , 1 ) . However, by the above result, for any ϵ > 0 \epsilon>0 ϵ > 0 , we can pick an integer k k k that ensures G ( Q F T m , n ( ϕ , α ) ) = O ( n 1 + ϵ ) G(\mathbf{QFT}_{m,n}(\phi,\alpha))=O(n^{1+\epsilon}) G ( QFT m , n ( ϕ , α )) = O ( n 1 + ϵ ) .
Computing the trailing zero count coherently
Lemma C.1 (Trailing zero count). Let m ≥ 1 m \ge1 m ≥ 1 . There exists a quantum circuit for T Z m , ⌈ log ( m + 1 ) ⌉ \mathrm{TZ}_{m,\lceil\log(m+1)\rceil} TZ m , ⌈ l o g ( m + 1 )⌉ that uses O ( m ) O(m) O ( m ) gates and no ancillas.
Proof. To recap, T Z m , ⌈ log ( m + 1 ) ⌉ \mathrm{TZ}_{m,\lceil\log(m+1)\rceil} TZ m , ⌈ l o g ( m + 1 )⌉ is defined as follows. For x ∈ Z 2 m x \in\mathbb{Z}_{2^m} x ∈ Z 2 m , tz ( x ) = log ( gcd ( x , 2 m ) ) \operatorname{tz}(x)=\log(\gcd(x,2^m)) tz ( x ) = log ( g cd( x , 2 m )) ,
T Z m , ⌈ log ( m + 1 ) ⌉ : ∣ x ⟩ m ∣ 0 ⟩ ⌈ log ( m + 1 ) ⌉ ⟼ ∣ x ⟩ m ∣ tz ( x ) ⟩ ⌈ log ( m + 1 ) ⌉ . (113) \mathrm{TZ}_{m,\lceil\log(m+1)\rceil}: |x\rangle_m |0\rangle_{\lceil\log(m+1)\rceil} \longmapsto|x\rangle_m |\operatorname{tz}(x)\rangle_{\lceil\log(m+1)\rceil}. \tag*{(113)} TZ m , ⌈ l o g ( m + 1 )⌉ : ∣ x ⟩ m ∣0 ⟩ ⌈ l o g ( m + 1 )⌉ ⟼ ∣ x ⟩ m ∣ tz ( x ) ⟩ ⌈ l o g ( m + 1 )⌉ . (113)
This operation counts the number of trailing zeroes tz ( x ) \operatorname{tz}(x) tz ( x ) of the bitstring x x x .
Label the registers ∣ x ⟩ m |x\rangle_m ∣ x ⟩ m and ∣ 0 ⟩ ⌈ log ( m + 1 ) ⌉ |0\rangle_{\lceil\log(m+1)\rceil} ∣0 ⟩ ⌈ l o g ( m + 1 )⌉ , as X X X and Y Y Y . Let CX ( X i → Y j ) \operatorname{CX}(X_i \to Y_j) CX ( X i → Y j ) denote a CNOT gate that is controlled on i th i^{\text{th}} i th qubit of X X X with the j th j^{\text{th}} j th qubit of Y Y Y as the target. Let CCX ( X i , X k → Y j ) \operatorname{CCX}(X_i,X_k \to Y_j) CCX ( X i , X k → Y j ) denote a Toffoli gate with controls X i , X k X_i,X_k X i , X k and target Y j Y_j Y j . We use X i ‾ \overline{X_i} X i to indicate that a gate controlled on X i X_i X i is negatively controlled.
We prove the claim by first proving it for some special cases. For m = 1 m=1 m = 1 , the input is ∣ x ⟩ 1 ∣ 0 ⟩ 1 |x\rangle_1|0\rangle_1 ∣ x ⟩ 1 ∣0 ⟩ 1 so all we need is a single CNOT,
CX ‾ ( X 0 ‾ → Y 0 ) ∣ x ⟩ 1 ∣ 0 ⟩ 1 ⟼ ∣ x ⟩ 1 ∣ 1 ( x = 0 ) ⟩ 1 = ∣ x ⟩ 1 ∣ tz ( x ) ⟩ 1 . (114) \overline{\operatorname{CX}}(\overline{X_0}\to Y_0)\,|x\rangle_1|0\rangle_1 \longmapsto|x\rangle_1|\mathbb{1}(x=0)\rangle_1=|x\rangle_1|\operatorname{tz}(x)\rangle_1. \tag*{(114)} CX ( X 0 → Y 0 ) ∣ x ⟩ 1 ∣0 ⟩ 1 ⟼ ∣ x ⟩ 1 ∣1 ( x = 0 ) ⟩ 1 = ∣ x ⟩ 1 ∣ tz ( x ) ⟩ 1 . (114)
For m = 2 i m=2^i m = 2 i , i ≥ 0 i\ge0 i ≥ 0 we prove the claim by induction on i i i . Note that we have just proved the base case of this induction, which clearly requires O ( 1 ) O(1) O ( 1 ) gates and no ancilla qubits. m = 2 i m=2^i m = 2 i for some i ≥ 0 i\ge0 i ≥ 0 implies ⌈ log ( m + 1 ) ⌉ = i + 1 = log m + 1 \lceil\log(m+1)\rceil=i+1=\log m+1 ⌈ log ( m + 1 )⌉ = i + 1 = log m + 1 .
Assume that T Z m / 2 , log m \mathrm{TZ}_{m/2,\log m} TZ m /2 , l o g m can be implemented using O ( m / 2 ) O(m/2) O ( m /2 ) gates and no ancillas. On input ∣ x ⟩ m ∣ 0 ⟩ log m + 1 |x\rangle_m|0\rangle_{\log m+1} ∣ x ⟩ m ∣0 ⟩ l o g m + 1 , apply T Z m / 2 , log m \mathrm{TZ}_{m/2,\log m} TZ m /2 , l o g m to ∣ x m / 2 − 1 ⋯ x 0 ⟩ m / 2 ∣ 0 ⟩ log m |x_{m/2-1}\cdots x_0\rangle_{m/2}|0\rangle_{\log m} ∣ x m /2 − 1 ⋯ x 0 ⟩ m /2 ∣0 ⟩ l o g m ,
T Z m / 2 , log m ⟼ ∣ x m − 1 ⋯ x m / 2 ⟩ m / 2 ∣ x m / 2 − 1 ⋯ x 0 ⟩ m / 2 ∣ 0 ⟩ 1 ∣ tz ( x ′ ) ⟩ log m , (115) \mathrm{TZ}_{m/2,\log m}\longmapsto|x_{m-1}\cdots x_{m/2}\rangle_{m/2}|x_{m/2-1}\cdots x_0\rangle_{m/2}|0\rangle_1|\operatorname{tz}(x')\rangle_{\log m}, \tag*{(115)} TZ m /2 , l o g m ⟼ ∣ x m − 1 ⋯ x m /2 ⟩ m /2 ∣ x m /2 − 1 ⋯ x 0 ⟩ m /2 ∣0 ⟩ 1 ∣ tz ( x ′ ) ⟩ l o g m , (115)
where here x ′ = x m o d 2 m / 2 x'=x\bmod2^{m/2} x ′ = x mod 2 m /2 . Observe that since tz ( x ′ ) ≤ m / 2 = 2 log m − 1 \operatorname{tz}(x')\le m/2=2^{\log m-1} tz ( x ′ ) ≤ m /2 = 2 l o g m − 1 it can require log m \log m log m qubits to store. Since
tz ( x ) = 1 ( x ′ = 0 ) tz ( x m − 1 ⋯ x m / 2 ) + tz ( x ′ ) , (116) \operatorname{tz}(x)=\mathbb{1}(x'=0)\operatorname{tz}(x_{m-1}\cdots x_{m/2})+\operatorname{tz}(x'), \tag*{(116)} tz ( x ) = 1 ( x ′ = 0 ) tz ( x m − 1 ⋯ x m /2 ) + tz ( x ′ ) , (116)
we need to add tz ( x m − 1 ⋯ x m / 2 ) \operatorname{tz}(x_{m-1}\cdots x_{m/2}) tz ( x m − 1 ⋯ x m /2 ) to the Y Y Y register when x ′ = 0 x'=0 x ′ = 0 and leave Y Y Y unchanged when x ′ ≠ 0 x'\ne0 x ′ = 0 .
Suppose x ′ = 0 x'=0 x ′ = 0 , implying tz ( x ′ ) = 2 log m − 1 \operatorname{tz}(x')=2^{\log m-1} tz ( x ′ ) = 2 l o g m − 1 . Then we have the state
∣ x m − 1 ⋯ x m / 2 ⟩ m / 2 ∣ 0 ⟩ m / 2 ∣ 0 ⟩ 1 ∣ 1 ⟩ 1 ∣ 0 ⟩ log m − 1 . (117) |x_{m-1}\cdots x_{m/2}\rangle_{m/2}|0\rangle_{m/2}|0\rangle_1|1\rangle_1|0\rangle_{\log m-1}. \tag*{(117)} ∣ x m − 1 ⋯ x m /2 ⟩ m /2 ∣0 ⟩ m /2 ∣0 ⟩ 1 ∣1 ⟩ 1 ∣0 ⟩ l o g m − 1 . (117)
In this case, the lower half of the input register provides conditionally clean qubits [37 ] . Apply the following sequence of controlled operations, to the X X X register
CX ‾ ( X m / 2 ‾ → X 0 ) ⟼ ∣ x m − 1 ⋯ x m / 2 ⟩ m / 2 ∣ 0 ⟩ m / 2 − 1 ∣ 1 ( x m / 2 = 0 ) ⟩ 1 ∣ 0 ⟩ 1 ∣ 1 ⟩ 1 ∣ 0 ⟩ log m − 1 . (118) \overline{\operatorname{CX}}(\overline{X_{m/2}}\to X_0)\longmapsto|x_{m-1}\cdots x_{m/2}\rangle_{m/2}|0\rangle_{m/2-1}|\mathbb{1}(x_{m/2}=0)\rangle_1|0\rangle_1|1\rangle_1|0\rangle_{\log m-1}. \tag*{(118)} CX ( X m /2 → X 0 ) ⟼ ∣ x m − 1 ⋯ x m /2 ⟩ m /2 ∣0 ⟩ m /2 − 1 ∣1 ( x m /2 = 0 ) ⟩ 1 ∣0 ⟩ 1 ∣1 ⟩ 1 ∣0 ⟩ l o g m − 1 . (118)
CCX ( X m / 2 + j ‾ , X j − 1 → X j ) ⟼ ∣ x m − 1 ⋯ x m / 2 ⟩ m / 2 ∣ 1 ( x m − 1 = 0 ∧ ⋯ ∧ x m / 2 = 0 ) ⟩ 1 ⋯ ⋯ ∣ 1 ( x m / 2 + 1 = 0 ∧ x m / 2 = 0 ) ⟩ 1 ∣ 1 ( x m / 2 = 0 ) ⟩ 1 ∣ 0 ⟩ 1 ∣ 1 ⟩ 1 ∣ 0 ⟩ log m − 1 , (119) \begin{aligned}
\operatorname{CCX}(\overline{X_{m/2+j}},X_{j-1}\to X_j)
&\longmapsto|x_{m-1}\cdots x_{m/2}\rangle_{m/2}|\mathbb{1}(x_{m-1}=0\land\cdots\land x_{m/2}=0)\rangle_1\cdots\\
&\qquad\cdots|\mathbb{1}(x_{m/2+1}=0\land x_{m/2}=0)\rangle_1|\mathbb{1}(x_{m/2}=0)\rangle_1|0\rangle_1|1\rangle_1|0\rangle_{\log m-1}, \tag*{(119)}
\end{aligned} CCX ( X m /2 + j , X j − 1 → X j ) ⟼ ∣ x m − 1 ⋯ x m /2 ⟩ m /2 ∣1 ( x m − 1 = 0 ∧ ⋯ ∧ x m /2 = 0 ) ⟩ 1 ⋯ ⋯ ∣1 ( x m /2 + 1 = 0 ∧ x m /2 = 0 ) ⟩ 1 ∣1 ( x m /2 = 0 ) ⟩ 1 ∣0 ⟩ 1 ∣1 ⟩ 1 ∣0 ⟩ l o g m − 1 , (119)
where j = 1 , … , m / 2 − 1 j=1,\ldots,m/2-1 j = 1 , … , m /2 − 1 .
For each j ∈ Z m / 2 j\in\mathbb{Z}_{m/2} j ∈ Z m /2 , the indicator variable on X j X_j X j tells us if tz ( x m − 1 ⋯ x m / 2 ) ≥ j + 1 \operatorname{tz}(x_{m-1}\cdots x_{m/2})\ge j+1 tz ( x m − 1 ⋯ x m /2 ) ≥ j + 1 . We can first add tz ( x m − 2 ⋯ x m / 2 ) \operatorname{tz}(x_{m-2}\cdots x_{m/2}) tz ( x m − 2 ⋯ x m /2 ) , which is at most m / 2 − 1 m/2-1 m /2 − 1 , to the last log m − 1 \log m-1 log m − 1 qubits of the Y Y Y register, Y log m − 2 , … , Y 0 Y_{\log m-2},\ldots,Y_0 Y l o g m − 2 , … , Y 0 . This is possible through a sequence of controlled increments by 1, controlled on each of the qubits X 0 , … , X m / 2 − 2 X_0,\ldots,X_{m/2-2} X 0 , … , X m /2 − 2 . This incrementer can be built with a few observations.
For an integer j j j consider the set S j = { k ≥ 0 : 2 k ∣ ( j + 1 ) } S_j=\{k\geq0:2^k\mid(j+1)\} S j = { k ≥ 0 : 2 k ∣ ( j + 1 )} . k ∈ S j k\in S_j k ∈ S j if and only if the bitstrings j j j and j + 1 j+1 j + 1 differ on their k th k^{\text{th}} k th bit. Thus, to map ∣ j ⟩ log m − 1 ↦ ∣ j + 1 ⟩ log m − 1 \lvert j\rangle_{\log m-1}\mapsto\lvert j+1\rangle_{\log m-1} ∣ j ⟩ l o g m − 1 ↦ ∣ j + 1 ⟩ l o g m − 1 for any j ∈ Z m / 2 − 1 j\in\mathbb{Z}_{m/2-1} j ∈ Z m /2 − 1 we flip Y k Y_k Y k for each k ∈ S j k\in S_j k ∈ S j . Thus, the following sequence of Toffolis counts the number of trailing zeroes for x m − 2 ⋯ x m / 2 x_{m-2}\cdots x_{m/2} x m − 2 ⋯ x m /2 ,
CCX ( X j , Y log m − 1 → Y k ) j = 0 , … , m / 2 − 2 , k ∈ S j ∣ x m − 1 ⋯ x m / 2 ⟩ m / 2 ∣ 1 ( x m − 1 = 0 ∧ ⋯ ∧ x m / 2 = 0 ) ⟩ 1 ⋯ (120) \underset{\substack{j=0,\ldots,m/2-2,\\ k\in S_j}}{\operatorname{CCX}(X_j,Y_{\log m-1}\to Y_k)}
\lvert x_{m-1}\cdots x_{m/2}\rangle_{m/2}\lvert\mathbb{1}(x_{m-1}=0\land\cdots\land x_{m/2}=0)\rangle_1\cdots \tag*{(120)} j = 0 , … , m /2 − 2 , k ∈ S j CCX ( X j , Y l o g m − 1 → Y k ) ∣ x m − 1 ⋯ x m /2 ⟩ m /2 ∣ 1 ( x m − 1 = 0 ∧ ⋯ ∧ x m /2 = 0 ) ⟩ 1 ⋯ (120)
⋯ ∣ 1 ( x m / 2 + 1 = 0 ∧ x m / 2 = 0 ) ⟩ 1 ∣ 1 ( x m / 2 = 0 ) ⟩ 1 ∣ 0 ⟩ 1 ∣ 1 ⟩ 1 ∣ tz ( x m − 2 ⋯ x m / 2 ) ⟩ log m − 1 . \cdots\lvert\mathbb{1}(x_{m/2+1}=0\land x_{m/2}=0)\rangle_1\lvert\mathbb{1}(x_{m/2}=0)\rangle_1\lvert0\rangle_1\lvert1\rangle_1\lvert\operatorname{tz}(x_{m-2}\cdots x_{m/2})\rangle_{\log m-1}. ⋯ ∣ 1 ( x m /2 + 1 = 0 ∧ x m /2 = 0 ) ⟩ 1 ∣ 1 ( x m /2 = 0 ) ⟩ 1 ∣ 0 ⟩ 1 ∣ 1 ⟩ 1 ∣ tz ( x m − 2 ⋯ x m /2 ) ⟩ l o g m − 1 .
The importance of also controlling on Y log m − 1 Y_{\log m-1} Y l o g m − 1 for these operations will become clear later.
The Y Y Y register is now
tz ( x ′ ) + tz ( x m − 2 ⋯ x m / 2 ) . (121) \operatorname{tz}(x')+\operatorname{tz}(x_{m-2}\cdots x_{m/2}). \tag*{(121)} tz ( x ′ ) + tz ( x m − 2 ⋯ x m /2 ) . (121)
If X m / 2 − 1 = ∣ 0 ⟩ 1 X_{m/2-1}=\lvert0\rangle_1 X m /2 − 1 = ∣ 0 ⟩ 1 , this equals tz ( x ) \operatorname{tz}(x) tz ( x ) . If not, then x = 0 x=0 x = 0 , so tz ( x ) = 2 log m \operatorname{tz}(x)=2^{\log m} tz ( x ) = 2 l o g m and the Y Y Y register contains 2 log m − 1 2^{\log m}-1 2 l o g m − 1 . Applying the following operations, sets Y = ∣ tz ( x ) ⟩ log m + 1 Y=\lvert\operatorname{tz}(x)\rangle_{\log m+1} Y = ∣ tz ( x ) ⟩ l o g m + 1 for x = 0 x=0 x = 0 also.
CCX ( X m / 2 − 1 , Y log m − 1 → Y log m ) ∣ x m − 1 ⋯ x m / 2 ⟩ m / 2 ∣ 1 ( x m − 1 = 0 ∧ ⋯ ∧ x m / 2 = 0 ) ⟩ 1 ⋯ (122) \operatorname{CCX}(X_{m/2-1},Y_{\log m-1}\to Y_{\log m})\lvert x_{m-1}\cdots x_{m/2}\rangle_{m/2}\lvert\mathbb{1}(x_{m-1}=0\land\cdots\land x_{m/2}=0)\rangle_1\cdots \tag*{(122)} CCX ( X m /2 − 1 , Y l o g m − 1 → Y l o g m ) ∣ x m − 1 ⋯ x m /2 ⟩ m /2 ∣ 1 ( x m − 1 = 0 ∧ ⋯ ∧ x m /2 = 0 ) ⟩ 1 ⋯ (122)
∣ 1 ( x m / 2 + 1 = 0 ∧ x m / 2 = 0 ) ⟩ 1 ∣ 1 ( x m / 2 = 0 ) ⟩ 1 ∣ 1 ( x = 0 ) ⟩ 1 ∣ 1 ⟩ 1 ∣ tz ( x m − 2 ⋯ x m / 2 ) ⟩ log m − 1 , \lvert\mathbb{1}(x_{m/2+1}=0\land x_{m/2}=0)\rangle_1\lvert\mathbb{1}(x_{m/2}=0)\rangle_1\lvert\mathbb{1}(x=0)\rangle_1\lvert1\rangle_1\lvert\operatorname{tz}(x_{m-2}\cdots x_{m/2})\rangle_{\log m-1}, ∣ 1 ( x m /2 + 1 = 0 ∧ x m /2 = 0 ) ⟩ 1 ∣ 1 ( x m /2 = 0 ) ⟩ 1 ∣ 1 ( x = 0 ) ⟩ 1 ∣ 1 ⟩ 1 ∣ tz ( x m − 2 ⋯ x m /2 ) ⟩ l o g m − 1 ,
CX ( Y log m → Y j ) j = 0 , … , log m − 1 ∣ x m − 1 ⋯ x m / 2 ⟩ m / 2 ∣ 1 ( x m − 1 = 0 ∧ ⋯ ∧ x m / 2 = 0 ) ⟩ 1 ⋯ (123) \underset{j=0,\ldots,\log m-1}{\operatorname{CX}(Y_{\log m}\to Y_j)}
\lvert x_{m-1}\cdots x_{m/2}\rangle_{m/2}\lvert\mathbb{1}(x_{m-1}=0\land\cdots\land x_{m/2}=0)\rangle_1\cdots \tag*{(123)} j = 0 , … , l o g m − 1 CX ( Y l o g m → Y j ) ∣ x m − 1 ⋯ x m /2 ⟩ m /2 ∣ 1 ( x m − 1 = 0 ∧ ⋯ ∧ x m /2 = 0 ) ⟩ 1 ⋯ (123)
⋯ ∣ 1 ( x m / 2 + 1 = 0 ∧ x m / 2 = 0 ) ⟩ 1 ∣ 1 ( x m / 2 = 0 ) ⟩ 1 ∣ tz ( x ) ⟩ log m + 1 . \cdots\lvert\mathbb{1}(x_{m/2+1}=0\land x_{m/2}=0)\rangle_1\lvert\mathbb{1}(x_{m/2}=0)\rangle_1\lvert\operatorname{tz}(x)\rangle_{\log m+1}. ⋯ ∣ 1 ( x m /2 + 1 = 0 ∧ x m /2 = 0 ) ⟩ 1 ∣ 1 ( x m /2 = 0 ) ⟩ 1 ∣ tz ( x ) ⟩ l o g m + 1 .
Finally, reverse the initial sequence of controlled operations that we used to compute the indicator variables.
CCX ( X ‾ m / 2 + j , X j − 1 → X j ) j = m / 2 − 1 , … , 1 ∣ x m − 1 ⋯ x m / 2 ⟩ m / 2 ∣ 0 ⟩ m / 2 − 1 ∣ 1 ( x m / 2 = 0 ) ⟩ 1 ∣ tz ( x ) ⟩ log m + 1 (124) \underset{j=m/2-1,\ldots,1}{\operatorname{CCX}(\overline{X}_{m/2+j},X_{j-1}\to X_j)}
\lvert x_{m-1}\cdots x_{m/2}\rangle_{m/2}\lvert0\rangle_{m/2-1}\lvert\mathbb{1}(x_{m/2}=0)\rangle_1\lvert\operatorname{tz}(x)\rangle_{\log m+1} \tag*{(124)} j = m /2 − 1 , … , 1 CCX ( X m /2 + j , X j − 1 → X j ) ∣ x m − 1 ⋯ x m /2 ⟩ m /2 ∣ 0 ⟩ m /2 − 1 ∣ 1 ( x m /2 = 0 ) ⟩ 1 ∣ tz ( x ) ⟩ l o g m + 1 (124)
CX ( X ‾ m / 2 → X 0 ) ∣ x m − 1 ⋯ x m / 2 ⟩ m / 2 ∣ 0 ⟩ m / 2 ∣ tz ( x ) ⟩ log m + 1 = ∣ x ⟩ m ∣ tz ( x ) ⟩ log m + 1 . (125) \operatorname{CX}(\overline{X}_{m/2}\to X_0)\lvert x_{m-1}\cdots x_{m/2}\rangle_{m/2}\lvert0\rangle_{m/2}\lvert\operatorname{tz}(x)\rangle_{\log m+1}=\lvert x\rangle_m\lvert\operatorname{tz}(x)\rangle_{\log m+1}. \tag*{(125)} CX ( X m /2 → X 0 ) ∣ x m − 1 ⋯ x m /2 ⟩ m /2 ∣ 0 ⟩ m /2 ∣ tz ( x ) ⟩ l o g m + 1 = ∣ x ⟩ m ∣ tz ( x ) ⟩ l o g m + 1 . (125)
Suppose instead that x ′ ≠ 0 x'\neq0 x ′ = 0 implying tz ( x ) < m / 2 \operatorname{tz}(x)<m/2 tz ( x ) < m /2 . Returning to the input state from Equation C.3, we can clearly rewrite it as
∣ x m − 1 ⋯ x m / 2 ⟩ m / 2 ∣ x m / 2 − 1 ⋯ x 0 ⟩ m / 2 ∣ 0 ⟩ 1 ∣ 0 ⟩ 1 ∣ tz ( x ′ ) ⟩ log m − 1 = ∣ x ⟩ m ∣ tz ( x ) ⟩ log m + 1 . (126) \lvert x_{m-1}\cdots x_{m/2}\rangle_{m/2}\lvert x_{m/2-1}\cdots x_0\rangle_{m/2}\lvert0\rangle_1\lvert0\rangle_1\lvert\operatorname{tz}(x')\rangle_{\log m-1}=\lvert x\rangle_m\lvert\operatorname{tz}(x)\rangle_{\log m+1}. \tag*{(126)} ∣ x m − 1 ⋯ x m /2 ⟩ m /2 ∣ x m /2 − 1 ⋯ x 0 ⟩ m /2 ∣ 0 ⟩ 1 ∣ 0 ⟩ 1 ∣ tz ( x ′ ) ⟩ l o g m − 1 = ∣ x ⟩ m ∣ tz ( x ) ⟩ l o g m + 1 . (126)
Excluding the sequence of controlled operations that we used to compute and uncompute indicator variables, all of the operations in the above algorithm are either controlled on Y log m − 1 Y_{\log m-1} Y l o g m − 1 or Y log m Y_{\log m} Y l o g m . This implies that only the initial and final operations to compute indicator variables are applied when tz ( x ′ ) < m / 2 \operatorname{tz}(x')<m/2 tz ( x ′ ) < m /2 , and these cancel each other out. Thus, the state on the Y Y Y register remains the same.
By induction, there exists a circuit for T Z m , log m + 1 \mathbf{TZ}_{m,\log m+1} TZ m , l o g m + 1 for all m m m satisfying m = 2 i m=2^i m = 2 i for some i ≥ 0 i\geq0 i ≥ 0 . The gate count of this circuit is
G ( T Z m , log m + 1 ) = G ( T Z m / 2 , log m ) + O ( m / 2 ) + O ( ∑ j = 0 m / 2 − 2 ∣ S j ∣ ) = O ( m ) . (127) G(\mathbf{TZ}_{m,\log m+1})=G(\mathbf{TZ}_{m/2,\log m})+O(m/2)+O\left(\sum_{j=0}^{m/2-2}\lvert S_j\rvert\right)=O(m). \tag*{(127)} G ( TZ m , l o g m + 1 ) = G ( TZ m /2 , l o g m ) + O ( m /2 ) + O j = 0 ∑ m /2 − 2 ∣ S j ∣ = O ( m ) . (127)
The circuit also requires no ancillas.
Next, we prove the claim for integers m ≥ 1 m \ge1 m ≥ 1 of the form m ≠ 2 i m \ne2^{i} m = 2 i for all i ≥ 0 i \ge0 i ≥ 0 . In this case m = 2 ⌊ log m ⌋ + t m = 2^{\lfloor\log m \rfloor} + t m = 2 ⌊ l o g m ⌋ + t for some 0 < t < 2 ⌊ log m ⌋ 0 < t < 2^{\lfloor\log m \rfloor} 0 < t < 2 ⌊ l o g m ⌋ . This means ⌈ log ( m + 1 ) ⌉ = ⌊ log m ⌋ + 1 \lceil\log(m+1) \rceil= \lfloor\log m \rfloor+ 1 ⌈ log ( m + 1 )⌉ = ⌊ log m ⌋ + 1 . Let n = 2 ⌊ log m ⌋ n = 2^{\lfloor\log m \rfloor} n = 2 ⌊ l o g m ⌋ . On input ∣ x ⟩ n + t ∣ 0 ⟩ log n + 1 |x\rangle_{n+t}|0\rangle_{\log n+1} ∣ x ⟩ n + t ∣0 ⟩ l o g n + 1 , apply T Z n , log n + 1 \mathbf{TZ}_{n,\log n+1} TZ n , l o g n + 1 to ∣ x n − 1 ⋯ x 0 ⟩ n ∣ 0 ⟩ log n + 1 |x_{n-1}\cdots x_0\rangle_n|0\rangle_{\log n+1} ∣ x n − 1 ⋯ x 0 ⟩ n ∣0 ⟩ l o g n + 1
T Z n , log n + 1 ⟶ ∣ x n + t − 1 ⋯ x n ⟩ t ∣ x n − 1 ⋯ x 0 ⟩ n ∣ tz ( x n − 1 ⋯ x 0 ) ⟩ log n + 1 . (128) \mathbf{TZ}_{n,\log n+1} \longrightarrow|x_{n+t-1}\cdots x_n\rangle_t |x_{n-1}\cdots x_0\rangle_n | \operatorname{tz}(x_{n-1}\cdots x_0)\rangle_{\log n+1}. \tag*{(128)} TZ n , l o g n + 1 ⟶ ∣ x n + t − 1 ⋯ x n ⟩ t ∣ x n − 1 ⋯ x 0 ⟩ n ∣ tz ( x n − 1 ⋯ x 0 ) ⟩ l o g n + 1 . (128)
Then, for the remaining t t t qubits, we apply an algorithm that is similar to the power of two case.
Suppose tz ( x n − 1 ⋯ x 0 ) = n \operatorname{tz}(x_{n-1}\cdots x_0)=n tz ( x n − 1 ⋯ x 0 ) = n . This means our input is
∣ x n + t − 1 ⋯ x n ⟩ t ∣ 0 ⟩ n ∣ 1 ⟩ 1 ∣ 0 ⟩ log n . (129) |x_{n+t-1}\cdots x_n\rangle_t |0\rangle_n |1\rangle_1 |0\rangle_{\log n}. \tag*{(129)} ∣ x n + t − 1 ⋯ x n ⟩ t ∣0 ⟩ n ∣1 ⟩ 1 ∣0 ⟩ l o g n . (129)
Apply the following operations,
⟶ C X ( X ^ n → X 0 ) ∣ x n + t − 1 ⋯ x n ⟩ t ∣ 0 ⟩ n − 1 ∣ 1 ( x n = 0 ) ⟩ 1 ∣ 1 ⟩ 1 ∣ 0 ⟩ log n , (130) \underset{\mathrm{CX}(\widehat{X}_n\to X_0)}{\longrightarrow} |x_{n+t-1}\cdots x_n\rangle_t |0\rangle_{n-1} |1(x_n=0)\rangle_1 |1\rangle_1 |0\rangle_{\log n}, \tag*{(130)} CX ( X n → X 0 ) ⟶ ∣ x n + t − 1 ⋯ x n ⟩ t ∣0 ⟩ n − 1 ∣1 ( x n = 0 ) ⟩ 1 ∣1 ⟩ 1 ∣0 ⟩ l o g n , (130)
C C X ( X ^ n + j , X j − 1 → X j ) j = 1 , … , t − 1 ⟶ ∣ x n + t − 1 ⋯ x n ⟩ t ∣ 0 ⟩ n − t ∣ 1 ( x n + t − 1 = 0 ∧ ⋯ ∧ x n = 0 ) ⟩ 1 ⋯ \underset{j=1,\ldots,t-1}{\mathrm{CCX}(\widehat{X}_{n+j},X_{j-1}\to X_j)} \longrightarrow|x_{n+t-1}\cdots x_n\rangle_t |0\rangle_{n-t} |1(x_{n+t-1}=0\land\cdots\land x_n=0)\rangle_1\cdots j = 1 , … , t − 1 CCX ( X n + j , X j − 1 → X j ) ⟶ ∣ x n + t − 1 ⋯ x n ⟩ t ∣0 ⟩ n − t ∣1 ( x n + t − 1 = 0 ∧ ⋯ ∧ x n = 0 ) ⟩ 1 ⋯
⋯ ∣ 1 ( x n + 1 = 0 ∧ x n = 0 ) ⟩ 1 ∣ 1 ( x n = 0 ) ⟩ 1 ∣ 1 ⟩ 1 ∣ 0 ⟩ log n . (131) \cdots|1(x_{n+1}=0\land x_n=0)\rangle_1 |1(x_n=0)\rangle_1 |1\rangle_1 |0\rangle_{\log n}. \tag*{(131)} ⋯ ∣1 ( x n + 1 = 0 ∧ x n = 0 ) ⟩ 1 ∣1 ( x n = 0 ) ⟩ 1 ∣1 ⟩ 1 ∣0 ⟩ l o g n . (131)
Since tz ( x n + t − 1 ⋯ x n ) ≤ t < 2 log n \operatorname{tz}(x_{n+t-1}\cdots x_n)\le t<2^{\log n} tz ( x n + t − 1 ⋯ x n ) ≤ t < 2 l o g n , we can apply,
C C X ( X j , Y log n → Y k ) j = 0 , … , t − 1 , k ∈ S j ⟶ ∣ x n + t − 1 ⋯ x n ⟩ t ∣ 0 ⟩ n − t ∣ 1 ( x n + t − 1 = 0 ∧ ⋯ ∧ x n = 0 ) ⟩ 1 ⋯ \underset{\substack{j=0,\ldots,t-1,\\ k\in S_j}}{\mathrm{CCX}(X_j,Y_{\log n}\to Y_k)} \longrightarrow|x_{n+t-1}\cdots x_n\rangle_t |0\rangle_{n-t} |1(x_{n+t-1}=0\land\cdots\land x_n=0)\rangle_1\cdots j = 0 , … , t − 1 , k ∈ S j CCX ( X j , Y l o g n → Y k ) ⟶ ∣ x n + t − 1 ⋯ x n ⟩ t ∣0 ⟩ n − t ∣1 ( x n + t − 1 = 0 ∧ ⋯ ∧ x n = 0 ) ⟩ 1 ⋯
⋯ ∣ 1 ( x n + 1 = 0 ∧ x n = 0 ) ⟩ 1 ∣ 1 ( x n = 0 ) ⟩ 1 ∣ 1 ⟩ 1 ∣ tz ( x n + t − 1 ⋯ x n ) ⟩ log n . (132) \cdots|1(x_{n+1}=0\land x_n=0)\rangle_1 |1(x_n=0)\rangle_1 |1\rangle_1 |\operatorname{tz}(x_{n+t-1}\cdots x_n)\rangle_{\log n}. \tag*{(132)} ⋯ ∣1 ( x n + 1 = 0 ∧ x n = 0 ) ⟩ 1 ∣1 ( x n = 0 ) ⟩ 1 ∣1 ⟩ 1 ∣ tz ( x n + t − 1 ⋯ x n ) ⟩ l o g n . (132)
Reversing the initial controlled sequence used to compute the indicator variables, we get,
C C X ( X ^ n + j , X j − 1 → X j ) j = t − 1 , … , 1 ⟶ ∣ x n + t − 1 ⋯ x n ⟩ t ∣ 0 ⟩ n − 1 ∣ 1 ( x n = 0 ) ⟩ 1 ∣ tz ( x ) ⟩ log n + 1 , (133) \underset{j=t-1,\ldots,1}{\mathrm{CCX}(\widehat{X}_{n+j},X_{j-1}\to X_j)} \longrightarrow|x_{n+t-1}\cdots x_n\rangle_t |0\rangle_{n-1} |1(x_n=0)\rangle_1 |\operatorname{tz}(x)\rangle_{\log n+1}, \tag*{(133)} j = t − 1 , … , 1 CCX ( X n + j , X j − 1 → X j ) ⟶ ∣ x n + t − 1 ⋯ x n ⟩ t ∣0 ⟩ n − 1 ∣1 ( x n = 0 ) ⟩ 1 ∣ tz ( x ) ⟩ l o g n + 1 , (133)
⟶ C X ( X ^ n → X 0 ) ∣ x n + t − 1 ⋯ x n ⟩ t ∣ 0 ⟩ n ∣ tz ( x ) ⟩ log n + 1 = ∣ x ⟩ n + t ∣ tz ( x ) ⟩ log n + 1 . (134) \underset{\mathrm{CX}(\widehat{X}_n\to X_0)}{\longrightarrow} |x_{n+t-1}\cdots x_n\rangle_t |0\rangle_n |\operatorname{tz}(x)\rangle_{\log n+1} = |x\rangle_{n+t} |\operatorname{tz}(x)\rangle_{\log n+1}. \tag*{(134)} CX ( X n → X 0 ) ⟶ ∣ x n + t − 1 ⋯ x n ⟩ t ∣0 ⟩ n ∣ tz ( x ) ⟩ l o g n + 1 = ∣ x ⟩ n + t ∣ tz ( x ) ⟩ l o g n + 1 . (134)
Suppose instead tz ( x n − 1 ⋯ x 0 ) < n \operatorname{tz}(x_{n-1}\cdots x_0)<n tz ( x n − 1 ⋯ x 0 ) < n . This implies Y log n ≠ 1 Y_{\log n}\ne1 Y l o g n = 1 , and so the state will remain ∣ x ⟩ m ∣ tz ( x n − 1 ⋯ x 0 ) ⟩ log n + 1 |x\rangle_m|\operatorname{tz}(x_{n-1}\cdots x_0)\rangle_{\log n+1} ∣ x ⟩ m ∣ tz ( x n − 1 ⋯ x 0 ) ⟩ l o g n + 1 after the above operations. This is what we want since tz ( x ) = tz ( x n − 1 ⋯ x 0 ) \operatorname{tz}(x)=\operatorname{tz}(x_{n-1}\cdots x_0) tz ( x ) = tz ( x n − 1 ⋯ x 0 ) . This proves that there exists a circuit for T Z n + t , log n + 1 \mathbf{TZ}_{n+t,\log n+1} TZ n + t , l o g n + 1 with gate count
G ( T Z n + t , log n + 1 ) = G ( T Z n , log n + 1 ) + O ( t ) + O ( ∑ j = 0 t − 1 ∣ S j ∣ ) = O ( n ) , (135) G(\mathbf{TZ}_{n+t,\log n+1})=G(\mathbf{TZ}_{n,\log n+1})+O(t)+O\left(\sum_{j=0}^{t-1}|S_j|\right)=O(n), \tag*{(135)} G ( TZ n + t , l o g n + 1 ) = G ( TZ n , l o g n + 1 ) + O ( t ) + O ( j = 0 ∑ t − 1 ∣ S j ∣ ) = O ( n ) , (135)
requiring no ancillas.
Thus, we have proven that for all m ≥ 1 m\ge1 m ≥ 1 , T Z m , ⌈ log ( m + 1 ) ⌉ \mathbf{TZ}_{m,\lceil\log(m+1)\rceil} TZ m , ⌈ l o g ( m + 1 )⌉ can be implemented using O ( m ) O(m) O ( m ) gates and no ancilla qubits.
Inversion mod 2 n 2^n 2 n
To recap, the operations × \times × and ÷ \div ÷ are defined for x ∈ Z 2 m × x \in\mathbb{Z}_{2^m}^{\times} x ∈ Z 2 m × and y ∈ Z 2 n y \in\mathbb{Z}_{2^n} y ∈ Z 2 n as follows,
× ( x ) : ∣ x ⟩ m ∣ y ⟩ n ⟼ ∣ x ⟩ m ∣ y x m o d 2 n ⟩ n , ÷ ( x ) : ∣ x ⟩ m ∣ y ⟩ n ⟼ ∣ x ⟩ m ∣ y ( x − 1 ) m o d 2 n ⟩ n . \times(x): |x\rangle_m |y\rangle_n \longmapsto|x\rangle_m |yx \bmod2^n\rangle_n,\qquad\div(x): |x\rangle_m |y\rangle_n \longmapsto|x\rangle_m |y(x^{-1}) \bmod2^n\rangle_n. × ( x ) : ∣ x ⟩ m ∣ y ⟩ n ⟼ ∣ x ⟩ m ∣ y x mod 2 n ⟩ n , ÷ ( x ) : ∣ x ⟩ m ∣ y ⟩ n ⟼ ∣ x ⟩ m ∣ y ( x − 1 ) mod 2 n ⟩ n .
Since Lemma 5.3 also requires modular additions, we introduce the symbols + + + and − - − representing quantum circuits that are defined for x ∈ Z 2 m x \in\mathbb{Z}_{2^m} x ∈ Z 2 m and y ∈ Z 2 n y \in\mathbb{Z}_{2^n} y ∈ Z 2 n as follows,
+ ( x ) : ∣ x ⟩ m ∣ y ⟩ n ⟼ ∣ x ⟩ m ∣ y + x m o d 2 n ⟩ n , − ( x ) : ∣ x ⟩ m ∣ y ⟩ n ⟼ ∣ x ⟩ m ∣ y − x m o d 2 n ⟩ n . +(x): |x\rangle_m |y\rangle_n \longmapsto|x\rangle_m |y+x \bmod2^n\rangle_n,\qquad-(x): |x\rangle_m |y\rangle_n \longmapsto|x\rangle_m |y-x \bmod2^n\rangle_n. + ( x ) : ∣ x ⟩ m ∣ y ⟩ n ⟼ ∣ x ⟩ m ∣ y + x mod 2 n ⟩ n , − ( x ) : ∣ x ⟩ m ∣ y ⟩ n ⟼ ∣ x ⟩ m ∣ y − x mod 2 n ⟩ n .
As justified in the main text, for m = O ( n ) m=O(n) m = O ( n ) and any ϵ > 0 \epsilon>0 ϵ > 0 , × \times × and ÷ \div ÷ require O ( n 1 + ϵ ) O(n^{1+\epsilon}) O ( n 1 + ϵ ) gates and no ancilla qubits. By [28 Sections 2.9–2.10] , + + + and − - − require O ( n ) O(n) O ( n ) gates and at most one dirty qubit. We drop the mod 2 n 2^n 2 n notation from equations on the understanding that all operations are modular.
Lemma D.1 (Doubling the number of inverse bits). Let 2 ≤ 2 h < m 2 \le2h < m 2 ≤ 2 h < m . Let x ∈ Z 2 m × x \in\mathbb{Z}_{2^m}^{\times} x ∈ Z 2 m × , y ∈ Z 2 h × y \in\mathbb{Z}_{2^h}^{\times} y ∈ Z 2 h × and z ∈ Z 2 m − h z \in\mathbb{Z}_{2^{m-h}} z ∈ Z 2 m − h such that x y ≡ 1 + 2 h z ( m o d 2 m ) xy \equiv1+2^h z \pmod{2^m} x y ≡ 1 + 2 h z ( mod 2 m ) . There exists a quantum circuit for performing the operation
∣ z ⟩ m − h ∣ y ⟩ h ⟼ ∣ z ′ ⟩ m − 2 h ∣ y ′ ⟩ 2 h , (136) |z\rangle_{m-h}|y\rangle_h \longmapsto|z'\rangle_{m-2h}|y'\rangle_{2h}, \tag*{(136)} ∣ z ⟩ m − h ∣ y ⟩ h ⟼ ∣ z ′ ⟩ m − 2 h ∣ y ′ ⟩ 2 h , (136) with y ′ ∈ Z 2 2 h y' \in\mathbb{Z}_{2^{2h}} y ′ ∈ Z 2 2 h , z ′ ∈ Z 2 m − 2 h z' \in\mathbb{Z}_{2^{m-2h}} z ′ ∈ Z 2 m − 2 h satisfying the relation x y ′ ≡ 1 + 2 2 h z ′ ( m o d 2 m ) xy' \equiv1+2^{2h}z' \pmod{2^m} x y ′ ≡ 1 + 2 2 h z ′ ( mod 2 m ) . The circuit uses six modular multiplications, four modular additions and no ancilla qubits.
Proof. We start by proving that y ′ = y + 2 h ( ( − y z ) m o d 2 h ) y'=y+2^h((-yz)\bmod2^h) y ′ = y + 2 h (( − y z ) mod 2 h ) is the unique multiplicative inverse of x x x mod 2 2 h 2^{2h} 2 2 h . Since y ′ ≡ − 2 h y z + y ( m o d 2 2 h ) y'\equiv-2^hyz+y\pmod{2^{2h}} y ′ ≡ − 2 h y z + y ( mod 2 2 h ) , the claim follows from
x ( − 2 h y z + y ) = x y ( − 2 h z + 1 ) ≡ ( 1 + 2 h z ) ( − 2 h z + 1 ) ≡ 1 ( m o d 2 2 h ) . (137) x(-2^hyz+y)=xy(-2^hz+1)\equiv(1+2^hz)(-2^hz+1)\equiv1\pmod{2^{2h}}. \tag*{(137)} x ( − 2 h y z + y ) = x y ( − 2 h z + 1 ) ≡ ( 1 + 2 h z ) ( − 2 h z + 1 ) ≡ 1 ( mod 2 2 h ) . (137)
This motivates the first step of our algorithm. On input ∣ z ⟩ m − h ∣ y ⟩ h |z\rangle_{m-h}|y\rangle_h ∣ z ⟩ m − h ∣ y ⟩ h , apply
→ × ( − y ) ∣ − y z ⟩ m − h ∣ y ⟩ h . (138) \xrightarrow{\times(-y)} |{-yz}\rangle_{m-h}|y\rangle_h. \tag*{(138)} × ( − y ) ∣ − y z ⟩ m − h ∣ y ⟩ h . (138)
Thus, the least significant 2 h 2h 2 h qubits of the input are now ∣ − y z m o d 2 h ⟩ h ∣ y ⟩ h = ∣ y ′ ⟩ 2 h |-yz\bmod2^h\rangle_h|y\rangle_h=|y'\rangle_{2h} ∣ − y z mod 2 h ⟩ h ∣ y ⟩ h = ∣ y ′ ⟩ 2 h . Our task becomes to prepare ∣ z ′ ⟩ m − 2 h |z'\rangle_{m-2h} ∣ z ′ ⟩ m − 2 h on the most significant m − 2 h m-2h m − 2 h qubits. To do this we need to express z ′ z' z ′ in terms of the current state.
Consider v ∈ Z 2 m − 2 h v\in\mathbb{Z}_{2^{m-2h}} v ∈ Z 2 m − 2 h and u ∈ Z 2 h u\in\mathbb{Z}_{2^h} u ∈ Z 2 h such that − y z m o d 2 m − h = 2 h v + u -yz\bmod2^{m-h}=2^hv+u − y z mod 2 m − h = 2 h v + u . We claim that,
z ′ = − y ′ v + u 2 y 2 ( m o d 2 m − 2 h ) . (139) z'=-\frac{y'v+u^2}{y^2}\pmod{2^{m-2h}}. \tag*{(139)} z ′ = − y 2 y ′ v + u 2 ( mod 2 m − 2 h ) . (139)
To see this, observe that x y ′ 2 ≡ y + 2 h y z ≡ y − 2 h u − 2 2 h v ( m o d 2 m ) xy'^2\equiv y+2^hyz\equiv y-2^hu-2^{2h}v\pmod{2^m} x y ′2 ≡ y + 2 h y z ≡ y − 2 h u − 2 2 h v ( mod 2 m ) . Therefore,
y 2 2 2 h z ′ ≡ y 2 ( x y ′ − 1 ) ≡ ( y − 2 h u − 2 2 h v ) ( 2 h u + y ) − y 2 ≡ − 2 2 h u 2 − 2 2 h y ′ v ( m o d 2 m ) . (140) \begin{aligned}
y^2 2^{2h}z'&\equiv y^2(xy'-1)\\
&\equiv(y-2^hu-2^{2h}v)(2^hu+y)-y^2\\
&\equiv-2^{2h}u^2-2^{2h}y'v\pmod{2^m}. \tag*{(140)}
\end{aligned} y 2 2 2 h z ′ ≡ y 2 ( x y ′ − 1 ) ≡ ( y − 2 h u − 2 2 h v ) ( 2 h u + y ) − y 2 ≡ − 2 2 h u 2 − 2 2 h y ′ v ( mod 2 m ) . (140)
This makes clear the algorithm we require. Rewriting the current state as ∣ v ⟩ m − 2 h ∣ u ⟩ h ∣ y ⟩ h |v\rangle_{m-2h}|u\rangle_h|y\rangle_h ∣ v ⟩ m − 2 h ∣ u ⟩ h ∣ y ⟩ h , we apply the following sequence of operations,
→ × ( y ′ ) ∣ y ′ v ⟩ m − 2 h ∣ u ⟩ h ∣ y ⟩ h (141) \xrightarrow{\times(y')} |y'v\rangle_{m-2h}|u\rangle_h|y\rangle_h \tag*{(141)} × ( y ′ ) ∣ y ′ v ⟩ m − 2 h ∣ u ⟩ h ∣ y ⟩ h (141)
→ + ( u 2 ) ∣ y ′ v + u 2 ⟩ m − 2 h ∣ u ⟩ h ∣ y ⟩ h → ÷ ( y ) ∣ ( y ′ v + u 2 ) / y ⟩ m − 2 h ∣ u ⟩ h ∣ y ⟩ h → ÷ ( − y ) ∣ − ( y ′ v + u 2 ) / y 2 ⟩ m − 2 h ∣ u ⟩ h ∣ y ⟩ h = ∣ z ′ ⟩ m − 2 h ∣ y ⟩ 2 h . (142) \begin{aligned}
&\xrightarrow{+(u^2)} |y'v+u^2\rangle_{m-2h}|u\rangle_h|y\rangle_h \\
&\xrightarrow{\div(y)} |(y'v+u^2)/y\rangle_{m-2h}|u\rangle_h|y\rangle_h \\
&\xrightarrow{\div(-y)} |-(y'v+u^2)/y^2\rangle_{m-2h}|u\rangle_h|y\rangle_h = |z'\rangle_{m-2h}|y\rangle_{2h}. \tag*{(142)}
\end{aligned} + ( u 2 ) ∣ y ′ v + u 2 ⟩ m − 2 h ∣ u ⟩ h ∣ y ⟩ h ÷ ( y ) ∣ ( y ′ v + u 2 ) / y ⟩ m − 2 h ∣ u ⟩ h ∣ y ⟩ h ÷ ( − y ) ∣ − ( y ′ v + u 2 ) / y 2 ⟩ m − 2 h ∣ u ⟩ h ∣ y ⟩ h = ∣ z ′ ⟩ m − 2 h ∣ y ⟩ 2 h . (142)
The multiplication by y ′ y' y ′ uses ∣ u ⟩ h ∣ y ⟩ h = ∣ y ′ ⟩ 2 h |u\rangle_h|y\rangle_h=|y'\rangle_{2h} ∣ u ⟩ h ∣ y ⟩ h = ∣ y ′ ⟩ 2 h . The only operation in this algorithm which we have not yet defined is the addition by u 2 u^2 u 2 .
We now give an algorithm for adding u 2 u^2 u 2 . Let u = 2 u h i + u 0 u=2u_{\mathrm{hi}}+u_0 u = 2 u hi + u 0 for u 0 ∈ Z 2 u_0\in\mathbb{Z}_2 u 0 ∈ Z 2 , u h i ∈ Z 2 h − 1 u_{\mathrm{hi}}\in\mathbb{Z}_{2^{h-1}} u hi ∈ Z 2 h − 1 , and let ∣ w ⟩ m − 2 h |w\rangle_{m-2h} ∣ w ⟩ m − 2 h be the target register for modular addition of u 2 u^2 u 2 . To add u 2 u^2 u 2 , we first add 4 u h i 2 4u_{\mathrm{hi}}^2 4 u hi 2 using the identity
( 1 + 2 u h i ) ( w 1 + 2 u h i + 2 u h i ) − 2 u h i = w + 4 u h i 2 . (143) (1+2u_{\mathrm{hi}})\left(\frac{w}{1+2u_{\mathrm{hi}}}+2u_{\mathrm{hi}}\right)-2u_{\mathrm{hi}}=w+4u_{\mathrm{hi}}^2. \tag*{(143)} ( 1 + 2 u hi ) ( 1 + 2 u hi w + 2 u hi ) − 2 u hi = w + 4 u hi 2 . (143)
Adding 4 u 0 u h i 4u_0u_{\mathrm{hi}} 4 u 0 u hi and u 0 u_0 u 0 then gives w + u 2 w+u^2 w + u 2 .
Our extension of Theorem 2.5 to even inputs in Appendix § 12 is useful here. By Corollary E.2, we have a circuit that acts as × ( u ) \times(u) × ( u ) when u u u is odd and as × ( u + 1 ) \times(u+1) × ( u + 1 ) when u u u is even. Both cases give the required operation × ( 1 + 2 u h i ) \times(1+2u_{\mathrm{hi}}) × ( 1 + 2 u hi ) using the existing register ∣ u ⟩ h |u\rangle_h ∣ u ⟩ h , without preparing a separate register for this odd integer. We apply the following sequence of operations:
∣ w ⟩ m − 2 h ∣ u h i ⟩ h − 1 ∣ u 0 ⟩ 1 (144) |w\rangle_{m-2h}|u_{\mathrm{hi}}\rangle_{h-1}|u_0\rangle_1 \tag*{(144)} ∣ w ⟩ m − 2 h ∣ u hi ⟩ h − 1 ∣ u 0 ⟩ 1 (144)
→ ÷ ( 1 + 2 u h i ) ∣ w / ( 1 + 2 u h i ) ⟩ m − 2 h ∣ u h i ⟩ h − 1 ∣ u 0 ⟩ 1 (145) \xrightarrow{\div(1+2u_{\mathrm{hi}})} |w/(1+2u_{\mathrm{hi}})\rangle_{m-2h}|u_{\mathrm{hi}}\rangle_{h-1}|u_0\rangle_1 \tag*{(145)} ÷ ( 1 + 2 u hi ) ∣ w / ( 1 + 2 u hi ) ⟩ m − 2 h ∣ u hi ⟩ h − 1 ∣ u 0 ⟩ 1 (145)
→ + ( 2 u h i ) ∣ w / ( 1 + 2 u h i ) + 2 u h i ⟩ m − 2 h ∣ u h i ⟩ h − 1 ∣ u 0 ⟩ 1 (146) \xrightarrow{+(2u_{\mathrm{hi}})} |w/(1+2u_{\mathrm{hi}})+2u_{\mathrm{hi}}\rangle_{m-2h}|u_{\mathrm{hi}}\rangle_{h-1}|u_0\rangle_1 \tag*{(146)} + ( 2 u hi ) ∣ w / ( 1 + 2 u hi ) + 2 u hi ⟩ m − 2 h ∣ u hi ⟩ h − 1 ∣ u 0 ⟩ 1 (146)
→ × ( 1 + 2 u h i ) ∣ w + 2 u h i + 4 u h i 2 ⟩ m − 2 h ∣ u h i ⟩ h − 1 ∣ u 0 ⟩ 1 (147) \xrightarrow{\times(1+2u_{\mathrm{hi}})} |w+2u_{\mathrm{hi}}+4u_{\mathrm{hi}}^2\rangle_{m-2h}|u_{\mathrm{hi}}\rangle_{h-1}|u_0\rangle_1 \tag*{(147)} × ( 1 + 2 u hi ) ∣ w + 2 u hi + 4 u hi 2 ⟩ m − 2 h ∣ u hi ⟩ h − 1 ∣ u 0 ⟩ 1 (147)
→ − ( 2 u h i ) ∣ w + 4 u h i 2 ⟩ m − 2 h ∣ u h i ⟩ h − 1 ∣ u 0 ⟩ 1 (148) \xrightarrow{-(2u_{\mathrm{hi}})} |w+4u_{\mathrm{hi}}^2\rangle_{m-2h}|u_{\mathrm{hi}}\rangle_{h-1}|u_0\rangle_1 \tag*{(148)} − ( 2 u hi ) ∣ w + 4 u hi 2 ⟩ m − 2 h ∣ u hi ⟩ h − 1 ∣ u 0 ⟩ 1 (148)
→ + ( 4 u 0 u h i ) ∣ w + 4 u h i 2 + 4 u 0 u h i ⟩ m − 2 h ∣ u h i ⟩ h − 1 ∣ u 0 ⟩ 1 (149) \xrightarrow{+(4u_0u_{\mathrm{hi}})} |w+4u_{\mathrm{hi}}^2+4u_0u_{\mathrm{hi}}\rangle_{m-2h}|u_{\mathrm{hi}}\rangle_{h-1}|u_0\rangle_1 \tag*{(149)} + ( 4 u 0 u hi ) ∣ w + 4 u hi 2 + 4 u 0 u hi ⟩ m − 2 h ∣ u hi ⟩ h − 1 ∣ u 0 ⟩ 1 (149)
→ + ( u 0 ) ∣ w + 4 u h i 2 + 4 u 0 u h i + u 0 ⟩ m − 2 h ∣ u h i ⟩ h − 1 ∣ u 0 ⟩ 1 = ∣ w + u 2 ⟩ m − 2 h ∣ u ⟩ h . (150) \xrightarrow{+(u_0)} |w+4u_{\mathrm{hi}}^2+4u_0u_{\mathrm{hi}}+u_0\rangle_{m-2h}|u_{\mathrm{hi}}\rangle_{h-1}|u_0\rangle_1=|w+u^2\rangle_{m-2h}|u\rangle_h. \tag*{(150)} + ( u 0 ) ∣ w + 4 u hi 2 + 4 u 0 u hi + u 0 ⟩ m − 2 h ∣ u hi ⟩ h − 1 ∣ u 0 ⟩ 1 = ∣ w + u 2 ⟩ m − 2 h ∣ u ⟩ h . (150)
The final equality follows from u 2 = 4 u h i 2 + 4 u 0 u h i + u 0 u^2=4u_{\mathrm{hi}}^2+4u_0u_{\mathrm{hi}}+u_0 u 2 = 4 u hi 2 + 4 u 0 u hi + u 0 .
We use the constructions of [28 Sections 2.9–2.10] . For ± ( 2 u h i ) \pm(2u_{\mathrm{hi}}) ± ( 2 u hi ) , we apply ± ( u h i ) \pm(u_{\mathrm{hi}}) ± ( u hi ) to all but the least significant qubit of the target w w w . For + ( 4 u 0 u h i ) +(4u_0u_{\mathrm{hi}}) + ( 4 u 0 u hi ) , we apply + ( u h i ) +(u_{\mathrm{hi}}) + ( u hi ) to all but the least two significant target qubits, controlled on u 0 u_0 u 0 . The operation + ( u 0 ) +(u_0) + ( u 0 ) is a controlled increment. When required, we use ∣ y 0 ⟩ 1 |y_0\rangle_1 ∣ y 0 ⟩ 1 as a dirty qubit, although it is, in fact, a clean qubit, and restore it to its original state after each operation. Therefore, each addition uses O ( m − 2 h ) O(m-2h) O ( m − 2 h ) gates and no additional qubits.
The addition by u 2 u^2 u 2 requires two modular multiplications and four modular additions. This brings the total for the algorithm to six modular multiplications and four modular additions. The first multiplication acts on an ( m − h ) (m-h) ( m − h ) qubit register, and the remaining five act on an ( m − 2 h ) (m-2h) ( m − 2 h ) qubit register.
Main Theorem (Extended)
The main theorem of this work, Theorem 2.5 , involves modular multiplication with a quantum multiplier w ∈ Z 2 s w\in\mathbb{Z}_{2^s} w ∈ Z 2 s . The restriction to odd w w w ensures that this operation is reversible and hence unitary. For even w w w , distinct inputs can give the same output, so the operation must be modified to remain reversible.
In this section, we extend the operation defined in Theorem 2.5 to all w ∈ Z 2 s w \in\mathbb{Z}_{2^s} w ∈ Z 2 s . With some small modifications to the algorithm, an even multiplier w w w has the same effect on the target registers as the odd multiplier w + 1 w+1 w + 1 . The action for odd w w w remains unchanged. We use this extension in the proof of Lemma 5.3 in Appendix § 11 , and it may also be of independent interest.
Theorem E.1 (Main Theorem, Extended Version). Let m > 0 m > 0 m > 0 , s = O ( m ) s = O(m) s = O ( m ) and t = O ( m ) t = O(m) t = O ( m ) . Let w ∈ Z 2 s w \in\mathbb{Z}_{2^s} w ∈ Z 2 s , x ∈ Z 2 t x \in\mathbb{Z}_{2^t} x ∈ Z 2 t , y ∈ Z 2 m y \in\mathbb{Z}_{2^m} y ∈ Z 2 m , b , c ∈ Z ∖ 2 Z b,c \in\mathbb{Z} \setminus2\mathbb{Z} b , c ∈ Z ∖ 2 Z , and α , β ∈ { 0 , 1 } \alpha,\beta\in\{0,1\} α , β ∈ { 0 , 1 } . Let w 0 w_0 w 0 be the least significant bit of w w w and define f ( w ) = w + 1 ( w 0 = 0 ) f(w) = w + 1(w_0 = 0) f ( w ) = w + 1 ( w 0 = 0 ) . For any ϵ > 0 \epsilon> 0 ϵ > 0 , there exists a quantum circuit for performing the operation
∣ w ⟩ s ∣ x ⟩ t ∣ y ⟩ m ⟼ ∣ w ⟩ s ∣ 2 m b x f ( w ) α + c y f ( w ) β m o d 2 t + m ⟩ t + m (151) |w\rangle_s |x\rangle_t |y\rangle_m \longmapsto|w\rangle_s |2^m b x f(w)^\alpha+ c y f(w)^\beta\bmod2^{t+m}\rangle_{t+m} \tag*{(151)} ∣ w ⟩ s ∣ x ⟩ t ∣ y ⟩ m ⟼ ∣ w ⟩ s ∣ 2 m b x f ( w ) α + cy f ( w ) β mod 2 t + m ⟩ t + m (151) that uses O ( m 1 + ϵ ) O(m^{1+\epsilon}) O ( m 1 + ϵ ) gates and no ancilla qubits. For the special case s = 0 s = 0 s = 0 , the circuit has O ( m log m ) O(m \log m) O ( m log m ) depth. In this case, we adopt the convention f ( w ) α = f ( w ) β = 1 f(w)^\alpha= f(w)^\beta= 1 f ( w ) α = f ( w ) β = 1 .
Proof. We use the algorithm of Theorem 2.5 with some small modifications to prove the claim. For w ∈ Z 2 s w \in\mathbb{Z}_{2^s} w ∈ Z 2 s , u ∈ Z 2 n u \in\mathbb{Z}_{2^n} u ∈ Z 2 n and ϕ ∈ Z 2 n × \phi\in\mathbb{Z}_{2^n}^{\times} ϕ ∈ Z 2 n × , consider the operation
QFT ~ s , n ( ϕ , α ) : ∣ w ⟩ s ∣ u ⟩ n ⟼ ∣ w ⟩ s 1 2 n ∑ v ∈ Z 2 n e 2 π i ϕ v u f ( w ) α / 2 n ∣ v ‾ ⟩ n (152) \widetilde{\operatorname{QFT}}_{s,n}(\phi,\alpha): |w\rangle_s |u\rangle_n \longmapsto|w\rangle_s \frac{1}{\sqrt{2^n}} \sum_{v \in\mathbb{Z}_{2^n}} e^{2\pi i \phi v u f(w)^\alpha/2^n} |\overline{v}\rangle_n \tag*{(152)} QFT s , n ( ϕ , α ) : ∣ w ⟩ s ∣ u ⟩ n ⟼ ∣ w ⟩ s 2 n 1 v ∈ Z 2 n ∑ e 2 π i ϕ v u f ( w ) α / 2 n ∣ v ⟩ n (152)
which is just the controlled and parametrised quantum Fourier transform when restricted to the domain w ∈ Z 2 s , u ∈ Z 2 n w \in\mathbb{Z}_{2^s}, u \in\mathbb{Z}_{2^n} w ∈ Z 2 s , u ∈ Z 2 n . Using the same approach as Lemma 2.4 , Appendix § 9 , it can be shown that for s ≥ 2 s \ge2 s ≥ 2 ,
QFT ~ s , n ( ϕ , 1 ) = ( I ⌈ n / 2 ⌉ ⊗ QFT ~ s , ⌊ n / 2 ⌋ ( ϕ , 1 ) ) ∘ ( P ‾ 0 , ⌈ n / 2 ⌉ , ⌊ n / 2 ⌋ ( ϕ / 2 n ) ) ∘ ( P ‾ s − 1 , ⌈ n / 2 ⌉ , ⌊ n / 2 ⌋ ( 2 ϕ / 2 n , 1 ) ) ∘ ( QFT ~ s , ⌈ n / 2 ⌉ ( ϕ , 1 ) ⊗ I ⌊ n / 2 ⌋ ) (153) \widetilde{\operatorname{QFT}}_{s,n}(\phi,1) =
\left(I_{\lceil n/2\rceil} \otimes\widetilde{\operatorname{QFT}}_{s,\lfloor n/2\rfloor}(\phi,1)\right)
\circ
\left(\overline{P}_{0,\lceil n/2\rceil,\lfloor n/2\rfloor}(\phi/2^n)\right)
\circ
\left(\overline{P}_{s-1,\lceil n/2\rceil,\lfloor n/2\rfloor}(2\phi/2^n,1)\right)
\circ
\left(\widetilde{\operatorname{QFT}}_{s,\lceil n/2\rceil}(\phi,1) \otimes I_{\lfloor n/2\rfloor}\right) \tag*{(153)} QFT s , n ( ϕ , 1 ) = ( I ⌈ n /2 ⌉ ⊗ QFT s , ⌊ n /2 ⌋ ( ϕ , 1 ) ) ∘ ( P 0 , ⌈ n /2 ⌉ , ⌊ n /2 ⌋ ( ϕ / 2 n ) ) ∘ ( P s − 1 , ⌈ n /2 ⌉ , ⌊ n /2 ⌋ ( 2 ϕ / 2 n , 1 ) ) ∘ ( QFT s , ⌈ n /2 ⌉ ( ϕ , 1 ) ⊗ I ⌊ n /2 ⌋ ) (153)
since f ( w ) = 2 ⌊ w / 2 ⌋ + 1 f(w)=2\lfloor w/2\rfloor+1 f ( w ) = 2 ⌊ w /2 ⌋ + 1 . This operation requires O ( n 1 + ϵ ) O(n^{1+\epsilon}) O ( n 1 + ϵ ) gates and no ancilla qubits, for any ϵ > 0 \epsilon> 0 ϵ > 0 . For s ≤ 1 s \le1 s ≤ 1 , QFT ~ s , n ( ϕ , 1 ) \widetilde{\operatorname{QFT}}_{s,n}(\phi,1) QFT s , n ( ϕ , 1 ) can be implemented with QFT ~ s , n ( ϕ , 0 ) \widetilde{\operatorname{QFT}}_{s,n}(\phi,0) QFT s , n ( ϕ , 0 ) .
Using this new operation QFT ~ s , n ( ϕ , α ) \widetilde{\operatorname{QFT}}_{s,n}(\phi,\alpha) QFT s , n ( ϕ , α ) and slightly modified integer multiplications in the phase, we apply the algorithm of Theorem 2.5 .
QFT ~ s , t ( b , α ) ⊗ I m ⟼ ∣ w ⟩ s 1 2 t ∑ v ∈ Z 2 t e 2 π i b x v f ( w ) α / 2 t ∣ v ‾ ⟩ t ∣ y ⟩ m (154) \widetilde{\operatorname{QFT}}_{s,t}(b,\alpha)\otimes I_m
\longmapsto
|w\rangle_s \frac{1}{\sqrt{2^t}}\sum_{v\in\mathbb{Z}_{2^t}} e^{2\pi i b x v f(w)^\alpha/2^t}|\overline{v}\rangle_t |y\rangle_m \tag*{(154)} QFT s , t ( b , α ) ⊗ I m ⟼ ∣ w ⟩ s 2 t 1 v ∈ Z 2 t ∑ e 2 π ib xv f ( w ) α / 2 t ∣ v ⟩ t ∣ y ⟩ m (154)
I s ⊗ P ‾ 0 , t , m ( c / 2 t + m ) ⟼ ∣ ⌊ w / 2 ⌋ ⟩ s − 1 ∣ w 0 ⟩ 1 1 2 t ∑ v ∈ Z 2 t e 2 π i ( b x v f ( w ) α / 2 t + c v y / 2 t + m ) ∣ v ‾ ⟩ t ∣ y ⟩ m (155) I_s\otimes\overline{P}_{0,t,m}(c/2^{t+m})
\longmapsto
|\lfloor w/2\rfloor\rangle_{s-1}|w_0\rangle_1 \frac{1}{\sqrt{2^t}}\sum_{v\in\mathbb{Z}_{2^t}}e^{2\pi i(bxvf(w)^\alpha/2^t+cv y/2^{t+m})}|\overline{v}\rangle_t|y\rangle_m \tag*{(155)} I s ⊗ P 0 , t , m ( c / 2 t + m ) ⟼ ∣ ⌊ w /2 ⌋ ⟩ s − 1 ∣ w 0 ⟩ 1 2 t 1 v ∈ Z 2 t ∑ e 2 π i ( b xv f ( w ) α / 2 t + c v y / 2 t + m ) ∣ v ⟩ t ∣ y ⟩ m (155)
If β = 0 \beta=0 β = 0 or s ≤ 1 s\le1 s ≤ 1 , we don’t bother applying the next operation; otherwise apply,
I 1 ⊗ P ‾ s − 1 , t , m ( 2 c / 2 t + m , β ) ⟼ ∣ ⌊ w / 2 ⌋ ⟩ s − 1 ∣ w 0 ⟩ 1 1 2 t ∑ v ∈ Z 2 t e 2 π i ( b x v f ( w ) α / 2 t + c v y f ( w ) β / 2 t + m ) ∣ v ‾ ⟩ t ∣ y ⟩ m (156) I_1\otimes\overline{P}_{s-1,t,m}(2c/2^{t+m},\beta)
\longmapsto
|\lfloor w/2\rfloor\rangle_{s-1}|w_0\rangle_1 \frac{1}{\sqrt{2^t}}\sum_{v\in\mathbb{Z}_{2^t}}e^{2\pi i(bxvf(w)^\alpha/2^t+cv y f(w)^\beta/2^{t+m})}|\overline{v}\rangle_t|y\rangle_m \tag*{(156)} I 1 ⊗ P s − 1 , t , m ( 2 c / 2 t + m , β ) ⟼ ∣ ⌊ w /2 ⌋ ⟩ s − 1 ∣ w 0 ⟩ 1 2 t 1 v ∈ Z 2 t ∑ e 2 π i ( b xv f ( w ) α / 2 t + c v y f ( w ) β / 2 t + m ) ∣ v ⟩ t ∣ y ⟩ m (156)
Then apply,
I t ⊗ QFT ~ s , m ( c , β ) ⟼ ∣ w ⟩ s 1 2 t + m ∑ v ∈ Z 2 t z ∈ Z 2 m e 2 π i ( b x v f ( w ) α / 2 t + c v y f ( w ) β / 2 t + m + c y z f ( w ) β / 2 m ) ∣ v ‾ ⟩ t ∣ z ‾ ⟩ m (157) I_t\otimes\widetilde{\operatorname{QFT}}_{s,m}(c,\beta)
\longmapsto
|w\rangle_s \frac{1}{\sqrt{2^{t+m}}}\sum_{\substack{v\in\mathbb{Z}_{2^t}\\ z\in\mathbb{Z}_{2^m}}}e^{2\pi i(bxvf(w)^\alpha/2^t+cv y f(w)^\beta/2^{t+m}+cyz f(w)^\beta/2^m)}|\overline{v}\rangle_t|\overline{z}\rangle_m \tag*{(157)} I t ⊗ QFT s , m ( c , β ) ⟼ ∣ w ⟩ s 2 t + m 1 v ∈ Z 2 t z ∈ Z 2 m ∑ e 2 π i ( b xv f ( w ) α / 2 t + c v y f ( w ) β / 2 t + m + cy z f ( w ) β / 2 m ) ∣ v ⟩ t ∣ z ⟩ m (157)
I s ⊗ Q F T 0 , t + m ( 1 ) − 1 ⟶ ∣ w ⟩ s ∣ 2 m b x f ( w ) α + c y f ( w ) β m o d 2 t + m ⟩ t + m . (158) I_s \otimes\mathrm{QFT}_{0,t+m}(1)^{-1} \longrightarrow|w\rangle_s |2^m b x f(w)^\alpha+ c y f(w)^\beta\bmod2^{t+m}\rangle_{t+m}. \tag*{(158)} I s ⊗ QFT 0 , t + m ( 1 ) − 1 ⟶ ∣ w ⟩ s ∣ 2 m b x f ( w ) α + cy f ( w ) β mod 2 t + m ⟩ t + m . (158)
The result follows from Lemmas 2.2 and 2.3 , and the above statements about the cost of Q F T s , n ( φ , α ) \mathrm{QFT}_{s,n}(\varphi,\alpha) QFT s , n ( φ , α ) .
Corollary E.2 (Modular multiplication by a quantum integer mod 2 n 2^n 2 n , Extended Version). Let z ∈ Z 2 n z \in\mathbb{Z}_{2n} z ∈ Z 2 n , m = O ( n ) m = O(n) m = O ( n ) and x ∈ Z 2 m x \in\mathbb{Z}_{2m} x ∈ Z 2 m . Let x 0 x_0 x 0 denote the least significant bit of x x x and define f ( x ) = x + 1 ( x 0 = 0 ) f(x) = x + 1(x_0 = 0) f ( x ) = x + 1 ( x 0 = 0 ) . For any ϵ > 0 \epsilon> 0 ϵ > 0 , there exists a quantum circuit for performing the operation
∣ x ⟩ m ∣ z ⟩ n ⟼ ∣ x ⟩ m ∣ f ( x ) z m o d 2 n ⟩ n (159) |x\rangle_m |z\rangle_n \longmapsto|x\rangle_m |f(x)z \bmod2^n\rangle_n \tag*{(159)} ∣ x ⟩ m ∣ z ⟩ n ⟼ ∣ x ⟩ m ∣ f ( x ) z mod 2 n ⟩ n (159) that uses O ( n 1 + ϵ ) O(n^{1+\epsilon}) O ( n 1 + ϵ ) gates and no ancilla qubits.
Proof. Let s = m s = m s = m , t = ⌈ n / 2 ⌉ t = \lceil n/2\rceil t = ⌈ n /2 ⌉ , m = ⌊ n / 2 ⌋ m = \lfloor n/2\rfloor m = ⌊ n /2 ⌋ , α = β = 1 \alpha= \beta= 1 α = β = 1 , w = x w = x w = x , $2^m x + y = z$, b = c = 1 b = c = 1 b = c = 1 and apply Theorem E.1 .