Skip to main content

View on GitHub

Open this notebook in GitHub to run it yourself

Classical Neural Networks

Neural networks is one of the major branches in machine learning, with wide use in applications and research. A neural network - or, more generally, a deep neural network - is a parametric function of a specific structure (inspired by neural networks in biology), which is trained to capture specific functionality. In its most basic form, a neural network for learning a function f:RNRM\vec{f}: \mathbb{R}^N\rightarrow \mathbb{R}^M looks as follows:
  1. There is an input vector of size NN (red circles in Fig. 1).
  2. Each entry of the input goes into a hidden layer of size KK, where each neuron (blue circles in Fig. 1) is defined with an “activation function” yk(w(1);x)y^{k}(\vec{w}^{(1)}; \vec{x}) for k=1,,Kk=1,\dots,K, and w(1)\vec{w}^{(1)} are parameters.
  3. The output of the hidden layer is sent to the output layer (green circles in Fig. 1) f~m(w(2);y)\tilde{f}^{m}(\vec{w}^{(2)};\vec{y}) for m=1,,Mm=1,\dots,M, and w(2)\vec{w}^{(2)} are parameters.
The output f~\vec{\tilde{f}} is thus a parametric function (in w(1),w(2)\vec{w}^{(1)},\,\vec{w}^{(2)}), which can be trained to capture the target function f\vec{f}. png
Deep neural networks are similar to the description above, having more than one hidden layer. This provides a more complex structure that can capture more complex functionalities.

Quantum Neural Networks

The idea of a quantum neural network refers to combining parametric circuits as a replacement for all or some of the classical layers in classical neural networks. The basic object in QNN is thus a quantum layer, which has a classical input and returns a classical output. The output is obtained by running a quantum program. A quantum layer is thus composed of three parts:
  1. A quantum part that encodes the input: This is a parametric quantum function for representing the entries of a single data point.
There are three canonical ways to encode a data vector of size NN: angle-encoding using NN qubits, dense angle-encoding using N/2\lceil N/2\rceil qubits, and amplitude-encoding using log2N\lceil\log_2N\rceil qubits.
  1. A quantum ansatz part: This is a parametric quantum function, whose parameters are trained as the weights in classical layers.
  2. A postprocess classical part, for returning an output classical vector.
The integration of quantum layers in classical neural networks may offer reduction in resources for a given functionality, as the network (or part of it) is expressed via the Hilbert space, providing different expressibility compared to classical networks. This notebook demonstrates QNN by treating a specific function - the subset majority - for which we construct, train, and verify a hybrid classical-quantum neural network. The notebook assumes familiarity with Classiq and NN with PyTorch. See the QML guide with Classiq.

Example: Hybrid Neural Network for the Subset Majority Function

For an integer NN and a given subset of indices S{0,1,,N}S \subset \{0,1,\dots,N\} we define the subset majority function, MS:{0,1}×N{0,1}M_{S}:\{0,1\}^{\times N}\rightarrow \{0,1\} that acts on binary strings of size NN as follows: it returns 1 if the number of ones within the substring according to SS is larger than S//2|S|//2, and 0 otherwise, MS(b)={1if jSbj>S//2,0otherwiseM_S(\vec{b}) = \left\{ \begin{array}{l l } 1 & \text{if } \sum_{j\in S} b_{j}>|S|//2, \\ 0 & \text{otherwise} \end{array} \right . For example, we consider N=7N=7 and S={0,1,4}S=\{0,1,4\}:
  • The string 0101110 corresponds to the substring 011, for which the number of ones is 2(>1). Therefore, MS(0101110)=1M_S(0101110)=1.
  • The string 0011111 corresponds to the substring 001, for which the number of ones is 1(=1). Therefore, MS(0101110)=0M_S(0101110)=0.

Generating Data for a Specific Example

Let us consider a specific example for our demonstration. We choose N=10N=10 and generate all possible data of 2N2^N bit strings. We also take a specific subset S={1,3,4,6,7,9}S=\{1, 3, 4, 6, 7, 9\}.
We choose data for training and data for verification, and define the batch size for the corresponding data loaders:

Constructing a Hybrid Network

We build the following hybrid neural network: Data flattening \rightarrow A classical linear layer of size 10 to 4 with Tanh activation \rightarrow A qlayer of size 4 to 2 \rightarrow a classical linear layer of size 2 to 1 with ReLU activation. The classical layers can be defined with PyTorch built-in functions. The quantum layer is constructed with (1) a dense angle-encoding function (2) a simple ansatz with RY and RZZ rotations (3) a postprocess that is based on a measurement per qubit

The Quantum Layer

Output:

The Full Hybrid Network

Now, we can define the full network.

Training and Verifying the Networks

We define some hyperparameters such as loss function and optimization method, and a training function:
Next, we define a train function:
We also define a validation function, check_accuracy, which tests a trained network on new data:

Training and Verifying the Network

For convenience, we load a pre-trained model and set the epoch size to
  1. Training a network takes around 30 epochs.
Output:
Output: