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?

  • $\begingroup$ $1.321$ is an approximation of the arccos of $1/3$, to get 1 with probability $1/3$, or a third of times of the simulation. $\endgroup$
    – NotaChoice
    Commented May 1 at 21:05

2 Answers 2


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.

enter image description here

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


I would recommend reading this article:

Effient quantum algorithms for GHZ and W states, and implementation on the IBM Q.

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
            #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 = _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)

  • $\begingroup$ What do "mm", "nn" and "m" represent in the above code. $\endgroup$ Commented Dec 11, 2022 at 13:46
  • 1
    $\begingroup$ @rabahhacenebenaissa: m is an abbrev for Math library in Python which should be imported. Other variables refer to nodes in a tree used in construction of W state, see the linked article for details. $\endgroup$ Commented Dec 12, 2022 at 7:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.