neurophox package

neurophox.helpers module

neurophox.helpers.butterfly_layer_permutation(units, frequency)[source]
neurophox.helpers.butterfly_permutation(num_layers)[source]
neurophox.helpers.fix_phase_tf(fixed, mask)[source]
neurophox.helpers.fix_phase_torch(fixed, mask, device=device(type='cpu'), dtype=torch.complex64)[source]
neurophox.helpers.get_alpha_checkerboard(units, num_layers, include_off_mesh=False, flipud=False)[source]
neurophox.helpers.get_alpha_checkerboard_general(units, num_layers)[source]
neurophox.helpers.get_default_coarse_grain_block_sizes(units, use_cg_sequence=True)[source]
neurophox.helpers.get_efficient_coarse_grain_block_sizes(units, tunable_layers_per_block=2, use_cg_sequence=True)[source]
neurophox.helpers.get_haar_diagonal_sequence(diagonal_length, parity_odd=False)[source]
neurophox.helpers.glue_permutations(perm_idx_1, perm_idx_2)[source]
neurophox.helpers.grid_permutation(units, num_layers)[source]
neurophox.helpers.grid_viz_permutation(units, num_layers, flip=False)[source]
neurophox.helpers.inverse_permutation(permuted_indices)[source]
neurophox.helpers.neurophox_matplotlib_setup(plt)[source]
neurophox.helpers.ordered_viz_permutation(units, num_layers)[source]
neurophox.helpers.pairwise_off_diag_permutation(units)[source]
neurophox.helpers.plot_complex_matrix(plt, matrix)[source]
neurophox.helpers.prm_permutation(units, tunable_block_sizes, sampling_frequencies, butterfly=False)[source]
neurophox.helpers.random_gaussian_batch(batch_size, units, covariance_matrix=None, seed=None)[source]
Return type

ndarray

neurophox.helpers.rectangular_permutation(units, frequency)[source]
neurophox.helpers.to_absolute_theta(theta)[source]
Return type

ndarray

neurophox.helpers.to_stripe_array(nparray, units)[source]

Convert a numpy array of phase shifts of size (num_layers, units) or (batch_size, num_layers, units) into striped array for use in general feedforward mesh architectures.

Parameters
  • nparray (ndarray) – phase shift values for all columns

  • units (int) – dimension the stripe array acts on (depends on parity)

Returns

A general mesh stripe array arrangement that is of size (units, num_layers) or (batch_size, units, num_layers)

neurophox.helpers.tri_phase_tf(phase_range)[source]
neurophox.helpers.tri_phase_torch(phase_range)[source]

neurophox.initializers module

class neurophox.initializers.ConstantPhaseInitializer(units, num_layers, constant_phase)[source]

Bases: neurophox.initializers.MeshPhaseInitializer

to_np()[source]

Returns: Initialized Numpy array

Return type

ndarray

class neurophox.initializers.HaarRandomPhaseInitializer(units, num_layers=None, hadamard=False, tri=False)[source]

Bases: neurophox.initializers.MeshPhaseInitializer

Haar-random initialization of rectangular and triangular mesh architectures.

Parameters
  • units (int) – Input dimension, \(N\)

  • num_layers (Optional[int]) – Number of layers, \(L\)

  • hadamard (bool) – Whether to use Hadamard convention

  • tri (bool) – Initializer for the triangular mesh architecture

to_np()[source]

Returns: Initialized Numpy array

Return type

ndarray

class neurophox.initializers.MeshPhaseInitializer(units, num_layers)[source]

Bases: object

to_np()[source]
Return type

ndarray

Returns

Initialized Numpy array

to_tf(phase_varname)[source]
Return type

Variable

Returns

Initialized Tensorflow Variable

to_torch(is_trainable=True)[source]
Returns

Initialized torch Parameter

class neurophox.initializers.PRMPhaseInitializer(units, hadamard, tunable_layers_per_block=None)[source]

Bases: neurophox.initializers.MeshPhaseInitializer

to_np()[source]

Returns: Initialized Numpy array

Return type

ndarray

class neurophox.initializers.PhaseInitializer(phase, units)[source]

