# W state (Qiskit) [[w-state|W state]] implementation using Qiskit. The three-qubit W state $\lvert W\rangle = (\lvert 001\rangle + \lvert 010\rangle + \lvert 100\rangle)/\sqrt{3}$ is prepared using the recursive decomposition $\lvert W_3\rangle = \frac{1}{\sqrt{3}}\lvert 1\rangle\lvert 00\rangle + \sqrt{\frac{2}{3}}\lvert 0\rangle\lvert W_2\rangle$. ```python import numpy as np from qiskit import QuantumCircuit from qiskit.quantum_info import Statevector qc = QuantumCircuit(3) # Step 1: Ry on q[0] to set amplitude 1/sqrt(3) for |1> (the |100> term) theta = 2 * np.arccos(np.sqrt(2/3)) qc.ry(theta, 0) # sqrt(2/3)|0> + 1/sqrt(3)|1> # Step 2: anti-controlled preparation of |W_2> = (|01> + |10>)/sqrt(2) on q[1],q[2] # "anti-controlled" = apply when q[0]=0, achieved by flipping q[0] around the block qc.x(0) qc.cx(0, 2) # X on q[2] (sets the first excitation) qc.ch(0, 1) # H on q[1] (creates superposition) qc.ccx(0, 1, 2) # Toffoli: restores q[2] when q[1] also fired qc.x(0) print(Statevector(qc)) # Statevector([0, 0.57735027, 0.57735027, 0, 0.57735027, 0, 0, 0], dims=(2,2,2)) # non-zero at |001>, |010>, |100> with amplitude 1/sqrt(3) ```