Repository navigation
Conversation
…Kernel Alignment stfc#1069.
|
I'll copy-paste associated comments from the previous review. I'd appreciate @JohnKerf if you could address these. Thanks! |
m-meller
left a comment
There was a problem hiding this comment.
hi @JohnKerf, thanks for this PR. I did review it and overall approach and logic looks good to me. Tests (except one -- which is a known issue), linting and style also pass.
There are few comments that require further action/tests, to make sure that the introduced changes do not break existing code and to consider some edge cases.
| quantum_kernel: ( | ||
| TrainableFidelityQuantumKernel | TrainableFidelityStatevectorKernel | None | ||
| ) = None, | ||
| C: float = 1000.0, |
There was a problem hiding this comment.
Is this a typo? in PegasosQSVC default parameters are C: float = 1.0 and num_shots: int = 1000, whereas here C: float = 1000.0 and num_shots is optional (generally it shouldn't).
There was a problem hiding this comment.
I looked at the code on which you based your implementation and now I understand the numbers here. Note that our PegasosQSVC differs in those parameters so it probably has to be adjusted, as highlighted in other comments.
There was a problem hiding this comment.
I have updated the default params to be inline with the current QSVC defaults. This included making num_steps not optional.
| super().__init__( | ||
| quantum_kernel=quantum_kernel, | ||
| C=C, | ||
| num_steps=num_steps, |
There was a problem hiding this comment.
in PegasosQKA num_steps is optional, whereas in PegasosQSVC it is not. Passing here None will cause problems.
There was a problem hiding this comment.
num_steps is no longer optional so this should no longer be an issue.
| parameter_dict[parameter] = value | ||
|
|
||
| full_array = np.zeros((x_vec.shape[0], self._num_features + self._num_training_parameters)) | ||
| for i, x in enumerate(x_vec): |
There was a problem hiding this comment.
original _parameter_array updates self._parameter_dict which is then used in parameter_values getter (line 85). Here it is no longer updated (although it might as parameter_dict is a shallow copy). Please review if the change does not break parameter_values method.
There was a problem hiding this comment.
For the current QKA implementation we dont actual ever update the kernel classes self._parameter_dict. The param history is handled in the QKA class with self._support_thetas storing the theta history and self._theta sotring the current set of params.
When we call _parameter_array() we essenatially just create a copy of the dict to temporarily assign paramters for the kernel evaluation, leaving the original dict unchanged.
I have added a test to ensure the copy doesnt overwrite the classes dict but strictly speaking this is not neccasary since the params are actually handled in the QKA class.
| "before calling `.evaluate()`." | ||
| ) | ||
| else: | ||
| if len(parameter_values) != self._num_training_parameters: |
There was a problem hiding this comment.
A similar check to one in lines 134-140 might be required here, as it seems to me that if parameter_values are of type Sequence[ParameterValueType] then ParameterValueType can be unbound (this type is union of float and ParameterExpression)
There was a problem hiding this comment.
Added addional check on the individual param values and added a test case for this.
| def test_qka(self): | ||
| """Test PegasosQKA""" | ||
|
|
||
| pegasos_qka = PegasosQKA(quantum_kernel=self.qkernel, C=1000, num_steps=self.tau) |
There was a problem hiding this comment.
Note depending on other comments:
if num_steps is intended to be optional then it would be good to test it with None value.
There was a problem hiding this comment.
num_steps is no longer optional so this is no longer needed
|
Hi @m-meller I have addressed your comments. One thing I noticed when working on the PegasosQKA inits is that currently the kernel is optional but we do not create a default when it is None (like in QSVC). Doing this isnt as trivial in the QSVC case since wed need to provide a feature map, trainable params etc. We can either make it not optional, or we can add some more logic to create a simple one on init, i guess similar to something done in the test cases? Let me know your thoughts. |
Coverage Report for CI Build 45447998367Coverage increased (+0.2%) to 89.954%Details
Uncovered ChangesNo uncovered changes found. Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
|
Hi @JohnKerf, thanks for updating everything. Overall lgtm. Regarding PegasosQKA inits, I would make the kernel a required parameter. My understanding that in PegasosQSVC there is a clear use case for passing It boils down to a question if a simple default kernel is what users would want/use. It could default to |
Hi @m-meller, |
Note
Recreated contribution by @JohnKerf which implements Pegasos Quantum Kernel Alignment #1069.
All credits for providing this solution go to @JohnKerf. Thank you John.
The fork containing proposed changes was deleted before the changes were merged.
Because of this, PR #1069 was closed automatically, which was not intended.
For clarity, below is the description from #1069:
Summary
Details and comments
The majority of the PegasosQKA code follows from https://zenodo.org/records/7804477 but has been stripped back to make the implementation more streamlined.
Decided not to include the self._blasphemy logic (computing kernel without historical thetas) as this is not how the algorithm is intended to be ran.
Decided at this time not to include a callback however this could be a possible future feature.
Precomputed kernel functionality is not needed for QKA since by design we change the kernel throught the algorithm.
Decided warm starting and calibration/recalibration is not needed. It seems calibration was not used in the paper and adds a lot of extra bloat to the algorithm so I thought it was best to not include it (
learning_rateandperturbationsare passed as fixed variables).To be able to cleanly inherit PegasosQSVC I extracted the step update logic into its own function
_update_stepand also the kernel evaulation logic into_evaluate_kernel. This meant we can cleanly overide these in the PegasosQKA class and reuse the bulk of the QSVC algorithm.Previously the trainable kernels did not have the option to set seperate paramters for the left and right feature maps. The paper logic introduced a new pseudo kernel but to avoid adding this I have introduced the ability to run and evaluate the trainable kernels with the seperate left and right parameters.
AI Disclosure - *ChatGPT was used in this work primarily to help with producing tests and also helped me better understand the PegasosQSVC/PegasosQKA algorithms. The main body of code already existed from the paper source code and all implementation/checks were done by myself. I fully understand the code I have produced but I will note that during the process ChatGPT did help verify/clarify some of the logic I was imlpementing. *