Strawberryfields.fock device not loading/working

Hello! I am trying to code in a displaced fock state kernel and am having trouble simply getting the fock state in strawberryfields.fock to work! Here is my code,

import pennylane as qml
import numpy as np

dev_gaussian = qml.device("strawberryfields.fock", wires=2, cutoff_dim=10)
@qml.qnode(dev_gaussian)
def mean_photon_fock(x1, x2,x3,x4, phi):
    qml.FockState(3,0)
    qml.FockState(3,1)
    qml.Displacement(x1, x2, wires=0)
    qml.Displacement(x3, x4, wires=1)
    qml.Beamsplitter(np.pi/4, 0, wires=[0, 1])


    # qml.Rotation(phi, wires=0)
    return qml.expval(qml.NumberOperator(0))

print(mean_photon_fock(1,1,-1,-2,0))


print(qml.about())

And my error is as follows:

Traceback (most recent call last):
  File "c:\Users\Rishi\OneDrive\Documents\Rishi\Uni\2023\Sem 2\PHYS3900\#pennylane test.py", line 38, in <module>
    dev_gaussian = qml.device("strawberryfields.fock", wires=2, cutoff_dim=10)
  File "C:\Users\Rishi\AppData\Local\Programs\Python\Python39\lib\site-packages\pennylane\__init__.py", line 317, in device
    plugin_device_class = plugin_devices[name].load()
  File "C:\Users\Rishi\AppData\Local\Programs\Python\Python39\lib\site-packages\pkg_resources\__init__.py", line 2517, in load
    return self.resolve()
  File "C:\Users\Rishi\AppData\Local\Programs\Python\Python39\lib\site-packages\pkg_resources\__init__.py", line 2523, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
  File "C:\Users\Rishi\AppData\Local\Programs\Python\Python39\lib\site-packages\pennylane_sf\__init__.py", line 18, in <module>
    from .fock import StrawberryFieldsFock
  File "C:\Users\Rishi\AppData\Local\Programs\Python\Python39\lib\site-packages\pennylane_sf\fock.py", line 36, in <module>
    import strawberryfields as sf
  File "C:\Users\Rishi\AppData\Local\Programs\Python\Python39\lib\site-packages\strawberryfields\__init__.py", line 24, in <module>
    from . import apps
  File "C:\Users\Rishi\AppData\Local\Programs\Python\Python39\lib\site-packages\strawberryfields\apps\__init__.py", line 36, in <module>     
    import strawberryfields.apps.qchem
  File "C:\Users\Rishi\AppData\Local\Programs\Python\Python39\lib\site-packages\strawberryfields\apps\qchem\__init__.py", line 34, in <module>
    import strawberryfields.apps.qchem.dynamics
  File "C:\Users\Rishi\AppData\Local\Programs\Python\Python39\lib\site-packages\strawberryfields\apps\qchem\dynamics.py", line 73, in <module>
    from strawberryfields.utils import operation
  File "C:\Users\Rishi\AppData\Local\Programs\Python\Python39\lib\site-packages\strawberryfields\utils\__init__.py", line 21, in <module>    
    from .program_functions import *
  File "C:\Users\Rishi\AppData\Local\Programs\Python\Python39\lib\site-packages\strawberryfields\utils\program_functions.py", line 22, in <module>
    import tensorflow as tf
  File "C:\Users\Rishi\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\__init__.py", line 45, in <module>
    from tensorflow._api.v2 import __internal__
  File "C:\Users\Rishi\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\_api\v2\__internal__\__init__.py", line 8, in <module>
    from tensorflow._api.v2.__internal__ import autograph
  File "C:\Users\Rishi\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\_api\v2\__internal__\autograph\__init__.py", line 8, in <module>
    from tensorflow.python.autograph.core.ag_ctx import control_status_ctx # line: 34
  File "C:\Users\Rishi\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\autograph\core\ag_ctx.py", line 21, in <module>
    from tensorflow.python.autograph.utils import ag_logging
  File "C:\Users\Rishi\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\autograph\utils\__init__.py", line 17, in <module>
    from tensorflow.python.autograph.utils.context_managers import control_dependency_on_returns
  File "C:\Users\Rishi\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\autograph\utils\context_managers.py", line 19, in <module>
    from tensorflow.python.framework import ops
  File "C:\Users\Rishi\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\framework\ops.py", line 5906, in <module>  
    ) -> Optional[Callable[[Any], message.Message]]:
  File "C:\Users\Rishi\AppData\Local\Programs\Python\Python39\lib\typing.py", line 243, in inner
    return func(*args, **kwds)
  File "C:\Users\Rishi\AppData\Local\Programs\Python\Python39\lib\typing.py", line 316, in __getitem__
    return self._getitem(self, parameters)
  File "C:\Users\Rishi\AppData\Local\Programs\Python\Python39\lib\typing.py", line 433, in Optional
    return Union[arg, type(None)]
  File "C:\Users\Rishi\AppData\Local\Programs\Python\Python39\lib\typing.py", line 243, in inner
    return func(*args, **kwds)
  File "C:\Users\Rishi\AppData\Local\Programs\Python\Python39\lib\typing.py", line 316, in __getitem__
    return self._getitem(self, parameters)
  File "C:\Users\Rishi\AppData\Local\Programs\Python\Python39\lib\typing.py", line 421, in Union
    parameters = _remove_dups_flatten(parameters)
  File "C:\Users\Rishi\AppData\Local\Programs\Python\Python39\lib\typing.py", line 215, in _remove_dups_flatten
    all_params = set(params)
