# photonic_pennylane_pytorch.py
import torch
import torch.nn as nn
import torch.optim as optim
import pennylane as qml
import numpy as np
dev3 = qml.device(“default.gaussian”, wires=2)
@qml.qnode(dev3)
def measure_n_coherent(alpha, phi):
qml.Squeezing(alpha, phi, wires = 0)
return qml.expval(qml.NumberOperator(0))
coherent_expval = measure_n_coherent(1, np.pi / 3)
print(“Expected number of photons: {}”.format(coherent_expval))
The above code measures the expected photon numbers in wire 0, i.e. <n_0>. I want to know if it is possible to measure <n_0 n_1> in this code or other libraries such as strawberryfields. I want to use it for some machine learning models, so the gradient should flow properly.
I don’t think your current code would allow you to measure what you need.
The PennyLane demo on photonic quantum computing may guide you on ways to make different kinds of measurements. For example the section on measuring quadratures shows you how to simulate a homodyne measurement. Note that here we use several qnodes, each with one measurement, since default.gaussian doesn’t support multiple measurements nor products of measurements.
I’m not entirely sure whether this is differentiable and compatible with Torch, but I think it should be. Just make sure not to use both NumPy and Torch since this is a common source for errors.
Thanks for your clarification. If default.gaussian does not work itself, what device should I use then? I went through the mentioned demo, it does not include what I want. Could you guide me into how to measure products of the number operators? A brief code would be best.
Once you have you environment with the pennylane-sf plugin, you can write your code and use qml.TensorN() as your observable. This computes the tensor product of the NumberOperator acting on different wires.
The code below shows an example of how to use the 'strawberryfields.gaussian' device with the qml.TensorN observable.
× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.
note: This error originates from a subprocess, and is likely not a problem with pip.
Getting requirements to build wheel … error error: subprocess-exited-with-error
× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.
note: This error originates from a subprocess, and is likely not a problem with pip.
I dont understand why this happens. Do you have any idea?
I can reproduce your error in Google Colab. I’m not exactly sure what the error is but it seems like a version mismatch in some set of packages or Python itself. The easiest solution might be to create a local Conda environment with the requirements specified in the GitHub.
If this doesn’t work for you let me know and I can check the versions I have in the environment where I made this test. Note that only Python 3.10 and below are supported, so my guess is that this is the main source for the error.
But I got this error; this is a bit surprising; could you also help me with this:
File “/home/shuteng/miniconda3/envs/photonic/lib/python3.9/site-packages/pennylane/\_device.py”, line 772, in batch_transform
circuits, hamiltonian_fn = qml.transforms.split_non_commuting(circuit)
File “/home/shuteng/miniconda3/envs/photonic/lib/python3.9/site-packages/pennylane/transforms/batch_transform.py”, line 331, in **call**
return self.\_tape_wrapper(\*targs, \*\*tkwargs)(qnode)
File “/home/shuteng/miniconda3/envs/photonic/lib/python3.9/site-packages/pennylane/transforms/batch_transform.py”, line 421, in
return lambda tape: self.construct(tape, \*targs, \*\*tkwargs)
File “/home/shuteng/miniconda3/envs/photonic/lib/python3.9/site-packages/pennylane/transforms/batch_transform.py”, line 403, in construct
tapes, processing_fn = self.transform_fn(tape, \*args, \*\*kwargs)
File “/home/shuteng/miniconda3/envs/photonic/lib/python3.9/site-packages/pennylane/transforms/split_non_commuting.py”, line 158, in split_non_commuting
groups, group_coeffs = qml.pauli.group_observables(obs_list, range(len(obs_list)))
File “/home/shuteng/miniconda3/envs/photonic/lib/python3.9/site-packages/pennylane/pauli/grouping/group_observables.py”, line 227, in group_observables
if qml.math.shape(coefficients)\[0\] != len(observables):
IndexError: tuple index out of range
I think these two observables may not commute so this may be why you’re getting an error. I think it might be better in that case to have each measurement being returned by separate qnodes. E.g.:
Thanks for the code and the solution. Yes, I also tried it and it indeed works. However, it is quite slow.
In my project, I want to use the correlated PNR measurements (local to global) which also results in exponentially growing dimension of the output. Therefore, it is better that there is an efficient way to return this in a single function or a single call. Otherwise, it is incredibly slow (with my tests).
Do you know if there is something like this provided by Pennylane?
Unfortunately I don’t know of any faster way to do this. Do you know why or where the slowdown is happening? You could try to use a profiler like Snakeviz to identify the bottleneck. Maybe this could give us some ideas on what to change.