A4: Generate One-hot Superposition State I

Time Limit: 3 sec

Memory Limit: 512 MiB

Score: 200

Writer: ST12

Editorial

First, we apply an XX gate to the first qubit of the zero state 0000...0\ket{0000...0}.

0000...0X(0)1000...0\begin{equation} \ket{0000...0} \xrightarrow{X(0)} \ket{1000...0} \end{equation}

Next, we apply a controlled RyR_y gate to this quantum state 1000...0\ket{1000...0} to create a superposition state of 1000...0\ket{1000...0} and 1100...0\ket{1100...0}. Considering that the amplitude of 1000...0\ket{1000...0} should be 1/n1/\sqrt{n} in the final state, the rotation angle θ1\theta_1 can be determined by solving the following simultaneous equations:

{cos(θ12)=1nsin(θ12)=n1n\begin{equation} \left\{ \, \begin{aligned} & \cos\left(\frac{\theta_1}{2}\right) = \frac{1}{\sqrt{n}} \\ & \sin\left(\frac{\theta_1}{2}\right) = \sqrt{\frac{n-1}{n}} \end{aligned} \right. \end{equation}

Solving these equations gives the following solution within the range 0θ12π0 \leq \theta_1 \leq 2\pi:

θ1=2arctan(n1)\begin{equation} \theta_1 = 2 \arctan{(\sqrt{n-1})} \end{equation}

Therefore, we apply a controlled RyR_y gate with rotation angle θ1\theta_1 to the quantum state 1000...0\ket{1000...0}, using the first qubit as the control qubit and the second qubit as the target qubit:

1000...0CRy(θ1,0,1)1n1000...0+n1n1100...0\begin{equation} \ket{1000...0} \xrightarrow{CRy(\theta_1,0,1)} \frac{1}{\sqrt{n}} \ket{1000...0} + \sqrt{\frac{n-1}{n}} \ket{1100...0} \end{equation}

Then, we apply a controlled XX gate with the second qubit as the control qubit and the first qubit as the target qubit:

1n1000...0+n1n1100...0CX(1,0)1n1000...0+n1n0100...0\begin{equation} \frac{1}{\sqrt{n}} \ket{1000...0} + \sqrt{\frac{n-1}{n}} \ket{1100...0} \xrightarrow{CX(1,0)} \frac{1}{\sqrt{n}} \ket{1000...0} + \sqrt{\frac{n-1}{n}} \ket{0100...0} \end{equation}

Repeating these operations—applying controlled RyR_y and controlled XX gates sequentially—a total of nn times allows us to solve this problem.

0000...0X(0)1000...0CRy(θ1,0,1)1n1000...0+n1n1100...0CX(1,0)1n1000...0+n1n0100...0CRy(θ2,1,2)1n(1000...0+0100...0)+n2n10110...0CX(2,1)1n(1000...0+0100...0)+n2n10010...0CX(n,n1)1n(1000...0+0100...0++0000...1)\begin{align} \ket{0000...0} &\xrightarrow{X(0)} \ket{1000...0} \nonumber \\ &\xrightarrow{CRy(\theta_1, 0, 1)} \frac{1}{\sqrt{n}} \ket{1000...0} + \sqrt{\frac{n-1}{n}} \ket{1100...0} \nonumber\\ &\xrightarrow{CX(1, 0)} \frac{1}{\sqrt{n}} \ket{1000...0} + \sqrt{\frac{n-1}{n}} \ket{0100...0} \nonumber\\ &\xrightarrow{CRy(\theta_2, 1, 2)} \frac{1}{\sqrt{n}} (\ket{1000...0} + \ket{0100...0}) + \sqrt{\frac{n-2}{n-1}} \ket{0110...0} \nonumber\\ &\xrightarrow{CX(2, 1)} \frac{1}{\sqrt{n}} (\ket{1000...0} + \ket{0100...0}) + \sqrt{\frac{n-2}{n-1}} \ket{0010...0} \nonumber\\ &\qquad \qquad \qquad \qquad \qquad \qquad \vdots \nonumber\\ &\xrightarrow{CX(n, n-1)} \frac{1}{\sqrt{n}} (\ket{1000...0} + \ket{0100...0} + \cdots + \ket{0000...1}) \end{align}

Sample Code

Below is a sample program:

import math
 
from qiskit import QuantumCircuit
 
 
def solve(n: int) -> QuantumCircuit:
    qc = QuantumCircuit(n)
 
    theta = [2 * math.atan(math.sqrt(i)) for i in range(n - 1, 0, -1)]
 
    qc.x(0)
 
    for i in range(n - 1):
        qc.cry(theta[i], i, i + 1)
        qc.cx(i + 1, i)
 
    return qc