The length of an arrow in the Bloch sphere
I'm trying to build something called bloch sphere. This represents a state of a quantum bits in the form of an arrow in a sphere, whose radius is 1.0.
I wrote the codes below.
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from itertools import product, combinations
print("Put angle theta and phi, 0≤theta≤180, 0≤phi≤360")
theta = input("theta:")
phi = input("phi:")
theta = float(theta)
phi = float(phi)
X = np.sin(phi)
Y = np.sin(theta)
Z = np.cos(theta)
class quantum_gates:
def __init__(self,X,Y,Z):
self.X = float(X)
self.Y = float(Y)
self.Z = float(Z)
if theta <0 or theta >180 or phi < 0 or phi >360:
print("Put the value of angles again")
else:
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.set_aspect("equal")
u, v = np.mgrid[0:2*np.pi:20j, 0:np.pi:10j]
x = np.cos(u)*np.sin(v)
y = np.sin(u)*np.sin(v)
z = np.cos(v)
ax.set_xlabel('y')
ax.set_ylabel('x')
ax.set_zlabel('z')
ax.plot_wireframe(y, x, z, color="black")
ax.quiver(0,0,0,Y,X,Z,color="red",length=1.0)
When I put (theta, phi) = (30,0), the tip of the arrow reaches the surface of the sphere. However, when I put (theta,phi) = (30,30), the tip of the arrow goes outside of the sphere.
You can see the image of the current situation from the link below.
1 answer

I guess you perform transformation between coordinates in a wrong way.
X
,Y
, andZ
should be calculated as follows (wikipedia link):X = np.sin(theta) * np.cos(phi) Y = np.sin(theta) * np.sin(phi) Z = np.cos(theta)
Also, numpy trigonometric functions accept values in radians. So, theta should be in the range [0, pi], phi should be in the range [0, 2 * pi). To convert degrees to radians you may use
numpy.radians()
.