qiskit-community / qiskit-machine-learning

Quantum Machine Learning
https://qiskit-community.github.io/qiskit-machine-learning/
Apache License 2.0
645 stars 317 forks source link

VQC inside session #770

Open dhirajmadan1 opened 6 months ago

dhirajmadan1 commented 6 months ago

What should we add?

How to train/ test a VQC model using qiskit runtime sessions? I tried encapsulating under session as below. But it seems to be running on simulator even though a real backend is selected.

service = QiskitRuntimeService(channel="ibm_quantum", token=qAccesKey)
backend = least_busy(service.backends(simulator=False, operational=True, min_num_qubits=args.dim))

with Session(service = service, backend = backend):
        vqc = VQC(feature_map=feature_map,
                           ansatz=ansatz,
                           optimizer=NFT(maxiter=args.blk),
                           callback=callback_graph,
                        )
        vqc.fit(train_data, train_labels)
woodsp-ibm commented 6 months ago

VQC has a sampler parameter

sampler (BaseSampler | None) – an optional Sampler primitive instance to be used by the underlying SamplerQNN neural network. If None is passed then an instance of the reference Sampler will be used.

With None passed (default) as it states it uses the reference Sampler primitive from Qiskit. To use a real backend you need to instantiate and explicitly pass a runtime Sampler. As your code is above the Session is unused since that is only meaningful when used with the runtime primitives.

dhirajmadan1 commented 6 months ago

Thanks a lot @woodsp-ibm . I have made the above change i.e. adding a sampler. Now I am getting this exception:- qiskit_machine_learning.exceptions.QiskitMachineLearningError: 'Sampler job failed.'

woodsp-ibm commented 6 months ago

Its not a lot to go on. It should work. Maybe add your updated code here just in case. You can also look at the jobs in your account and see if there is any further error information listed there.

edoaltamura commented 1 month ago

@dhirajmadan1 does the issue persist when running VQC in a Session? I think your implementation as you wrote above should work. Please, feel free to describe more of the context of your problem, if necessary, so that we can further assist you.