TypeError: unhashable type: 'list'

Finally, here is the output of qml.about():

Name: PennyLane
Version: 0.29.1
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: https://github.com/XanaduAI/pennylane
Author:
Author-email:
License: Apache License 2.0
Location: c:\users\rishi\appdata\local\programs\python\python39\lib\site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, retworkx, scipy, semantic-version, toml    
Required-by: PennyLane-Lightning, PennyLane-SF

Platform info:           Windows-10-10.0.22621-SP0
Python version:          3.9.0
Numpy version:           1.23.5
Scipy version:           1.10.1
Installed devices:
- default.gaussian (PennyLane-0.29.1)
- default.mixed (PennyLane-0.29.1)
- default.qubit (PennyLane-0.29.1)
- default.qubit.autograd (PennyLane-0.29.1)
- default.qubit.jax (PennyLane-0.29.1)
- default.qubit.tf (PennyLane-0.29.1)
- default.qubit.torch (PennyLane-0.29.1)
- default.qutrit (PennyLane-0.29.1)
- null.qubit (PennyLane-0.29.1)
- lightning.qubit (PennyLane-Lightning-0.30.0)
- strawberryfields.fock (PennyLane-SF-0.29.1)
- strawberryfields.gaussian (PennyLane-SF-0.29.1)
- strawberryfields.gbs (PennyLane-SF-0.29.1)
- strawberryfields.remote (PennyLane-SF-0.29.1)
- strawberryfields.tf (PennyLane-SF-0.29.1)
None

Hey @Rishi, welcome to the forum!

Interesting… I can’t replicate the issue you’re having :thinking:. Here’s my qml.about():

Name: PennyLane
Version: 0.29.1
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: https://github.com/XanaduAI/pennylane
Author: 
Author-email: 
License: Apache License 2.0
Location: /Users/isaac/.virtualenvs/pennylane-strawberryfields/lib/python3.11/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, retworkx, scipy, semantic-version, toml
Required-by: PennyLane-Lightning, PennyLane-SF

Platform info:           macOS-14.4-arm64-arm-64bit
Python version:          3.11.6
Numpy version:           1.23.5
Scipy version:           1.12.0
Installed devices:
- default.gaussian (PennyLane-0.29.1)
- default.mixed (PennyLane-0.29.1)
- default.qubit (PennyLane-0.29.1)
- default.qubit.autograd (PennyLane-0.29.1)
- default.qubit.jax (PennyLane-0.29.1)
- default.qubit.tf (PennyLane-0.29.1)
- default.qubit.torch (PennyLane-0.29.1)
- default.qutrit (PennyLane-0.29.1)
- null.qubit (PennyLane-0.29.1)
- lightning.qubit (PennyLane-Lightning-0.30.0)
- strawberryfields.fock (PennyLane-SF-0.29.1)
- strawberryfields.gaussian (PennyLane-SF-0.29.1)
- strawberryfields.gbs (PennyLane-SF-0.29.1)
- strawberryfields.remote (PennyLane-SF-0.29.1)
- strawberryfields.tf (PennyLane-SF-0.29.1)

I’m using python 3.11, but that shouldn’t matter.

What I recommend here is to create a fresh virtual environment, install the right packages in that environment, and make sure you use that environment when you run your code!

Let me know if that helps :slight_smile:

1 Like

Perfect thanks so much that works perfectly in a new environment!

1 Like

Awesome! Glad I could help :slight_smile: