A3: Generate State 12(∣0⟩−∣2n−1⟩)\frac{1}{\sqrt{2}}\lparen\ket{0}-\ket{2^{n}-1}\rparen I

Time Limit: 3 seconds

Memory Limit: 512 MiB

Score: 300 points

Writer:ST12

Editorial

First, apply the Hadamard gate to the first qubit.

∣0000⋯0⟩→H(0)12(∣0000⋯0⟩+∣1000⋯0⟩)\begin{equation} \ket{0000 \cdots 0} \xrightarrow{H(0)} \frac{1}{\sqrt{2}} \lparen \ket{0000 \cdots 0} + \ket{1000 \cdots 0} \rparen \end{equation}

Next, we transform the state ∣1000⋯0⟩\ket{1000 \cdots 0} to ∣1100⋯0⟩\ket{1100 \cdots 0}. To achieve this, apply a controlled-X gate (CNOT gate) with the first qubit as the control bit and the second qubit as the target bit:

12(∣0000⋯0⟩+∣1000⋯0⟩)→CX(0,1)12(∣0000⋯0⟩+∣1100⋯0⟩)\begin{equation} \frac{1}{\sqrt{2}} \lparen \ket{0000 \cdots 0} + \ket{1000 \cdots 0} \rparen \xrightarrow{CX(0,1)} \frac{1}{\sqrt{2}} \lparen \ket{0000 \cdots 0} + \ket{1100 \cdots 0} \rparen \end{equation}

Similarly, to transform ∣1100⋯0⟩\ket{1100 \cdots 0} to ∣1110⋯0⟩\ket{1110 \cdots 0}, apply a controlled-X gate, with the first qubit as the control bit and the third qubit as the target bit.

12(∣0000⋯0⟩+∣1100⋯0⟩)→CX(0,2)12(∣0000⋯0⟩+∣1110⋯0⟩)\begin{equation} \frac{1}{\sqrt{2}} \lparen \ket{0000 \cdots 0} + \ket{1100 \cdots 0} \rparen \xrightarrow{CX(0,2)} \frac{1}{\sqrt{2}} \lparen \ket{0000 \cdots 0} + \ket{1110 \cdots 0} \rparen \end{equation}

By continuing to apply the controlled-X gate until the nn-th qubit is the target bit, we can prepare the quantum state ∣ψ⟩=12(∣00⋯00⟩+∣11⋯11⟩)\ket{\psi} = \frac{1}{\sqrt{2}} (\ket{00 \cdots 00} + \ket{11 \cdots 11}).

12(∣0000⋯0⟩+∣1110⋯0⟩)→CX(0,3) →CX(0,4)⋯→CX(0,n−1)12(∣0...0⟩+∣1...1⟩)\begin{equation} \frac{1}{\sqrt{2}} \lparen \ket{0000 \cdots 0} + \ket{1110 \cdots 0} \rparen \xrightarrow{CX(0,3)} \, \xrightarrow{CX(0,4)} \cdots \xrightarrow{CX(0,n-1)} \frac{1}{\sqrt{2}} (\ket{0...0} + \ket{1...1}) \end{equation}

Finally, apply a Z gate to flip the phase of ∣11⋯11⟩\ket{11 \cdots 11}.

12(∣0...0⟩+∣1...1⟩)→Z(0)12(∣0...0⟩−∣1...1⟩)\begin{equation} \frac{1}{\sqrt{2}} \lparen \ket{0...0} + \ket{1...1} \rparen \xrightarrow{Z(0)} \frac{1}{\sqrt{2}} \lparen \ket{0...0} - \ket{1...1} \rparen \end{equation}

Summarizing these operations, we obtain the following circuit:

Sample Code

Below is a sample program:

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