A4: Cyclic Shift Oracle II

Time Limit: 3 seconds

Memory Limit: 512 MiB

Score: 200 points

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.

{∣00⟩→CX(0,1)→CX(1,0)→CX(0,1)∣00⟩∣10⟩→CX(0,1)→CX(1,0)→CX(0,1)∣01⟩∣01⟩→CX(0,1)→CX(1,0)→CX(0,1)∣10⟩∣11⟩→CX(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}.

{∣00⟩→CX(0,1)→CX(1,0)∣00⟩→CX(0,1)∣00⟩∣10⟩→CX(0,1)→CX(1,0)∣01⟩→CX(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 xn−1=0x_{n-1} = 0, xn−2x_{n-2} and xn−1x_{n-1} can be swapped in 22 steps, and the state transitions are give by

∣x0x1⋯xn−4xn−3xn−2xn−1⟩→CX(n−2,n−1)→CX(n−1,n−2)∣x0x1⋯xn−4xn−3xn−1xn−2⟩.\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 xn−1=0x_{n-1} = 0, xn−3x_{n-3} and xn−1x_{n-1} can be swapped in 22 steps, and the state transitions are given by

∣x0x1⋯xn−4xn−3xn−1xn−2⟩→CX(n−3,n−2)→CX(n−2,n−3)∣x0x1⋯xn−4xn−1xn−3xn−2⟩.\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 n−1n-1 times, the desired quantum state ∣xn−1x0x1⋯xn−2⟩\ket{x_{n-1} x_0 x_1 \cdots x_{n-2}} can be prepared with a depth of 2(n−1)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,…,n−11, 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,…,n−11, 2, \ldots, n-1 bits.

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

Consequently, the desired quantum state ∣xn−1x0x1⋯xn−2⟩\ket{x_{n-1} x_0 x_1 \cdots x_{n-2}} can be prepared with a depth of 2(n−1)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