File:Wigner quasiprobability distribution of cat states, grid.webm

From testwiki
Jump to navigation Jump to search
Wigner_quasiprobability_distribution_of_cat_states,_grid.webm (file size: 2.23 MB, MIME type: video/webm)

This file is from Wikimedia Commons and may be used by other projects. The description on its file description page there is shown below.

Summary

Description
English: Wigner quasiprobability distribution of cat states, grid. Cat states with 2, 3, 4 cats. The separation between cats range from 0.5, 1, 2, 3, showing increasingly sharp inference.

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" : 20,
    "radius" : 4.5,
    "resolution" : 600,
    "angles" : range(360),
    "dir_path" : ""

for separation in [0.5, 1, 2, 3]:

   for cat_number in [2, 3, 4]:
       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)


Sh

for dir in ./*/; do
    # Extract folder name
    folder_name=$(basename "$dir")
    # Create output path
    output_path="./${folder_name}.webm"
    # Convert images to webm
    echo $output_path
  #  ffmpeg -y -framerate 24 -i "$dir"%03d.png -c:v libvpx-vp9 -b:v 0 -crf 30 -pix_fmt yuva420p "$output_path"
done

ffmpeg -y \
  -i ./cat_2_0.5.webm \
  -i ./cat_2_1.0.webm \
  -i ./cat_2_2.0.webm \
  -i ./cat_2_3.0.webm \
  -i ./cat_3_0.5.webm \
  -i ./cat_3_1.0.webm \
  -i ./cat_3_2.0.webm \
  -i ./cat_3_3.0.webm \
  -i ./cat_4_0.5.webm \
  -i ./cat_4_1.0.webm \
  -i ./cat_4_2.0.webm \
  -i ./cat_4_3.0.webm \
  -filter_complex "
    [0:v]scale=800:800[v0];
    [1:v]scale=800:800[v1];
    [2:v]scale=800:800[v2];
    [3:v]scale=800:800[v3];
    [4:v]scale=800:800[v4];
    [5:v]scale=800:800[v5];
    [6:v]scale=800:800[v6];
    [7:v]scale=800:800[v7];
    [8:v]scale=800:800[v8];
    [9:v]scale=800:800[v9];
    [10:v]scale=800:800[v10];
    [11:v]scale=800:800[v11];
    [v0][v1][v2][v3]hstack=inputs=4[row0];
    [v4][v5][v6][v7]hstack=inputs=4[row1];
    [v8][v9][v10][v11]hstack=inputs=4[row2];
    [row0][row1][row2]vstack=inputs=3[out]
  " \
  -map "[out]" \
   -c:v libvpx-vp9 -b:v 0 -crf 30 -pix_fmt yuva420p \
  output_grid.webm

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.

Captions

Add a one-line explanation of what this file represents

Items portrayed in this file

depicts

video/webm

2,339,361 byte

7.5 second

2,400 pixel

3,200 pixel

1624482b98bde49a6c125b8202ea93572ab9a5c8

File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeDimensionsUserComment
current05:01, 31 August 2024 (2.23 MB)wikimediacommons>Cosmia Nebulahigher resolution

The following page uses this file: