-
Notifications
You must be signed in to change notification settings - Fork 375
Description
Environment
- Qiskit Machine Learning version: 0.8.2/0.8.3
- Python version: 3.10
- Operating system: MacOS
What is happening?
While creating a custom feature map (FM), I was mistakenly using all the parameters as trainable parameters.
In the TrainableFidelityQuantumKernel
class this would mean overriding the value of num_features
.
qiskit-machine-learning/qiskit_machine_learning/kernels/trainable_fidelity_quantum_kernel.py
Lines 94 to 95 in ef44f5a
# override the num of features defined in the base class | |
self._num_features = self.feature_map.num_parameters - self._num_training_parameters |
So, with my custom FM,
num_features = 0, #since
self.feature_map.num_parameters = self._num_training_parameters
Next step in the program is to call the evaluate function in the FidelityQuantumKernel
class:
qiskit-machine-learning/qiskit_machine_learning/kernels/fidelity_quantum_kernel.py
Line 100 in ef44f5a
def evaluate(self, x_vec: np.ndarray, y_vec: np.ndarray | None = None) -> np.ndarray: |
And, from here, the code logic goes into the _validate_inputs
method in the BaseKernel
class, which does this,
qiskit-machine-learning/qiskit_machine_learning/kernels/base_kernel.py
Lines 110 to 120 in ef44f5a
if x_vec.shape[1] != self._num_features: | |
# before raising an error we try to adjust the feature map | |
# to the required number of qubit. | |
try: | |
self._feature_map.num_qubits = x_vec.shape[1] | |
except AttributeError as a_e: | |
raise ValueError( | |
f"x_vec and class feature map have incompatible dimensions.\n" | |
f"x_vec has {x_vec.shape[1]} dimensions, " | |
f"but feature map has {self._feature_map.num_parameters}." | |
) from a_e |
Now, the logic in this code checks if x_vec.shape[1] != self._num_features
but returns a different ValueError.
With my custom FM, x_vec,shape[1]=8, self._num_features=0, and feature_map.num_parameters=8
. However, the ValueError incorrectly returns this,
...but feature map has {self._feature_map.num_parameters}.
This translates to the error that I have been receiving: x_vec and class feature map have incompatible dimensions.
x_vec has 8 dimensions, but feature map has 8.
Instead the ValueError should be,
...but feature map has {self._num_features}.
There is an error with my custom FM and an incorrect ValueError made it difficult to debug. I think the ValueError should be corrected.
How can we reproduce the issue?
Create a custom feature map with all the parameters as trainable parameters.
You can also refer to this slack discussion thread between me and @woodsp-ibm about this issue: https://qiskit.slack.com/archives/CB6C24TPB/p1745436047257769
What should happen?
ValueError must be corrected to reflect the logic it checks, i.e., if x_vec.shape[1] != self._num_features
raise ValueError(
f"x_vec and class feature map have incompatible dimensions.\n"
f"x_vec has {x_vec.shape[1]} dimensions, "
f"but feature map has {self._num_features}."
) from a_e
Any suggestions?
No response