A3: Cyclic Shift Oracle I

Time Limit: 3 sec

Memory Limit: 512 MiB

Score: 200

Writer: fortoobye

Editorial

You can solve this problem by using the swap gate implemented in problem A2.

Let's consider the following example of 33 qubits:

First, swap the second and third qubits.

x0x1x2CX(1,2)CX(2,1)CX(1,2)x0x2x1\begin{equation} \ket{x_0x_1x_2} \xrightarrow{CX(1,2)} \xrightarrow{CX(2,1)} \xrightarrow{CX(1,2)} \ket{x_0x_2x_1} \end{equation}

Next, swap the first and second qubits.

x0x2x1CX(0,1)CX(1,0)CX(0,1)x2x0x1\begin{equation} \ket{x_0x_2x_1} \xrightarrow{CX(0,1)} \xrightarrow{CX(1,0)} \xrightarrow{CX(0,1)} \ket{x_2x_0x_1} \end{equation}

As a result, the quantum state x2x0x1\ket{x_2x_0x_1} can be obtained by two swaps.

By generalizing the above operation, the quantum state xn1x0x1xn2\ket{x_{n-1}x_0x_1\cdots x_{n-2}} can be generated by n1n-1 swaps. Since each swap can be implemented with a depth 33 quantum circuit, the depth of the entire quantum circuit is 3(n1)3(n-1), allowing you to solve this problem.

Sample Code

Below is a sample program:

from qiskit import QuantumCircuit
 
 
def solve(n: int) -> QuantumCircuit:
    def swap(n: int, qubit1: int, qubit2: int) -> QuantumCircuit:
        qc = QuantumCircuit(n)
 
        qc.cx(qubit1, qubit2)
        qc.cx(qubit2, qubit1)
        qc.cx(qubit1, qubit2)
 
        return qc
 
    qc = QuantumCircuit(n)
 
    for i in reversed(range(n - 1)):
        qc.compose(swap(n, i, i + 1), inplace=True)
 
    return qc

Follow-up

After the contest, a solution with a circuit depth of 3log2n3 \lceil \log_2 n \rceil was discovered by hiryuN (tweet). This algorithm is derived by reinterpreting the algorithm for A4: Cyclic Shift Oracle, which transforms x(x+1)modn\ket{x}\rightarrow \ket{(x+1) \bmod n}, as an operation that replaces the ii-th bit with the (i+1)modn(i+1) \bmod n-th bit.