Closed gines-carrascal closed 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)
@gines-carrascal did my reply help?
I'm closing the issue as resolved. Feel free to re-open if the problem persists.
@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
@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.
@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)
The code won't work for a few reasons:
prepare_state()
which is not irreversible, while quantum kernel computes <U_dg, U>. (num_samples, 2)
and is not compatible with the one qubit circuit. @adekusar-drl Thank you for your prompt response. My idea to implement amplitude encoding via state prep as kernel for qsvc.
Unfortunately it won't work this way. You need another implementation of amplitude encoding that is reversible.
okay got it. Is there any link which i can follow up for amplitude encoding in qiskit
There's no suitable for your needs implementation of amplitude encoding in Qiskit. Or I'm not aware of, it is still possible.
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