WState

WState is a function that returns a W-state on a given number of qubits. For example, the usual $3$-qubit W-state is $\frac{1}{\sqrt{3}}(|100\rangle + |010\rangle + |001\rangle)$. More generally, the W-state on $q$ qudits is $\frac{1}{\sqrt{q}}(|10\cdots 00\rangle + |01\cdots 00\rangle + \cdots + |00\cdots 01\rangle)$. The output of this function is a sparse vector.

Syntax

 * W_STATE = WState(Q)
 * W_STATE = WState(Q,COEFF)

Argument descriptions

 * Q: The number of qubits.
 * COEFF (optional, default [1,1,...,1]/sqrt(Q)): A vector whose $j$-th entry is the coefficient of the term $|0\rangle^{\otimes (j-1)} \otimes |1\rangle \otimes |0\rangle^{\otimes q-j}$ in the sum that defines the W-state.

3-qubit W-state
The following code generates the usual 3-qubit GHZ state:

A generalized W-state
The following code generates the following generalized 4-qubit W-state:
 * $$\frac{1}{\sqrt{30}}\big( |1000\rangle + 2|0100\rangle + 3|0010\rangle + 4|0001\rangle \big).$$

A very large W-state
This script has no trouble creating W-states on very large numbers of qubits. The following code generates the 30-qubit W-state: