我试图绘制一个称为bloch sphere的东西,但是即使它不应该发生,我也能得到它的图像。我写了以下代码。
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from itertools import product, combinations
class quantum_gates:
def __init__(self,x2,y2,z2):
x2 = input("x:")
y2 = input("y:")
z2 = input("z:")
x2 = float(x2)
y2 = float(y2)
z2 = float(z2)
self.x2 = x2
self.y2 = y2
self.z2 = z2
if self.x2**2 + self.y2**2 + self.z2**2 != 1:
print("Wrong input(x^2+y^2+z^2 has to be 1)")
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('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.plot_wireframe(x, y, z, color="black")
ax.quiver(0,0,0,x2,y2,z2,color="red")
例如,当我放置quantum_gates(2,3,1)
的{{1}}时,会得到球体的图像和红色箭头。
只有在self.x2^2 + self.y2^2 + self.z2^2 != 1
时我该怎么做才能得到它们?