Quantum Machine Learning model saved in Keras .pb format?

Has anyone made a Quantum Machine Learning model that has been saved in the Keras Layers .pb format with associated folders? Or any format that can be converted to the Keras layers format?



  • Hello,

    I have not heard of this happening to my knowledge, but I have inquired to see what is available, and what might exist already.

    Is this a preferred format that you are particularly familiar with?

    If so, then perhaps it's something that you could adapt to a graph model or Binary Quadratic Model, with a little assistance.

    I am not familiar with the Keras Layers format myself, but am looking into how it works.

    Comment actions Permalink
  • Thanks so much for replying User N my question is slightly incorrect, Keras files are saved in the .h5 format which can be converted to Tensorflow Keras .pb format. This format can then be converted to any of the Tensorflow versions: regular Tensorflow, TensorflowJs, TensorflowLite, TensorflowMicro.  


    With some help I would really like to make the simplest xOr deep learning model on a quantum computer and save it in a useable format. 


    The xOr problem takes 2 inputs, a hidden layer of say 10 nodes and then has one output.

    0,0 = 0

    0,1 = 1

    1,0 = 1

    1,1 = 0


    I have been simplifying machine learning problems to teach high school kids for a few years now. A better explanation of the xOr model is here https://hpssjellis.github.io/beginner-tensorflowjs-examples-in-javascript/beginner-keras/20keras-xOr.html 



    I really hope you can help with this, I would be willing to make videos like this one about the dWave abilities with quantum computers.



    Also I just live down the road about 45 minutes from dWave Burnaby BC.


    Jeremy Ellis.







    Comment actions Permalink
  • Hello,

    Are you able to describe in simple terms how to decide the value to use for each node, and how that decision is made iteratively?

    Maybe then we can help map the problem to the QPU.

    I'm just trying to make sure I understand the problem fully.

    Comment actions Permalink
  • Well that is where it gets tricky as a Newbie to quantum. In Machine Learning the I define the model with layers. In this case 2 inputs, 10 hidden layers and 1 output. I give it some data with labels, or predetermined outputs in this case:


    [0,0] = label 0

    [1,0] = label 1

    [0,1] = label 1

    [1,1] = label 0


    and the neural network assigns weights and biases to the nodes of the network. The machine is trained. I get the feeling in quantum you have to understand the individual problem a lot more intimately.



    Can anyone give any suggestions. I don't mind trying to write the code, but wondering if anyone has any hints.

    Comment actions Permalink
  • Hello,

    I think in order to help with this, I would need to understand better how the neural network assigns weights and biases in the training process.

    Do you have any material on this like you had provided above that I could take a look at?

    I will try to find something like that elsewhere. 
    I imagine the effectiveness of the model will depend on the rules around assigning and modifying those weights and biases.

    Hopefully we can get something working soon!

    Best regards,


    Comment actions Permalink
  • That is a great question David J since in Machine Learning the weights and biases are assigned gradually during the training of massive amounts of data. I have no idea how massive amount of data gets into a quantum computer and how it can then create a trained model that can be used on a conventional computer. I feel like people have solved these issues already.



    I have found some interesting examples around the web from Tensorflow Quantum and PennylaneAI. 


    (I think I am losing code formatting when submitting this, I will include links)





    In my TensorflowJS example the "fit" function repetitively trains the model




    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






    This example from Pennylaneai seems to do the xOr issue on a quantum computer, but does not train a model



    import pennylane as qml
      import numpy as np
      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






    and this bit of code seems to do some Tensorflow Keras training




    import pennylane as qml
      import numpy as np
      import tensorflow as tf
      import sklearn.datasets
      n_qubits = 2
      dev = qml.device("default.qubit", wires=n_qubits)
      def qnode(inputs, weights):
      qml.templates.AngleEmbedding(inputs, wires=range(n_qubits))
      qml.templates.StronglyEntanglingLayers(weights, wires=range(n_qubits))
      return qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliZ(1))
      weight_shapes = {"weights": (3, n_qubits, 3)}
      qlayer = qml.qnn.KerasLayer(qnode, weight_shapes, output_dim=2)
      clayer1 = tf.keras.layers.Dense(2)
      clayer2 = tf.keras.layers.Dense(2, activation="softmax")
      model = tf.keras.models.Sequential([clayer1, qlayer, clayer2])
      data = sklearn.datasets.make_moons()
      X = tf.constant(data[0])
      Y = tf.one_hot(data[1], depth=2)
      opt = tf.keras.optimizers.SGD(learning_rate=0.5)
      model.compile(opt, loss='mae')





    Comment actions Permalink
  • Hello,

    After taking a look at a few different tutorials, and the above code examples you have provided, it doesn't look like we have anything like the Keras Layers model.

    I was taking a look at this tutorial for example, which had some math and code information about the process:

    It might be possible to generate a model in a similar fashion to the above examples to run on the Quantum Annealer, however in order to accomplish this, most of the libraries would have to be implemented by the user, in a similar method as described in the tutorial I linked to just above.

    The QPU could be used to represent the input, hidden, and output layers, and actually run samples against the model.

    It might be possible to improve on the Machine Learning model generation step as well, but a lot of the methodology would also have to be innovated and thought out in order to best use the QPU for this kind of problem.

    At present we do not have any libraries on offer for Machine Learning, but please do stay tuned, in case this changes.

    I will continue to look into this further on our end, but please feel free to try running some simple problems in order to better understand how the QPU optimizes QUBO problems. 
    This will help to better understand how one would go about adapting problems to run on the QPU architecture, and how Machine Learning could work with it.

    Here are a couple of links in the mean time that might help understand how problems are represented, and sampled from on the QPU:


    Comment actions Permalink

Please sign in to leave a comment.

Didn't find what you were looking for?

New post