A4: Cyclic Shift Oracle II

Time Limit: 3 sec

Memory Limit: 512 MiB

Score: 200

Writer: fortoobye

Editorial

Consider the swap gate that we implemented in problem A2.

Solution 1

First, let's consider 22 qubits swap. Swapping the first and second qubits can be achieved using a controlled XX gate, and the state transitions are as follows.

{00CX(0,1)CX(1,0)CX(0,1)0010CX(0,1)CX(1,0)CX(0,1)0101CX(0,1)CX(1,0)CX(0,1)1011CX(0,1)CX(1,0)CX(0,1)11\begin{equation} \begin{cases} \ket {00} \xrightarrow{CX(0, 1)} \xrightarrow{CX(1, 0)} \xrightarrow{CX(0, 1)} \ket{00} \\ \ket {10} \xrightarrow{CX(0, 1)} \xrightarrow{CX(1, 0)} \xrightarrow{CX(0, 1)} \ket{01} \\ \ket {01} \xrightarrow{CX(0, 1)} \xrightarrow{CX(1, 0)} \xrightarrow{CX(0, 1)} \ket{10} \\ \ket {11} \xrightarrow{CX(0, 1)} \xrightarrow{CX(1, 0)} \xrightarrow{CX(0, 1)} \ket{11} \end{cases} \end{equation}

Next, we focus on the state transitions when the second qubit is 00, i.e., 00\ket{00} and 10\ket{10}.

{00CX(0,1)CX(1,0)00CX(0,1)0010CX(0,1)CX(1,0)01CX(0,1)01\begin{equation} \begin{cases} \ket {00} \xrightarrow{CX(0, 1)} \xrightarrow{CX(1, 0)} \ket{00} \xrightarrow{CX(0, 1)} \ket{00} \\ \ket {10} \xrightarrow{CX(0, 1)} \xrightarrow{CX(1, 0)} \ket{01} \xrightarrow{CX(0, 1)} \ket{01} \end{cases} \end{equation}

Consequently, we can see that the first CX(0,1)CX(0, 1) and the next CX(1,0)CX(1, 0) realize the swap of 22 qubits. By utilizing this, we can solve this problem.

Given that xn1=0x_{n-1} = 0, xn2x_{n-2} and xn1x_{n-1} can be swapped in 22 steps, and the state transitions are give by

x0x1xn4xn3xn2xn1CX(n2,n1)CX(n1,n2)x0x1xn4xn3xn1xn2.\begin{equation} \ket{x_0x_1 \cdots x_{n-4} x_{n-3} x_{n-2} x_{n-1}} \xrightarrow{CX(n-2,n-1)} \xrightarrow{CX(n-1,n-2)} \ket{x_0x_1 \cdots x_{n-4} x_{n-3} x_{n-1} x_{n-2}}. \end{equation}

Similarly, when xn1=0x_{n-1} = 0, xn3x_{n-3} and xn1x_{n-1} can be swapped in 22 steps, and the state transitions are given by

x0x1xn4xn3xn1xn2CX(n3,n2)CX(n2,n3)x0x1xn4xn1xn3xn2.\begin{equation} \ket{x_0x_1 \cdots x_{n-4} x_{n-3} x_{n-1} x_{n-2}} \xrightarrow{CX(n-3,n-2)} \xrightarrow{CX(n-2,n-3)} \ket{x_0x_1 \cdots x_{n-4} x_{n-1} x_{n-3} x_{n-2}}. \end{equation}

By repeating this operation n1n-1 times, the desired quantum state xn1x0x1xn2\ket{x_{n-1} x_0 x_1 \cdots x_{n-2}} can be prepared with a depth of 2(n1)2(n-1).

The circuit diagram for n=3n = 3 is as follows.

Solution 2

As mentioned in the editorial of A2, the XOR swap algorithm is a classical algorithm to swap 22 variables xx and yy in 33 steps. When y=0y=0, the third step of "replace yy with x+y(mod2)x + y \pmod 2" can be omitted, so XOR swap can be achieved in 22 steps by replacing "replace yy with x+y(mod2)x + y \pmod 2" and "replace xx with x+y(mod2)x + y \pmod 2". By utilizing this, we can solve this problem.

First, replace yy with x+y(mod2)x + y \pmod 2 using a controlled XX gate for 1,2,,n11, 2, \ldots, n-1 bits.

for i in reversed(range(n - 1)):
    qc.cx(i, i + 1)  

Next, replace xx with x+y(mod2)x + y \pmod 2 using a controlled XX gate for 1,2,,n11, 2, \ldots, n-1 bits.

for i in reversed(range(n - 1)):
    qc.cx(i + 1, i)  

Consequently, the desired quantum state xn1x0x1xn2\ket{x_{n-1} x_0 x_1 \cdots x_{n-2}} can be prepared with a depth of 2(n1)2(n-1).

The circuit diagram for n=3n = 3 is as follows.

Sample Code

Below is a sample program:

Solution 1

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

Solution 2

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