Error in Expectation value of the FockstateTF backend

There is an error in using quad_expectation in the FockstateTF backend

import strawberryfields as sf
from strawberryfields import ops

eng = sf.Engine("tf", backend_options={"cutoff_dim": 10})
prog = sf.Program(1)
with prog.context as q:
    Xgate(1) | q[0]

state= eng.run(prog).state
exp = state.quad_expectation([0])

The error message


InvalidArgumentError                      Traceback (most recent call last)
Cell In[36], line 10
      7     Xgate(1) | q[0]
      9 state= eng.run(prog5).state
---> 10 exp = state.quad_expectation([0])

File ~\anaconda3\Lib\site-packages\strawberryfields\backends\tfbackend\states.py:383, in FockStateTF.quad_expectation(self, mode, phi, **kwargs)
    380 if not self.batched:
    381     rho = tf.expand_dims(rho, 0)  # add fake batch dimension
--> 383 flat_rho = tf.reshape(rho, [-1, self.cutoff_dim**2])
    384 flat_quad = tf.reshape(quad, [1, self.cutoff_dim**2])
    385 flat_quad2 = tf.reshape(quad2, [1, self.cutoff_dim**2])

File ~\anaconda3\Lib\site-packages\tensorflow\python\ops\weak_tensor_ops.py:88, in weak_tensor_unary_op_wrapper.<locals>.wrapper(*args, **kwargs)
     86 def wrapper(*args, **kwargs):
     87   if not ops.is_auto_dtype_conversion_enabled():
---> 88     return op(*args, **kwargs)
     89   bound_arguments = signature.bind(*args, **kwargs)
     90   bound_arguments.apply_defaults()

File ~\anaconda3\Lib\site-packages\tensorflow\python\util\traceback_utils.py:153, in filter_traceback.<locals>.error_handler(*args, **kwargs)
    151 except Exception as e:
    152   filtered_tb = _process_traceback_frames(e.__traceback__)
--> 153   raise e.with_traceback(filtered_tb) from None
    154 finally:
    155   del filtered_tb

File ~\anaconda3\Lib\site-packages\tensorflow\python\eager\execute.py:53, in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     51 try:
     52   ctx.ensure_initialized()
---> 53   tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
     54                                       inputs, attrs, num_outputs)
     55 except core._NotOkStatusException as e:
     56   if name is not None:

InvalidArgumentError: {{function_node __wrapped__Reshape_device_/job:localhost/replica:0/task:0/device:CPU:0}} Input to reshape is a tensor with 1 values, but the requested shape requires a multiple of 100 [Op:Reshape]

How to solve this problem?

Hi @hhtzds, welcome to the Forum!

You can solve this issue by removing the square brackets in the last line. So you specify just 0 instead of [0].

Enjoy using Strawberry Fields!