qiboteam / qibo

A framework for quantum computing
https://qibo.science
Apache License 2.0
288 stars 60 forks source link

`Gate.parameters` as a single backend array #1466

Open BrunoLiegiBastonLiegi opened 2 weeks ago

BrunoLiegiBastonLiegi commented 2 weeks ago

Currently the parameters of a ParametrizedGate are returned as a tuple which is perfectly fine when you set them as simple floats

from qibo import gates

rz = gates.RZ(0, 0.)
u3 = gates.U3(0, 1., 2., 3.)
print(rz.parameters)
# this is (0.,)
print(u3.parameters)
# this is (1., 2., 3.)

However, in some cases, you might want them to be arrays of a particular backend (for instance this is needed for backpropagation through pytorch), and you can set the parameters manually:

import numpy as np

rz.parameters = np.array([0.])
u3.parameters = np.array([1., 2., 3.])

if you try to recover them, though, you'll find them split and returned as a tuple again:

print(rz.parameters)
# this is (array([0]),) 
print(u3.parameters)
# this is (array([1.]), array([2.]), array([3.]))

which begs for further unneeded machinery to restore the native representation. Furthermore, for the sake of not riskying to brake the trace that torch, tensorflow and jax do, it would be better to cut to the bone any operation that is performed on the parameters that is not really needed in the parameters property (as well as in the Circuit.get/set_parameters).

alecandido commented 2 weeks ago

https://github.com/qiboteam/qibo-core/issues/22