Skip to content

Feat - Pegasos Quantum Kernel Alignment - #1081

Open
m-meller wants to merge 8 commits into
stfc:mainfrom
m-meller:feat-pegasos-trainable-qk
Open

m-meller wants to merge 8 commits into
stfc:mainfrom
m-meller:feat-pegasos-trainable-qk

Conversation

@m-meller

Copy link
Copy Markdown
Collaborator

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_rate and perturbations are passed as fixed variables).

  • To be able to cleanly inherit PegasosQSVC I extracted the step update logic into its own function _update_step and 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. *

@m-meller

Copy link
Copy Markdown
Collaborator Author

I'll copy-paste associated comments from the previous review. I'd appreciate @JohnKerf if you could address these. Thanks!

@m-meller m-meller left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in PegasosQKA num_steps is optional, whereas in PegasosQSVC it is not. Passing here None will cause problems.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note depending on other comments:
if num_steps is intended to be optional then it would be good to test it with None value.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

num_steps is no longer optional so this is no longer needed

@m-meller

Copy link
Copy Markdown
Collaborator Author

hi @JohnKerf, I gave you access to my fork so that you can edit and commit to these PRs (#1081, #1082, #1083) and associated branches.

@JohnKerf

JohnKerf commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator

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.

@coveralls

coveralls commented Nov 24, 2026 •

Copy link
Copy Markdown

Coverage Report for CI Build 45447998367

Coverage increased (+0.2%) to 89.954%

Details

  • Coverage increased (+0.2%) from the base build.
  • Patch coverage: 117 of 117 lines across 7 files are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 6102
Covered Lines: 5489
Line Coverage: 89.95%
Coverage Strength: 0.9 hits per line

💛 - Coveralls

@m-meller

Copy link
Copy Markdown
Collaborator Author

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 None (to use precomputed kernel). Here I don't think it applies.

It boils down to a question if a simple default kernel is what users would want/use. It could default to TrainableFidelityQuantumKernel, with default parameters, resulting in 2 qubit ZZFeatureMap, that is not trained (because by default no training parameters are passed). I would assume that the kernel heavily depends on the problem you are solving and characteristics of the dataset that is used (but please let me know if you disagree).

@JohnKerf

Copy link
Copy Markdown
Collaborator

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 None (to use precomputed kernel). Here I don't think it applies.

It boils down to a question if a simple default kernel is what users would want/use. It could default to TrainableFidelityQuantumKernel, with default parameters, resulting in 2 qubit ZZFeatureMap, that is not trained (because by default no training parameters are passed). I would assume that the kernel heavily depends on the problem you are solving and characteristics of the dataset that is used (but please let me know if you disagree).

Hi @m-meller,
I was leaning towards making the kernel required as well. I have made that change. If you're happy then that should be all changes completed for this.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants