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?