2

I am trying to plot live data on a bloch sphere using Qutip's function bloch().

So far, the code always interrupts, when I have a b.show() in there.

I found a lot of solutions online to similar problems, but most of them make use of direct matplotlib commands like matplotlib.draw() which doesn't seem to work with the bloch class. Then, there are other solutions which make use of for example Tk or GTKagg (e.g. https://stackoverflow.com/a/15742183/3276735 or real-time plotting in while loop with matplotlib)

Can somebody please help me how to deal with the same problem in the bloch class?

Edit: Here's a minimal example:

Basically, I want to update my plot with one point at a time, preferably in a loop. My goal is to display live data in the plot that has to be read from a file.

import qutip as qt
import numpy as np


b = qt.Bloch()

theta = np.arange(0,np.pi,0.1)

for ii in range(len(theta)):
     b.add_points([np.sin(theta[ii]),0,np.cos(theta[ii])])
     b.show()
Community
  • 1
  • 1
Mechanix
  • 95
  • 1
  • 8

1 Answers1

1

I think you are breaking your plot because you are calling show for every point. Try calling show outside the loop (in the end).

import qutip as qt
import numpy as np


b = qt.Bloch()

theta = np.arange(0,np.pi,0.1)

for ii in range(len(theta)):
     b.add_points([np.sin(theta[ii]),0,np.cos(theta[ii])])

b.show() # Changed here

EDIT: Animated plot

Consider show as an absolute command to call the plot into view. It's not a draw command (or redraw). If you do want to show an image every "n" seconds or so you'll need to clear the plot before calling it again. You may try this:

import qutip as qt
import numpy as np

b = qt.Bloch()

theta = np.arange(0,np.pi,0.1)

for ii in range(len(theta)):
     b.clear()
     b.add_points([np.sin(theta[ii]),0,np.cos(theta[ii])])
     b.show()
     # wait time step and load new value from file.

, I don't have QuTip in my current distribution so I can't really test it but I'm betting its heavily based in matplotlib. My best advise however is for you to use the formulation give for animation in the QuTiP docs. By following this recipe:

from pylab import *
import matplotlib.animation as animation
from mpl_toolkits.mplot3d import Axes3D

fig = figure()
ax = Axes3D(fig,azim=-40,elev=30)
sphere=Bloch(axes=ax)

def animate(i):
    sphere.clear()
    sphere.add_vectors([sin(theta),0,cos(theta)])
    sphere.add_points([sx[:i+1],sy[:i+1],sz[:i+1]])
    sphere.make_sphere()
    return ax

def init():
    sphere.vector_color = ['r']
    return ax

ani = animation.FuncAnimation(fig, animate, np.arange(len(sx)),
                            init_func=init, blit=True, repeat=False)
ani.save('bloch_sphere.mp4', fps=20, clear_temp=True)

, you should be able to modify the animate function to perform all operations you need.

armatita
  • 12,825
  • 8
  • 48
  • 49
  • Hi @armatita, thanks for the reply. My problem is I want to display live data that I want to see in a plot. Therefore I can't put everything into an array and display everything at once. So there will be a file that is updated every 0.2 seconds and i want to read the last line into python and plot it into the graph to see live how the measurement evolves. That's why i want to be able to plot from inside a loop. – Mechanix Mar 23 '16 at 18:55
  • @Mechanix I've updated my post. My answer relies on the fact that I think QuTip might work a lot like matplotlib. In case it doesn't work I advise you to just use the animation recipe given in the site. Links in the post. – armatita Mar 23 '16 at 20:10
  • Thanks again. The problem remains how i can display the animated things on the screen ... any ideas about that? – Mechanix Mar 23 '16 at 21:20
  • @Mechanix That recipe is based on matplotlib which displays live animation on screen. You just need to adapt it to your case study. In this link you'll see another similar animation using live random generated data. Notice how the same call is being used: http://matplotlib.org/examples/animation/strip_chart_demo.html – armatita Mar 23 '16 at 21:42
  • Ok, it's funny, I was actually looking at the same example. The problem is, that they use plt.draw(), which supports this live updating. In my case, i just get the message "AttributeError: Bloch instance has no attribute 'draw'" – Mechanix Mar 23 '16 at 21:56
  • Try the "render" function instead (but make sure you clear the contents before drawing new data). http://qutip.org/docs/3.1.0/apidoc/classes.html#qutip.bloch.Bloch.render – armatita Mar 24 '16 at 08:11