flatspin.encoder#

Input encoding

class flatspin.encoder.Encoder(**params)#

Encoder base class

An encoder translates logical input to an external field protocol.

Input takes the form of arrays of shape (n_inputs, input_dim). 1D input arrays may be used as a shorthand for (n_inputs, 1)

The encoding process consists of one or more steps, where the output of one step is input to the next step:

input -> step1 -> step2 -> … -> h_ext

In general, signals can take any shape as part of the encoding process. However, the last step must produce an output of either (time, 2): a global vector signal (time, H, W, 2): a local vector signal on a grid

Each step is a simple function with optional parameters, e.g.:

def step(input, param1=default1, param2=default2):

The Encoder will inspect the signature of each step to discover the available parameters. The parameters can then be set during encoder initialization, or afterwards via set_params. Note that parameter names may overlap, in which case all matching parameters will be set to the same value.

steps = None#
encode(input)#

Encode input as an external field

Parameters:

input (array_like) – Input shape should be (n_inputs, input_dim). 1D input arrays may be used as a shorthand for (n_inputs, 1).

Returns:

h_ext – The returned array is either: 1. A global time-dependent field: h_ext.shape = (time, 2) 2. A spatial time-dependent field: h_ext.shape = (time, height, width, 2)

Return type:

ndarray

classmethod get_default_params()#
get_params()#

Get available encoder parameters

set_param(name, value)#

Set encoder parameter name=value

set_params(**params)#

Set encoder parameters

flatspin.encoder.expand_dims(input)#

Ensure last dimension of input is single-dimensional

flatspin.encoder.check_input(input)#
flatspin.encoder.scale(input, H0=0, H=1)#

Scale input to range [H0, H]

flatspin.encoder.multiply(input, H=1)#

Multiply input by H

flatspin.encoder.angle(input, phi0=0, phi=360)#

Map input to angle in range [phi0, phi] -> vector components

flatspin.encoder.angle_grid(input, phi0=0, phi=360, grid=None)#

Convenience function to offset all angles based on grid

flatspin.encoder.sw(input, rotation=0)#

Multiply input vectors by SW astroid

flatspin.encoder.broadcast_waveform(input, waveform)#

Broadcast waveform over input

flatspin.encoder.repeat(input, timesteps=1)#

Repeat input a number of timesteps

flatspin.encoder.sin(input, timesteps=100, phase=0)#

Multiply input by sine wave

flatspin.encoder.triangle(input, timesteps=100, phase=90)#

Multiply input by triangle wave

flatspin.encoder.rotate(input, timesteps=100, phase=0)#

Multiply input by rotation vector

flatspin.encoder.ppm(input, pos0=0, pos=1.0, timesteps=100)#

Pulse position modulation

flatspin.encoder.fixed_vector(input, phi=0)#

Convert scalar input to vectors at some angle phi

flatspin.encoder.rotate_vector(input, phi=0)#

Rotate vector input by some angle phi

flatspin.encoder.ensure2d(input, fill=1.0)#

Convert scalar input to vectors

flatspin.encoder.grid(input, grid_size=(3, 3), grid=None)#

Encode input onto grid

Parameters:
  • grid_size (tuple) – Create grid of fixed size (uniform weights) Ignored if grid is set

  • grid (array) – Create grid of given size (non-uniform weights)

flatspin.encoder.onehot(input, nbits=2)#
class flatspin.encoder.Constant(**params)#

Encode input as the magnitude of a global field.

Each field value is repeated timesteps times. The magnitude is scaled between H0 and H. The field has a fixed angle phi.

steps = (<function repeat>, <function scale>, <function fixed_vector>)#
class flatspin.encoder.Direct(**params)#

Encode input directly as a global field.

Note: input should already be in vector form.

The input vectors are multiplied by H.

steps = (<function multiply>,)#
class flatspin.encoder.Angle(**params)#

Encode input as the angle of a global field.

The angle is scaled between phi0 and phi. The field has a fixed magnitude H.

steps = (<function angle>, <function multiply>)#
class flatspin.encoder.Sine(**params)#

Encode input as the amplitude of a sinusoidal global field.

The amplitude is scaled between H0 and H. The field has a fixed angle of phi degrees. The sine wave has a resolution timesteps samples. The sine wave has a phase of phase degrees.

steps = (<function scale>, <function sin>, <function fixed_vector>)#
class flatspin.encoder.Triangle(**params)#

Encode input as the amplitude of a triangle wave global field.

The amplitude is scaled between H0 and H. The field has a fixed angle of phi degrees. The triangle wave has a resolution timesteps samples. The triangle wave has a phase of phase degrees.

