CQCL / lambeq

A high-level Python library for Quantum Natural Language Processing
https://docs.quantinuum.com/lambeq/
Apache License 2.0
455 stars 111 forks source link

real life usage #6

Closed nlpirate closed 2 years ago

nlpirate commented 2 years ago

I'm trying to replicate the binary classifier that is described in the documentation (step 4, complete use case). The accuracy is much lower than what is shown in the documentation, using the training and testing datasets provided by Lorenz et al., 2021 QNLP .

As suggested, I also tried to run the scripts proposed on their github, in particular rp_task_simulation.ipynb using google colab but I am getting the following error message back

UnfilteredStackTrace                      Traceback (most recent call last)

<ipython-input-20-0fff64bd88f1> in <module>()
      4     start = time()
----> 5     print('Cost: ', get_cost_jit(rand_unshaped_pars))
      6     print('Time taken for this iteration: ', time()-start)

32 frames

UnfilteredStackTrace: jax._src.errors.TracerArrayConversionError: The numpy.ndarray conversion method __array__() was called on the JAX Tracer object Traced<ShapedArray(float32[])>with<DynamicJaxprTrace(level=0/1)>
While tracing the function get_cost at <ipython-input-18-b448627ced96>:8 for jit, this concrete value was not available in Python because it depends on the value of the argument 'unshaped_params'.
See https://jax.readthedocs.io/en/latest/errors.html#jax.errors.TracerArrayConversionError

The stack trace below excludes JAX-internal frames.
The preceding is the original exception that occurred, unmodified.

--------------------

The above exception was the direct cause of the following exception:

TracerArrayConversionError                Traceback (most recent call last)

/usr/local/lib/python3.7/dist-packages/discopy/quantum/gates.py in array(self)
    410     def array(self):
    411         half_theta = self.modules.pi * self.phase
--> 412         sin, cos = self.modules.sin(half_theta), self.modules.cos(half_theta)
    413         return Tensor.np.array([[cos, -1j * sin], [-1j * sin, cos]])
    414 

TracerArrayConversionError: The numpy.ndarray conversion method __array__() was called on the JAX Tracer object Traced<ShapedArray(float32[])>with<DynamicJaxprTrace(level=0/1)>
While tracing the function get_cost at <ipython-input-18-b448627ced96>:8 for jit, this concrete value was not available in Python because it depends on the value of the argument 'unshaped_params'.
See https://jax.readthedocs.io/en/latest/errors.html#jax.errors.TracerArrayConversionError

I have already set the variable IMPORT_JAX = True in discopy's config.py. any suggestions on how to fix it?

y-richie-y commented 2 years ago

Hi @nlpirate, which version of discopy are you using? The README of that repository requires discopy 0.3.5.

dimkart commented 2 years ago

This issue was resolved in the "QNLP-in-Practice" paper repository: https://github.com/CQCL/qnlp_lorenz_etal_2021_resources/issues/2.