gausstorch.utils package

Submodules

gausstorch.utils.bcolors module

class gausstorch.utils.bcolors.bcolors[source]

Bases: object

Class containing colors for command line prints, using ANSI escape sequences. Source code taken from stackoverflow.

BOLD = '\x1b[1m'
ENDC = '\x1b[0m'
FAIL = '\x1b[91m'
HEADER = '\x1b[95m'
OKBLUE = '\x1b[94m'
OKCYAN = '\x1b[96m'
OKGREEN = '\x1b[92m'
UNDERLINE = '\x1b[4m'
WARNING = '\x1b[93m'

gausstorch.utils.display module

Functions providing templates for human-readable display of data. By default, the axis dimensions are written with my PhD thesis text width and font size in mind.

gausstorch.utils.display.fock_state_to_str_general(fock_combination: list, mode_comb: list) str[source]

Generates string describing a Fock state occupation probability

Parameters:
  • fock_combination (list) – Combination of photon numbers in each mode

  • mode_comb (list) – Combination of mode indexes

Returns:

Fock state occupation probability notation

Return type:

str

gausstorch.utils.display.fock_states_to_str_list(fock_combs_per_mode_comb: dict) list[source]

Returns a list of Fock state probability occupations, as defined in fock_state_to_str_general().

Parameters:

fock_combs_per_mode_comb (dict) – Keys are combinations of modes, and values are lists of photon number combinations, as tuples.

Returns:

Fock state occupation probability notations

Return type:

list

gausstorch.utils.display.plot_evolution_N(tspan: ndarray, means: ndarray, width_ratio: float = 0.48, xlabel: str = 'time (ns)', ylabel: str = '$\\boldsymbol{N}$', yscale: str = 'linear') tuple[source]

Template which returns the fig and ax of a mean photon number evolution.

Parameters:
  • tspan (np.ndarray) – Discrete time values.

  • means (np.ndarray) – Mean photon number values.

  • width_ratio (float, optional) – Ratio of width ratio to TEXTWIDTH_INCH. Defaults to 0.48.

  • xlabel (str, optional) – Label of the X axis. Defaults to “time (ns)”.

  • ylabel (str, optional) – Label of the Y axis. Defaults to r”$boldsymbol{N}$”.

  • yscale (str, optional) – Y axis scale. Defaults to “linear”.

Returns:

Fig and ax of the plot

Return type:

tuple

gausstorch.utils.display.plot_evolution_fock(tspan: ndarray, probs: ndarray, labels: list, width_ratio: float = 0.48, xlabel: str = 'time (ns)', ylabel: str = 'Probability', yscale: str = 'linear') tuple[source]

Template which returns the fig and ax of a set of fock state probability evolutions.

Parameters:
  • tspan (np.ndarray) – Discrete time values.

  • probs (np.ndarray) – Fock state occupation probabilities.

  • labels (list) – Label of each fock state occupation probability.

  • width_ratio (float, optional) – Ratio of width to TEXTWIDTH_INCH. Defaults to 0.48.

  • xlabel (str, optional) – Label of the X axis. Defaults to “time (ns)”.

  • ylabel (str, optional) – Label of the Y axis. Defaults to r”Probability”.

  • yscale (str, optional) – Y axis scale. Defaults to “linear”.

Returns:

Fig and ax of the plot

Return type:

tuple

gausstorch.utils.display.plot_wigner(ax: axes, alpha: Tensor, sigma: Tensor, quad1_key: str, quad2_key: str) QuadMesh[source]

Plots the 2D cross section of the wigner quasi-probabiliy distribution of a (alpha, sigma) state, for quadratures quad1_key and quad2_key.

Parameters:
  • ax (plt.axes) – Ax to plot the QuadMesh into

  • alpha (torch.Tensor) – Field operator displacement vector

  • sigma (torch.Tensor) – Field operator covariance matrix

  • quad1_key (str) – First quadrature chosen for the cross-section

  • quad2_key (str) – Second quadrature chosen for the cross-section

Return type:

matplotlib.collections.QuadMesh

gausstorch.utils.display.print_model_parameters(named_parameters: dict) None[source]

Pretty prints the parameters of an torch.nn.Module.named_parameters()

Parameters:

named_parameters (dict) – named_parameters() evalutation of an torch.nn.Module

gausstorch.utils.display.print_other_pars(other_pars: dict) None[source]

Pretty prints the other_pars instance attribute of the gausstorch.libs.qsyst.Qsyst class

Parameters:

other_pars (dict) – other_pars attribute of the gausstorch.libs.qsyst.Qsyst class

gausstorch.utils.display.print_state_dict(state_dict: dict) None[source]

Pretty prints a torch.nn.Module state dict, which is just a regular dictionnary.

Parameters:

state_dict (dict) – State dict of a torch.nn.Module

gausstorch.utils.display.setup_tex(usetex: bool = True) None[source]

Sets up matplotlib.rcParams to write tex code.

Parameters:

usetex (bool, optional) – If True, add text.usetex to rcParams. Defaults to True.

