qiskit-community / qiskit-machine-learning

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

FidelityQuantumKernel with SamplerV2 (NoiseModel) #852

Closed luib01 closed 3 hours ago

luib01 commented 4 hours ago

What should we add?

I would like to know if there is the possibility to simulate the noise not from the shots but from a noisemodel of a real backend

oscar-wallis commented 3 hours ago

You definitely can already, so the new 0.8 release makes it so that we now support Sampler and Estimator V2s, though we admit, that the documentation isn't where it needs to be right now. So you'll need to use FidelityQuantumKernel (as FidelityStatevectorKernel is a pure statevector implementation) with computeuncompute and a SamplerV2 and you're off to the races. As to how you want to introduce your noise that's up to you, either checkout something like GenericBackend or runtime fake_providers or qiskit_aer with a NoiseModel.

Keep in Mind

You pass your virtual circuit and a custom ComputeUncompute fidelity to the FidelityQuantumKernel. The ComputeUncompute needs the SamplerV2 with whatever run options you wanted, a num_virtual_qubits (the number of qubits in your virtual circuit pre-transpilation) and pass_manager, the pass_manager you would like to use to transpile your circuit onto your backend. I am gonna close the issue with this message but feel free to re-open with any problems your have!

luib01 commented 1 hour ago

thanks for the fast replay. actually i tried it but i get this error, The sampler should be an instance of BaseSampler, but got <class 'qiskit_ibm_runtime.sampler.SamplerV2'> .

Create an empty noise model

    noise_model = NoiseModel.from_backend(self.backend)

    self.composed_circuit=self.qcircuit.compose(self.feature_map,inplace=False)
    #self.composed_circuit=insert_noise(self.composed_circuit,noise_model=noise_model)
    transpiler= generate_preset_pass_manager(backend=self.backend, optimization_level=1)
    transpiled=transpiler.run(self.composed_circuit)

    print("Noise model inserted form: "+self.backend.name)
    #self.job=self.backend.run(transpiled)
    sampler=Sampler(self.backend)
    fidelity=ComputeUncompute(sampler=sampler)
    print('Quantum Circuit:')
    print(self.qcircuit)
    print('FEATURE MAP:')
    print(self.composed_circuit)

    kernel=FidelityQuantumKernel(feature_map=transpiled,fidelity=fidelity,evaluate_duplicates='all')

thi is the code. the importation are the following. from qiskit_ibm_runtime import QiskitRuntimeService, Session, SamplerV2 as Sampler from qiskit_machine_learning.kernels import FidelityQuantumKernel,BaseKernel from qiskit_algorithms.state_fidelities import ComputeUncompute

oscar-wallis commented 1 hour ago

Hi, you need to import ComputeUncompute from qiskit_machine_learning, we have absorbed pieces of qiskit algorithms as this is no longer being maintained. Additionally, when you do use ComputeUncompute make sure you follow the Keep in Mind steps above.

luib01 commented 40 minutes ago

ok thank you, now it works but is really slow due to the fact that use the fidelity Fidelityquantumkernel and the complexity is O(N^2),while for FidelityStatevectorKernel is O(N). there is any possibility to speed up the computation or add the noise to FidelityStatevectorKernel?