quantum icon indicating copy to clipboard operation
quantum copied to clipboard

Crash when trying to run tfq.layers

Open calmacx opened this issue 4 years ago • 5 comments

Hi,

I'm trying to follow some tutorial examples and came across the same problem seen in #160. I had followed the instructions to update modules (though that seemed to be a windows fix) but it still gives me the same problem.

Mysetup

  • OS == macOS Catalina 10.15.7
  • python == 3.8.0
  • tf == 2.3.1
  • tfg == 0.5.0 (installed via pip tfq-nightly)
  • cirq == 0.9.1

Error

~/.pyenv/versions/3.8.0/lib/python3.8/site-packages/tensorflow/python/framework/ops.py in raise_from_not_ok_status(e, name)
   6841   message = e.message + (" name: " + name if name is not None else "")
   6842   # pylint: disable=protected-access
-> 6843   six.raise_from(core._status_to_exception(e.code, message), None)
   6844   # pylint: enable=protected-access
   6845 

~/.pyenv/versions/3.8.0/lib/python3.8/site-packages/six.py in raise_from(value, from_value)

InvalidArgumentError: TypeError: cannot pickle 'module' object
Traceback (most recent call last):
...
File "/Users/calummacdonald/.pyenv/versions/3.8.0/lib/python3.8/multiprocessing/reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)

TypeError: cannot pickle 'module' object

 [Op:EagerPyFunc]

Code I'm trying to run

q = cirq.GridQubit(0, 0)
(a, b, c) = sp.symbols("a b c")
circuit = cirq.Circuit(
    cirq.rz(a)(q),
    cirq.rx(b)(q),
    cirq.rz(c)(q),
    cirq.rx(-b)(q),
    cirq.rz(-a)(q)
)
measurement = [cirq.X(q), cirq.Y(q), cirq.Z(q)]
outputs = tfq.layers.PQC(
    circuit,
    measurement,
    repetitions=5000,
    backend=cirq.Simulator(),
    differentiator=tfq.differentiators.ParameterShift())
quantum_data = tfq.convert_to_tensor([
    cirq.Circuit(),
    cirq.Circuit(cirq.X(q))
])
res = outputs(quantum_data)
res

calmacx avatar Nov 11 '20 13:11 calmacx

with the default backend, aka without backend=cirq.Simulator(), this will run

calmacx avatar Nov 11 '20 14:11 calmacx

Hmmm looks like either the windows fix didn't translate well over to Mac or over the course of releases from 0.3.1 -> 0.5.0dev we managed to re-introduce the problem somewhere. I'll take a look into this.

I will also mention that leaving backend=None is definitely the preferred method of use when doing simulations with TensorFlow Quantum. It is the fastest and most reliable going directly through to qsim. We initially wanted to be able to specify backend in order to support using backends like this which are not super fast (since you're already going over the internet and through a lot of other software layers). It turns out that you can use other simulation backends, but that was really just a nice bonus and they are unlikely to be any faster than the default C++ backend that is provided.

Going forward we do plan to incorporate noisy simulation capabilities into qsim so that our fast C++ backends can support cirq.Circuits that contain noise channels. For more info on this there is the discussion here: https://github.com/tensorflow/quantum/issues/250

MichaelBroughton avatar Nov 12 '20 03:11 MichaelBroughton

Thanks a lot @MichaelBroughton and thanks for the extra info on why this is even an option!

calmacx avatar Nov 12 '20 08:11 calmacx

Any updates on this issue @calmacx or can it be closed?

lockwo avatar Aug 24 '22 18:08 lockwo

hey @lockwo - I really don't remember this, so it didn't become a bigger issue, so please close - many thanks

calmacx avatar Aug 24 '22 18:08 calmacx