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:
- Determine which devices support which operations.
- Separate the nodes accordingly.
- 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!