# Creating a W state for $2^k$ qubits

This question is available in Quantum Katas here

In Task 2.6 of this notebook, we are required to create the W State for $$2^k$$ qubits.

Input: $$𝑁=2^𝑘$$ qubits in the |0…0⟩ state.

Goal: Change the state of the qubits to the W state - an equal superposition of 𝑁 basis states on 𝑁 qubits which have Hamming weight of 1.

For example, for $$𝑁=4$$ the required state is $$\frac{1}{2}(|1000⟩+|0100⟩+|0010⟩+|0001⟩)$$

And since this problem is taken from Katas, it came with a solution which is as follows: s

operation WState_PowerOfTwo (qs : Qubit[]) : Unit is Adj+Ctl {
let N = Length(qs);

if (N == 1) {
// base of recursion: |1⟩
X(qs[0]);
} else {
let K = N / 2;
using (anc = Qubit()) {
H(anc);

(ControlledOnInt(0, WState_PowerOfTwo))([anc], qs[0 .. K - 1]);
(ControlledOnInt(1, WState_PowerOfTwo))([anc], qs[K .. N - 1]);

for (i in K .. N - 1) {
CNOT(qs[i], anc);
}
}
}
}


While there is absolutely nothing wrong with the proposed answer, I was trying to solve this task without using an ancilla qubit, here's my approach to this question:

operation WState_PowerOfTwo (qs : Qubit[]) : Unit {
let length_qs = Length(qs);
if (length_qs == 1){
X(qs[0]);
}
else{
H(qs[0]);

for(i in 1..length_qs-1){
if(i != length_qs-1){
for (j in 0..i-1){
X(qs[j]);
}
Controlled H(qs[0..i-1], qs[i]);
for (j in 0..i-1){
X(qs[j]);
}
}
else{
for (j in 0..i-1){
X(qs[j]);
}
Controlled X(qs[0..i-1], qs[i]);
for (j in 0..i-1){
X(qs[j]);
}
}
}
}
}


This logics works fine till N=2 but it shows the following error when testing for hidden cases:

The desired state for N = 1
# wave function for qubits with ids (least to most significant): 0
∣0❭:     0.000000 +  0.000000 i  ==                          [ 0.000000 ]
∣1❭:     1.000000 +  0.000000 i  ==     ******************** [ 1.000000 ]     --- [  0.00000 rad ]
The actual state:
# wave function for qubits with ids (least to most significant): 0
∣0❭:     0.000000 +  0.000000 i  ==                          [ 0.000000 ]
∣1❭:     1.000000 +  0.000000 i  ==     ******************** [ 1.000000 ]     --- [  0.00000 rad ]
Test case passed
The desired state for N = 2
# wave function for qubits with ids (least to most significant): 0;1
∣0❭:     0.000000 +  0.000000 i  ==                          [ 0.000000 ]
∣1❭:     0.707107 +  0.000000 i  ==     ***********          [ 0.500000 ]     --- [  0.00000 rad ]
∣2❭:     0.707107 +  0.000000 i  ==     ***********          [ 0.500000 ]     --- [  0.00000 rad ]
∣3❭:     0.000000 +  0.000000 i  ==                          [ 0.000000 ]
The actual state:
# wave function for qubits with ids (least to most significant): 0;1
∣0❭:     0.000000 +  0.000000 i  ==                          [ 0.000000 ]
∣1❭:     0.707107 +  0.000000 i  ==     ***********          [ 0.500000 ]     --- [  0.00000 rad ]
∣2❭:     0.707107 +  0.000000 i  ==     ***********          [ 0.500000 ]     --- [  0.00000 rad ]
∣3❭:     0.000000 +  0.000000 i  ==                          [ 0.000000 ]
Test case passed
Testing on hidden test cases...
Released qubits are not in zero state.
Try again!


I cannot find a root for this problem, is there any error in my logic or am I missing something here?

If you take the task one step further, to $$N = 4$$, the desired state is $$\frac{1}{2}(|1000\rangle + |0100\rangle + |0010\rangle + |0001\rangle)$$, and the state your solution prepares is $$\frac{1}{\sqrt2}|1000\rangle + \frac{1}{2}|0100\rangle + \frac{1}{2\sqrt2}(|0010\rangle + |0001\rangle)$$. You can see this for yourself if you modify the testing harness in VS or VS Code to have AssertEqualOnZeroState(4, WState_PowerOfTwo, WState_PowerOfTwo_Reference, true, "N = 4"); so that it prints the expected state and the actual state when testing your solution.
• one H gate to modify the amplitude of the state in which $$1$$ is in the first position,
• two H gates to modify the amplitude of the state in which $$1$$ is in the second position,
• and three H gates to modify the amplitude of the states in which $$1$$ is in the third or fourth positions.
It is possible to solve this task without allocating extra qubits, but you'll need to use arbitrary rotations to adjust the amplitudes with more control than the H gate allows; for example, on the first step you'll need to convert $$|0000\rangle$$ to $$\frac{1}{2}|1000\rangle + \frac{\sqrt3}{2}|0000\rangle$$ so that the amplitude of the first state is correct. You can check the reference solution for task 2.7 for this approach.