# Three qubits W-state

I'm trying to implement a circuit wich prepare this three qubits state : $$\frac{1}{\sqrt{3}}(|100\rangle + |010\rangle + |001\rangle)$$

It seems that the three qubits W-state can produce this state, and I found this code.

But I have seen a simple circuit on this page wich seems to work, but I don't understand how the person find the Ry rotation of 1.321, does someone could explain me how 1.321 is found?

## 2 Answers

This is because $$RY(1.321)$$ put the qubit from the state $$|\psi_0\rangle = |0\rangle$$ to the state $$|\psi_1 \rangle = \sqrt{\dfrac{2}{3}} |0\rangle + \sqrt{\dfrac{1}{3}} |1\rangle$$. One of the thing to note here is also that $$RY$$ is rotating around the $$Y$$ axis so it is the rotation gate that we want to use if we want to keep the amplitude of the quantum state real.

From there you can work out the rest of the circuit.

I would recommend reading this article:

It provides general algorithm for constructing a circuit producing W-state with arbitrary number of qubits.

Here is a code based on the article:

def _w_state_circuit(circuit, nn, mm, qubitC):
global qubitT #reference to global variable qubitT

if nn == 0 and mm == 1:
pass #do nothing in this case
elif nn == 1 and mm == 2: #case (1,2)
circuit.cu3(m.pi/2, 0, 0, q[qubitC], q[qubitT])
circuit.cx(q[qubitT], q[qubitC])
qubitT = qubitT + 1
else: #otherwise
theta = 2*np.arccos(m.sqrt(nn/mm))
circuit.cu3(theta, 0, 0, q[qubitC], q[qubitT])
circuit.cx(q[qubitT], q[qubitC])

qubitTRecurse = qubitT #saving target qubit index, used as control qubit for lower child
qubitT = qubitT + 1

a = m.floor(nn/2)
b = m.floor(mm/2)
c = m.ceil(nn/2)
d = m.ceil(mm/2)

if a == 1 and b == 1: #upper child (1,1) => (1,2) became upper child
circuit = _w_state_circuit(circuit, 1, 2, qubitC)
#there is no lower child
elif c == 1 and d == 1: #lower child (1,1) => (1,2) became lower child
circuit = _w_state_circuit(circuit, 1, 2, qubitTRecurse)
#there is no upper child
else:
#upper child
circuit = _w_state_circuit(circuit, a, b, qubitC)
#lower child
circuit = _w_state_circuit(circuit, c, d, qubitTRecurse)

return circuit

def w_state_circuit (qubits, qRegister, cRegister):
global qubitT
qubitT = 1 #index of a qubit a new gate acts on (hard to compute inside recursion => global variable)
circuit = QuantumCircuit(qRegister, cRegister)
circuit.x(q[0])
circuit = _w_state_circuit(circuit, m.floor(qubits/2), qubits, 0)
return circuit

#construction of 6 qubits W-state
qubits = 6

q = QuantumRegister(qubits, name = 'q')
c = ClassicalRegister(qubits, name = 'c')

circuit = w_state_circuit(qubits, q, c)

circuit.draw()