neurophox.numpy package

neurophox.numpy.generic module

class neurophox.numpy.generic.CompoundTransformerNumpyLayer(units, transformer_list)[source]

Bases: neurophox.numpy.generic.TransformerNumpyLayer

inverse_transform(outputs)[source]

Transform outputs using layer

Parameters

outputs (ndarray) – Outputs to be inverse-transformed by layer

Return type

ndarray

Returns

Transformed outputs

transform(inputs)[source]

Transform inputs using layer (needs to be overwritten by child classes)

Parameters

inputs (ndarray) – Inputs to be transformed by layer

Return type

ndarray

Returns

Transformed inputs

class neurophox.numpy.generic.MeshNumpy(model)[source]

Bases: object

Parameters

model (MeshModel) – The MeshModel model of the mesh network (e.g., rectangular, triangular, custom, etc.)

beamsplitter_layers()[source]
Returns

List of beamsplitter layers to be used by any instance of MeshNumpyLayer

mesh_layers(phases, use_different_errors=False)[source]
Parameters
  • phases (MeshPhases) – The MeshPhases object containing \(\boldsymbol{\theta}, \boldsymbol{\phi}, \boldsymbol{\gamma}\)

  • use_different_errors – use different errors for the left and right beamsplitters

Return type

List[MeshVerticalNumpyLayer]

Returns

List of mesh layers to be used by any instance of MeshNumpyLayer

class neurophox.numpy.generic.MeshNumpyLayer(mesh_model, phase_loss_fn=None)[source]

Bases: neurophox.numpy.generic.TransformerNumpyLayer

Mesh network layer for unitary operators implemented in numpy

Parameters

mesh_model (MeshModel) – The MeshModel model of the mesh network (e.g., rectangular, triangular, custom, etc.)

adjoint_variable_fields(inputs, adjoint_inputs)[source]
Return type

Tuple[ndarray, ndarray, ndarray]

adjoint_variable_gradient(inputs, adjoint_inputs)[source]
Return type

MeshPhases

inverse_propagate(outputs, explicit=False, viz_perm_idx=None)[source]

Inverse propagate outputs for each \(\ell < L\) (where \(U_\ell\) represents the matrix for layer \(\ell\)):

\[V_{\ell} = V_{\mathrm{out}} \prod_{\ell' = L}^{\ell} (U^{(\ell')})^\dagger,\]

where \(U \in \mathrm{U}(N)\) and \(V_{\ell}, V_{\mathrm{out}} \in \mathbb{C}^{M \times N}\).

Parameters
  • outputs (ndarray) – outputs batch represented by matrix \(V_{\mathrm{out}} \in \mathbb{C}^{M \times N}\)

  • explicit (bool) – explicitly show field propagation through the MZIs (useful for photonic simulations)

  • viz_perm_idx (Optional[ndarray]) – permutation of fields to visualize the propagation (None means do not permute fields),

  • is useful for grid meshes (this) –

  • rectangular and triangular meshes. (e.g.) –

Return type

ndarray

Returns

Propagation of outputs over all \(L\) layers to form an array \(V_{\mathrm{prop}} \in \mathbb{C}^{L \times M \times N}\), which is a concatenation of the \(V_{\ell}\).

inverse_transform(outputs)[source]

Performs the operation (where \(U\) represents the matrix for this layer):

\[V_{\mathrm{in}} = V_{\mathrm{out}} U^\dagger,\]

where \(U \in \mathrm{U}(N)\) and \(V_{\mathrm{out}}, V_{\mathrm{in}} \in \mathbb{C}^{M \times N}\).

Parameters

outputs (ndarray) – outputs batch represented by the matrix \(V_{\mathrm{out}} \in \mathbb{C}^{M \times N}\)

Return type

ndarray

Returns

Inverse transformation of outputs

property nullification_set

The nullification set is calculated to program layers in parallel from final layer towards the first layer since he architecture assumed for this calculation is currently the inverse of our feedforward mesh definition. Therefore, we find vectors that can be shined backwards (using inverse_propagate) starting from the outputs to program this device from final layer towards the first layer.

Return type

ndarray

Returns

The \(N imes L\) nullification set array for the inverse of this layer specified by model

property phases

Returns: The MeshPhases object for this layer

propagate(inputs, explicit=False, viz_perm_idx=None)[source]

Propagate inputs for each \(\ell < L\) (where \(U_\ell\) represents the matrix for layer \(\ell\)):

\[V_{\ell} = V_{\mathrm{in}} \prod_{\ell' = 1}^{\ell} U^{(\ell')},\]

where \(U \in \mathrm{U}(N)\) and \(V_{\ell}, V_{\mathrm{in}} \in \mathbb{C}^{M \times N}\).

Parameters
  • inputs (ndarray) – inputs batch represented by matrix \(V_{\mathrm{in}} \in \mathbb{C}^{M \times N}\)

  • explicit (bool) – explicitly show field propagation through the MZIs (useful for photonic simulations)

  • viz_perm_idx (Optional[ndarray]) – permutation of fields to visualize the propagation (None means do not permute fields),

  • is useful for grid meshes (this) –

  • rectangular and triangular meshes. (e.g.) –

  • phase_loss_fn – a function converting phase shift to loss

Return type

ndarray

Returns

Propagation of inputs over all \(L\) layers to form an array \(V_{\mathrm{prop}} \in \mathbb{C}^{L \times M \times N}\), which is a concatenation of the \(V_{\ell}\).

transform(inputs)[source]

Performs the operation (where \(U\) represents the matrix for this layer):

\[V_{\mathrm{out}} = V_{\mathrm{in}} U,\]

where \(U \in \mathrm{U}(N)\) and \(V_{\mathrm{out}}, V_{\mathrm{in}} \in \mathbb{C}^{M \times N}\).

Parameters

inputs (ndarray) – inputs batch represented by the matrix \(V_{\mathrm{in}} \in \mathbb{C}^{M \times N}\)

Return type

ndarray

Returns

Forward transformation of inputs

class neurophox.numpy.generic.MeshParam(param, units)[source]

Bases: object

A class that arranges parameters to simulate any feedforward mesh

Parameters
  • param (ndarray) – parameter to arrange in mesh

  • units (int) – number of inputs/outputs of the mesh

property checkerboard_arrangement

Returns: Checkerboard arrangement of phases useful for grid mesh structures (e.g. rectangular and triangular meshes)

Return type

ndarray

property common_mode_arrangement

The common-mode arrangement based on the \(C(\theta)\) transfer matrix for PhaseShiftCommonMode is one where elements of param are on the even rows and repeated on respective odd rows.

In particular, given the param array \(\boldsymbol{\theta} = [\boldsymbol{\theta}_1, \boldsymbol{\theta}_2, \ldots \boldsymbol{\theta}_M]^T\), where \(\boldsymbol{\theta}_n\) represent row vectors and \(M = \lfloor\frac{N}{2}\rfloor\), the common-mode arrangement has the stripe array form \(\widetilde{\boldsymbol{\theta}} = [\boldsymbol{\theta}_1, \boldsymbol{\theta}_1, \boldsymbol{\theta}_2, \boldsymbol{\theta}_2, \ldots \boldsymbol{\theta}_M, \boldsymbol{\theta}_M]^T\) where \(\widetilde{\boldsymbol{\theta}}\) defines the spatial arrangement of mesh phases.

Return type

ndarray

Returns

Common-mode arrangement array of phases

property differential_mode_arrangement

The differential-mode arrangement is based on the \(D(\theta)\) transfer matrix for PhaseShiftDifferentialMode.

Given the param array \(\boldsymbol{\theta} = [\cdots \boldsymbol{\theta}_m \cdots]^T\), where \(\boldsymbol{\theta}_n\) represent row vectors and \(M = \lfloor\frac{N}{2}\rfloor\), the differential-mode arrangement has the form \(\widetilde{\boldsymbol{\theta}} = \left[\cdots \frac{\boldsymbol{\theta}_m}{2}, -\frac{\boldsymbol{\theta}_m}{2} \cdots \right]^T\) where \(\widetilde{\boldsymbol{\theta}} \in \mathbb{R}^{N \times L}\) defines the spatial arrangement of mesh phases.

Return type

ndarray

Returns

Differential-mode arrangement array of phases

param_list(mask)[source]
Parameters

mask (ndarray) – Mask to ignore params in output

Return type

ndarray

Returns

A flattened array of unmasked params in param

property single_mode_arrangement

The single-mode arrangement based on the \(L(\theta)\) transfer matrix for PhaseShiftUpper is one where elements of param are on the even rows and all odd rows are zero.

In particular, given the param array \(\boldsymbol{\theta} = [\boldsymbol{\theta}_1, \boldsymbol{\theta}_2, \ldots \boldsymbol{\theta}_M]^T\), where \(\boldsymbol{\theta}_m\) represent row vectors and \(M = \lfloor\frac{N}{2}\rfloor\), the single-mode arrangement has the stripe array form \(\widetilde{\boldsymbol{\theta}} = [\boldsymbol{\theta}_1, \boldsymbol{0}, \boldsymbol{\theta}_2, \boldsymbol{0}, \ldots \boldsymbol{\theta}_M, \boldsymbol{0}]^T\) where \(\widetilde{\boldsymbol{\theta}}\) defines the spatial arrangement of mesh phases and \(\boldsymbol{0}\) represents an array of zeros of the same size as \(\boldsymbol{\theta}_m\).

Return type

ndarray

Returns

Single-mode arrangement array of phases

class neurophox.numpy.generic.MeshPhases(theta, phi, gamma, mask=None, basis='bloch', hadamard=False, phase_loss_fn=None)[source]

Bases: object

Arranges the phases in the mesh appropriately depending on basis using the MeshParam class.

Parameters
  • theta (ndarray) – Array to be converted to \(\boldsymbol{\theta}\)

  • phi (ndarray) – Array to be converted to \(\boldsymbol{\phi}\)

  • gamma (ndarray) – Array to be converted to \(\boldsymbol{\gamma}\)

  • mask (Optional[ndarray]) – Mask over values of theta and phi that are not in bar state

  • basis (str) – Phase basis to use

  • hadamard (bool) – Whether to use Hadamard convention

  • phase_loss_fn (Optional[Callable[[ndarray], ndarray]]) – Incorporate phase shift-dependent loss into the model

property external_phase_shift_layers

Elementwise applying complex exponential to external_phase_shifts.

Returns

External phase shift layers corresponding to \(\boldsymbol{\phi}\)

property external_phase_shifts

The external phase shift matrix of the mesh corresponds to an L times N array of phase shifts (outside of beamsplitters, thus external) where \(L\) is number of layers and \(N\) is number of inputs/outputs

Returns

External phase shift matrix corresponding to \(\boldsymbol{\phi}\)

property internal_phase_shift_layers

Elementwise applying complex exponential to internal_phase_shifts.

Returns

Internal phase shift layers corresponding to \(\boldsymbol{\theta}\)

property internal_phase_shifts

The internal phase shift matrix of the mesh corresponds to an L times N array of phase shifts (in between beamsplitters, thus internal) where \(L\) is number of layers and \(N\) is number of inputs/outputs

Returns

Internal phase shift matrix corresponding to \(\boldsymbol{\theta}\)

property params
class neurophox.numpy.generic.MeshVerticalNumpyLayer(tunable_layer, right_perm_idx=None, left_perm_idx=None)[source]

Bases: neurophox.numpy.generic.TransformerNumpyLayer

Parameters
  • tunable_layer (ndarray) – tunable layer

  • left_perm_idx (Optional[ndarray]) – the permutation for the mesh vertical layer (prior to the coupling operation)

  • right_perm_idx (Optional[ndarray]) – the right permutation for the mesh vertical layer (usually for the final layer and after the coupling operation)

inverse_transform(outputs)[source]

Inverse-propagate inputs through single layer \(\ell < L\) (where \(U_\ell\) represents the matrix for layer \(\ell\)):

\[V_{\mathrm{in}} = V_{\mathrm{out}} (U^{(\ell')})^\dagger,\]

where \(V_{\mathrm{out}}, V_{\mathrm{in}} \in \mathbb{C}^{M \times N}\).

Parameters

outputs (ndarray) – outputs batch represented by the matrix \(V_{\mathrm{out}} \in \mathbb{C}^{M \times N}\)

Returns

Inverse propagation of outputs through single layer \(\ell\) to form an array \(V_{\mathrm{in}} \in \mathbb{C}^{M \times N}\).

transform(inputs)[source]

Propagate inputs through single layer \(\ell < L\) (where \(U_\ell\) represents the matrix for layer \(\ell\)):

\[V_{\mathrm{out}} = V_{\mathrm{in}} U^{(\ell')},\]

where \(V_{\mathrm{out}}, V_{\mathrm{in}} \in \mathbb{C}^{M \times N}\).

Parameters

inputs (ndarray) – inputs batch represented by the matrix \(V_{\mathrm{in}} \in \mathbb{C}^{M \times N}\)

Returns

Propagation of inputs through single layer \(\ell\) to form an array \(V_{\mathrm{out}} \in \mathbb{C}^{M \times N}\).

class neurophox.numpy.generic.TransformerNumpyLayer(units)[source]

Bases: object

Base transformer layer class for transformers in numpy (invertible functions, usually linear)

Parameters

units (int) – Dimension of the input, \(N\).

property inverse_matrix

Shortcut of transformer.inverse_transform(np.eye(self.units))

Returns

Inverse matrix implemented by layer

inverse_transform(outputs)[source]

Transform outputs using layer

Parameters

outputs (ndarray) – Outputs to be inverse-transformed by layer

Return type

ndarray

Returns

Transformed outputs

property matrix

Shortcut of transformer.transform(np.eye(self.units))

Returns

Matrix implemented by layer

plot(plt)[source]

Plot transformer.matrix.

Parameters

pltmatplotlib.pyplot for plotting

transform(inputs)[source]

Transform inputs using layer (needs to be overwritten by child classes)

Parameters

inputs (ndarray) – Inputs to be transformed by layer

Return type

ndarray

Returns

Transformed inputs

neurophox.numpy.layers module

class neurophox.numpy.layers.BMNumpy(num_layers, basis='bloch', bs_error=0.0, hadamard=False, theta_init='random_theta', phi_init='random_phi')[source]

Bases: neurophox.numpy.generic.MeshNumpyLayer

class neurophox.numpy.layers.PRMNumpy(units, tunable_layers_per_block=None, num_tunable_layers_list=None, sampling_frequencies=None, bs_error=0.0, hadamard=False, theta_init='haar_prm', phi_init='random_phi')[source]

Bases: neurophox.numpy.generic.MeshNumpyLayer

class neurophox.numpy.layers.RMNumpy(units, num_layers=None, hadamard=False, basis='bloch', bs_error=0.0, theta_init='haar_rect', phi_init='random_phi', gamma_init='random_gamma')[source]

Bases: neurophox.numpy.generic.MeshNumpyLayer

inverse_propagate(inputs, explicit=False, viz_perm_idx=None)[source]

Inverse propagate outputs for each \(\ell < L\) (where \(U_\ell\) represents the matrix for layer \(\ell\)):

\[V_{\ell} = V_{\mathrm{out}} \prod_{\ell' = L}^{\ell} (U^{(\ell')})^\dagger,\]

where \(U \in \mathrm{U}(N)\) and \(V_{\ell}, V_{\mathrm{out}} \in \mathbb{C}^{M \times N}\).

Parameters
  • outputsoutputs batch represented by matrix \(V_{\mathrm{out}} \in \mathbb{C}^{M \times N}\)

  • explicit (bool) – explicitly show field propagation through the MZIs (useful for photonic simulations)

  • viz_perm_idx (Optional[ndarray]) – permutation of fields to visualize the propagation (None means do not permute fields),

  • is useful for grid meshes (this) –

  • rectangular and triangular meshes. (e.g.) –

Return type

ndarray

Returns

Propagation of outputs over all \(L\) layers to form an array \(V_{\mathrm{prop}} \in \mathbb{C}^{L \times M \times N}\), which is a concatenation of the \(V_{\ell}\).

propagate(inputs, explicit=False, viz_perm_idx=None)[source]

Propagate inputs for each \(\ell < L\) (where \(U_\ell\) represents the matrix for layer \(\ell\)):

\[V_{\ell} = V_{\mathrm{in}} \prod_{\ell' = 1}^{\ell} U^{(\ell')},\]

where \(U \in \mathrm{U}(N)\) and \(V_{\ell}, V_{\mathrm{in}} \in \mathbb{C}^{M \times N}\).

Parameters
  • inputs (ndarray) – inputs batch represented by matrix \(V_{\mathrm{in}} \in \mathbb{C}^{M \times N}\)

  • explicit (bool) – explicitly show field propagation through the MZIs (useful for photonic simulations)

  • viz_perm_idx (Optional[ndarray]) – permutation of fields to visualize the propagation (None means do not permute fields),

  • is useful for grid meshes (this) –

  • rectangular and triangular meshes. (e.g.) –

  • phase_loss_fn – a function converting phase shift to loss

Return type

ndarray

Returns

Propagation of inputs over all \(L\) layers to form an array \(V_{\mathrm{prop}} \in \mathbb{C}^{L \times M \times N}\), which is a concatenation of the \(V_{\ell}\).

class neurophox.numpy.layers.TMNumpy(units, hadamard=False, basis='bloch', bs_error=0.0, theta_init='haar_tri', phi_init='random_phi', gamma_init='random_gamma')[source]

Bases: neurophox.numpy.generic.MeshNumpyLayer

inverse_propagate(inputs, explicit=False, viz_perm_idx=None)[source]

Inverse propagate outputs for each \(\ell < L\) (where \(U_\ell\) represents the matrix for layer \(\ell\)):

\[V_{\ell} = V_{\mathrm{out}} \prod_{\ell' = L}^{\ell} (U^{(\ell')})^\dagger,\]

where \(U \in \mathrm{U}(N)\) and \(V_{\ell}, V_{\mathrm{out}} \in \mathbb{C}^{M \times N}\).

Parameters
  • outputsoutputs batch represented by matrix \(V_{\mathrm{out}} \in \mathbb{C}^{M \times N}\)

  • explicit (bool) – explicitly show field propagation through the MZIs (useful for photonic simulations)

  • viz_perm_idx (Optional[ndarray]) – permutation of fields to visualize the propagation (None means do not permute fields),

  • is useful for grid meshes (this) –

  • rectangular and triangular meshes. (e.g.) –

Return type

ndarray

Returns

Propagation of outputs over all \(L\) layers to form an array \(V_{\mathrm{prop}} \in \mathbb{C}^{L \times M \times N}\), which is a concatenation of the \(V_{\ell}\).

propagate(inputs, explicit=False, viz_perm_idx=None)[source]

Propagate inputs for each \(\ell < L\) (where \(U_\ell\) represents the matrix for layer \(\ell\)):

\[V_{\ell} = V_{\mathrm{in}} \prod_{\ell' = 1}^{\ell} U^{(\ell')},\]

where \(U \in \mathrm{U}(N)\) and \(V_{\ell}, V_{\mathrm{in}} \in \mathbb{C}^{M \times N}\).

Parameters
  • inputs (ndarray) – inputs batch represented by matrix \(V_{\mathrm{in}} \in \mathbb{C}^{M \times N}\)

  • explicit (bool) – explicitly show field propagation through the MZIs (useful for photonic simulations)

  • viz_perm_idx (Optional[ndarray]) – permutation of fields to visualize the propagation (None means do not permute fields),

  • is useful for grid meshes (this) –

  • rectangular and triangular meshes. (e.g.) –

  • phase_loss_fn – a function converting phase shift to loss

Return type

ndarray

Returns

Propagation of inputs over all \(L\) layers to form an array \(V_{\mathrm{prop}} \in \mathbb{C}^{L \times M \times N}\), which is a concatenation of the \(V_{\ell}\).