Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FidelityStatevectorKernel #6

Open
gcattan opened this issue Nov 27, 2023 · 10 comments
Open

FidelityStatevectorKernel #6

gcattan opened this issue Nov 27, 2023 · 10 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@gcattan
Copy link

gcattan commented Nov 27, 2023

Hello,

I am testing qiskit-symb with FidelityStatevectorKernel, but encounter the following error: 'Statevector' object has no attribute 'data'

from qiskit_symb.quantum_info import Statevector
quantum_kernel = FidelityStatevectorKernel(feature_map=..., statevector_type=Statevector)
@SimoneGasperini
Copy link
Owner

Hi @gcattan and thank you for posting this!
I'm sorry but I'm not even sure how to reproduce this issue. I tried to run the following code and it works for me.

from qiskit.circuit.library import ZZFeatureMap
from qiskit_machine_learning.kernels import FidelityStatevectorKernel
from qiskit_symb.quantum_info import Statevector

qc = ZZFeatureMap(feature_dimension=3).decompose()
quantum_kernel = FidelityStatevectorKernel(feature_map=qc, statevector_type=Statevector)

Maybe your error is raised when you call the FidelityStatevectorKernel.evaluate method?

@gcattan
Copy link
Author

gcattan commented Dec 1, 2023

Hi, sorry, I just put here half of the example.
Let me know if you can access this link: https://colab.research.google.com/drive/1gB5FY4NfWBIOhGbgWkeUk43nCsOcRivg?usp=sharing

@SimoneGasperini
Copy link
Owner

Hello again! Yes, I can access the notebook and I see your issue now.

The short answer would be that you can't plug in the symbolic Statevector type in the construction of your FidelityStatevectorKernel like you're trying to do in your code. This is because the qiskit_symb.quantum_info.Statevector is internally different compared to the standard qiskit.quantum_info.Statevector. For instance, the latter has an attribute called 'data' (that your QSVC model is trying to access) but the former does NOT provide the corresponding member with the same name.

Even if this feature is not directly provided in qiskit_symb, I post here a simple snippet of code (far from being optimal in terms of performance) to workaround this problem and achieve exactly what you want to do:

import numpy as np
from qiskit_symb.quantum_info import Statevector

class SymbFidelityStatevectorKernel:
    def __init__(self, circuit):
        self.circuit = circuit
        self.function = Statevector(circuit).to_lambda()

    def evaluate(self, x_vec, y_vec=None):
        if y_vec is None:
            y_vec = x_vec
        shape = (len(x_vec), len(y_vec))
        kernel_matrix = np.empty(shape=shape)
        for i, x in enumerate(x_vec):
            for j, y in enumerate(y_vec):
                fidelity = abs(self.function(*x, *y)[0, 0]) ** 2
                kernel_matrix[i, j] = fidelity
        return kernel_matrix

Once you have defined this new SymbFidelityStatevectorKernel object, you can use it to build your QSVC model:

from qiskit.circuit.library import ZZFeatureMap
from qiskit_machine_learning.algorithms import QSVC

fm1 = ZZFeatureMap(feature_dimension=3, parameter_prefix="a")
fm2 = ZZFeatureMap(feature_dimension=3, parameter_prefix="b")
circuit = fm1.compose(fm2.inverse()).decompose()

quantum_kernel = SymbFidelityStatevectorKernel(circuit=circuit)
qsvc = QSVC(quantum_kernel=quantum_kernel)

# Get your data and fit the model
# ...

@gcattan
Copy link
Author

gcattan commented Dec 4, 2023

Looks really nice! Thank you for the help.
I am not sure. But do you think we could provide a property "data" like this to the Statevector inside this library:

@property
  def data(self):
    return self._data

@SimoneGasperini
Copy link
Owner

Yes, making the 'data' attribute accessible as a property looks reasonable (I was actually trying to do something similar). However, this would not be enough to solve your issue with the QSVC model here.
To make qiskit-symb features fully compatible with the QSVC and possibly more qiskit_machine_learning models, a little bit more work is required. If you have any idea and want to give it a try, feel free to start working on it and open a PR as soon as you have something working so we can further discuss ;)

@SimoneGasperini SimoneGasperini added enhancement New feature or request help wanted Extra attention is needed labels Dec 6, 2023
@gcattan
Copy link
Author

gcattan commented Sep 25, 2024

Hi @SimoneGasperini
I recently made some testing with real data on this, to improve the simulation time in our library.

The feature map contained 5 qubits.

The SymbFidelityStatevectorKernel ran in 35s
The FidelityStatevectorKernel in 17s,
and the FidelityQuantumKernel in 572s.

Do you have may be some tips to optimize the code above?

The main complexity comes from this part here:

image

@gcattan
Copy link
Author

gcattan commented Sep 26, 2024

Please, do not hesitate to comment on the PR above if you have some time :)

@SimoneGasperini
Copy link
Owner

SimoneGasperini commented Sep 30, 2024

Hi @gcattan!
First of all, thank you for your work on the qiskit-symb integration within your project.
I took a quick look at the pyRiemann-qiskit repository and it seems interesting.

Concerning this issue here about the symbolic evaluation performance, I got few new ideas for potential improvements: in particular, I would like to integrate into my project the symbolic Quantum Mechanics module available in sympy, which offers an interesting set of tools that may lead to a significant speed-up for several qiskit-symb features.

Unfortunately, this idea probably requires quite some time and I can't immediately deal with this to give you an exhaustive answer. However, I will go back to work during the next few weeks and I have good reasons to believe that the planned upgrades will be beneficial also for your quantum feature map evaluation use-case.

Thank you for your patience and let's keep in touch!

@gcattan
Copy link
Author

gcattan commented Sep 30, 2024

Thanks for the feedback!

Looking forward to this new integration with the Quantum Mechanics module, that could be a great idea :)

@SimoneGasperini
Copy link
Owner

SimoneGasperini commented Nov 4, 2024

Hello again @gcattan!

I recently managed to complete the integration of the sympy Quantum Mechanics module into qiskit-symb and I think it should already be possible to appreciate an improvement in the symbolic evaluation performance compared to the previous version you used in your testing.

If interested, I would suggest you to run all your tests again as soon as you find some time. That would be great also for me to understand whether the effort I put into this code optimization led to an actual benefit in the performance.
To get the new version (release v0.3.0) of qiskit-symb (including the new implementation of the sympy-based evaluation engine), make sure to pip install again the package with the upgrade option flag:

pip install --upgrade qiskit-symb

Let me know how it goes!

PS: the interface is exactly the same so you should be able to run your code with no change at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants