Exercise 1.7.1 - Getting Error

Hi there,

in exercise 1.7.1 I think I specified the angles in such a way that the output state is proportional to the |+> state, but I still get an error after submission.

Here is a screenshot showing the complex amplitudes of my final state vector:

Does anyone know why I am getting an error?

Thanks in advance!

Hello @NoName!

The Codercise is a bit more general–the whole circuit must implement the Hadamard gate. This means that the output of the circuit has to be |+\rangle when the input is |0\rangle and |-\rangle when the input is |1\rangle. Could you check that your circuit also acts properly on |1\rangle?

Cheers,

Alvaro

Hello Alvaro,

thank you for your answer. To check the effect on |1> as a initial qubit, I added

    qml.PauliX(wires=0)

as the first command:

As far as I can see, the result is proportional to the |-> state.

Hello @NoName

While both states seem to be fine independently, the circuit as a whole has to be the Hadamard gate up to a global phase. That means that the unitary matrix U of the circuit must be of the form

U = e^{i\alpha}\begin{pmatrix} 1/\sqrt{2} & 1\sqrt{2} \\ 1/\sqrt{2} & -1\sqrt{2} \end{pmatrix}.

In your case (please feel free to verify my math, it’s a bit rusty):

U\vert 0\rangle = e^{-i \pi/4}\vert + \rangle,
U\vert 1\rangle = -e^{i \pi/4}\vert - \rangle.

The phases e^{-i \pi/4} and -e^{i \pi/4} are not the same, and they should be! Your solution is introducing a relative phase. Explicitly, the matrix for your solution is

U = e^{-i\pi/4}\begin{pmatrix} 1/\sqrt{2} & -i/\sqrt{2} \\ 1/\sqrt{2} & i/\sqrt{2} \end{pmatrix},

which is not in the form above.

Let me know if this helps or if you need an extra push in the right direction :pray:!

Cheers,

Alvaro

As far as I can see, your math looks fine :slight_smile:

I see… So even if U and H both transform |0> and |1> to |+> and |-> respectively (up to a global phase), U and H differ by a relative phase… I would not have expected this, but this is interesting… Thanks!

Exactly, the issue here is that if you were to apply the Hadamard gate H to some superposition

\vert \psi \rangle = \alpha \vert 0 \rangle + \beta \vert 1 \rangle,

the answer should be

H \vert \psi \rangle = \alpha \vert + \rangle + \beta \vert - \rangle.

But if we apply the U in your code we get

U \vert \psi \rangle = e^{-i\pi/4}(\alpha\vert + \rangle -i \beta \vert - \rangle).

Note that H \vert \psi \rangle and U \vert \psi \rangle do not differ by a global phase, there’s a relative phase. So while U is apparently acting properly on \vert 0 \rangle and \vert 1 \rangle, it doesn’t work for all possible superposition states :cry:

Hope this clarifies things a little better.

Cheers,

Alvaro

Yeah, now it makes totally sense when applying U to a superposition of |0> and |1> and seeing that there is a relative phase between |+> and |->.

Thank you very much for your nice explanations!