Using A CV Circuit

Hello everyone I would like to train a CV quantum circuit. But I am receiving error with my code, can you help?

I have used StrawberryFields before and trying to combine these tutorials:


This is the code

dev = qml.device("default.gaussian", wires=1)

@qml.qnode(dev)
def mean_photon(mag_alpha, phase_alpha, phi):
    qml.Displacement(mag_alpha, phase_alpha) | wires = 0
    qml.RX(phi) | wires = 0
    qml.Kerr(phi) | wires = 0
    return qml.expval(qml.NumberOperator(0))

def cost(params):
    return (mean_photon(params[0], params[1], params[2]) - 1.0) ** 2

init_params = [0.015, 0.02, 0.005]

qml.grad(cost)(init_params)```

The error is

    qml.Displacement(mag_alpha, phase_alpha) | wires = 0
    ^
SyntaxError: can't assign to operator

Hi @eloquant,

Welcome to the Xanadu Discussion Forum! Thank you providing context, your code, and the tutorials.

There are a couple things that strike me off the bat.

The first thing that jumps out at me is the use of the | operator. Although the bit-wise inclusive OR assignment is used in Python, I think that it may not be best-used here.

An alternative for the def mean_photon(mag_alpha, phase_alpha, phi): code-block is as follows:

@qml.qnode(dev)
def mean_photon(mag_alpha, phase_alpha, phi):
    qml.Displacement(mag_alpha, phase_alpha, wires = 0)
    qml.RX(phi, wires = 0)
    qml.Kerr(phi, wires = 0)
    return qml.expval(qml.NumberOperator(0))

But, this brings us to the second thing that I noticed. If you try to run the code with this changed bit, you’ll notice a new error:

DeviceError: Gate RX not supported on device default.gaussian

or perhaps

DeviceError: Gate Kerr not supported on device default.gaussian.

This is because of what I see as a bigger issue, in that those two gates aren’t supported operations on the Gaussian simulator device.

In the tutorial, you’ll notice that they use qml.Rotation(phi, wires=0), which is a supported operation. You’ll recall from your experience with StrawberryFields that there is also a set of supported operations for the StrawberryFields Gaussian Device (see here).

A device that does support the RX gate operation, for example, is the default_qubit device.

Now, if we simply get rid of the two unsupported operations from your code, and get rid of the bitwise operations, we get the following code:

import pennylane as qml
from pennylane import numpy as np

dev = qml.device("default.gaussian", wires=1)

@qml.qnode(dev)
def mean_photon(mag_alpha, phase_alpha, phi):
    qml.Displacement(mag_alpha, phase_alpha, wires = 0)
    return qml.expval(qml.NumberOperator(0))

def cost(params):
    return (mean_photon(params[0], params[1], params[2]) - 1.0) ** 2

init_params = [0.015, 0.02, 0.005]

qml.grad(cost)(init_params)

which runs successfully, but no longer fits your purposes.

But now we know that different devices support different operations, and we can begin to work with this.

How do we deal with different devices? Well luckily for us Pennylane offers a really simple solution!

We can have multiple devices through different through two different quantum nodes, similar to how you were able to construct a single quantum node.

In this tutorial demo, you can scroll down to the section titled “Hybrid computation”, and the first code-block you’ll see demonstrates exactly how to call two quantum nodes.

So the steps I’d recommend to alleviate your issue are as follows:

  1. Determine which devices support which operations.
  2. Separate the nodes accordingly.
  3. Run and tweak as according to the overall requirements of your problem.

I really hope that this helps, and that I’ve provided you with all the resources you need to succeed!

Thanks for providing fixed code. If I copy your code and add qml.Kerr(phi, wires=0) after displacement gate I get error

DeviceError: Gate Kerr not supported on device default.gaussian

If I change device to default.qubit I get a similar error

DeviceError: Gate Displacement not supported on device default.qubit.autograd

Which device should I use? I am making a basic version of the demo https://pennylane.ai/qml/demos/quantum_neural_net.html

Thank you a lot!

Happy to help! To clarify, similar to how the RX gate should work for the default_qubit device, for the Kerr gate you’d want to have a node that uses the StrawberryFields Fock device.

At the top of the tutorial you linked – which I’m grateful to you for since it allowed me to find the relevant device – you’ll notice that they use

dev = qml.device("strawberryfields.fock", wires=1, cutoff_dim=10)

meaning that had in mind the StrawberryFields Fock device when defining each layer of the variational circuit. In the layer, this is where they have Kerr.

Thank you for quick reply! Using this device I am able to execute the circuit.

1 Like

Fantastic to hear, @eloquant! Thank you once again for providing the details I needed to help.

I wish you the best in your journey with Pennylane and Xanadu, and invite you to participate in the Xanadu discussion forums again in the future (potentially helping others too)!