qmath#

Common functions

Functions

align_shape(cov, mean, weight)

Align the shape for Bosonic state.

create_subset(num_coincidence)

Create all subsets from \(\{1,2,...,n\}\).

cv_to_wigner(state, wire[, xrange, prange, ...])

Get the discretized Wigner function of the specified mode from a CV state.

dirac_rep(state[, den_mat, topk])

Convert the batched Fock state tensors to the dictionary of Dirac representation.

fock_combinations(nmode, nphoton[, cutoff, ...])

Generate all possible combinations of Fock states for a given number of modes, photons, and cutoff.

fock_to_wigner(state, wire, nmode, cutoff[, ...])

Get the discretized Wigner function of the specified mode from a Fock state using the iterative method.

get_powerset(n)

Get the powerset of \(\{0,1,...,n-1\}\).

ladder_ops(cutoff[, dtype, device])

Get the matrix representation of the annihilation and creation operators.

ladder_to_quadrature(tensor[, symplectic])

Transform the representation in aaa^+a^+ ordering to the representation in xxpp ordering.

measure_fock_tensor(state[, shots, ...])

Measure the batched Fock state tensors.

permanent(mat)

Calculate the permanent.

permanent_ryser(mat)

Calculate the permanent by Ryser's formula.

photon_number_mean_var_cv(cov, mean[, weight])

Get the expectation value and variance of the photon number for single-mode Gaussian (Bosonic) states.

photon_number_mean_var_fock(state, nmode, ...)

Get the expectation value and variance of the photon number for Fock state tensors.

plot_wigner(wigner, xvec, pvec[, k])

Plot a 2D contour and a 3D surface of a discretized Wigner function W(x, p).

product_factorial(state)

Get the product of the factorial from the Fock state, i.e., \(|s_1,s_2,...s_n> -> s_1!s_2!...s_n!\).

quadrature_mean_fock(state, nmode, cutoff, wires)

Get the expectation value of the quadrature x for Fock state tensors.

quadrature_to_ladder(tensor[, symplectic])

Transform the representation in xxpp ordering to the representation in aaa^+a^+ ordering.

sample_homodyne_fock(state, wire, nmode, cutoff)

Get the samples of homodyne measurement for batched Fock state tensors on one mode.

sample_reject_bosonic(cov, mean, weight, ...)

Get the samples of the Bosonic states via rejection sampling.

schur_anti_symm_even(mat)

Schur decomposition for a real antisymmetric and even-dimensional matrix.

shift_func(lst, nstep)

Shift a list by a number of steps.

sort_dict_fock_basis(state_dict[, idx])

Sort the dictionary of Fock basis states in the descending order of probabilities.

sqrtm_herm(mat)

Compute the positive matrix square root of a Hermitian matrix using eigenvalue decomposition.

sub_matrix(u, input_state, output_state)

Get the submatrix for calculating the transfer amplitude and transfer probabilities.

takagi(a)

Tagaki decomposition for a symmetric complex matrix.

williamson(cov)

Williamson decomposition.

xpxp_to_xxpp(matrix)

Transform the representation in xpxp ordering to the representation in xxpp ordering.

xxpp_to_xpxp(matrix)

Transform the representation in xxpp ordering to the representation in xpxp ordering.

align_shape(cov: Tensor, mean: Tensor, weight: Tensor) list[Tensor][source]#

Align the shape for Bosonic state.

create_subset(num_coincidence: int) Generator[Tensor, None, None][source]#

Create all subsets from \(\{1,2,...,n\}\).

cv_to_wigner(state: list, wire: int, xrange: int | list = 10, prange: int | list = 10, npoints: int | list = 100, plot: bool = True, k: int = 0, normalize: bool = True)[source]#

Get the discretized Wigner function of the specified mode from a CV state.

Parameters:
  • state (list) – The input Gaussianstate or BosonicState.

  • wire (int) – The Wigner function for the given wire.

  • xrange (int | list) – The range of quadrature x. Default: 10

  • prange (int | list) – The range of quadrature p. Default: 10

  • npoints (int | list) – The number of discretization points for quadratures. Default: 100

  • plot (bool) – Whether to plot the Wigner function. Default: True

  • k (int) – The index of the Wigner function within the batch to plot. Default: 0

  • normalize (bool) – Whether to normalize the Wigner function. Default: True

