File:Wigner quasiprobability distribution of a coherent state.webm

Summary

Description
English: Wigner quasiprobability distribution of a coherent state.

Matplotlib

import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
from IPython.display import display
from qutip import (about, basis, coherent, coherent_dm, displace, fock, ket2dm,
                   plot_wigner, squeeze, thermal_dm, wigner_cmap, wigner)

import scipy.ndimage
import os
from tqdm import tqdm

def rotate_and_crop(array, angle, xvec, yvec):
    rotated_array = scipy.ndimage.rotate(array, -angle, reshape=False)
    rows, cols = rotated_array.shape
    center_row, center_col = rows // 2, cols // 2
    target_rows, target_cols = len(yvec), len(xvec)
    start_row = center_row - target_rows // 2
    end_row = start_row + target_rows
    start_col = center_col - target_cols // 2
    end_col = start_col + target_cols
    return rotated_array[start_row:end_row, start_col:end_col]

def plot_wigner_marginals(W, xvec, yvec, marginal_max, resolution=200, angle=0):
    wmap = wigner_cmap(W)
    wlim = np.abs(W).max()
    cmap = plt.colormaps['RdBu']

    fig = plt.figure()
    n, m = 5, 1
    fig, axes = plt.subplot_mosaic(
        [ ["top"] * n + ["3d"] * m ] * m + [ ["mid"] * n + ["right"] * m] * n,
    figsize=(20, 20),
    layout="constrained",
    width_ratios=[1.05] * (n+m))

    ax = axes["mid"]
    norm = mpl.colors.Normalize(-wlim, wlim)
    ax.contourf(xvec, yvec, W, resolution // 3, norm=norm, cmap=cmap)
    

    ax = axes["top"]
    x_marginal = np.sum(W, axis=0)
    y_marginal = np.sum(W, axis=1)
    ax.fill_between(xvec, x_marginal, 0, color='#938fba', alpha=0.5)
    ax.plot(xvec, x_marginal, color='#4a5a90')
    ax.set_xlim(min(xvec), max(xvec))
    ax.set_ylim(0, marginal_max * 1.05)
    ax.set_xticks([])
    ax.set_yticks([])

    ax = axes["right"]
    ax.fill_betweenx(yvec, np.sum(W, axis=1), 0, color='#938fba', alpha=0.5)
    ax.plot(y_marginal, yvec, color='#4a5a90')
    ax.set_xlim(0, marginal_max * 1.05)
    ax.set_ylim(min(yvec), max(yvec))
    ax.set_xticks([])
    ax.set_yticks([])

    ax = axes["3d"]
    ax.axis('off')

    return fig

def plot_wigner_with_marginals(psi, **kwargs):
    
    radius = kwargs.get('radius', 5) 
    resolution = kwargs.get('resolution', 500)
    angles = kwargs.get('angles', np.linspace(0, 2*np.pi, 100))
    dir_path = kwargs.get('dir_path', './output')
    
    xvec_upscaled = np.linspace(-radius*1.5, radius*1.5, int(resolution*1.5))
    yvec_upscaled = np.linspace(-radius*1.5, radius*1.5, int(resolution*1.5))
    xvec = np.linspace(-radius, radius, int(resolution))
    yvec = np.linspace(-radius, radius, int(resolution))

    W_upscaled = wigner(psi, xvec_upscaled, yvec_upscaled)
    marginal_max = max(max(np.sum(W_upscaled, axis=0)), max(np.sum(W_upscaled, axis=1)))
    print(f"outputting to {dir_path}")
    for N, angle in tqdm(enumerate(angles)):
        W = rotate_and_crop(W_upscaled, angle, xvec, yvec)
        fig = plot_wigner_marginals(W, xvec, yvec, marginal_max=marginal_max, resolution=resolution, angle=angle)

        if not os.path.exists(dir_path):
            os.makedirs(dir_path)
        fig.savefig(f"{dir_path}/{N:03d}.png",bbox_inches='tight')
        plt.close(fig)
    
mpl.use('agg')
configs = {
    "N_dim" : 50,
    "radius" : 4.5,
    "resolution" : 600,
    "angles" : [3 * i for i in range(120)],
    "dir_path" : ""
}
for separation in [2]:
    for cat_number in [1]:
        psi = sum(
            [coherent(configs["N_dim"], separation * np.exp(2j * np.pi * m / cat_number))
             for m in range(cat_number)]
        ).unit()
        configs["dir_path"] = f"./cat/cat_{cat_number}_{separation:.1f}"
        plot_wigner_with_marginals(psi, **configs)

Date
Source Own work
Author Cosmia Nebula

Licensing

I, the copyright holder of this work, hereby publish it under the following license:
w:en:Creative Commons
attribution share alike
This file is licensed under the Creative Commons Attribution-Share Alike 4.0 International license.
You are free:
  • to share – to copy, distribute and transmit the work
  • to remix – to adapt the work
Under the following conditions:
  • attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
  • share alike – If you remix, transform, or build upon the material, you must distribute your contributions under the same or compatible license as the original.
Category:CC-BY-SA-4.0#Wigner%20quasiprobability%20distribution%20of%20a%20coherent%20state.webm
Category:Self-published work Category:Wigner functions Category:Coherent states Category:Created with Matplotlib Category:Animations of quantum wave functions
Category:Animations of quantum wave functions Category:CC-BY-SA-4.0 Category:Coherent states Category:Created with Matplotlib Category:Pages with syntax highlighting errors Category:Self-published work Category:Wigner functions