Models

Models package is central to SQUID. It contains MainModel around which whole package is built, as well as defines classes that can be it’s building blocks.

As mentioned in SQUID package MainModel consists of three parts: Encoder, Middle Model and Decoder. Encoder and Decoder are both Classical Models, or some custom subclass of ClassicalModel. Middle Model on the other hand has to be differentiable. This means that any function, quantum or classical that has a defined derivative can be used here. Currently two types of such functions: classical, mentioned earlier, and QuantumModel, with corresponding implementations in Quantum Models.

All of the models are shown on this page, and in summary below. Additionally, Abstract Models are provided in bottom, for further development. Lastly, utilities (both quantum and classical) are documented at the end of this page.

MainModel(model1, model2, …)

Wrapper for a sequential model, where data flows: model1 -> model2 -> model3. It allows model2 to be basically anything (under some constraints: given data outputs data, and calculates gradient with respect to input.)

ConvFeedForward(in_shape, conv_layers, …)

ConvFeedForward is a general convolutional and feed forward network.

FeedForward(layers, str]], in_features, …)

FeedForward is a general feed forward network wrapper.

SelectSingleOutput(qubit_idx, int] = 0, …)

SelectSingleOutput simulates standard technique of looking at a Single Qubit in determining the value of the function.

VQC(num_quantum_params, num_qubits, *args, …)

Main Model

class squid.models.MainModel(model1: squid.models.abstract_models.ClassicalModel, model2: Union[squid.models.abstract_models.ClassicalModel, squid.models.abstract_models.QuantumModel], model3: squid.models.abstract_models.ClassicalModel, config: Optional[Dict[str, Any]] = None)[source]

Wrapper for a sequential model, where data flows: model1 -> model2 -> model3. It allows model2 to be basically anything (under some constraints: given data outputs data, and calculates gradient with respect to input.)

Parameters
  • model1 (ClassicalModel) – A torch nn.Module. Network which outputs shape compatible with input to model 2.

  • model2 (Union[ClassicalModel, QuantumModel]) – Either a torch nn.Module, or any algorithm operating on numpy arrays.

  • model3 (ClassicalModel) – A torch nn.Module. Network which input shape is compatible with output to model 2.

  • config (Dict[str, Any], optional) – Configuration specifying various aspects of a model, for example whether certain model is trainable or not. Defaults to None.

Available Models

Classical Models

class squid.models.ConvFeedForward(in_shape: List[int], conv_layers: List[Union[Tuple, str]], layers: List[Union[int, str]], num_classes: Optional[int] = None, *args, **kwargs)[source]

ConvFeedForward is a general convolutional and feed forward network. Any (valid) sequence of nn.Conv2d, nn.Linear and non-linearities (from nn.functional) can be represented by it.

Parameters
  • in_shape (List[int]) – Shape of incoming data (one example, without batch dimension, channels first). This is needed to create the first layer of this model.

  • conv_layers (List[Union[Tuple, str]]) –

    A list of either:

    • Tuple - A tuple description of convolution,

      where first element specifies type of convolution (either C1-3, CT1-3, M1-3, for convolution, convolution transpose, maxpool respectively, through 1 to 3 dimensions). For example (“C2”, 6, 6, 3) is a Conv2d layer from 6 in channels to 6 out channels with kernel size of 3.

    • str - an activation function in a given layer.

    These are then converted to actual nn classes and functions, which PyTorch can run through and optimize.

  • layers (List[Union[int, str]]) –

    A list of either:

    • int - which specifies number of neurons in a given layer.

    • str - an activation function in a given layer.

    These are then converted to actual nn.Linear and nn functions, which PyTorch can run through and optimize.

  • num_classes (int, optional) – Number of classes to classify. This can be used to create last layer of this model. Note that it does this model to be the last one in the stack of models, if this model is part of larger one (like MainModel). If set to None (default), outgoing shape of this model is equal to layers[-1].

