Editorial
First, apply the Hadamard gate to the first qubit.
∣ 0000 ⋯ 0 ⟩ → H ( 0 ) 1 2 ( ∣ 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} ∣ 0000 ⋯ 0 ⟩ H ( 0 ) 2 1 ( ∣ 0000 ⋯ 0 ⟩ + ∣ 1000 ⋯ 0 ⟩ )
Next, we transform the state ∣ 1000 ⋯ 0 ⟩ \ket{1000 \cdots 0} ∣ 1000 ⋯ 0 ⟩ to ∣ 1100 ⋯ 0 ⟩ \ket{1100 \cdots 0} ∣ 1100 ⋯ 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:
1 2 ( ∣ 0000 ⋯ 0 ⟩ + ∣ 1000 ⋯ 0 ⟩ ) → C X ( 0 , 1 ) 1 2 ( ∣ 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} 2 1 ( ∣ 0000 ⋯ 0 ⟩ + ∣ 1000 ⋯ 0 ⟩ ) CX ( 0 , 1 ) 2 1 ( ∣ 0000 ⋯ 0 ⟩ + ∣ 1100 ⋯ 0 ⟩ )
Similarly, to transform ∣ 1100 ⋯ 0 ⟩ \ket{1100 \cdots 0} ∣ 1100 ⋯ 0 ⟩ to ∣ 1110 ⋯ 0 ⟩ \ket{1110 \cdots 0} ∣ 1110 ⋯ 0 ⟩ , apply a controlled-X gate, with the first qubit as the control bit and the third qubit as the target bit.
1 2 ( ∣ 0000 ⋯ 0 ⟩ + ∣ 1100 ⋯ 0 ⟩ ) → C X ( 0 , 2 ) 1 2 ( ∣ 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} 2 1 ( ∣ 0000 ⋯ 0 ⟩ + ∣ 1100 ⋯ 0 ⟩ ) CX ( 0 , 2 ) 2 1 ( ∣ 0000 ⋯ 0 ⟩ + ∣ 1110 ⋯ 0 ⟩ )
By continuing to apply the controlled-X gate until the n n n -th qubit is the target bit, we can prepare the quantum state ∣ ψ ⟩ = 1 2 ( ∣ 00 ⋯ 00 ⟩ + ∣ 11 ⋯ 11 ⟩ ) \ket{\psi} = \frac{1}{\sqrt{2}} (\ket{00 \cdots 00} + \ket{11 \cdots 11}) ∣ ψ ⟩ = 2 1 ( ∣ 00 ⋯ 00 ⟩ + ∣ 11 ⋯ 11 ⟩ ) .
1 2 ( ∣ 0000 ⋯ 0 ⟩ + ∣ 1110 ⋯ 0 ⟩ ) → C X ( 0 , 3 ) → C X ( 0 , 4 ) ⋯ → C X ( 0 , n − 1 ) 1 2 ( ∣ 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} 2 1 ( ∣ 0000 ⋯ 0 ⟩ + ∣ 1110 ⋯ 0 ⟩ ) CX ( 0 , 3 ) CX ( 0 , 4 ) ⋯ CX ( 0 , n − 1 ) 2 1 ( ∣ 0...0 ⟩ + ∣ 1...1 ⟩ )
Finally, apply a Z gate to flip the phase of ∣ 11 ⋯ 11 ⟩ \ket{11 \cdots 11} ∣ 11 ⋯ 11 ⟩ .
1 2 ( ∣ 0...0 ⟩ + ∣ 1...1 ⟩ ) → Z ( 0 ) 1 2 ( ∣ 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} 2 1 ( ∣ 0...0 ⟩ + ∣ 1...1 ⟩ ) Z ( 0 ) 2 1 ( ∣ 0...0 ⟩ − ∣ 1...1 ⟩ )
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