qiskit-community / qiskit-machine-learning

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

Unable to buil new feature map for QuantumKernel (QSVC) #463

Closed gines-carrascal closed 2 years ago

gines-carrascal commented 2 years ago

Environment

What is happening?

In the moving from Aqua, the superclass "FeatureMap" has disapeared. In the QuantumKernel class you can see: self._feature_map: QuantumCircuit | None = None # type is required by mypy If you use a QuantumCircuit to initialize the kernel, errors appear calling parameters not present in QuantumCircuit (i.e. number of features)

How can we reproduce the issue?

Creating a QuantumKernel with a Parametrized QuantumCircuit as FeatureMap

What should happen?

You should be able to use your own circuit as Feature Map in a Quantum Kernel

Any suggestions?

One of two options: 1.- Implement QuantumKernel to work with a generic QuantumCircuit or 2.- Create a FeatureMap interface with the required parameters and functions to implement, and use this interface in QuantumKernel

adekusar-drl commented 2 years ago

@gines-carrascal Unfortunately, you have not provided enough information to reproduce the problem, but I have a counter-example that works with a parameterized circuit in QuantumKernel. Let me know if it works for you and this is what you were looking for.

import numpy as np
from qiskit import QuantumCircuit
from qiskit.circuit import ParameterVector
from qiskit.providers.aer import AerSimulator

from qiskit_machine_learning.algorithms import QSVC
from qiskit_machine_learning.kernels import QuantumKernel

if __name__ == '__main__':
    qc = QuantumCircuit(2)
    params = ParameterVector("x", 2)
    qc.ry(params[0], 0)
    qc.rx(params[1], 1)
    qk = QuantumKernel(qc, quantum_instance=AerSimulator())

    features = np.random.random(20)
    features = 2 * features - 1
    features = features.reshape(-1, 2)
    labels = np.sign(features[:, 0] * features[:, 1])

    qsvc = QSVC(quantum_kernel=qk)
    qsvc.fit(features, labels)
    score = qsvc.score(features, labels)
    print(score)
adekusar-drl commented 2 years ago

@gines-carrascal did my reply help?

adekusar-drl commented 2 years ago

I'm closing the issue as resolved. Feel free to re-open if the problem persists.

deepquantum88 commented 1 year ago

@adekusar-drl @gines-carrascal

Hi, I try to execute the code mention. But it throws an error. But I am executing it on aer_simulator. Because the syntax has been changed.

ValueEerror: x_vec and class feature map have incompatible dimensions x_vec has 2 dimensions, but feature map has 0.

can you please look in to it and help?

Thanks

adekusar-drl commented 1 year ago

@deepquantum88 The code I post with a minor change in the imports still works perfectly fine. Without looking at the code I can't do much.

deepquantum88 commented 1 year ago

@adekusar-drl I changed the code as:

import numpy as np
from qiskit import QuantumCircuit
from qiskit_machine_learning.algorithms import QSVC
from qiskit_machine_learning.kernels import QuantumKernel

if __name__ == '__main__':
    state = (1 / np.sqrt(10)) * np.array([1, 3])
    num_qubits = 1
    circ = QuantumCircuit(num_qubits)
    circ.prepare_state(state, [0])
    qk = QuantumKernel(circ, quantum_instance= BasicAer.get_backend("qasm_simulator"))

    features = np.random.random(20)
    features = 2 * features - 1
    features = features.reshape(-1, 2)
    labels = np.sign(features[:, 0] * features[:, 1])

    qsvc = QSVC(quantum_kernel=qk)
    qsvc.fit(features, labels)
    score = qsvc.score(features, labels)
    print(score)
adekusar-drl commented 1 year ago

The code won't work for a few reasons:

deepquantum88 commented 1 year ago

@adekusar-drl Thank you for your prompt response. My idea to implement amplitude encoding via state prep as kernel for qsvc.

adekusar-drl commented 1 year ago

Unfortunately it won't work this way. You need another implementation of amplitude encoding that is reversible.

deepquantum88 commented 1 year ago

okay got it. Is there any link which i can follow up for amplitude encoding in qiskit

adekusar-drl commented 1 year ago

There's no suitable for your needs implementation of amplitude encoding in Qiskit. Or I'm not aware of, it is still possible.