-
Notifications
You must be signed in to change notification settings - Fork 2
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
Comments
Hi @gcattan and thank you for posting this! 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 |
Hi, sorry, I just put here half of the example. |
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 Even if this feature is not directly provided in 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 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
# ... |
Looks really nice! Thank you for the help.
|
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 |
Hi @SimoneGasperini The feature map contained 5 qubits. The Do you have may be some tips to optimize the code above? The main complexity comes from this part here: |
Please, do not hesitate to comment on the PR above if you have some time :) |
Hi @gcattan! 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 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! |
Thanks for the feedback! Looking forward to this new integration with the Quantum Mechanics module, that could be a great idea :) |
Hello again @gcattan! I recently managed to complete the integration of the 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.
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. |
Hello,
I am testing
qiskit-symb
withFidelityStatevectorKernel
, but encounter the following error: 'Statevector' object has no attribute 'data'The text was updated successfully, but these errors were encountered: