neurophox.torch package¶
neurophox.torch.generic module¶
-
class
neurophox.torch.generic.
CompoundTransformerLayer
(units, transformer_list, is_trainable=False)[source]¶ Bases:
neurophox.torch.generic.TransformerLayer
-
training
: bool¶
-
-
class
neurophox.torch.generic.
MeshParamTorch
(param, units)[source]¶ Bases:
object
A class that cleanly arranges parameters into a specific arrangement that can be used to simulate any mesh
- Parameters
param (
Tensor
) – parameter to arrange in meshunits (
int
) – number of inputs/outputs of the mesh
-
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}_N, \boldsymbol{\theta}_N]^T\). where \(\widetilde{\boldsymbol{\theta}} \in \mathbb{R}^{N \times L}\) defines the \(\boldsymbol{\theta}\) of the final mesh.- Return type
Tensor
- 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 \(\boldsymbol{\theta}\) of the final mesh.- Return type
Tensor
- Returns
Differential-mode arrangement array of phases
-
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}_N, \boldsymbol{0}]^T\). where \(\widetilde{\boldsymbol{\theta}} \in \mathbb{R}^{N \times L}\) defines the \(\boldsymbol{\theta}\) of the final mesh and \(\boldsymbol{0}\) represents an array of zeros of the same size as \(\boldsymbol{\theta}_n\).- Return type
Tensor
- Returns
Single-mode arrangement array of phases
-
class
neurophox.torch.generic.
MeshPhasesTorch
(theta, phi, mask, gamma, units, basis='sm', hadamard=False, theta_fn=None, phi_fn=None, gamma_fn=None, phase_loss_fn=None)[source]¶ Bases:
object
Organizes the phases in the mesh into appropriate arrangements
- Parameters
theta (
Parameter
) – Array to be converted to \(\boldsymbol{\theta}\)phi (
Parameter
) – Array to be converted to \(\boldsymbol{\phi}\)gamma (
Parameter
) – Array to be converted to \(\boldsymbol{\gamma}\)mask (
ndarray
) – Mask over values oftheta
andphi
that are not in bar statebasis (
str
) – Phase basis to usehadamard (
bool
) – Whether to use Hadamard conventiontheta_fn (
Optional
[Callable
]) – Pytorch-friendly phi function call to reparametrize phi (example use cases: can use a mask to keep some values of theta fixed or always bound theta between 0 and pi). By default, use identity function.phi_fn (
Optional
[Callable
]) – Pytorch-friendly phi function call to reparametrize phi (example use cases: can use a mask to keep some values of phi fixed or always bound phi between 0 and 2 * pi). By default, use identity function.gamma_fn (
Optional
[Callable
]) – Pytorch-friendly gamma function call to reparametrize gamma (example use cases: can use a mask to keep some values of gamma fixed or always bound gamma between 0 and 2 * pi). By default, use identity function.phase_loss_fn (
Optional
[Callable
[[Tensor
],Tensor
]]) – Incorporate phase shift-dependent loss into the model. The function is of the form phase_loss_fn(phases), which returns the loss
-
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}\)
-
class
neurophox.torch.generic.
MeshTorchLayer
(mesh_model)[source]¶ Bases:
neurophox.torch.generic.TransformerLayer
Mesh network layer for unitary operators implemented in numpy
- Parameters
mesh_model (
MeshModel
) – The model of the mesh network (e.g., rectangular, triangular, butterfly)
-
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 (
Tensor
) –outputs
batch represented by the matrix \(V_{\mathrm{out}} \in \mathbb{C}^{M \times N}\)- Return type
Tensor
- Returns
Inverse transformed
outputs
, \(V_{\mathrm{in}}\)
-
mesh_layers
(phases)[source]¶ - Parameters
phases (
MeshPhasesTorch
) – TheMeshPhasesTensorflow
object containing \(\boldsymbol{\theta}, \boldsymbol{\phi}, \boldsymbol{\gamma}\)- Return type
List
[MeshVerticalLayer
]- Returns
List of mesh layers to be used by any instance of
MeshLayer
-
property
phases
¶ Returns: The
MeshPhases
object for this layer- Return type
-
training
: bool¶
-
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 (
Tensor
) –inputs
batch represented by the matrix \(V_{\mathrm{in}} \in \mathbb{C}^{M \times N}\)- Return type
Tensor
- Returns
Transformed
inputs
, \(V_{\mathrm{out}}\)
-
class
neurophox.torch.generic.
MeshVerticalLayer
(pairwise_perm_idx, diag, off_diag, right_perm=None, left_perm=None)[source]¶ Bases:
neurophox.torch.generic.TransformerLayer
- Parameters
diag (
Tensor
) – the diagonal terms to multiplyoff_diag (
Tensor
) – the off-diagonal terms to multiplyleft_perm (
Optional
[PermutationLayer
]) – the permutation for the mesh vertical layer (prior to the coupling operation)right_perm (
Optional
[PermutationLayer
]) – 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,\]- Parameters
outputs (
Tensor
) –outputs
batch represented by the matrix \(V_{\mathrm{out}} \in \mathbb{C}^{M \times N}\)- Return type
Tensor
- Returns
Inverse propaged
outputs
through single layer \(\ell\) to form an array \(V_{\mathrm{in}} \in \mathbb{C}^{M \times N}\).
-
training
: bool¶
-
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')},\]- Parameters
inputs (
Tensor
) –inputs
batch represented by the matrix \(V_{\mathrm{in}} \in \mathbb{C}^{M \times N}\)- Return type
Tensor
- Returns
Propaged
inputs
through single layer \(\ell\) to form an array \(V_{\mathrm{out}} \in \mathbb{C}^{M \times N}\).
-
class
neurophox.torch.generic.
PermutationLayer
(permuted_indices)[source]¶ Bases:
neurophox.torch.generic.TransformerLayer
-
training
: bool¶
-
-
class
neurophox.torch.generic.
TransformerLayer
(units, is_trainable=False)[source]¶ Bases:
torch.nn.modules.module.Module
Base transformer class for transformer layers (invertible functions, usually linear)
- Parameters
units (
int
) – Dimension of the input to be transformed by the transformeractivation – Nonlinear activation function (
None
if there’s no nonlinearity)
-
forward
(x)[source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
-
training
: bool¶
neurophox.torch.layers module¶
-
class
neurophox.torch.layers.
BMTorch
(num_layers, hadamard=False, basis='bloch', bs_error=0.0, theta_init='haar_rect', phi_init='random_phi', gamma_init='random_gamma')[source]¶ Bases:
neurophox.torch.generic.MeshTorchLayer
Butterfly mesh unitary layer
- Parameters
hadamard (
bool
) – Hadamard convention for the beamsplittersbasis (
str
) – Phase basis to usebs_error (
float
) – Beamsplitter split ratio errortheta_init (
Union
[str
,tuple
,ndarray
]) – Initializer fortheta
(\(\boldsymbol{\theta}\) or \(\theta_{n\ell}\)) astr
,ndarray
, or tuple of the form(theta_init, theta_fn)
.phi_init (
Union
[str
,tuple
,ndarray
]) – Initializer forphi
(\(\boldsymbol{\phi}\) or \(\phi_{n\ell}\)): astr
,ndarray
, or tuple of the form(phi_init, phi_fn)
.gamma_init (
Union
[str
,tuple
,ndarray
]) – Initializer forgamma
(\(\boldsymbol{\gamma}\) or \(\gamma_{n}\)): astr
,ndarray
, or tuple of the form(gamma_init, gamma_fn)
.
-
training
: bool¶
-
class
neurophox.torch.layers.
ButterflyPerm
(units, frequency)[source]¶ Bases:
neurophox.torch.generic.PermutationLayer
Butterfly (FFT) permutation layer
The butterfly or FFT permutation for a frequency \(f\) corresponds to switching all inputs that are \(f\) inputs apart. This works most cleanly in a butterfly mesh architecture where the number of inputs, \(N\), and the frequencies, \(f\) are powers of two.
- Parameters
units (
int
) – Dimension of the input (number of input waveguide ports), \(N\)frequency (
int
) – Frequency of interacting mesh wires (waveguides)
-
training
: bool¶
-
class
neurophox.torch.layers.
PRMTorch
(units, tunable_layers_per_block=None, num_tunable_layers_list=None, sampling_frequencies=None, bs_error=0.0, hadamard=False, theta_init='haar_rect', phi_init='random_phi', gamma_init='random_gamma')[source]¶ Bases:
neurophox.torch.generic.MeshTorchLayer
Permuting rectangular mesh unitary layer
- Parameters
units (
int
) – The dimension of the unitary matrix (\(N\)) to be modeled by this transformertunable_layers_per_block (
Optional
[int
]) – The number of tunable layers per block (overridesnum_tunable_layers_list
,sampling_frequencies
)num_tunable_layers_list (
Optional
[List
[int
]]) – Number of tunable layers in each block in order from left to rightsampling_frequencies (
Optional
[List
[int
]]) – Frequencies of sampling frequencies between the tunable layersbs_error (
float
) – Photonic error in the beamsplittertheta_init (
Union
[str
,tuple
,ndarray
]) – Initializer fortheta
(\(\boldsymbol{\theta}\) or \(\theta_{n\ell}\)) astr
,ndarray
, or tuple of the form(theta_init, theta_fn)
.phi_init (
Union
[str
,tuple
,ndarray
]) – Initializer forphi
(\(\boldsymbol{\phi}\) or \(\phi_{n\ell}\)): astr
,ndarray
, or tuple of the form(phi_init, phi_fn)
.gamma_init (
Union
[str
,tuple
,ndarray
]) – Initializer forgamma
(\(\boldsymbol{\gamma}\) or \(\gamma_{n}\)): astr
,ndarray
, or tuple of the form(gamma_init, gamma_fn)
.
-
training
: bool¶
-
class
neurophox.torch.layers.
RMTorch
(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.torch.generic.MeshTorchLayer
Rectangular mesh network layer for unitary operators implemented in tensorflow
- Parameters
units (
int
) – The dimension of the unitary matrix (\(N\))num_layers (
Optional
[int
]) – The number of layers (\(L\)) of the meshhadamard (
bool
) – Hadamard convention for the beamsplittersbasis (
str
) – Phase basis to usebs_error (
float
) – Beamsplitter split ratio errortheta_init (
Union
[str
,tuple
,ndarray
]) – Initializer fortheta
(\(\boldsymbol{\theta}\) or \(\theta_{n\ell}\)) astr
,ndarray
, or tuple of the form(theta_init, theta_fn)
.phi_init (
Union
[str
,tuple
,ndarray
]) – Initializer forphi
(\(\boldsymbol{\phi}\) or \(\phi_{n\ell}\)): astr
,ndarray
, or tuple of the form(phi_init, phi_fn)
.gamma_init (
Union
[str
,tuple
,ndarray
]) – Initializer forgamma
(\(\boldsymbol{\gamma}\) or \(\gamma_{n}\)): astr
,ndarray
, or tuple of the form(gamma_init, gamma_fn)
.
-
training
: bool¶
-
class
neurophox.torch.layers.
RectangularPerm
(units, frequency)[source]¶ Bases:
neurophox.torch.generic.PermutationLayer
Rectangular permutation layer
The rectangular permutation layer for a frequency \(f\) corresponds effectively is equivalent to adding \(f\) layers of cross state MZIs in a grid configuration to the existing mesh.
- Parameters
units (
int
) – Dimension of the input (number of input waveguide ports), \(N\)frequency (
int
) – Frequency of interacting mesh wires (waveguides)
-
training
: bool¶
-
class
neurophox.torch.layers.
TMTorch
(units, hadamard=False, basis='bloch', bs_error=0.0, theta_init='haar_rect', phi_init='random_phi', gamma_init='random_gamma')[source]¶ Bases:
neurophox.torch.generic.MeshTorchLayer
Triangular mesh network layer for unitary operators implemented in tensorflow
- Parameters
units (
int
) – The dimension of the unitary matrix (\(N\))hadamard (
bool
) – Hadamard convention for the beamsplittersbasis (
str
) – Phase basis to usebs_error (
float
) – Beamsplitter split ratio errortheta_init (
Union
[str
,tuple
,ndarray
]) – Initializer fortheta
(\(\boldsymbol{\theta}\) or \(\theta_{n\ell}\)) astr
,ndarray
, or tuple of the form(theta_init, theta_fn)
.phi_init (
Union
[str
,tuple
,ndarray
]) – Initializer forphi
(\(\boldsymbol{\phi}\) or \(\phi_{n\ell}\)): astr
,ndarray
, or tuple of the form(phi_init, phi_fn)
.gamma_init (
Union
[str
,tuple
,ndarray
]) – Initializer forgamma
(\(\boldsymbol{\gamma}\) or \(\gamma_{n}\)): astr
,ndarray
, or tuple of the form(gamma_init, gamma_fn)
.
-
training
: bool¶