A5: Minus One Oracle

Time Limit: 3 sec

Memory Limit: 512 MiB

Score: 300

Writer: fortoobye

Editorial

First, let's consider an operation x(x1)mod2n\ket{x} \rightarrow \ket{(x - 1) \bmod {2^n}} that is the inverse of x(x+1)mod2n\ket{x} \rightarrow \ket{(x + 1) \bmod {2^n}}.

Let xx be represented by a bit string x0x1xn1x_0x_1 \cdots x_{n-1}.

x=x0x1xn1n\begin{equation} \ket{x} = \ket{x_0x_1 \cdots x_{n-1}}_n \end{equation}

Based on this representation, the operation x(x+1)mod2n\ket{x} \rightarrow \ket{(x + 1) \bmod {2^n}} can be rephrased as follows:

  • Convert all consecutive 11's from the least significant bit to 00 and flip the first 00 bit to 11.
    • Specifically, find ii (1i<n)(1 \leq i < n) such that "x0=x1==xi1=1,xi=0x_0 = x_1 = \cdots = x_{i-1} = 1, x_{i} = 0" and flip x0,x1,,xi1x_0, x_1, \cdots , x_{i-1} to 00 and xix_i to 11. If "x0=x1==xn1=1x_0 = x_1 = \cdots = x_{n-1} = 1", flip x0,x1,,xn1x_0, x_1, \cdots , x_{n-1} to 00. If "x0=x1==xn1=0x_0 = x_1 = \cdots = x_{n-1} = 0", flip x0x_0 to 11.

Let's consider the example of n=3n = 3 below.

Assume that the input state is 110\ket{110}.

First, the state transition is as follows by the "multi-controlled XX gate1" with the first and second qubits as control bits and the third qubit as the target bit.

110MCX((0,1),2)111\begin{equation} \ket{110} \xrightarrow{MCX((0,1),2)} \ket{111} \end{equation}

Next, the state transition is as follows by the "controlled XX gate with the first qubit as the control bit and the second qubit as the target bit".

111CX(0,1)101\begin{equation} \ket{111} \xrightarrow{CX(0,1)} \ket{101} \end{equation}

Finally, the state transition is as follows by the XX gate.

101X(0)001\begin{equation} \ket{101} \xrightarrow{X(0)} \ket{001} \end{equation}

As a result, the operation 110001\ket{110} \rightarrow \ket{001} is realized.

By generalizing the above operation, the operation x(x+1)mod2n\ket{x} \rightarrow \ket{(x + 1) \bmod {2^n}} can be realized, and the code is as follows:

from qiskit import QuantumCircuit
 
 
def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
 
    for i in reversed(range(1, n)):
        qc.mcx(list(range(i)), i)
 
    qc.x(0)
 
    return qc

By applying these gates in reverse order, the operation x(x1)mod2n\ket{x} \rightarrow \ket{(x - 1) \bmod {2^n}} is realized.

Sample Code

Below is a sample program:

from qiskit import QuantumCircuit
 
 
def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
 
    qc.x(0)
 
    for i in range(1, n):
        qc.mcx(list(range(i)), i)
 
    return qc

Equivalently, you can use the inverse method.

from qiskit import QuantumCircuit
 
 
def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
 
    for i in reversed(range(1, n)):
        qc.mcx(list(range(i)), i)
 
    qc.x(0)
 
    return qc.inverse()

Follow-up

By decomposing a positive integer aa into the sum of 20,21,2^0, 2^1, \ldots and 2n20,2n21,2^n-2^0, 2^{n}-2^1, \ldots, the algorithm of this problem can realize xx+a(mod2n)\ket{x} \rightarrow \ket{x + a \pmod {2^n}} by applying the algorithm at most log2(a)+12\left\lceil \frac{\log_2(a) + 1}{2}\right\rceil times.

Footnotes

  1. In Qiskit, multiple controlled gates can be implemented using the control method of each gate class.