weight_shapes = {"weights": (num_layers,n_qubits,3)}
# Model
input_m = tf.keras.layers.Input(shape=(2 ** n_qubits,), name = "input_0")
print(input_m)
keras_1 = qml.qnn.KerasLayer(circuit, weight_shapes, output_dim=n_qubits, name = "keras_1")(input_m)
print(keras_1)
output = tf.keras.layers.Dense(num_classes_q, activation='softmax', name = "dense_1")(keras_1)
I have a circuit defined here, and I am creating a qnn model here. Is there a way to print the circuit and the specs? qml.draw() needs arguments weights, but using keras that’s predefined.
If you want to draw the quantum circuit you can use qml.draw() or qml.draw_mpl() with a random set of weights.
You could use something like numpy.random.random with size=weight_shapes to generate the weights for drawing. You can then use the weights defined by Keras for training.
Hi @VQX, I think the issue is that you weren’t specifying the “inputs” argument to your circuit.
I couldn’t replicate your error exactly but I could create a version of the code that works.
import pennylane as qml
import numpy as np
import tensorflow as tf
n_qubits = 3
num_layers = 2
dev = qml.device("default.qubit", wires = n_qubits)
@qml.qnode(dev, diff_method='adjoint')
def circuit(weights, inputs=None):
''' Quantum QVC Circuit'''
# print(weights)
weights_each_layer = tf.split(weights, num_or_size_splits=num_layers, axis=0)
for i, W in enumerate(weights):
if i % 2 == 0:
qml.MottonenStatePreparation(inputs, wires = range(n_qubits))
qml.StronglyEntanglingLayers(weights_each_layer[i], wires=range(n_qubits))
return [qml.expval(qml.PauliZ(i)) for i in range(n_qubits)]
weight_shapes = {"weights": (num_layers,n_qubits,3)}
w = np.random.random(size=weight_shapes.get('weights'))
state = np.array([1, 2j, 3, 4j, 5, 6j, 7, 8j])
state = state / np.linalg.norm(state)
print(w)
print(qml.draw(circuit)(w,state))
fig,ax = qml.draw_mpl(circuit)(w,state)
fig.show()