Speeding up qnode circuit

Is there some way to speed up a qnode circuit which calls DisplacementEmbedding and CVNeuralNetLayers functions
with measurements for generating quanvolutional features
of a large dataset? The device is strawberryfields.tf.

Hey @art!

You can try broadcasting over the inputs, but I’m not sure that it will work given some incompatibilities with TensorFlow and broadcasting with certain operations in PennyLane. That, and the latest PennyLane version you can safely use here is v0.29.

We just released a video on this, as well!

See here for more information if native broadcasting doesn’t work:

Let me know if any of this helps!

@isaacdevlugt
using qml.batch_input DisplacementEmbedding() gives a batching error in the following script:

import pennylane as qml
import tensorflow as tf

dev = qml.device('strawberryfields.tf', wires=3, cutoff_dim=5)

@qml.batch_input
@qml.qnode(dev, interface="tf")
def circuit(feature_vector):
    qml.DisplacementEmbedding(features=feature_vector, wires=range(3), method='phase', c=0.5)
    qml.QuadraticPhase(0.1, wires=1)
    return qml.expval(qml.NumberOperator(wires=1))

x = tf.random.uniform((10,3), 0, 1)
print(circuit(x))

line 9, in circuit
qml.DisplacementEmbedding(features=feature_vector, wires=range(3), method=‘phase’, c=0.5)
File “/home/tamu/anaconda3/envs/tamu_py38/lib/python3.8/site-packages/pennylane/templates/embeddings/displacement.py”, line 109, in init
raise ValueError(f"Features must be a one-dimensional tensor; got shape {shape}.")
ValueError: Features must be a one-dimensional tensor; got shape (10, 3).

Yeah it looks like DisplacementEmbedding doesn’t support broadcasting. Another thing you can try to do is to use adjoint differentiation as your differentiation method (see here for more details: Gradients and training — PennyLane 0.33.0 documentation).

Besides that, if getting better performance is important to your application / research and since the PL-SF plugin isn’t supported for PennyLane versions more recent than v0.29, it might be worthwhile to see if you can implement your code using native StrawberryFields or MrMustard:

Let me know if this helps!