Editorial
First, expand in binary representation:
Then, the condition can be rewritten as and , and the condition can be rewritten as and .
Therefore, this problem can be solved by applying the operation in B3 twice with as the control bit:
For computational basis states that satisfy or , the problem does not specify the transition, so any transition is acceptable.
Sample Code
Below is a sample program:
import math
from qiskit import QuantumCircuit
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
# B2
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
# B3
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 solve(n: int, s: int, t: int) -> QuantumCircuit:
qc = QuantumCircuit(n + 1)
# when k_{n-1} = 1
qc.compose(cadd(n, 2 ** (n + 1) - t), qubits=range(n + 1), inplace=True)
# when k_{n-1} = 0
qc.x(n)
qc.compose(cadd(n, -s), qubits=range(n + 1), inplace=True)
qc.x(n)
return qc