Simple xOr Quantum to Keras

I am a newbie so I apologize if I have done something wrong.

I am trying to find information about making the easiest Qauntum Neural Network such as the xOr solution, that the output model can be saved in the Keras .h5 (or Tensorflow .pb) format and that the code could be run on as many Quantum Computer systems as possible.

The xOr issue is explained here using TensorflowJS:

It is a 2 digital (or float) input, 2 a 10 node hidden layer, that outputs a single floating point number.

[0,0] = ~0
[1,0] = ~1
[0,1] = ~1
[1,1] = ~0

The result is a floating point number which when properly trained is either close to zero or close to 1. When things get complex sometimes the easiest solution is a great starting point.

This topic looks fairly similar to

I have no problem making a stab at the code, but wondering if anyone has any suggestions or links to helper code / documentation.

Thanks in advance.

I found this link so I am doing better now

Still would appreciate opinions, and links to suggestions.

Hi @jerteach,

As for encoding the XOR function, there are multiple ways of how a circuit could be implemented. A fairly simple approach could be to prepare 2 qubits in the computational basis, apply the CNOT gate with wire 0 being the control and wire 1 being the target. Then, we’d consider the measurement outcome of wire 1 in the computational basis.

import pennylane as qml

dev = qml.device('default.qubit', wires=2, analytic=True)

def circuit(x=None, y=None):
    qml.BasisState(np.array([x,y]), wires=[0,1])
    return qml.probs(wires=[1])

# Get the probability of the first wire being in state 1


Note: the first element of the output array is the probability of the state on wire 1 being in the zero state, while the second element is the probability of wire 1 being in state 1.

In order to create a variational circuit, the basis state preparation could be done using rotation gates that are equivalent to flipping a qubit (i.e., PauliX). Essentially parameters to these rotation gates would have to be learnt. Note that there would still be subtleties to work out, e.g., when defining the exact cost function that allows finding the correct parameters.

As for the Keras integration, linking the KerasLayer documentation including examples (most probably you’ve come across it before).

Hope this helps further!

It is a great start. Thank you so much @antalszava.

I feel like it is 2015 when I started working on Machine Learning and knew nothing. Now I can confidently, teach high school kids a simplified version of Deep Learning using Tensorflowjs in the browser

I would eventually like to be able to include some form of Quantum Computing in this High School Curriculum.

1 Like