Search code examples
python-3.xqutip

When plotting the Wigner function of a coherent state using QuTiP strange patterns appear


I noticed something strange this day when I plotted the Wigner function of a coherent state using the open source quantum toolbox QuTiP in python.

When I do the plot I noticed these strange patterns just around the edge of the plot that are not supposed to be there. I believe it's just some sort of numerical error but I don't know how I can get rid or minimize them or most impartant: what's causing them.

Here is the code

# import packages
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import matplotlib as mpl
from matplotlib import cm
from qutip import *

N = 60 # number of levels in Hilbert space

# density matrix of a coherent state
rho_coherent = coherent_dm(N, 1-1j)

X = np.linspace(-3, 3, 300)
Y = np.linspace(-3, 3, 300)

# Wigner function
W = wigner(rho_coherent, X, Y, 'iterative', 2)

X, Y = np.meshgrid(X, Y)

# Color Normalization
class MidpointNormalize(colors.Normalize):
    def __init__(self, vmin=None, vmax=None, midpoint=None, clip=False):
        self.midpoint = midpoint
        colors.Normalize.__init__(self, vmin, vmax, clip)
    def __call__(self, value, clip=None):
        x, y = [self.vmin, self.midpoint, self.vmax], [0, 0.5, 1]
        return np.ma.masked_array(np.interp(value, x, y))

# contour plot
plt.subplot(111, aspect='equal')
plt.contourf(X, Y, W, 100, cmap = cm.RdBu_r, norm = MidpointNormalize(midpoint=0.))
plt.show()

and here is the plot

enter image description here

The blue spots as you can clearly see that's around the edges are not supposed to be there! The blue spots indicate that the Wigner function is negative at that point, but a coherent state should have a Wigner function thats positive everywhere!

I also noticed that when I reduce the linspace steps from 300 to 100 the blue parts disappear.

Would appreciate very much if someone can explain what's causing this problem to appear.


Solution

  • This is simply due to truncation. When using a finite number of modes (in your case N=60), the Wigner function will go negative at some point.

    Reducing the linspace steps brings the negative regions you see on the plot into the zero value increment and displays these regions as zero. Reducing the linspace steps is probably the best solution to your problem. Your plot will only be as accurate as the errors introduced by truncation, so simply reduce the resolution until those errors disappear.