CQCL / lambeq

A high-level Python library for Quantum Natural Language Processing
https://cqcl.github.io/lambeq-docs
Apache License 2.0
450 stars 108 forks source link

Trainnig models is IMPOSSIBLE !! #75

Closed yousrabou closed 1 year ago

yousrabou commented 1 year ago

Dear, Lambeq team.

I'm unable to train models (spider, cups, etc.) while this error occurs frequently!!

---------------------------------------------------------Code-------------------------------------------------------------------------------------

     49 val_dataset= Dataset(val_circuits, val_labels, shuffle=False)
---> 50 Spiders_trainer.fit(train_dataset, val_dataset, logging_step=12)

---------------------------------------------------------Error----------------------------------------------------------------------------------
8 frames
[/usr/local/lib/python3.9/dist-packages/lambeq/training/quantum_trainer.py](https://localhost:8080/#) in fit(self, train_dataset, val_dataset, evaluation_step, logging_step)
    197             val_dataset: Optional[Dataset] = None,
    198             evaluation_step: int = 1,
--> 199             logging_step: int = 1) -> None:
    200 
    201         self.model._training = True

[/usr/local/lib/python3.9/dist-packages/lambeq/training/trainer.py](https://localhost:8080/#) in fit(self, train_dataset, val_dataset, evaluation_step, logging_step)
    373                     x, y_label = batch
    374                     y_hat, loss = self.training_step(batch)
--> 375                     if (self.evaluate_on_train
    376                             and self.evaluate_functions is not None):
    377                         for metr, func in self.evaluate_functions.items():

[/usr/local/lib/python3.9/dist-packages/lambeq/training/quantum_trainer.py](https://localhost:8080/#) in training_step(self, batch)
    161         -------
    162         Tuple of np.ndarray and float
--> 163             The model predictions and the calculated loss.
    164 
    165         """

[/usr/local/lib/python3.9/dist-packages/lambeq/training/spsa_optimizer.py](https://localhost:8080/#) in backward(self, batch)
    139         xminus = self.project(x - self.ck * delta)
    140         self.model.weights = xminus
--> 141         y1 = self.model(diagrams)
    142         loss1 = self.loss_fn(y1, targets)
    143         if self.bounds is None:

[/usr/local/lib/python3.9/dist-packages/lambeq/training/quantum_model.py](https://localhost:8080/#) in __call__(self, *args, **kwargs)
    144     @abstractmethod
    145     def forward(self, x: list[Diagram]) -> Any:
--> 146         """Compute the forward pass of the model using
    147         `get_model_output`
    148 

[/usr/local/lib/python3.9/dist-packages/lambeq/training/tket_model.py](https://localhost:8080/#) in forward(self, x)
    131 
    132         """
--> 133         return self.get_diagram_output(x)

[/usr/local/lib/python3.9/dist-packages/lambeq/training/tket_model.py](https://localhost:8080/#) in get_diagram_output(self, diagrams)
    100 
    101         lambdified_diagrams = [self._make_lambda(d) for d in diagrams]
--> 102         tensors = Circuit.eval(
    103             *[diag_f(*self.weights) for diag_f in lambdified_diagrams],
    104             **self.backend_config,

[/usr/local/lib/python3.9/dist-packages/discopy/quantum/circuit.py](https://localhost:8080/#) in eval(self, backend, mixed, contractor, *others, **params)
    286             return type(box)(box.dom, box.cod, box.array + 0j)
    287         circuits = [circuit.to_tk() for circuit in (self, ) + others]
--> 288         results, counts = [], circuits[0].get_counts(
    289             *circuits[1:], backend=backend, **params)
    290         for i, circuit in enumerate(circuits):

[/usr/local/lib/python3.9/dist-packages/discopy/quantum/tk.py](https://localhost:8080/#) in get_counts(self, backend, *others, **params)
    131         if compilation is not None:
    132             for circuit in (self, ) + others:
--> 133                 compilation.apply(circuit)
    134         handles = backend.process_circuits(
    135             (self, ) + others, n_shots=n_shots, seed=seed)

**ValueError: Non-unitary matrix passed to two_qubit_canonical**
Thommy257 commented 1 year ago

Hi, can you provide some example circuits that you try to execute? Also, which version of lambeq are you using?

yousrabou commented 1 year ago

Lambeq Version: 0.3.0

i'm trying to execute example from the lambeq documentation using RP dataset, which mean that i'am trying to execute _rp_traincircuits and _rp_testcircuits

Thommy257 commented 1 year ago

Can you try to downgrade to 0.2.8 and try again?

Thommy257 commented 1 year ago

Unfortunately, I can't replicate the error. Can you start from a fresh python environment please?

Also, can you please upgrade pytket and pytket-qiskit. If that does not help, pleas provide some information about your system (OS, etc.).

pip install --upgrade pytket pytket-qiskit
yousrabou commented 1 year ago

Thank you, Thom.

When I downgrade the lambeq version to 0.2.8 with pytket and pytket-qiskit (1.13.2 & 0.37.1) , the discocat or other lambeq model is well trained on the RP dataset, either on my local machine or on Google Colab virtual machine.

BUT, when I use the least version of lambeq (0.3.0), I get the same issue both on my local system and Google Colab.

I'm hoping that this issue will be fixed soon so that we will be able to benefit of new functions and modules.

dimkart commented 1 year ago

@yousrabou Still not possible for us to reproduce this error, looks it's something very specific to your code. We'll close this issue, but feel free to re-open if there is more news on this.