qml.qnn.KerasLayer

class KerasLayer(qnode, weight_shapes: dict, output_dim, weight_specs: Optional[dict] = None, **kwargs)[source]

Bases: tensorflow.python.keras.engine.base_layer.Layer

Converts a QNode() to a Keras Layer.

The result can be used within the Keras Sequential or Model classes for creating quantum and hybrid models.

Parameters
  • qnode (qml.QNode) – the PennyLane QNode to be converted into a Keras Layer

  • weight_shapes (dict[str, tuple]) – a dictionary mapping from all weights used in the QNode to their corresponding shapes

  • output_dim (int) – the output dimension of the QNode

  • weight_specs (dict[str, dict]) – An optional dictionary for users to provide additional specifications for weights used in the QNode, such as the method of parameter initialization. This specification is provided as a dictionary with keys given by the arguments of the add_weight(). method and values being the corresponding specification.

  • **kwargs – additional keyword arguments passed to the Layer base class

Example

First let’s define the QNode that we want to convert into a Keras Layer:

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

@qml.qnode(dev)
def qnode(inputs, weights_0, weight_1):
    qml.RX(inputs[0], wires=0)
    qml.RX(inputs[1], wires=1)
    qml.Rot(*weights_0, wires=0)
    qml.RY(weight_1, wires=1)
    qml.CNOT(wires=[0, 1])
    return qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliZ(1))

The signature of the QNode must contain an inputs named argument for input data, with all other arguments to be treated as internal weights. We can then convert to a Keras Layer with:

>>> weight_shapes = {"weights_0": 3, "weight_1": 1}
>>> qlayer = qml.qnn.KerasLayer(qnode, weight_shapes, output_dim=2)

The internal weights of the QNode are automatically initialized within the KerasLayer and must have their shapes specified in a weight_shapes dictionary. It is then easy to combine with other neural network layers from the tensorflow.keras.layers module and create a hybrid:

>>> clayer = tf.keras.layers.Dense(2)
>>> model = tf.keras.models.Sequential([qlayer, clayer])

QNode signature

The QNode must have a signature that satisfies the following conditions:

  • Contain an inputs named argument for input data.

  • All other arguments must accept an array or tensor and are treated as internal weights of the QNode.

  • All other arguments must have no default value.

  • The inputs argument is permitted to have a default value provided the gradient with respect to inputs is not required.

  • There cannot be a variable number of positional or keyword arguments, e.g., no *args or **kwargs present in the signature.

Initializing weights

The optional weight_specs argument of KerasLayer allows for a more fine-grained specification of the QNode weights, such as the method of initialization and any regularization or constraints. For example, the initialization method of the weights argument in the example above could be specified by:

weight_specs = {"weights": {"initializer": "random_uniform"}}

The values of weight_specs are dictionaries with keys given by arguments of the Keras add_weight() method. For the "initializer" argument, one can specify a string such as "random_uniform" or an instance of an Initializer class, such as tf.keras.initializers.RandomUniform.

If weight_specs is not specified, weights will be added using the Keras default initialization and without any regularization or constraints.

Additional example

The code block below shows how a circuit composed of templates from the qml.templates module can be combined with classical Dense layers to learn the two-dimensional moons dataset.

import pennylane as qml
import tensorflow as tf
import sklearn.datasets

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

@qml.qnode(dev)
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')

The model can be trained using:

>>> model.fit(X, Y, epochs=8, batch_size=5)
Train on 100 samples
Epoch 1/8
100/100 [==============================] - 9s 90ms/sample - loss: 0.3524
Epoch 2/8
100/100 [==============================] - 9s 87ms/sample - loss: 0.2441
Epoch 3/8
100/100 [==============================] - 9s 87ms/sample - loss: 0.1908
Epoch 4/8
100/100 [==============================] - 9s 87ms/sample - loss: 0.1832
Epoch 5/8
100/100 [==============================] - 9s 88ms/sample - loss: 0.1596
Epoch 6/8
100/100 [==============================] - 9s 87ms/sample - loss: 0.1637
Epoch 7/8
100/100 [==============================] - 9s 86ms/sample - loss: 0.1613
Epoch 8/8
100/100 [==============================] - 9s 87ms/sample - loss: 0.1474

input_arg

Name of the argument to be used as the input to the Keras Layer.

input_arg

Name of the argument to be used as the input to the Keras Layer. Set to "inputs".

build(input_shape)

Initializes the QNode weights.

call(inputs)

Evaluates the QNode on input data using the initialized weights.

compute_output_shape(input_shape)

Computes the output shape after passing data of shape input_shape through the QNode.

build(input_shape)[source]

Initializes the QNode weights.

Parameters

input_shape (tuple or tf.TensorShape) – shape of input data

call(inputs)[source]

Evaluates the QNode on input data using the initialized weights.

Parameters

inputs (tensor) – data to be processed

Returns

output data

Return type

tensor

compute_output_shape(input_shape)[source]

Computes the output shape after passing data of shape input_shape through the QNode.

Parameters

input_shape (tuple or tf.TensorShape) – shape of input data

Returns

shape of output data

Return type

tf.TensorShape