codercise1.8.1 ——State preparation, the answer is wrong

my code:


pennylane solution:

run these two code, results are different !

but these two different answer are all right!?

Hi @hch2984235287, welcome to the Forum!

The difference here is the global phase. Note that the Codercise says “up to a global phase”, so both solutions are correct even if the state is different.

You can check this by looking at the matrices for RZ and T. Note that the matrix for RZ has a term 𝑒^{−𝑖𝜙/2} in the top left, while the matrix for T has a 1. You can cancel out this term by multiplying the RZ matrix by 𝑒^{𝑖𝜙/2}, which is just adding a global phase. The code below shows an example of how to do this and it confirms that both answers are indeed equal, up to a global phase.

import pennylane as qml
import numpy as np

# T matrix
t_m = qml.T.compute_matrix()

# RZ(angle) matrix
angle = 5*np.pi/4
rz_m = qml.RZ.compute_matrix(angle)

# RZ(angle) matrix without global phase
# Multiply by the conjugate of the first element of rz_m
rz_m_no_phase = rz_m*np.e**(angle/2*1j)

# Print the outputs
print("T matrix: \n",t_m)
print("T**5 matrix: \n",t_m**5)
print("RZ(5*pi/4) matrix: \n",rz_m)
print("RZ(5*pi/4)*e^(i 5*pi/8) matrix: \n",rz_m_no_phase)
print("Are t_m**5 and rz_m_no_phase equal?: ", np.allclose(t_m**5,rz_m_no_phase))

I hope this helps!

Thank you, I understand now. Quantum states that differ only by a global phase are equivalent in physical meaning because the global phase has no effect on measurement results. I am a student just entered the field of quantum machine learning, and I greatly appreciate your patient explanation!Thank you.

1 Like