static _tuple_to_conv(prev_channels: int, tup: Tuple[Any, ])Tuple[torch.nn.modules.module.Module, int][source]

Converts tuple to actual nn.Conv, nn.ConvTranspose or nn.MaxPool layer.

Parameters
  • prev_channels (int) – Channels incoming to the layers

  • tup (Tuple[str, Any]) – Tuple defining layer (first element defining type of layer, remaining elements defining arguments to that layer).

Returns

Tuple, with first element being PyTorch layer, and second element being number of channels after this layer is applied.

Return type

Tuple[nn.Module, int]

class squid.models.FeedForward(layers: List[Union[int, str]], in_features: int, num_classes: Optional[int] = None, *args, **kwargs)[source]

FeedForward is a general feed forward network wrapper. Any sequence of nn.Linear and non-linearities (from nn.functional) can be represented by it.

Parameters
  • layers (List[Union[int, str]]) –

    A list of either:

    • int - which specifies number of neurons in a given layer.

    • str - an activation function in a given layer.

    These are then converted to actual nn.Linear and nn functions, which PyTorch can run through and optimize.

  • in_features (int) – Number of incoming features to this network. This is needed to create the first layer of this model.

  • num_classes (int, optional) – Number of classes to classify. This can be used to create last layer of this model. Note that it does this model to be the last one in the stack of models, if this model is part of larger one (like MainModel). If set to None (Default), outgoing shape of this model is equal to layers[-1].

class squid.models.SelectSingleOutput(qubit_idx: Union[List[int], int] = 0, use_linear_after: bool = True, num_classes: Optional[int] = None, *args, **kwargs)[source]

SelectSingleOutput simulates standard technique of looking at a Single Qubit in determining the value of the function. This class allows user to select either a single or multiple outputs from previous model. Then these outputs can be passed through a single Linear layer.

Linear Layer in the end can be particularly useful in case of choosing single qubit, which can have only positive values, since bias in a layer, can vary signs of these values. This in turn will SoftMax is depended on a sign of the output.

Parameters
  • qubit_idx (Union[List[int], int], optional) – Indices of qubit(-s) to be selected, defaults to the first qubit.

  • use_linear_after (bool, optional) – Whether to use linear layer after selecting the qubits or not, defaults to True If the use_linear_after is set to false, then the output will contain binary where first element is sum of chosen indexes, and the other is 1 - that sum (in case those are probabilities, this would correspond to sum of elements on other indexes).

  • num_classes (Optional[int], optional) – Ignored if use_linear_after is False. Number of classes to classify, defaults to number of chosen qubits.

Quantum Models

class squid.models.VQC(num_quantum_params: int, num_qubits: int, *args, **kwargs)[source]
Valid combinations of num_quantum_params and num_qubits

num_quantum_params

num_qubits

6

2

9

2

12

4

15

2

18

6

30

4

39

4

45

6

57

4

Parameters
  • num_quantum_params (int) – Number of quantum parameters/angles to use in the model.

  • num_qubits (int) – Number of qubits to use in the model.

clip(x: torch.Tensor, *args)torch.Tensor[source]

Scales inputs, using sigmoid, to \([0, 2\pi]\), which is required by VQC forward method.

Abstract Models

class squid.models.ClassicalModel[source]

Alias for PyTorch’s nn.Module.

class squid.models.QuantumModel[source]

One of three components of MainModel. It contains function that is trained on numpy arrays and backward prop has to be provided by hand.

clip(x: torch.Tensor, *args)torch.Tensor[source]

The only function in QuantumModel (QM) that operates on tensors. It should not be dependent on any trainable weights. Rather it can be thought of as a scaling on inputs to QM based on the actual encoding quantum model/device uses (such as amplitude, qsample, etc.).

Main Model will call it before QM’s forward function. It enables easy backprop through the model.

By default it’s a linear activation function.

Parameters

x (torch.Tensor) – Raw input to QuantumModel

Returns

Input to QuantumModel that forward function will accept.

Return type

torch.Tensor