Regression Problem in strawberry field and Pennylane CVNeuralNetwork

I am referring to the tutorial of CV-QNN in Pennylane(Strawberry Fields).
Each layer of CV-QNN can perform the operation below.

enter image description here

Unlike other quantum neural network tutorials, where the input X and y are given explicitly, this is not the case here. However, they have mentioned Encoding data outside of the position eigenbasis, for example using instead the Fock basis. How can I encode my data which is in the format of X and y, where each sample is a 6-dimensional vector and y is a real value in [0,1]? I have X as a pandas data frame of shape [150,6] and y is a pandas series of length 150. How to train the CV Neural Network using my data? If the strawberry field is not working, can i just use pennylane inbuild CVNeuralNetwork class to do so ?

Hi @psy, welcome to the Forum!

The picture that you added is the description of one layer. For a single quantum mode a layer is made of one rotation gate, one squeezer, another rotation, one displacement, and one Kerr gate as seen in the image below.


In the tutorial on function fitting with a photonic quantum neural network you can see that this is exactly how the layers are coded. In the Strawberry Fields code the difference is that the demo uses multiple modes per layer so it uses interferometers instead of rotation gates.

One big difference is that in the StrawberryFields example the demo uses a quantum neural network to learn a specific target state. The PennyLane tutorial instead does function fitting which is closer to what you want to do I guess. This demo encodes the inputs (x) into the state using a Displacement gate, which is something you could also add to the Strawberry Fields program before you add the layers (in the section that starts with with qnn.context as q:).

In summary, you can indeed use both PennyLane or Strawberry Fields to train your data. If you’re familiar with the CV model I would encourage you to try this directly in Strawberry Fields, otherwise you can use PennyLane together with the PennyLane-StrawberryFields plugin. Remember that this plugin only works with PennyLane v0.29 and lower versions.

Please let me know if you have any additional questions.

1 Like

Though @CatalinaAlbornoz has given the whole picture of implementation beautifully, I would like to add something that directly relates to your problem in hand. @CatalinaAlbornoz do correct me if I am wrong. I am assuming your input data is a 6-D vector with real values not just {0,1}. Then, you can encode the data using displacement gates. You would have to add another layer of Displacement Gates (6 of them for each qumode having parameters as your input data) to encode your input into the circuit before the CVQNN layers. Then you can measure a single qumode (any one of them) and match them with your target variables.
It becomes a lot easier if your input data is also binary. In that case, you need only one qumode with cutoff dimension as 6 and the rest follows.

1 Like

Hi @Prabhat_Prabhat,

What you suggest does seem like a reasonable way to encode the data. You could also encode the data directly into the layers. I’m not sure which one would be faster or give more accurate results so you could try different encodings and choose the best one. Since the input data is only 6D most encodings should be fairly quick :smiley: .


By the way, we have a new PennyLane survey. Let us know your thoughts about PennyLane in order to keep bringing you amazing features :sparkles:.

1 Like

Thank you for your answer, but this answer was not helpful to me in any way possible, nor did it answer my question. I asked in my question above:- In the tutorial of strawberry fields CV-QNN tutorial, there is no clear way in which I could use my own data to train a model using CV-QNN. When you look at the tutorial, can you see the input X vector and output Y label that we give to the circuit?

Also I am not looking forward to function fitting but to learn a target state.

Assume I have pandas dataframe where X denotes the input feature vectors (6-dimensional vector)and Y represents the output scalar. Using this as the data, can you give me a sample code for how I would load this in my quantum circuit in the strawberry field and do a training for regression?

Thank you for your time and efforts!

Hey @psy!

How can I encode my data which is in the format of X … X as a pandas data frame of shape [150,6]

If I’m interpreting your data’s dimensions properly, you have 150 samples of 6-dimensional data. So, you can definitely use something similar to the interferometer function that is provided in the StrawberryFields QNN demo to encode a 6D feature, where each entry of the 6D vector are, say, angles of rotation. To get a simple example up and running, I recommend making a simpler function than interferometer — just encode the data as angles of rotation.

and y is a pandas series of length 150

I’m not sure what the data you have represents, but you just need to make sure that your model’s output can be distilled down and compared to y in some way that makes sense in the context of an objective function (what you’re trying to minimize).

How to train the CV Neural Network using my data?

It’s tough to give a great answer here because I’m not sure what your data represents. Having said that, whether it’s your data, our data, or someone else’ data, the same rules of training any quanutm neural network apply. If you’re still stuck, let me know :slight_smile:

If the strawberry field is not working, can i just use pennylane inbuild CVNeuralNetwork class to do so ?

Are you referring to qml.CVNeuralNetLayers? If so, you can certainly use that as a template. Just please be aware that the PL-SF plugin is supported for versions <= v0.29 of PennyLane.

Lastly, please note that we are here to help you with your PennyLane and/or StrawberryFields code — give you ideas, inspiration, and pointers — but we can’t write the code for you. If you’re stuck on how to get started, we have a lot of great resources for that (see here for the SF tutorials: Tutorials — Strawberry Fields) :slight_smile:

Hope this helps!

Hey @psy, just wanted to check to see if you’ve fixed your issue? If / when your issue is fixed, we have a new PennyLane survey . Let us know your thoughts about PennyLane so that we can keep bringing you amazing features :star_struck:

1 Like

Hey @issacdevlugt! I gave up on the issue. I realized that both pennylane library and help on this forum by pennylane team is not up to expectations. You guys should check how answers are given in stack overflow, and how genuine help is given, rather than your comment " we can’t code for you".

Also the qiskit community and forums have much better help than what is here. So in summary, I have stopped using pennylane and strawberry fields and would advise and convince everyone in my research network and my PhD students to do so.

Also taking about bringing more amazing features :joy:, maybe you should introduce a feature which helps users understand a regression problem by specifying in tutorials how to load data into the circuit using strawberry fields.

So the goal for next years should be designing an easy to understand tutorial in strawberry fields which shows users that given a data frame of X and y labelled pairs, do a simple regression problem, that would be revolutionary :joy::joy:

For others suffering with the same issues, check out (Photonic Qiskit. Qiskit Circuits on a Photonic Quantum… | by Brian N. Siegelwax | Level Up Coding) and Bosonic Qiskit.


Hey @psy, I’m sorry to hear that you’re not able to code what you need on Strawberry Fields. It’s totally okay to try out a different tool if that works better for your specific use case. If you have any further questions please feel welcome to ask them here. And if you have any other feedback that can help us improve please let us know in the survey I shared with you :smile: