Need help with problem I.7.1 and I.8.1

I am completely stuck with the problem I.7.1.
It would be great if someone can help me on this.

dev = qml.device("default.qubit", wires=1)

##################
# YOUR CODE HERE #
##################

# ADJUST THE VALUES OF PHI, THETA, AND OMEGA
phi, theta, omega = 0.0, 0.0, 0.0

@qml.qnode(dev)
def hadamard_with_rz_rx():
    qml.RZ(phi, wires=0)
    qml.RX(theta, wires=0)
    qml.RZ(omega, wires=0)
    return qml.state()

Also, the question I.8.1.

dev = qml.device("default.qubit", wires=1)

@qml.qnode(dev)
def prepare_state():
    ##################
    # YOUR CODE HERE #
    ##################

    # APPLY OPERATIONS TO PREPARE THE TARGET STATE

    return qml.state()

Hey! Have you taken a look at this forum post Confusion with first problem of I.7 I.7 Universal gate sets? This might be a good place to start for I.7.1.

For I.8.1, would you be able to share some more details of the operations you have tried? Sharing your way of thinking would allow others to assist you better by narrowing down the scope, and it allows people to point where you might have gone wrong.

Thank you @Kamal_Mohamed for the response.
I eventually, got the I.8.1 working. Please review.

dev = qml.device("default.qubit", wires=1)

@qml.qnode(dev)
def prepare_state():
   

    # APPLY OPERATIONS TO PREPARE THE TARGET STATE
    qml.Hadamard(wires=0)
    qml.RZ(5/4*np.pi, wires=0)

    return qml.state()

But for I.7.1, I have tried all possible values, but I donot get the answer. Probably, I should try to print these values in my laptop after installing pennylane to see where I am going wrong.

One more problem in question I.7.1 is that probably I am not understanding the question correctly especially the line “acts the same as a Hadamard gate (up to a global phase)”. Not sure what is global phase?

Hi @Manu_Chaudhary, thanks for the question, and thank you @Kamal_Mohamed for your input!

One way of thinking about these two questions that might help is to visualize these states and transformations on the Bloch sphere. In general, remember that the state of a qubit can be represented mathematically by (Equation (2) in Codebook Node I.6

|\psi \rangle = \cos \Big(\frac{\theta}{2} \Big) |0 \rangle + \sin \Big( \frac{\theta}{2}\Big) e^{i\phi} |1 \rangle

where we follow the convention for the angles shown here.
The state of qubit is then represented by a vector on the Bloch sphere. For example, the state |0 \rangle (which corresponds to \theta = 0 in the expression above) lies at the ‘north pole’ of the Bloch sphere.

To start, you can see the effect of a Hadamard gate on the |0 \rangle or |1 \rangle states geometrically by identifying the corresponding states |+ \rangle = H |0 \rangle, and |- \rangle = H |1 \rangle on the second picture on the textbook side (right-hand side) of the Codebook Node I.6.

Likewise, following the third picture on the textbook of the Codebook Node I.6 you can see the effect of each rotation gate: RX rotates the vector about the x-axis, RY rotates the vector about the y-axis and RZ rotates the vector about the z-axis.

For example, one way of going from the state |0 \rangle to the state |1 \rangle is to apply an RY gate. You can try to do this same rotation (getting from the |0 \rangle to the state |1 \rangle) using an RX gate. Try it out just by visualizing the rotations (don’t use matrices). Can you get from the |0 \rangle to |1 \rangle using an RZ gate?

Finally, for the global phase, you can refer to Codebook Node I.5 for details, but basically, a global phase would be something like

|\psi' \rangle = e^{i \delta} \Big[ \cos \Big(\frac{\theta}{2} \Big) |0 \rangle + \sin \Big( \frac{\theta}{2}\Big) e^{i\phi} |1 \rangle \Big]

You can see - or try convincing yourself - that the extra phase e^{i \delta} (this is a global phase) has no effect on the outcome of measuring the state of a qubit. In other words, whether your qubit is in state |\psi\rangle or |\psi' \rangle, the probability of measuring the state of the qubit (either |0 \rangle or |1\rangle) is the same for both.

Therefore, when you build a quantum circuit, it does not matter if you are off by a global phase since that does not change the outcome of any measurement. For example, if you are trying to go from |0\rangle to the state

\frac{1}{\sqrt{2}} \Big( |0\rangle + e^{5i \pi/4}|1 \rangle \Big)

it does not matter if your sequence of gates gets you to the state

i \frac{1}{\sqrt{2}} \Big( |0\rangle + e^{5i \pi/4}|1 \rangle \Big)

(with a global phase of i).

Hope that helps!

1 Like

Thank you for the great explanation @DanielNino27. Your answer greatly helped me understanding the concept of global phase. Also, the notes, exercises and discussion forum of pennylane is excellent.
Can you also explain what is omega in universal gate? The wikipedia link you shared regarding the bloch sphere only shows theta and phi.

I really tried doing the question I.7.1, but I still not able to understand it geometrically and thus still could not solve it. May be, if somehow you can explain this question geometrically, it would be great great help and it will help me to develop a strong understanding in quantum computing.

Hi @Manu_Chaudhary,
The \omega in the universal gate is `just’ another phase angle. Looking at Exercise I.7.1 (in the textbook side) in Module I7 might help you understand that better. You can think of it as giving you an extra handle with which to rotate a qubit state.
The geometrical way of thinking about it is all in the picture below :

.

For the example I gave last time (going from the |0 \rangle to |1\rangle with a single RX rotation, say), Try to picture the vector moving from the north pole of the Bloch sphere to the south pole of the Bloch sphere passing along the arc that starts from north pole crosses the y-axis and then gets to the south pole (see blue line in picture below on left-most Bloch sphere:

You can likewise go from the north pole to the south pole with an RY rotation following the blue line on the right-most Bloch sphere above.

If the geometric picture does not work for you, you can also do it with the matrix for the universal single-qubit gate (Equation (1) in Node I.7). What values of each angle do you need to reproduce the matrix representation of a Hadamard gate (up to a global phase)?

Let me know if that helps!

Thank you for the response @DanielNino27.
The correct answer for this question is RZ(np.pi/2), RX(np.pi/2) , RZ(np.pi/2).
I think, the initial state should be considered as |0> and after all these rotations, it should reach |+> (H gate). I am really sorry, but I am still not able to visualize how this is happening? It would be great great help if you can draw the BLUE lines on Bloch sphere as you did in your previous answer so that I can visualize how it reaches |+>?

If you start from state |0\rangle, you only need the RX and RZ rotation with the angles you mentioned. These are shown in blue (RX rotation) and red (RZ) rotation. The first RZ rotation in this case would just add a global phase.
RotationGates_Rx_Rz

The first RZ rotation is needed if you start from an arbitrary state (not just the |0\rangle) . This one is harder to see on the Bloch sphere but it can be confirmed by looking at the matrices (you end up with the same general matrix as the Hadamard gate matrix).