Bases: neurophox.initializers.MeshPhaseInitializer

User-specified initialization of rectangular and triangular mesh architectures.

Parameters
  • phase (ndarray) – Phase to initialize

  • units (int) – Input dimension, \(N\)

to_np()[source]

Returns: Initialized Numpy array

Return type

ndarray

class neurophox.initializers.UniformRandomPhaseInitializer(units, num_layers, max_phase, min_phase=0)[source]

Bases: neurophox.initializers.MeshPhaseInitializer

to_np()[source]

Returns: Initialized Numpy array

Return type

ndarray

neurophox.initializers.get_haar_theta(units, num_layers, hadamard, tri=False)[source]
Return type

Union[Tuple[ndarray, ndarray], Tuple[Variable, Variable], Variable]

neurophox.initializers.get_initializer(units, num_layers, initializer_name, hadamard=False, testing=False)[source]
Return type

MeshPhaseInitializer

neurophox.initializers.get_ortho_haar_theta(units, num_layers, hadamard)[source]
Return type

Union[Tuple[ndarray, ndarray], Tuple[Variable, Variable], Variable]

neurophox.meshmodel module

class neurophox.meshmodel.ButterflyMeshModel(num_layers, hadamard=False, bs_error=0.0, basis='bloch', theta_init='random_theta', phi_init='random_phi', gamma_init='random_gamma')[source]

Bases: neurophox.meshmodel.MeshModel

Butterfly mesh

The butterfly mesh contains \(L\) layers and \(N = 2^L\) inputs/outputs to implement \(U \in \mathrm{U}(N)\). Unlike the triangular and full (\(L = N\)) rectangular mesh, the butterfly mesh is not universal. However, it has attractive properties for efficient machine learning and compact photonic implementations of unitary mesh models.

Parameters
  • num_layers (int) – Number of layers, \(L\)

  • hadamard (bool) – Hadamard convention

  • bs_error (float) – Beamsplitter layer

  • theta_init (Union[str, tuple, ndarray]) – Initializer for theta (\(\boldsymbol{\theta}\) or \(\theta_{n\ell}\)), see MeshModel.

  • phi_init (Union[str, tuple, ndarray]) – Initializer for phi (\(\boldsymbol{\phi}\) or \(\phi_{n\ell}\)), see MeshModel.

  • gamma_init (Union[str, tuple, ndarray]) – Initializer for gamma (\(\boldsymbol{\gamma}\) or \(\gamma_{n}\)), see MeshModel.

class neurophox.meshmodel.MeshModel(perm_idx, hadamard=False, num_tunable=None, bs_error=0.0, testing=False, use_different_errors=False, theta_init='random_theta', phi_init='random_phi', gamma_init='random_gamma', basis='bloch')[source]

Bases: object

Any feedforward mesh model of \(N\) inputs/outputs and \(L\) layers.

Parameters
  • perm_idx (ndarray) – A numpy array of \(N \times L\) permutation indices for all layers of the mesh

  • hadamard (bool) – Whether to use Hadamard convention

  • num_tunable (Optional[ndarray]) – A numpy array of \(L\) integers, where for layer \(\ell\), \(M_\ell \leq \lfloor N / 2\rfloor\), used to defined the phase shift mask.

  • bs_error (float) – Beamsplitter error (ignore for pure machine learning applications)

  • testing (bool) – Use a seed for randomizing error (ignore for pure machine learning applications)

  • use_different_errors (bool) – Use different errors for the left and right beamsplitter errors

  • theta_init (Union[str, tuple, ndarray]) – Initializer for theta (\(\boldsymbol{\theta}\) or \(\theta_{n\ell}\)) a str, ndarray, or tuple of the form (theta_init, theta_fn).

  • phi_init (Union[str, tuple, ndarray]) – Initializer for phi (\(\boldsymbol{\phi}\) or \(\phi_{n\ell}\)): a str, ndarray, or tuple of the form (phi_init, phi_fn).

  • gamma_init (Union[str, ndarray]) – Initializer for gamma (\(\boldsymbol{\gamma}\) or \(\gamma_{n}\)): a str, ndarray, or tuple of the form (gamma_init, gamma_fn).

  • basis (str) – Phase basis to use for controlling each pairwise unitary (simulated interferometer) in the mesh

