I'm trying to build something called a Bloch Sphere, which is the 3-D representation of single quantum bit. Currently, I'm creating a function that develops animation along the x-axis and here is the code that I've written.
def x_animation(self):
#Y and Z are inputs from users
Y1 = self.Y*(-1)
Z1 = self.Z*(-1)
#number of dots which consists animation
length = 10
for i in range(length+1):
# an array of X,Y,Z coordinates of 10 dots
xgate= []
xgate_y = np.linspace(self.Y,Y1,length+1)
xgate_z = np.linspace(self.Z,Z1,length+1)
xgate.append([self.X,round(xgate_y[i],1),round(xgate_z[i],1)])
plot(xgate[i][0],xgate[i][1],xgate[i][2])
However, I got the error below.
IndexError Traceback (most recent call last)
<ipython-input-5-f56aa4b3a487> in <module>()
----> 1 q.x_animation()
<ipython-input-3-f74dcce093d4> in x_animation(self)
57 xgate_z = np.linspace(self.Z,Z1,length+1)
58 xgate.append([self.X,round(xgate_y[i],1),round(xgate_z[i],1)])
---> 59 plot(xgate[i][0],xgate[i][1],xgate[i][2])
60
61 def x_gate(self):
IndexError: list index out of range
I would appreciate it if anyone help me with solving this problem.
Your code initializes the list xgate
to an empty list on each iteration, and then it appends one element to it, thus xgate
never has more than one element in it by the time plot
is called. But plot
tries to access i-th element of xgate
, which will succeed on the first iteration and fail on the second one (once i=1
).
You should be able to fix this by moving list initialization xgate = []
outside of the loop, so that it would actually accumulate the elements.
(I'm not sure initialization of xgate_y
and xgate_z
should be in the loop either, but those are not accumulated and should not cause this kind of an issue.)