# Implementing the implicit part of QML in feature HS

**URL:** <https://discuss.pennylane.ai/t/implementing-the-implicit-part-of-qml-in-feature-hs/173>\
**Category:** PennyLane Feedback\
**Created:** [July 13, 2019, 1:04am UTC](https://discuss.pennylane.ai/t/implementing-the-implicit-part-of-qml-in-feature-hs/173 "2019-07-13T01:04:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kareem\_essafty](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/kareem_essafty/32/401_2.png) [@kareem\_essafty](https://discuss.pennylane.ai/u/kareem_essafty)\
**Post date:** [July 13, 2019, 1:04am UTC](https://discuss.pennylane.ai/t/implementing-the-implicit-part-of-qml-in-feature-hs/173/1 "2019-07-13T01:04:50Z")

</div>

hi this is my attempt to implement the first part of this [paper](https://arxiv.org/pdf/1803.07128.pdf)  
I hope i coded it well 😃  
any feedback is much appreciated

> import numpy as np  
> from sklearn.linear\_model import LogisticRegression as lr  
> from sklearn.model\_selection import train\_test\_split  
> from sklearn.metrics import accuracy\_score

> from sklearn.datasets import make\_moons, make\_blobs, make\_circles  
> X,y = make\_circles(n\_samples=300,random\_state=0,noise=0.05,factor=.6)  
> X\_train, X\_test, Y\_train, Y\_test = train\_test\_split(X, y,test\_size=0.2,random\_state=0)

> def kernel(x1, x2,amp):
> 
> ```
> a = np.array((1/np.cosh(amp))*(1/np.cosh(amp)))          
> b = 1-(np.exp(1j*(x1-x2))*np.tanh(amp)*np.tanh(amp))       
> 
> return abs(np.dot(np.sqrt(a/b),np.sqrt(a/b)))
> 
> ```

> def gram(A, B,amp):
> 
> ```
> gram = np.zeros((len(A), len(B)))
> for id1, x1 in enumerate(A):
> for id2, x2 in enumerate(B):
> gram[id1, id2] = kernel(x1, x2,amp)
> 
> return gram
> 
> ```

> gram\_train = gram(X\_train, X\_train,.8)  
> gram\_test = gram(X\_test, X\_train,.8)

> model =lr(random\_state=0)  
> model.fit(gram\_train, Y\_train)
> 
> predictions\_test = model.predict(gram\_test)  
> accuracy\_score(predictions\_test, Y\_test)

> xx, yy = np.meshgrid(np.linspace(-1.4, 1.4, 20), np.linspace(-1.2, 1.4, 20))  
> X\_grid = [np.array([x, y]) for x, y in zip(xx.flatten(), yy.flatten())]  
> tt=gram(X\_grid, X\_train,.8)
> 
> # start plot
> 
> plt.figure()  
> cm = plt.cm.RdBu
> 
> # plot decision regions
> 
> predictions\_grid = [model.predict(tt)]  
> Z = np.reshape(predictions\_grid, xx.shape)  
> cnt = plt.contourf(xx, yy, Z, levels=np.arange(0., 1.1, 0.1), cmap=cm, alpha=.8)  
> plt.colorbar(cnt, ticks=[0, 0.5, 1])
> 
> # plot data
> 
> plt.scatter(X\_train[:, 0][Y\_train==0], X\_train[:, 1][Y\_train==0], c=‘r’, marker=‘^’, edgecolors=‘k’,label=‘train0’)  
> plt.scatter(X\_train[:, 0][Y\_train==1], X\_train[:, 1][Y\_train==1], c=‘b’, marker=‘^’, edgecolors=‘k’,label=‘train1’)  
> plt.scatter(X\_test[:, 0][Y\_test==0], X\_test[:, 1][Y\_test==0], c=‘r’, marker=‘o’, edgecolors=‘k’,label=‘test0’)  
> plt.scatter(X\_test[:, 0][Y\_test==1], X\_test[:, 1][Y\_test==1], c=‘b’, marker=‘o’, edgecolors=‘k’,label=‘test1’)
> 
> #plt.ylim(-0.4, 0.4)  
> #plt.xlim(-0.4, 0.4)  
> plt.legend()  
> plt.tight\_layout()  
> plt.show()

![image](https://yyz2.discourse-cdn.com/flex012/images/transparent.png)

---

<div class="post-metadata">

**Author:** ![kareem\_essafty](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/kareem_essafty/32/401_2.png) [@kareem\_essafty](https://discuss.pennylane.ai/u/kareem_essafty)\
**Post date:** [July 13, 2019, 2:41pm UTC](https://discuss.pennylane.ai/t/implementing-the-implicit-part-of-qml-in-feature-hs/173/2 "2019-07-13T14:41:16Z")

</div>

is my understanding right? @josh