steps = (<function scale>, <function triangle>, <function fixed_vector>)#
class flatspin.encoder.Rotate(**params)#

Encode input as the amplitude of a rotating global field.

The amplitude is scaled between H0 and H. The rotating field has a resolution timesteps samples. The rotating field has a phase of phase degrees.

steps = (<function scale>, <function rotate>)#
class flatspin.encoder.AngleSine(**params)#

Encode input as the angle of a sinusoidal global field.

The angle is scaled between phi0 and phi. The field has a fixed amplitude H. The sine wave has a resolution timesteps samples. The sine wave has a phase of phase degrees.

steps = (<function angle>, <function expand_dims>, <function sin>, <function multiply>)#
class flatspin.encoder.AngleTriangle(**params)#

Encode input as the angle of a triangle wave global field.

The angle is scaled between phi0 and phi. The field has a fixed amplitude H. The triangle wave has a resolution timesteps samples. The triangle wave has a phase of phase degrees.

steps = (<function angle>, <function expand_dims>, <function triangle>, <function multiply>)#
class flatspin.encoder.RotatePulse(**params)#

Encode input as pulses along a rotating global field.

Each input value is encoded as a pulse in a pulse train of timesteps samples. The position of the pulse will be between pos0 and pos, where 0 denotes the beginning of the pulse train and 1 denotes the end. The rotating field with have a base magnitude of H0 and H when there is a pulse.

steps = (<function ppm>, <function scale>, <function rotate>)#
class flatspin.encoder.Triangle2D(**params)#

TODO: Please document me

steps = (<function scale>, <function ensure2d>, <function triangle>, <function rotate_vector>)#
class flatspin.encoder.OneHot(**params)#

TODO: Please document me

steps = (<function onehot>, <function scale>, <function triangle>, <function fixed_vector>)#
class flatspin.encoder.ConstantGrid(**params)#

Encode input as the magnitude of local fields on a grid.

Each field value is repeated timesteps times.

The grid is defined by grid, a 2D array whose values define the weights of the input for each grid cell. For example:

grid = [[0, .5, 0],

[.5, 1, .5], [0, .5, 0]]

results in a 3x3 grid where the input is applied: * unmodified in the center cell (multiplied by 1) * halved in the edge cells (multiplied by 0.5) * discarded for the corner cells (multiplied by 0)

After the input has been scaled by the grid weights, it is scaled to the range H0 and H. Hence the magnitude of the field in a given cell is given by:

h_ext[i,j] = H0 + grid[i,j] * input * (H - H0)

For the above example grid, the field: * in the center cell will have magnitude H0 + 1 * input * (H - H0) * in the edge cells will have magnitude H0 + 0.5 * input * (H - H0) * in the corners will have a constant magnitude H0 (regardless of the input)

Finally, the fields will have a fixed angle phi.

steps = (<function repeat>, <function grid>, <function scale>, <function fixed_vector>)#
class flatspin.encoder.SineGrid(**params)#

Encode input as the amplitude of sinusoidal local fields on a grid.

The parameters grid, H0, H and phi are the same as for ConstantGrid. The sine wave has a resolution timesteps samples and a phase of phase degrees.

steps = (<function grid>, <function scale>, <function expand_dims>, <function sin>, <function fixed_vector>)#
class flatspin.encoder.TriangleGrid(**params)#

Encode input as the amplitude of a triangle wave local fields on a grid.

The parameters grid, H0, H and phi are the same as for ConstantGrid. The triangle wave has a resolution timesteps samples and a phase of phase degrees.

steps = (<function grid>, <function scale>, <function expand_dims>, <function triangle>, <function fixed_vector>)#
class flatspin.encoder.AngleSineGrid(**params)#

Encode input as the angle of sinusoidal local fields on a grid.

The parameters grid and H are the same as for ConstantGrid. The grid weights scale the amplitude of the sine wave in each cell (angle is unaffected).

The angle is scaled between phi0 and phi. The sine wave has a resolution timesteps samples and a phase of phase degrees.

steps = (<function angle>, <function expand_dims>, <function sin>, <function multiply>, <function grid>)#
class flatspin.encoder.AngleGridSine(**params)#

Encode input as the angle of sinusoidal local fields on a grid.

The parameters grid and H are the same as for ConstantGrid. The grid weights are used to offset the angle of each cell. The angle is scaled between phi0 and phi. The field has a fixed amplitude H. The sine wave has a resolution timesteps samples. The sine wave has a phase of phase degrees.

steps = (<function angle_grid>, <function expand_dims>, <function sin>, <function multiply>)#
class flatspin.encoder.RotatePulseGrid(**params)#

Encode input as pulses along a rotating field

steps = (<function ppm>, <function grid>, <function scale>, <function rotate>)#