Implement A Hybrid Quantum Classical Neural Network With Qiskit

Daniel Sierra-Sosa
Qiskit
Published in
8 min readMar 8, 2022

by Parmeet Singh Chani, Hojun Lee, Emilio Peláez, Anuranan Das and Daniel Sierra-Sosa

In this tutorial we focus on implementing the Hybrid Quantum Classical Neural Network proposed by Rongxin Xia and Sabre Kais using Qiskit. This neural network is composed of Parameterized Quantum Circuits (PQC), those with gates reliant on some input parameter. First, we will define a data encoding scheme to map the classical data into quantum states, then we will create the neural network architecture. Finally, we will use it to calculate the ground state energy of molecular hydrogen (H₂) at various interatomic distances. By the end of this blog, you will learn how to implement neural networks to calculate ground state energies, how to optimize the parameters of a PQC for a particular cost of Hamiltonian, and observe how using a neural network compares to using PQCs without mid-circuit measurement.

Editor’s note: while we attempted to simplify this blog’s language as much as possible, it still assumes a working knowledge of neural networks and quantum computing. To get started with both topics, refer to last year’s Qiskit Global Summer School, which focused on Quantum Machine Learning.

Introduction

Classical neural networks include two parts: artificial neurons built from linear components, and nonlinear activation functions tacked on after. Xia and Kais instead envision a hybrid quantum-classical neural network, where a PQC replaces the artificial neurons as the linear component, while the measurements — expectation values of the Pauli-Z operators on each qubit — serve as the classical piece and the nonlinear activation function which connects the quantum components. We first start by encoding the bond length of a molecule into our circuit, then add parametrized gates. After each layer, we perform the measurement and use these expectation values to initialize the next layer of our neural network.

This structure may look similar to other variational quantum algorithms, where the parameters are updated aiming to minimize the expectation value. However, neural networks are slightly different; they’re more flexible, and we do not need to optimize for each specific case. Once we have trained our system, we can use it to make predictions for other cases.

taken from: Hybrid Quantum Classical Neural Networks

In the above image, the blue part is the PQC, the top orange part represents the bond length encoding and the top yellow part is where we calculate the expectation values b_i of the Pauli z operator for each qubit:

After measurement, we repeat this step and feed the expectation values back into the circuit. Once we’ve finished this iteration, we move on to the bottom yellow step — we calculate the ground state energy, represented by the expectation value of the Hamiltonian.

We generate training and testing data from an array of values calculated by using a classical Eigensolver to minimize the ground state expectation values:

Quantum Circuit Implementation

We will start by loading the required Qiskit libraries, shown in the GitHub gist below. If you are new in Quantum Computing, check out the Qiskit Textbook. You can find more information and code your first circuit here.

Encoding

The first step is to encode our classical data into a quantum state to create the input. In our particular case we are interested in using variational encoding to encode the bond length into a quantum state, where we use an input to prepare quantum gates and their parameters. The idea is essentially to map classical data to a Hilbert space using a feature map, as you would in a classical neural network. You can find more information about data encoding in this lecture. Using variational encoding, any data set can be represented as:

where:

Ry is a rotation around the y axis by an angle inputted as a parameter and H is the Hadamard gate.

Then the encoding can be mathematically written as

Or, more simply, to each ground-state qubit, we apply a Hadamard gate and a parametrized rotation around the y axis, where each data point on the bond length is converted into the parameter for Ry:

Parameterized Quantum Circuit

The PQC is the core of the neural network — the blue part of the first image — created using Ry and CNOT gates, as in the gist below. You can see the parameters encoded into the circuit as parameters which will be optimized using a classical optimizer. As a reminder, routines involving parametrized quantum circuits begin with an ansatz, or an educated first guess, which we then optimize through repeated iterations of the circuit.

These parameters are used to produce the variational state |ψ(θ)>, which we’ll use alongside the molecule’s Hamiltonian to calculate the expectation value and obtain the molecule’s energy. We’ll figure out these parameters later by training our circuit on exact values we find through usual quantum chemistry methods.

Neural Network Circuit

So, next we need to calculate the expectation values. This can be achieved by calculating the probability of obtaining each of the basis states. Our example is a two-layer neural network — this depth is more than enough to obtain approximate values of the molecule’s ground state. For creating the neural network circuit we will combine the two functions we created above, encode and apply_ansatz, then we calculate the expectation value of the Pauli Z operator for each qubit after each layer. After each layer we use the qc.reset() command for initializing the next layer of the circuit. At the end of all the layers we calculate the energy.

The Neural Network circuit for some random parameters is given below

png

Data

The next part is to get the training and testing data to train and run our neural network. Here we calculate the ground state energy of molecular hydrogen at various interatomic distances. At each of these distances, we must create a driver for the molecule from a computational chemistry library. This driver can be used to obtain the Hamiltonian and give us additional information regarding the molecule.

The exact solutions that have been obtained using a classical Eigensolver are plotted below.

png

Training and Testing Data

We divide this data into training and testing data randomly. The training data will be used to obtain the optimized parameters of our circuit.

Hamiltonian

The qubit operator can be obtained using Qiskit Nature. We convert the fermion Hamiltonian of the given molecule to a Qubit Operator by using a mapper. In this tutorial we have used Parity Mapper. The function defined below returns a Qubit Operator which varies with the bond length of a hydrogen molecule.

Energy

Finally we use the state vector simulator to obtain the Statevector and then use matrix multiplication to obtain the expectation value. This method is only feasible on a simulator and more elaborate methods have to be used to obtain the expectation values on a Quantum Computer. The basic idea is to decompose the Qubit Hamiltonian operator into Pauli Operators and then use a Hadamard test to obtain the expectation values for these Pauli operators separately. Hadamard tests cannot be used directly as the obtained Qubit Operator is not unitary.

Note — Qiskit also provides us with a method of calculating the expectation value using StateFn(), but this requires us to have only quantum bits in our circuit and that is not possible as we have introduced the non-linearity based on classical bits.

Training

We will be doing unsupervised learning by minimizing the expectation value for each case, using theminimize function from scipy. In this function you have to pass as an argument an objective function with the parameters, which need to be optimized, defining the cost. Below we define this objective function.

Be patient with the command below as classical optimization takes quite some time. Furthermore rerunning the commands might produce different parameters, some of which may not produce good approximations.

Now, we have obtained the optimized parameters. Using these we will obtain the energy of the testing inputs.

Finally we plot the curve for testing and training data and observe that this is actually really close to the real answer.

png

Without non-linearity

The main idea behind the intermediate measurement is to introduce non-linearity into an otherwise linear space. So let us test what happens when we remove the measurement part.

The whole procedure of training, calculating cost, optimizing and testing remains the same as above.

The circuit with random parameters can be seen below. Everything remains the same, but the intermediate measurement and reinitialization part is removed.

png

And we run the training/testing code again and plot the results obtained:

png

Here we observe that the curve for training and testing is not at all close to the exact energy which shows that our neural network is a remarkable step up over plane parameterized quantum circuits. Or, in other words, the nonlinearity introduced by the mid-circuit measurement neural network provides an obvious benefit over just parametrized quantum circuits.

Our next steps are to explore different data encoding schemes to optimize resources, and study the feasibility of the implementation on real devices taking into account the noise influence in the results.

This tutorial was made as a part of Qiskit Advocate mentorship program by Parmeet Singh Chani, Hojun Lee, Emilio Peláez and Anuranan Das under the guidance of Daniel Sierra-Sosa.

--

--