qiboteam / qibo

A full-stack framework for quantum computing.
https://qibo.science
Apache License 2.0
293 stars 60 forks source link

Unwanted error logged when loading a circuit from json #1519

Open stavros11 opened 4 hours ago

stavros11 commented 4 hours ago

The following script

import json
from qibo import Circuit, gates

c = Circuit(2)
c.add(gates.CZ(0, 1))

with open("test.json", "w") as file:
    json.dump(c.raw, file)

with open("test.json", "r") as file:
    cd = json.load(file)
    cl = Circuit(2)
    cl = c.from_dict(cd)

produces this output

[Qibo 0.2.14|ERROR|2024-11-08 14:05:50]: Cannot use `controlled_by` method on gate <qibo.gates.gates.CZ object at 0x7f13cd1a1a60> because it is already controlled by (0,).

It does not fail, and the loaded circuit cl is usable, so I would not consider it very urgent, however it is weird that the error is printed. This is most likely due to some try-except in the loader.

alecandido commented 3 hours ago

Yes, the solution should be similar to the one in https://github.com/qiboteam/qibo/pull/1505: if you know that it's going to be used in a try ... except, you should avoid usage of raise_error() in favor of a plain raise.

raise_error() will first log, then raise. So, if it already logged, it is impossible to take it back, even if the error gets subsequently caught.