property init

Returns: Initializers for \(\boldsymbol{\theta}, \boldsymbol{\phi}, \gamma_n\).

Return type

Tuple[MeshPhaseInitializer, MeshPhaseInitializer, MeshPhaseInitializer]

property mzi_error_matrices

Returns: Error numpy arrays for Numpy MeshNumpyLayer

Return type

Tuple[ndarray, ndarray]

property mzi_error_tensors
class neurophox.meshmodel.PermutingRectangularMeshModel(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', gamma_init='random_gamma')[source]

Bases: neurophox.meshmodel.MeshModel

Permuting rectangular mesh model

Parameters
  • units (int) – Input dimension, \(N\)

  • tunable_layers_per_block (Optional[int]) – The number of tunable layers per block (overrides num_tunable_layers_list, sampling_frequencies)

  • num_tunable_layers_list (Optional[List[int]]) – Number of tunable layers in each block in order from left to right

  • sampling_frequencies (Optional[List[int]]) – Frequencies of sampling frequencies between the tunable layers

  • bs_error (float) – Photonic error in the beamsplitter

  • hadamard (bool) – Whether to use hadamard convention (otherwise use beamsplitter convention)

  • theta_init (Union[str, tuple, ndarray]) – Initializer for theta (\(\boldsymbol{\theta}\) or \(\theta_{n\ell}\)), see MeshModel.

  • phi_init (Union[str, tuple, ndarray]) – Initializer for phi (\(\boldsymbol{\phi}\) or \(\phi_{n\ell}\)), see MeshModel.

  • gamma_init (Union[str, tuple, ndarray]) – Initializer for gamma (\(\boldsymbol{\gamma}\) or \(\gamma_{n}\)), see MeshModel.

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

Bases: neurophox.meshmodel.MeshModel

Rectangular mesh

The rectangular mesh contains \(N\) inputs/outputs and \(L\) layers in rectangular grid arrangement of pairwise unitary operators to implement \(U \in \mathrm{U}(N)\).

Parameters
  • units (int) – Input dimension, \(N\)

  • num_layers (Optional[int]) – Number of layers, \(L\)

  • hadamard (bool) – Hadamard convention

  • bs_error (float) – Beamsplitter layer

  • basis (str) – Phase basis to use for controlling each pairwise unitary (simulated interferometer) in the mesh

  • theta_init (Union[str, tuple, ndarray]) – Initializer for theta (\(\boldsymbol{\theta}\) or \(\theta_{n\ell}\)), see MeshModel.

  • phi_init (Union[str, tuple, ndarray]) – Initializer for phi (\(\boldsymbol{\phi}\) or \(\phi_{n\ell}\)), see MeshModel.

  • gamma_init (Union[str, tuple, ndarray]) – Initializer for gamma (\(\boldsymbol{\gamma}\) or \(\gamma_{n}\)), see MeshModel.

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

Bases: neurophox.meshmodel.MeshModel

Triangular mesh

The triangular mesh contains \(N\) inputs/outputs and \(L = 2N - 3\) layers in triangular grid arrangement of pairwise unitary operators to implement any \(U \in \mathrm{U}(N)\).

Parameters
  • units (int) – Input dimension, \(N\)

  • hadamard (bool) – Hadamard convention

  • bs_error (float) – Beamsplitter layer

  • basis (str) – Phase basis to use for controlling each pairwise unitary (simulated interferometer) in the mesh

  • theta_init (Union[str, tuple, ndarray]) – Initializer for theta (\(\boldsymbol{\theta}\) or \(\theta_{n\ell}\)), see MeshModel.

  • phi_init (Union[str, tuple, ndarray]) – Initializer for phi (\(\boldsymbol{\phi}\) or \(\phi_{n\ell}\)), see MeshModel.

  • gamma_init (Union[str, tuple, ndarray]) – Initializer for gamma (\(\boldsymbol{\gamma}\) or \(\gamma_{n}\)), see MeshModel.