dirac_rep(state: Tensor, den_mat: bool = False, topk: int = 5) dict[source]#

Convert the batched Fock state tensors to the dictionary of Dirac representation.

fock_combinations(nmode: int, nphoton: int, cutoff: int | None = None, nancilla: int = 0) list[list[int]][source]#

Generate all possible combinations of Fock states for a given number of modes, photons, and cutoff.

Parameters:
  • nmode (int) – The number of modes in the system.

  • nphoton (int) – The total number of photons in the system.

  • cutoff (int | None) – The Fock space truncation. Default: None

  • nancilla (int) – The number of ancilla modes (NOT limited by cutoff). Default: 0

Returns:

A list of all possible Fock states, each represented by a list of occupation numbers for each mode.

Return type:

list[list[int]]

Examples

>>> fock_combinations(2, 3)
[[0, 3], [1, 2], [2, 1], [3, 0]]
>>> fock_combinations(3, 2)
[[0, 0, 2], [0, 1, 1], [0, 2, 0], [1, 0, 1], [1, 1, 0], [2, 0, 0]]
>>> fock_combinations(4, 4, 2)
[[1, 1, 1, 1]]
fock_to_wigner(state: Tensor, wire: int, nmode: int, cutoff: int, den_mat: bool = False, xrange: int | list = 10, prange: int | list = 10, npoints: int | list = 100, plot: bool = True, k: int = 0) Tensor[source]#

Get the discretized Wigner function of the specified mode from a Fock state using the iterative method.

See https://qutip.org/docs/4.7/modules/qutip/wigner.html

Parameters:
  • state (Tensor) – The input Fock state tensor or density matrix.

  • wire (int) – The Wigner function for the given wire.

  • nmode (int) – The mode number of the Fock state.

  • cutoff (int) – The Fock space truncation.

  • den_mat (bool) – Whether to use density matrix representation. Only valid for Fock state tensor. Default: False

  • xrange (int | list) – The range of quadrature x. Default: 10

  • prange (int | list) – The range of quadrature p. Default: 10

  • npoints (int | list) – The number of discretization points for quadratures. Default: 100

  • plot (bool) – Whether to plot the Wigner function. Default: True

  • k (int) – The index of the Wigner function within the batch to plot. Default: 0

get_powerset(n: int) list[source]#

Get the powerset of \(\{0,1,...,n-1\}\).

ladder_ops(cutoff: int, dtype=torch.complex64, device='cpu') tuple[Tensor, Tensor][source]#

Get the matrix representation of the annihilation and creation operators.

ladder_to_quadrature(tensor: Tensor, symplectic: bool = False) Tensor[source]#

Transform the representation in aaa^+a^+ ordering to the representation in xxpp ordering.

Parameters:
  • tensor (Tensor) – The input tensor in aaa^+a^+ ordering.

  • symplectic (bool) – Whether the transformation is applied for symplectic matrix or Gaussian state. Default: False (which means covariance matrix or displacement vector)

measure_fock_tensor(state: Tensor, shots: int = 1024, with_prob: bool = False, wires: int | list[int] | None = None, block_size: int = 16777216) dict | list[dict][source]#

Measure the batched Fock state tensors.

Parameters:
  • state (Tensor) – The quantum state to measure. It should be a tensor of shape \((\text{batch}, \text{cutoff}, ..., \text{cutoff})\).

  • shots (int) – The number of times to sample from the quantum state. Default: 1024

  • with_prob (bool) – A flag that indicates whether to return the probabilities along with the number of occurrences. Default: False

  • wires (int | list[int] | None) – The wires to measure. It can be an integer or a list of integers specifying the indices of the wires. Default: None (which means all wires are measured)

  • block_size (int) – The block size for sampling. Default: 2**24

permanent(mat: Tensor) Tensor[source]#

Calculate the permanent.

permanent_ryser(mat: Tensor) Tensor[source]#

Calculate the permanent by Ryser’s formula.

photon_number_mean_var_cv(cov: Tensor, mean: Tensor, weight: Tensor | None = None) tuple[Tensor, Tensor][source]#

Get the expectation value and variance of the photon number for single-mode Gaussian (Bosonic) states.

