mm.lab_dev.states.State

class mrmustard.lab_dev.states.State(name=None, representation=None, modes_out_bra=None, modes_in_bra=None, modes_out_ket=None, modes_in_ket=None)[source]

Bases: CircuitComponent

Base class for all states.

L2_norm

The L2 norm of a Ket, or the Hilbert-Schmidt norm of a DM.

adjoint

The AdjointView of this component.

bargmann_triple

The (A, b, c) triple that describes this state in the Bargmann representation.

dual

The DualView of this component.

is_pure

Whether this state is pure.

modes

The sorted list of modes of this component.

n_modes

The number of modes in this component.

name

The name of this component.

parameter_set

The set of parameters characterizing this component.

probability

Returns \(\langle\psi|\psi\rangle\) for Ket states \(|\psi\rangle\) and \(\text{Tr}(\rho)\) for DM states \(\rho\).

purity

The purity of this state.

representation

A representation of this circuit component.

wires

The wires of this component.

L2_norm

The L2 norm of a Ket, or the Hilbert-Schmidt norm of a DM.

adjoint

The AdjointView of this component.

bargmann_triple

The (A, b, c) triple that describes this state in the Bargmann representation.

Returns:

The (A, b, c) triple that describes this state in the Bargmann representation.

Raises:

ValueError – If the triple cannot be calculated given the state’s representation.

dual

The DualView of this component.

is_pure

Whether this state is pure.

modes

The sorted list of modes of this component.

n_modes

The number of modes in this component.

name

The name of this component.

parameter_set

The set of parameters characterizing this component.

probability

Returns \(\langle\psi|\psi\rangle\) for Ket states \(|\psi\rangle\) and \(\text{Tr}(\rho)\) for DM states \(\rho\).

purity

The purity of this state.

representation

A representation of this circuit component.

wires

The wires of this component.

fock_array([shape])

The array that describes this state in the Fock representation.

from_bargmann(modes, triple[, name, batched])

Initializes a state from an (A, b, c) triple defining a Bargmann representation.

from_fock(modes, array[, name, batched])

Initializes a state from an array describing the state in the Fock representation.

from_phase_space(modes, cov, means[, name, ...])

Initializes a state from the covariance matrix and the vector of means of a state in phase space.

from_quadrature()

Initializes a state from quadrature.

light_copy()

Creates a copy of this component by copying every data stored in memory for it by reference, except for its wires, which are copied by value.

on(modes)

Creates a copy of this component that acts on the given modes instead of on the original modes.

phase_space(s)

Returns the phase space parametrization of a state, consisting in a covariance matrix, a vector of means and a scaling coefficient.

to_fock_component([shape])

Returns a circuit component with the same attributes as this component, but with Fock representation.

visualize_2d([xbounds, pbounds, resolution, ...])

2D visualization of the Wigner function of this state.

visualize_3d([xbounds, pbounds, resolution, ...])

3D visualization of the Wigner function of this state on a surface plot.

visualize_dm([cutoff, return_fig])

Plots the absolute value \(abs(\rho)\) of the density matrix \(\rho\) of this state on a heatmap.

fock_array(shape=None)[source]

The array that describes this state in the Fock representation.

Uses the mrmustard.physics.converters.to_fock() method to convert the internal representation into a Fock object.

Parameters:
  • shape (Union[int, Sequence[int], None]) – The shape of the returned array. If shape``is given as an ``int, it is

  • None (broadcasted to all the dimensions. If) –

  • of (it defaults to the value) –

  • settings. (AUTOCUTOFF_MAX_CUTOFF in the) –

Return type:

ndarray[Tuple[int, ...], TypeVar(C, complex64, complex128)]

Returns:

The array that describes this state in the Fock representation.

classmethod from_bargmann(modes, triple, name=None, batched=False)[source]

Initializes a state from an (A, b, c) triple defining a Bargmann representation.

>>> from mrmustard.physics.representations import Bargmann
>>> from mrmustard.physics.triples import coherent_state_Abc
>>> from mrmustard.lab_dev import Ket

>>> modes = [0, 1]
>>> triple = coherent_state_Abc(x=[0.1, 0.2])

>>> coh = Ket.from_bargmann(modes, triple)
>>> assert coh.modes == modes
>>> assert coh.representation == Bargmann(*triple)
>>> assert isinstance(coh, Ket)
Parameters:
  • modes (Sequence[int]) – The modes of this states.

  • triple (tuple[ndarray[Tuple[int, int], TypeVar(C, complex64, complex128)], ndarray[Tuple[int], TypeVar(C, complex64, complex128)], complex]) – The (A, b, c) triple.

  • name (Optional[str]) – The name of this state.

  • batched (bool) – Whether the given triple is batched.

Return type:

State

Returns:

A state.

Raises:

ValueError – If the A or b have a shape that is inconsistent with the number of modes.

classmethod from_fock(modes, array, name=None, batched=False)[source]

Initializes a state from an array describing the state in the Fock representation.

>>> from mrmustard.physics.representations import Fock
>>> from mrmustard.physics.triples import coherent_state_Abc
>>> from mrmustard.lab_dev import Coherent, Ket

>>> modes = [0]
>>> array = Coherent(modes, x=0.1).to_fock_component().representation.array
>>> coh = Ket.from_fock(modes, array, batched=True)

>>> assert coh.modes == modes
>>> assert coh.representation == Fock(array)
>>> assert isinstance(coh, Ket)
Parameters:
  • modes (Sequence[int]) – The modes of this states.

  • array (ndarray[Tuple[int, ...], TypeVar(C, complex64, complex128)]) – The Fock array.

  • name (Optional[str]) – The name of this state.

  • batched (bool) – Whether the given array is batched.

Return type:

State

Returns:

A state.

Raises:

ValueError – If the given array has a shape that is inconsistent with the number of modes.

classmethod from_phase_space(modes, cov, means, name=None, atol_purity=0.001)[source]

Initializes a state from the covariance matrix and the vector of means of a state in phase space.

Parameters:
  • cov (ndarray[Tuple[int, int], TypeVar(C, complex64, complex128)]) – The covariance matrix.

  • means (ndarray[Tuple[int, int], TypeVar(C, complex64, complex128)]) – The vector of means.

  • modes (Sequence[int]) – The modes of this states.

  • name (Optional[str]) – The name of this state.

  • atol_purity (Optional[float]) – If atol_purity is given, the purity of the state is computed, and an error is raised if its value is smaller than 1-atol_purity or larger than 1+atol_purity. If None, this check is skipped.

Return type:

State

Returns:

A state.

Raises:
  • ValueError – If the given cov and means have shapes that are inconsistent with the number of modes.

  • ValueError – If atol_purity is not None and the purity of the returned state is smaller than 1-atol_purity or larger than 1+atol_purity.

classmethod from_quadrature()[source]

Initializes a state from quadrature.

Return type:

State

light_copy()

Creates a copy of this component by copying every data stored in memory for it by reference, except for its wires, which are copied by value.

Return type:

CircuitComponent

on(modes)

Creates a copy of this component that acts on the given modes instead of on the original modes.

Parameters:

modes (Sequence[int]) – The new modes that this component acts on.

Return type:

CircuitComponent

Returns:

The component acting on the specified modes.

Raises:

ValueError – If modes contains more or less modes than the original component.

phase_space(s)[source]

Returns the phase space parametrization of a state, consisting in a covariance matrix, a vector of means and a scaling coefficient. When a state is a linear superposition of Gaussians each of cov, means, coeff are arranged in a batch. Phase space representations are labelled by an s parameter (float) which modifies the exponent of \(D_s(\gamma) = e^{\frac{s}{2}|\gamma|^2}D(\gamma)\), which is the operator basis used to expand phase space density matrices. The s parameter typically takes the values of -1, 0, 1 to indicate Glauber/Wigner/Husimi functions. Note that the same (cov, means, coeff) triple can be used to parametrize the characteristic functions as well.

Parameters:
  • s (float) – The phase space parameter

  • Returns – The covariance matrix, the mean vector and the coefficient of the state in s-parametrized phase space.

Return type:

tuple[ndarray[Tuple[int, int], TypeVar(C, complex64, complex128)], ndarray[Tuple[int], TypeVar(C, complex64, complex128)], complex]

to_fock_component(shape=None)

Returns a circuit component with the same attributes as this component, but with Fock representation.

Uses the mrmustard.physics.converters.to_fock() method to convert the internal representation.

>>> from mrmustard.physics.converters import to_fock
>>> from mrmustard.lab_dev import Dgate

>>> d = Dgate([1], x=0.1, y=0.1)
>>> d_fock = d.to_fock_component(shape=3)

>>> assert d_fock.name == d.name
>>> assert d_fock.wires == d.wires
>>> assert d_fock.representation == to_fock(d.representation, shape=3)
Parameters:

shape (Union[int, Iterable[int], None]) – The shape of the returned representation. If shape``is given as an ``int, it is broadcasted to all the dimensions. If None, it defaults to the value of AUTOCUTOFF_MAX_CUTOFF in the settings.

Return type:

CircuitComponent

visualize_2d(xbounds=(-6, 6), pbounds=(-6, 6), resolution=200, colorscale='viridis', return_fig=False)[source]

2D visualization of the Wigner function of this state.

Plots the Wigner function on a heatmap, alongside the probability distributions on the two quadrature axis.

>>> from mrmustard.lab_dev import Coherent

>>> state = Coherent([0], x=1) / 2**0.5 + Coherent([0], x=-1) / 2**0.5
>>> # state.visualize_2d()
Parameters:
  • xbounds (tuple[int]) – The range of the x axis.

  • pbounds (tuple[int]) – The range of the p axis.

  • resolution (int) – The number of bins on each axes.

  • colorscale (str) – A colorscale. Must be one of Plotly's built-in continuous color scales.

  • return_fig (bool) – Whether to return the Plotly figure.

Return type:

Optional[Figure]

Returns:

A Plotly figure representing the state in 2D.

Raises:

ValueError – If this state is a multi-mode state.

visualize_3d(xbounds=(-6, 6), pbounds=(-6, 6), resolution=200, colorscale='viridis', return_fig=False)[source]

3D visualization of the Wigner function of this state on a surface plot.

Parameters:
  • xbounds (tuple[int]) – The range of the x axis.

  • pbounds (tuple[int]) – The range of the p axis.

  • resolution (int) – The number of bins on each axes.

  • colorscale (str) – A colorscale. Must be one of Plotly's built-in continuous color scales.

  • return_fig (bool) – Whether to return the Plotly figure.

Return type:

Optional[Figure]

Returns:

A Plotly figure representing the state in 3D.

Raises:

ValueError – If this state is a multi-mode state.

visualize_dm(cutoff=None, return_fig=False)[source]

Plots the absolute value \(abs(\rho)\) of the density matrix \(\rho\) of this state on a heatmap.

Parameters:
  • cutoff (Optional[int]) – The desired cutoff. Defaults to the value of AUTOCUTOFF_MAX_CUTOFF in the settings.

  • return_fig (bool) – Whether to return the Plotly figure.

Return type:

Optional[Figure]

Returns:

A Plotly figure representing absolute value of the density matrix of this state.

Raises:

ValueError – If this state is a multi-mode state.