gausstorch.utils.loop_hafnian_torch module

Taken from the github repository jakeffbulmer/gbs, from Jacob F.F.Bulmer for the paper “The boundary for quantum advantage in Gaussian boson sampling”.

gausstorch.utils.loop_hafnian_torch.loop_hafnian(A, D=None, reps=None, glynn=True)[source]

calculate loop hafnian with (optional) repeated rows and columns

Parameters:
  • A (torch tensor) – N x N matrix

  • D (torch tensor) – diagonal entries of matrix (optional). If not provided, D is the diagonal of A. If repetitions are provided, D should be provided explicitly

  • reps (list) – length-N list of repetitions of each row/col (optional), if not provided, each row/column assumed to be repeated once

  • glynn (bool) – If True, use Glynn-style finite difference sieve formula, if False, use Ryser style inclusion/exclusion principle.

Returns

torch.complex128: result of loop hafnian calculation

gausstorch.utils.operations module

This module contains useful functions which operate on the torch.Tensor objects manipulated in the gausstorch package.

gausstorch.utils.operations.cholesky_inverse_det(M: Tensor) tuple[source]

This function is faster and more accurate than torch.det and torch.inverse for symmetric positive definite matrices

Parameters:

M (torch.Tensor) – Symmetric definite tensor to inverse

Raises:

torch._C._LinAlgError – Matrix is not symmetrix definite

Returns:

Inverse of M, and determinant of M

Return type:

tuple

gausstorch.utils.operations.keep_quads_in_alpha_r(alpha_r: Tensor, quad_key_1: str, quad_key_2: str) Tensor[source]

Truncates a quadrature displacement vector by keeping only the quad_key_1 and quad_key_2 components

Parameters:
  • alpha_r (torch.Tensor) – Quadrature displacement vector

  • quad_key_1 (str) – Quadrature key (e.g: ‘P2’)

  • quad_key_2 (str) – Quadrature key (e.g: ‘X0’)

Returns:

Truncated quadrature vector

Return type:

torch.Tensor

gausstorch.utils.operations.keep_quads_in_sigma_r(sigma_r: Tensor, quad_key_1: str, quad_key_2: str) Tensor[source]

Truncates a quadrature covariance matrix by keeping only the quad_key_1 and quad_key_2 components

Parameters:
  • sigma_r (torch.Tensor) – Quadrature covariance matrix

  • quad_key_1 (str) – Quadrature key (e.g: ‘P2’)

  • quad_key_2 (str) – Quadrature key (e.g: ‘X0’)

Returns:

Truncated covariance matrix

Return type:

torch.Tensor

gausstorch.utils.operations.moments_to_quad_moments(alpha: Tensor, sigma: Tensor) tuple[source]

Transforms field operator moments to quadrature moments

Parameters:
  • alpha (torch.Tensor)

  • sigma (torch.Tensor) – 2M*2M tensor

  • alpha – (2M,1)-shape tensor, representing the displacement vector

  • sigma – (2M,2M)-shape tensor, representing the covariance matrix

Returns:

alpha and sigma of quadratures

Returns:

quadrature displacement and covariance matrix

Return type:

tuple

gausstorch.utils.operations.quad_key_to_index(key: str, M: int) int[source]

Returns the index at which to find the quadrature in the displacement or covariance matrix

Returns:

Parameters:
  • key (str) – Quadrature key (e.g: ‘P2’)

  • M (int) – Number of modes

Raises:

AssertionError – Checks if the quadrature is X or P

Returns:

Index at which to find the quadrature in the displacement or covariance matrix

Return type:

int

gausstorch.utils.operations.slice_2d_alpha(M: Tensor, i: int, j: int) Tensor[source]

Use to trace the displacement vector over modes not i and j.

Parameters:
  • M (torch.Tensor) – A (n,1) shape tensor. In the context of this function’s use, M is a displacement vector

  • i (int) – first index to trace lines over

  • j (int) – second index to trace lines over

Returns:

[[M[i,0]], [M[j]]]

Return type:

torch.Tensor

gausstorch.utils.operations.slice_2d_n1(M: Tensor, i: int) Tensor[source]

Used in slice_2d_alpha to get the value of the (n,1) shape tensor M at index i

Parameters:
  • M (torch.Tensor) – (n,1) shape tensor

  • i (int) – Index value

Returns:

value of M[i, 0]

Return type:

torch.Tensor

gausstorch.utils.operations.slice_2d_nn(M: Tensor, i: int, j: int) Tensor[source]

Used in slice_2d_sigma to get the value of the (n,n) shape tensor M at indexes i and j

Parameters:
  • M (torch.Tensor) – (n,n) shape tensor

  • i (int) – First index value

  • j (int) – Second index value

Returns:

Value of M[i,j]

Return type:

torch.Tensor

gausstorch.utils.operations.slice_2d_sigma(M: Tensor, i: int, j: int) Tensor[source]

Use to trace the covariance matrix over modes not i and j. Will usually be called with j = i + (M.shape[0] // 2).

Parameters:
  • M (torch.Tensor) – A square tensor. In the context of this function’s use, M is a covariance matrix

  • i (int) – first index to trace lines and columns over

  • j (int) – second index to trace lines and columns over

Returns:

[[M[i,i], M[i,j]], [M[j,i], M[j,j]]]

Return type:

torch.Tensor

gausstorch.utils.operations.torch_block(A: Tensor, B: Tensor, C: Tensor, D: Tensor) Tensor[source]

Creates a block tensor with 4 square tensors of equal sizes

Parameters:
  • A (torch.Tensor) – Square tensor

  • B (torch.Tensor) – Square tensor

  • C (torch.Tensor) – Square tensor

  • D (torch.Tensor) – Square tensor

Returns:

Block tensor [[A, B], [C, D]]

Return type:

torch.Tensor

gausstorch.utils.operations.truncate_alpha(alpha: Tensor, modes_kept: list) Tensor[source]

Truncates the displacement vector, keeping only the modes in modes_kept

Parameters:
  • alpha (torch.Tensor) – Full displacement vector

  • modes_kept (list) – List of modes to keep

Returns:

Truncated displacement vector

Return type:

torch.Tensor

gausstorch.utils.operations.truncate_sigma(sigma: Tensor, modes_kept: list) Tensor[source]

Truncates the covariance matrix, keeping only the modes in modes_kept

Parameters:
  • sigma (torch.Tensor) – Full covariance matrix (square tensor)

  • modes_kept (list) – List of modes to keep

Returns:

Truncated covariance matrix

Return type:

torch.Tensor

gausstorch.utils.operations.wigner(d: Tensor, alpha: Tensor, sigma: Tensor) tensor[source]

Returns the wigner function at a d phase space location

Parameters:
  • d (torch.Tensor) – (2M * 1 tensor) Phase space location

  • alpha (torch.Tensor) – (2M * 1 tensor) Quadrature displacement

  • sigma (torch.Tensor) – (2M*2M tensor) Quadrature covariance matrix

Returns:

wigner function at d coordinate

Return type:

torch.tensor

gausstorch.utils.operations.wigner_2d_map(alpha: Tensor, sigma: Tensor, xvec: Tensor = None, yvec: Tensor = None) Tensor[source]

Returns the Wigner quasi-probability distribution

Parameters:
  • alpha (torch.Tensor) – Quadrature displacement vector

  • sigma (torch.Tensor) – Covariance displacement vector

  • xvec (torch.Tensor, optional) – 1st quadrature values. Defaults to None.

  • yvec (torch.Tensor, optional) – 2nd quadrature values. Defaults to None.

Returns:

2D Tensor with wigner function values

Return type:

torch.Tensor

gausstorch.utils.param_processing module

gausstorch.utils.param_processing.rescale_law(unscaled_val: Tensor, par_key: str, R: Tensor) Tensor[source]

Returns rescaled value of a physical parameter.

Parameters:
  • unscaled_val (torch.Tensor) – unscaled value of parameter to set

  • par_key (str) – name of the parameter whose value to substitute

  • R (torch.Tensor) – rescaling factor

Returns:

Rescaled parameter

Return type:

torch.Tensor

gausstorch.utils.param_processing.rescale_pars(pars: dict, R: tensor) dict[source]

Returns a dict with all rescaled parameters from the pars argument

Parameters:
  • pars (dict) – Contains parameter key-value pairs

  • R (torch.tensor) – Scaling parameter

Returns:

Dict with rescaled parameters

Return type:

dict

gausstorch.utils.param_processing.unscale_law(rescaled_val: Tensor, par_key: str, R: Tensor) Tensor[source]

Performs the inverse operation to rescale_law()

Parameters:
  • rescaled_val (torch.Tensor) – rescaled value of parameter to unscale

  • par_key (str) – name of the parameter whose value to substitute

  • R (torch.Tensor) – rescaling factor

Raises:

NameError – If the par_key parameter key is not valid

Returns:

Unscaled value

Return type:

torch.Tensor

gausstorch.utils.test_operations module

Test module for the gausstorch.utils.operations module.

gausstorch.utils.test_operations.test_torch_block(n=3)[source]

Check the gausstorch.utils.operations.torch_block() function works properly

Parameters:

n (int, optional) – Size of component tensors. Defaults to 3.

gausstorch.utils.test_operations.test_wigner(d: Tensor = tensor([[0.], [0.]]), alpha: Tensor = tensor([[0.], [0.]]), sigma: Tensor = tensor([[0.5000, 0.0000], [0.0000, 0.5000]]))[source]

Checks if gausstorch.utils.operations.wigner() evaluation at zero of vacuum state is equal to 1/pi.

Parameters:
  • d (torch.Tensor) – Phase space location. Defaults to torch.zeros((2, 1)).

  • alpha (torch.Tensor) – Quadrature displacement vector. Defaults to torch.zeros((2, 1)).

  • sigma (torch.Tensor) – Quadrature covariance matrix. Defaults to 0.5*torch.eye(2).

Module contents