photon_number_mean_var_fock(state: Tensor, nmode: int, cutoff: int, wires: list[int], den_mat: bool = False) tuple[Tensor, Tensor][source]#

Get the expectation value and variance of the photon number for Fock state tensors.

plot_wigner(wigner: Tensor, xvec: Tensor, pvec: Tensor, k: int = 0)[source]#

Plot a 2D contour and a 3D surface of a discretized Wigner function W(x, p).

Parameters:
  • wigner (Tensor) – Discretized Wigner values with shape (batch, nx, np).

  • xvec (Tensor) – 1D grid for quadrature x.

  • pvec (Tensor) – 1D grid for quadrature p.

  • k (int) – The index of the Wigner function within the batch to plot. Default: 0

product_factorial(state: Tensor) Tensor[source]#

Get the product of the factorial from the Fock state, i.e., \(|s_1,s_2,...s_n> -> s_1!s_2!...s_n!\).

quadrature_mean_fock(state: Tensor, nmode: int, cutoff: int, wires: list[int], den_mat: bool = False) Tensor[source]#

Get the expectation value of the quadrature x for Fock state tensors.

quadrature_to_ladder(tensor: Tensor, symplectic: bool = False) Tensor[source]#

Transform the representation in xxpp ordering to the representation in aaa^+a^+ ordering.

Parameters:
  • tensor (Tensor) – The input tensor in xxpp ordering.

  • symplectic (bool) – Whether the transformation is applied for symplectic matrix or Gaussian state. Default: False (which means covariance matrix or displacement vector)

sample_homodyne_fock(state: Tensor, wire: int, nmode: int, cutoff: int, shots: int = 1, den_mat: bool = False, x_range: float = 15, nbin: int = 100000) Tensor[source]#

Get the samples of homodyne measurement for batched Fock state tensors on one mode.

sample_reject_bosonic(cov: Tensor, mean: Tensor, weight: Tensor, cov_m: Tensor, shots: int) Tensor[source]#

Get the samples of the Bosonic states via rejection sampling.

See https://arxiv.org/abs/2103.05530 Algorithm 1 in Section VI B

schur_anti_symm_even(mat: Tensor) tuple[Tensor, Tensor][source]#

Schur decomposition for a real antisymmetric and even-dimensional matrix.

This function decomposes a real antisymmetric matrix \(A\) into the form \(A = O T O^T\), where \(O\) is an orthogonal matrix and \(T\) is a block-diagonal matrix with \(2 \times 2\) antisymmetric blocks.

shift_func(lst: list, nstep: int) list[source]#

Shift a list by a number of steps.

If nstep is positive, it shifts to the left.

sort_dict_fock_basis(state_dict: dict, idx: int = 0) dict[source]#

Sort the dictionary of Fock basis states in the descending order of probabilities.

sqrtm_herm(mat: Tensor) Tensor[source]#

Compute the positive matrix square root of a Hermitian matrix using eigenvalue decomposition.

sub_matrix(u: Tensor, input_state: Tensor, output_state: Tensor) Tensor[source]#

Get the submatrix for calculating the transfer amplitude and transfer probabilities.

The rows are chosen according to the output state and the columns are chosen according to the input state.

Parameters:
  • u (Tensor) – The unitary matrix.

  • input_state (Tensor) – The input state.

  • output_state (Tensor) – The output state.

takagi(a: Tensor) tuple[Tensor, Tensor][source]#

Tagaki decomposition for a symmetric complex matrix.

See https://math.stackexchange.com/questions/2026110/

williamson(cov: Tensor) tuple[Tensor, Tensor][source]#

Williamson decomposition.

This function decomposes a real symmetric and even-dimensional positive definite matrix \(V\) into the form \(V = S D S^T\), where \(S\) is a symplectic matrix and \(D\) is a diagonal matrix with the symplectic eigenvalues.

See https://arxiv.org/pdf/2403.04596 Section VII.

xpxp_to_xxpp(matrix: Tensor) Tensor[source]#

Transform the representation in xpxp ordering to the representation in xxpp ordering.

xxpp_to_xpxp(matrix: Tensor) Tensor[source]#

Transform the representation in xxpp ordering to the representation in xpxp ordering.