Quantum Machine Learning with MERA/MPS model on Blueqat quantum computing SDK

Like Tensorflow follows quantum computing and NISQ variational circuit like tensorflow quantum,

I just tried to implement two model on blueqat with simple implementation, let’s look at how to create.

MERA/TTN

The model is from tensor network field. Here the tree structure of quantum state can be written in quantum circuit.

|0> --[input]--U3--*--
|
|0> --[input]--U3--X--U3--*--
|
|0> --[input]--U3--*-- |
| |
|0> --[input]--U3--X--U3--X--[m]-[expt]-[loss]-[output]

Here I used U3 gate which has 3 angle parameters and CX gate to operate on single quantum state and entanglement among multiple qubits.

#MERA circuit
def mera(a):

u = Circuit()
u.u3(a[0],a[1],a[2])[0]
u.u3(a[3],a[4],a[5])[1]
u.u3(a[6],a[7],a[8])[2]
u.u3(a[9],a[10],a[11])[3]
u.cx[0,1].cx[2,3]
u.u3(a[12],a[13],a[14])[1]
u.u3(a[15],a[16],a[17])[3]
u.cx[1,3]

return u

Finally to get the value we do measurement of Z axis to estimate the state vector indirectly and get the expectation value.

For the quantum computer we use sampling to run the circuit many times and get the expectation value of hamiltonian, but it takes a lot of time, here we use state vector directly to get the expectation value of Z hamiltonian.

This process is VQE step and after this, we use loss function for training

Input data and target data

The input data is quantum circuit here. If you enter the classical data of bit as 01, the data will be converted into quantum circuit using X gate to flip the initial state for quantum circuit.

The MERA circuit including parameters as angle on U3 gate randomly at first.

After the circuit is run and the expectation value is calculated, we can use loss function with target label.

Besides the input data is implemented as quantum circuit, the target label is scalar value. By evaluating the error rate using loss function with expectation value of Z measurement and target label, we can now do the optimization process.

Optimization

Using numerical differentiation, the gradient can be acquired. With the gradient we can update the angle parameters inside of the quantum circuit.

Repeatedly doing this process we can go to the much more lower point of function to get the local/global minimum of function.

Here we use stochastic gradient descent to randomly select the data from the data set and do gradient descent.

Training Process

Let’s do the training process on blueqat. First we import some tools and set the initial parameters to optimize.

from blueqat import Circuit
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import time
%matplotlib inline

np.random.seed(39)

#initial parameters
ainit = [np.random.rand()*np.pi*2 for i in range(18)]

Next we prepare some functions to calculate expectation value, loss function. The last one is to convert data to gate circuit.

#expectation value
def E(sv):
return sum(np.abs(sv[:8])**2)-sum(np.abs(sv[8:])**2)

#loss function
def L(p,t):
return (p-t)**2

#data to gate
def ix(l):
u = Circuit(4)
for i in l:
u.x[i]
return u

And prepare the training data and validation data.

#training data
inp = [[0,1],[2,3],[0],[3]]
tgt = [1,1,-1,-1]

#validation data
inp_c = [[1],[2],[0,2],[1,3]]
tgt_c = [-1,-1,1,1]

And let’s start training,

#initial parameters
a = ainit.copy()

#result list
ar = []

h = 0.01
e = 0.01

#iterations
nsteps = 800

start = time.time()
for i in range(nsteps):
r = np.random.randint(0,len(inp))

c = ix(inp[r])+mera(a)
loss = L(E(c.run()),tgt[r])

ar.append(loss)

at = [0 for i in range(len(a))]
for j in range(len(a)):
aa = a.copy()
aa[j] += h
loss2 = L(E((ix(inp[r])+mera(aa)).run()),tgt[r])
at[j] = a[j] - e*(loss2 - loss)/h

a = at

plt.plot(ar)
plt.show()

print(time.time() - start)

After the process, we get the figure and check the accuracy of the model.

#training accuracy
np.mean([E((ix(inp[i])+mera(a)).run())/tgt[i] for i in range(len(inp))])

0.9760245973174358

#validation accuracy
np.mean([E((ix(inp_c[i])+mera(a)).run())/tgt_c[i] for i in range(len(inp_c))])

0.9769492668551134

MPS

Matrix Product State is the very basic structure of tensor network. Using U3 and CX we can implement this,

|0> --[input]--U3--*--
|
|0> --[input]--U3--X--U3--*--
|
|0> --[input]---------U3--X--U3--*
|
|0> --[input]----------------U3--X--[m]-[expt]-[loss]-[output]

The model is,

#MPS circuit
def mps(a):

u = Circuit()
u.u3(a[0],a[1],a[2])[0]
u.u3(a[3],a[4],a[5])[1]
u.u3(a[6],a[7],a[8])[2]
u.u3(a[9],a[10],a[11])[3]
u.cx[0,1]
u.u3(a[12],a[13],a[14])[1]
u.cx[1,2]
u.u3(a[15],a[16],a[17])[2]
u.cx[2,3]

return u

Basic parameters are the same as MERA structure

start = time.time()

#initial parameters
a = ainit.copy()

#result list
ar = []

h = 0.01
e = 0.01

for i in range(nsteps):
r = np.random.randint(0,len(inp))

loss = L(E((ix(inp[r])+mps(a)).run()),tgt[r])
ar.append(loss)

at = [0 for i in range(len(a))]
for j in range(len(a)):
aa = a.copy()
aa[j] += h
loss2 = L(E((ix(inp[r])+mps(aa)).run()),tgt[r])
at[j] = a[j] - e*(loss2 - loss)/h

a = at

plt.plot(ar)
plt.show()
print(time.time() - start)

And the accuracy is,

#training accuracy
np.mean([E((ix(inp[i])+mps(a)).run())/tgt[i] for i in range(len(inp))])

0.9697437828925157

#validation accuracy
np.mean([E((ix(inp_c[i])+mps(a)).run())/tgt_c[i] for i in range(len(inp_c))])

0.9696755262709482

Future work

Today we checked MERA/MPS model by scrath on blueqat. It looks that the optimization process will be better done by using existing deep learning tools like keras or torch.optim. Using these efficient tools we can get much more faster optimized process for quantum machine learning, because the training process is very heavy for state vector increase as 2^N for the number of qubit N.

This is very basic structure and small dataset. We need much more bigger data set and efficient model.

--

--