Description
Hello,
... The more I discover this lib the more I think it's a wonderful tools for hybrid quantum classical machine learning ! ... Thanks for that !...
I try to see if there is a compatibility between torchquantum 's QLayer modules (for future complex torch models) and pytorch training/inference and a test deployment with ibm runtime jobs... (with Sampler, Estimator, Session objects...)
Is there is a example can I find for that ?
I've already try to define a QLayer with all the recomandations described in the torch quantum example "convert_tq_qiskit" with the "tq.static_support" decorator in the forward function...
"tq2qiskit" function works with it !...
But is there really working with trainable params of RX, RY etc... gates ?)
And most important : it seems like the measurement function (like tq.MeasureAll(), expval_joint_sampling function...) are not seen by the "tq2qiskit" converter function... (see error message during the test job at the end of my little test)
My little test :
——————————————————————————
class QLayer(tq.QuantumModule):
def __init__(self, n_qbits):
super().__init__()
self.n_wires = n_qbits
self.encoder = tq.GeneralEncoder(
[{'input_idx': [i], 'func': 'rx', 'wires': [i]} for i in range(self.n_wires)])
self.rx_list = [tq.RX(has_params=True, trainable=True) for _ in range(self.n_wires)]
self.ry_test = tq.RY(has_params=True, trainable=True)
#self.measure = tq.MeasureAll(tq.PauliZ)
@tq.static_support
def forward(self, q_device, x):
self.encoder(q_device, x)
for k in range(self.n_wires):
self.rx_list[k](q_device, wires=k)
self.ry_test(q_device, wires=0)
for k in range(self.n_wires):
if k==self.n_wires-1:
tqf.cnot(q_device, wires=[k, 0], static=self.static_mode, parent_graph=self.graph)
else:
tqf.cnot(q_device, wires=[k, k+1], static=self.static_mode, parent_graph=self.graph)
q_device = q_device.bfloat16()
output = expval_joint_sampling(q_device, 'ZXXX', n_shots=1024)
#output = tq.MeasureAll(tqf.PauliZ)(q_device)
return output
q_layer = QLayer(4)
x = torch.tensor(np.random.rand(1, 4), dtype=torch.float32)
q_dev = tq.QuantumDevice(n_wires=4, device="cpu", bsz=x.shape[0])
circuit = tq2qiskit(q_device=q_dev,m=q_layer, x=x, draw=True) # ----> IT WORKS !
sampler = Sampler(backend=backend, options=options)
job = sampler.run([circuit])
Error : (...)
ValueError: The 0-th circuit does not have any classical bit. Sampler requires classical bits, plus measurements on the desired qubits.
Here this is a test with a Sampler funtion but I was thinking to use Session, etc... to "package" a complete script training with a big tq.QuantumModule...
Perhaps, currently, there is another (very different ?) mean to do that ?...