# Machine Learning Logic

I simplify tech for High School students. I don’t necessarily get everything correct but I do get kids interested in new areas of technology.

@antalszava I am just trying to get my brain around the difference between TensorflowJS Keras Layers Deep Learning logic (Not much different than Regular Python Tensorflow) and Quantum Machine Learning logic.

The following code is a complete webpage that runs the simple xOr Deep Learning example. It could be cut and pasted and run on any Chrome web browser.

``````<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.3.0"> </script>

<input id="myButton123" type="button" value="xOr using Keras Layers" onclick="{

(async function myGo() {
document.getElementById('myButton123').style.backgroundColor = 'red'
const model = tf.sequential();

model.add(tf.layers.dense({inputShape: [2],  units: 10, activation: 'sigmoid',}) );  // 2 inputs to 10 hidden layer nodes
model.add(tf.layers.dense({inputShape: [10], units: 1,  activation: 'sigmoid',}) );  // from the 10 hidden layer nodes to 1 output layer
const myOptimizer = tf.train.sgd(0.5);

model.compile({optimizer: myOptimizer, loss: 'meanSquaredError'});

const xTrainingData   = tf.tensor2d([[0,0], [0,1], [1,0], [1,1]]);      // array defines shape  // note should also add , 'int32'
const yTrainingTarget = tf.tensor2d([0,1,1,0], shape=[4,1]);            // needs shape defined

var myFit = await model.fit(xTrainingData, yTrainingTarget, {
batchSize : 4,
epochs    : 3000,
callbacks:  {
onEpochEnd: async (epoch, logs) => {
document.getElementById('myDiv123').innerHTML = 'Epoch # ' + (epoch+1) + '/3000, Loss: ' + logs.loss + '<br><br>'
await tf.nextFrame();  // This improves UI responsiveness during training.
}    // end onEpochEnd callback
}      // end all callbacks
})       // end model.fit

const myPredictArray = await model.predict(xTrainingData).data()   // just throw all the training data back in

document.getElementById('myDiv123').innerHTML += '[0,0] = ' + myPredictArray[0].toFixed(4) +'<br>'
document.getElementById('myDiv123').innerHTML += '[1,0] = ' + myPredictArray[1].toFixed(4) +'<br>'
document.getElementById('myDiv123').innerHTML += '[0,1] = ' + myPredictArray[2].toFixed(4) +'<br>'
document.getElementById('myDiv123').innerHTML += '[1,1] = ' + myPredictArray[3].toFixed(4) +'<br>'
document.getElementById('myButton123').style.backgroundColor = 'lightgray'

} )() // end Async function

}"><br><br>

<div id='myDiv123'>...</div><br>

``````

It is fairly easy to follow the logic of this Deep Learning example:

1. A button
2. An asynchronous function
3. Keras layer model creation
4. xTrainingData, yTrainingTaget (labels)
5. Fit (Training)
6. Prediction
7. Data output

For my students we really just have to get data in the correct format (The students would need much more data than this xOr example) and figure out the best model design for what we want to study.

Not so easy with Quantum Computing.

It is not clear to me how the Quantum computer is used to help train the model. I have this sort of working code example which I might be able to convert to train my xOr example, but I am not really sure how this program would actually be effecting the model. It looks to me like the quantum part is happening before the model is trained.

``````
import pennylane as qml
import tensorflow as tf
import sklearn.datasets
import numpy as np

n_qubits = 1
layers = 1
data_dimension = 1

dev = qml.device("default.qubit", wires=n_qubits)

@qml.qnode(dev)
def qnode(inputs, m, c):
qml.templates.AngleEmbedding(inputs, wires=range(n_qubits))
qml.RX(m, wires=0)
qml.RZ(c, wires=0)
return [qml.expval(qml.PauliZ(i)) for i in range(n_qubits)]

weight_shapes = {"m": 1, "c":1}

qlayer = qml.qnn.KerasLayer(qnode, weight_shapes, output_dim=n_qubits)
clayer1 = tf.keras.layers.Dense(n_qubits)
clayer2 = tf.keras.layers.Dense(data_dimension, activation="linear")

model = tf.keras.models.Sequential([qlayer])

X = np.array([-40, -10,  0,  8, 15, 22,  38],  dtype=float)
Y = np.array([-40,  14, 32, 46, 59, 72, 100],  dtype=float)
X1 = X.reshape((len(X), 1))
Y1 = Y.reshape((len(Y), 1))

model.compile(loss='mean_squared_error',

model.fit(X, Y, epochs=60, batch_size=1)
model.summary()

# save model partially works
#tf.keras.models.save_model(model, './model.h5')

#print (dir(tf.keras.models.save_model))

qnn_results = model.evaluate(X, Y, verbose=2)

print ("qnn_results")
print (qnn_results)

``````

Any ideas what the above code is doing, and suggestions for what I want to make?

Hi @jerteach,

In your example code is a hybrid model with three layers, the first of which is a quantum layer. It is treated similarly to the classical layers - some data goes in, and the processed data comes out. It’s just that the processing here is done on a quantum device, according to the `qnode` defined there. Just like in a classical network, where the weights for each layer are learned during training, the weights `m` and `c` of `qnode` will be also be learned.

We’ve recently added a new tutorial that focuses on these types of hybrid models, and gives some more examples. The ability to mix classical layers with quantum layers gives the opportunity to construct some very interesting networks!

Hopefully that helps, please let us know if you have any follow up questions!

I appreciate it @glassnotes This example is very close to what I want to do, and I notice a keras version of it as well.

“Quantum Computing Educator & Researcher at Xanadu”

As a fellow educator (I teach High School Tech) you might like this Gitpod (online docker). My PennylanAI version takes about 10 minutes to load and needs you to have a Github account, but allows students 100 free hours a month and is really impressive. It loads basically all the quantum programs.

https://gitpod.io/#github.com/hpssjellis/my-examples-for-quantum-computing

If you want to look at the Github of the above Gitpod it is here
https://github.com/hpssjellis/my-examples-for-quantum-computing

I am still working on it but in this section is most of the pennylanAI examples here

Run code with (right click --> open terminal)

``````python3 file.py

``````

Thanks @jerteach, will check this out!