Got stuck in Codebook

Can you add a button to let students see the answer and continue their learning?

I got stuck at I.9 Measurements, where I was asked to write a quantum function to roate 0 state to y+ and 1 state to y-. I used qml.RX(-np.pi/2, wires=0).

The program said I was wrong.

Could anyone help me with this first? Thanks a lot!

Hey Wenlong!

I was going through this question a few months ago, and initially my approach was to use qml.RX(-np.pi/2, wires=0). Mathematically, it is easy to verify that applying this gate to |0> and |1> does not produce |y+> and |y->. I tried applying an H then applying another rotation gate. This worked for me, but there might be other ways around it as well.

However, I am also struggling to understand why one RX doesn’t work. When looking at the Bloch sphere,
image
the |y+> state lies on the y-z plane 90 degrees off |0>, which seems to imply that an RX rotation of -90 would be enough. I believe that my understanding is flawed, but I am struggling to see where the flaw is. If someone can provide some insight, that would be greatly appreciated.

Regarding having a button that reveals the answer, I personally find the current way works well with me as it forces me to dig deep to find the answer myself, and if i get stuck, I engage in discussions on the forum with others who help out. Though to be honest, in this particular question, I think more details aside from “Incorrect: the operators in the basis rotation aren’t quite right” would be helpful.

Hey @Kamal_Mohamed and welcome to the forum @Wenlong_Yang! :rocket:

Great discussion going on here. I’ll address @Wenlong_Yang’s original post first. If we apply an R_x gate to the Z basis states, this is what we get:

\begin{align*} R_x(\theta) \vert 0 \rangle &= \cos(\tfrac{\theta}{2}) \vert 0 \rangle - i \sin(\tfrac{\theta}{2}) \vert 1 \rangle \\ R_x(\theta) \vert 1 \rangle &= - i \sin(\tfrac{\theta}{2}) \vert 0 \rangle + \cos(\tfrac{\theta}{2})\vert 1 \rangle \end{align*}

The \vert y + \rangle and \vert y-\rangle states are:

\begin{align*} \vert y + \rangle &= \frac{1}{\sqrt{2}}\left( \vert 0 \rangle + i\vert 1 \rangle \right)\\ \vert y - \rangle &= \frac{1}{\sqrt{2}}\left( \vert 0 \rangle - i\vert 1 \rangle \right) \end{align*}

At first glance, it looks like we cannot get these states from applying an R_x gate. However, we can with a little trickery! Firstly,

\begin{align*} R_x(\theta = -\tfrac{\pi}{2}) \vert 0 \rangle &= \cos(-\tfrac{\pi}{4}) \vert 0 \rangle - i \sin(-\tfrac{\pi}{4}) \vert 1 \rangle \\ &= \frac{1}{\sqrt{2}}\left( \vert 0 \rangle + i\vert 1 \rangle \right) \\ &= \vert y + \rangle. \end{align*}

One down! One to go…

\begin{align*} R_x(\theta = -\tfrac{\pi}{2}) \vert 1 \rangle &= - i \sin(-\tfrac{\pi}{4}) \vert 0 \rangle + \cos(-\tfrac{\pi}{4}) \vert 1 \rangle \\ &= \frac{1}{\sqrt{2}}\left( i \vert 0 \rangle + \vert 1 \rangle \right) \end{align*}

Now, global phases — e^{i\varphi} — in quantum mechanics don’t affect any physics. So, I can safely multiply any state by a global phase. Let’s multiply the above by e^{ - i \tfrac{\pi}{2}} = -i:

\begin{align*} &= \frac{1}{\sqrt{2}}\left( -i \times i \vert 0 \rangle - i \vert 1 \rangle \right) \\ & = \frac{1}{\sqrt{2}}\left( \vert 0 \rangle - i \vert 1 \rangle \right) \\ & = \vert y - \rangle \end{align*}

And we’re done… sorta! But, this clears up your confusion @Kamal_Mohamed — your intuition on Bloch sphere rotations is correct!

However, this doesn’t really answer the codebook question, as we had to “cheat” — it’s not really cheating — with that global phase trick. One way to get the \vert y + \rangle and \vert y-\rangle states right away is to apply a Hadamard — qml.Hadamard — gate and a Phase gate — qml.S. Check out the docs for the phase gate operator, do the multiplication, and verify that you get the the \vert y + \rangle and \vert y-\rangle states!

Thank you all for the answers! I thought the states are considered as the same up to a global phase. I end up using Z gate, S gate to get -i phase and then do the X -pi/2 rotation.

I still think it is better to allow users to see answers. Discussion is great. But it will save everyone’s time if the answers are available. Thanks!

For this problem, I have been guessing whether I need to make two wires and one for 0 one for 1 or I only need to make 1 wire. So, the description of the problem is the confusing part, not the real problem. I have also been guessing whether I need to make a device outside of both functions or I need to create these states inside each function.

For this problem, I have been guessing whether I need to make two wires and one for 0 one for 1 or I only need to make 1 wire. So, the description of the problem is the confusing part, not the real problem. I have also been guessing whether I need to make a device outside of both functions or I need to create these states inside each function.

Every qubit state presented up that point is a one qubit state, so no need for more than one qubit :slight_smile:. Also, the comment above each function says: # WRITE A QUANTUM FUNCTION THAT .... It’s not uncommon for people to get confused about the difference between a quantum function and a QNode. I think these resources do a great job at explaining the key differences:

Let me know if that helps!