Editorial
First, expand in binary representation:
Then, can be expanded as follows:
Therefore, by using the operation of problem B5 with as the control bit, the problem can be solved by repeatedly applying the controlled circuit of problem B5.
Please note that the input of must be taken modulo .
The circuit depth is .
Sample Code
Below is a sample program:
import math
from qiskit import QuantumCircuit, QuantumRegister
def qft(n: int) -> QuantumCircuit:
qc = QuantumCircuit(n)
for i in reversed(range(n)):
qc.h(i)
for j in reversed(range(i)):
qc.cp(math.pi / 2 ** (i - j), j, i)
for i in range(n // 2):
qc.swap(i, n - i - 1)
return qc
def crot(n: int, a: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
for i in range(n):
theta = 2 * math.pi * a * 2**i / 2**n
qc.cp(theta, n, i)
return qc
def cadd(n: int, a: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
qc.compose(qft(n), qubits=range(n), inplace=True)
qc.compose(crot(n, a), qubits=range(n + 1), inplace=True)
qc.compose(qft(n).inverse(), qubits=range(n), inplace=True)
return qc
def ccrot(n: int, a: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 2)
for i in range(n):
theta = 2 * math.pi * a * 2**i / 2**n
qc.mcp(theta, [n, n + 1], i)
return qc
def ccadd(n: int, a: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 2)
qc.compose(qft(n), qubits=range(n), inplace=True)
qc.compose(ccrot(n, a), qubits=range(n + 2), inplace=True)
qc.compose(qft(n).inverse(), qubits=range(n), inplace=True)
return qc
def cpack(n: int, s: int, t: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 2)
qc.compose(ccadd(n, 2 ** (n + 1) - t), qubits=range(n + 2), inplace=True)
qc.x(n)
qc.compose(ccadd(n, -s), qubits=range(n + 2), inplace=True)
qc.x(n)
return qc
def solve(n: int, a: int, L: int) -> QuantumCircuit:
x, y = QuantumRegister(n), QuantumRegister(n + 1)
qc = QuantumCircuit(x, y)
for i in range(n):
b = (2**i * a) % L
targets = [*y, x[i]]
qc.compose(cadd(n + 1, 2**n - L), qubits=targets, inplace=True)
qc.compose(cadd(n + 1, b), qubits=targets, inplace=True)
qc.compose(cpack(n, 2**n - L + b, 2**n + b), qubits=targets, inplace=True)
qc.compose(cadd(n + 1, b), qubits=targets, inplace=True)
return qc