Quantum Neural Networks

Quantum Autoencoder

class QuantumAutoEncoder(n_qubits=4, n_aux_qubits=1, n_latent_qubits=1, device='default.qubit')[source]

Bases: torch.nn.modules.module.Module, qlearnkit.nn.qml_mixin.QmlMixin

Hybrid Quantum AutoEncoder, miming pytorch’s API and exploiting TorchLayer.

References:

[1] Romero et al.,

Quantum autoencoders for efficient compression of quantum data

Parameters
  • n_qubits – int, the actual number of qubits used in the training. It’s the n of the paper formalism.

  • n_aux_qubits – int, the number of auxiliary qubits, used as ancillas in the swap test.

  • n_latent_qubits – int, the number of qubits that can be discarded during the encoding.

  • device – Can be a string representing the device name or a valid Device having n_qubits wires

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(x)[source]

Forward propagation pass of the Neural network.

Parameters
  • xTensor,

  • input to be forwarded through the autoencoder

Returns

Array containing model’s prediction.

training: bool

QML Mixin

class QmlMixin[source]

Bases: object

Mixin for models built on top of Pennylane (QML)

property device: pennylane._device.Device
Return type

Device

property n_qubits: int
Return type

int

Quantum LSTM

class QLongShortTermMemory(input_size, hidden_size, n_layers=1, n_qubits=4, batch_first=True, device='default.qubit')[source]

Bases: torch.nn.modules.module.Module, qlearnkit.nn.qml_mixin.QmlMixin

Hybrid Quantum Long Short Term Memory, miming pytorch’s API and exploiting TorchLayer.

Applies a multi-layer long short-term memory (LSTM) RNN to an input sequence.

For each element in the input sequence, each layer computes the following function:

\[\begin{split}\begin{array}{ll} \\ i_t = \sigma(W_{ii} x_t + b_{ii} + W_{hi} h_{t-1} + b_{hi}) \\ f_t = \sigma(W_{if} x_t + b_{if} + W_{hf} h_{t-1} + b_{hf}) \\ g_t = \tanh(W_{ig} x_t + b_{ig} + W_{hg} h_{t-1} + b_{hg}) \\ o_t = \sigma(W_{io} x_t + b_{io} + W_{ho} h_{t-1} + b_{ho}) \\ c_t = f_t \odot c_{t-1} + i_t \odot g_t \\ h_t = o_t \odot \tanh(c_t) \\ \end{array}\end{split}\]

where \(h_t\) is the hidden state at time t, \(c_t\) is the cell state at time t, \(x_t\) is the input at time t, \(h_{t-1}\) is the hidden state of the layer at time t-1 or the initial hidden state at time 0, and \(i_t\), \(f_t\), \(g_t\), \(o_t\) are the input, forget, cell, and output gates, respectively. \(\sigma\) is the sigmoid function, and \(\odot\) is the Hadamard product.

References:

[1] Chen et al.,

Quantum Long Short-Term Memory

Parameters
  • input_size – the number of expected features in the input x

  • hidden_size – the number of features in the hidden state h

  • n_layers – the number of recurrent layers

  • n_qubits – the number of qubits

  • batch_first – if True, then the input and output tensors are provided as (batch, seq, feature) instead of (seq, batch, length)

  • device – Can be a string representing the device name or a valid Device having n_qubits wires

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(input, hx=None)[source]

Forward propagation pass of the Recurrent Neural network using a LSTM cell

Parameters
  • input – the input at time \(t\)

  • hx – Tensor containing the hidden state \(h(t)\) and the cell state \(c(t)\) at time \(t\)

Returns

the hidden sequence at time \(t+1\) and the new couple \(h(t+1)\), \(c(t+1)\).

training: bool

Quanvolutional Layer

class Quanv2DLayer(n_qubits=4, kernel_size=(2, 2), stride=2, out_channels=4, n_layers=1, device='default.qubit')[source]

Bases: torch.nn.modules.module.Module, qlearnkit.nn.qml_mixin.QmlMixin

Quantum Convolution Layer for Pytorch Hybrid Architectures

References:

[1] Henderson et al.,

Quanvolutional Neural Networks: Powering Image Recognition with Quantum Circuits

Parameters
  • n_qubits – int, The number of qubits of the quantum node. Default: 4.

  • kernel_size – Size of the convolutional kernel. Default: (2,2).

  • stride – Stride of the convolution. Default: 1

  • out_channels – Number of channels produced by the quanvolution. Default: 4.

  • n_layers – int, the number of layers of the variational quantum circuit. Default: 1.

  • device – Can be a string representing the device name or a valid Device having n_qubits wires. Default ‘default.qubit’

Initializes internal Module state, shared by both nn.Module and ScriptModule.

quanvolve(inputs, in_channels=1)[source]

Applies a single channel 2D quantum convolution (quanvolution)

Parameters
  • inputs – one channel input batch

  • in_channels – number of channel in the original input image

Returns:

training: bool
forward(inputs)[source]

Applies the quantum convolution operation for the forward pass

Parameters
  • inputsTensor,

  • input to be forwarded through the layer.

Returns

Array containing model’s prediction.