Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Qiskit wrapper #2

Merged
merged 152 commits into from
Nov 16, 2021
Merged

Qiskit wrapper #2

merged 152 commits into from
Nov 16, 2021

Conversation

gcattan
Copy link
Collaborator

@gcattan gcattan commented Sep 25, 2021

Issue:
pyRiemann/pyRiemann#141

Blocker:
#1

This PR is a "fresh one" but a similar PR with commit history was raised in #3

This was referenced Sep 25, 2021
.coveragerc Outdated Show resolved Hide resolved
Co-authored-by: Quentin Barthélemy <q.barthelemy@gmail.com>
gcattan referenced this pull request in gcattan/pyRiemann-qiskit Sep 28, 2021
gcattan and others added 2 commits September 28, 2021 11:08
Co-authored-by: Quentin Barthélemy <q.barthelemy@gmail.com>
examples/ERP/plot_classify_EEG_quantum.py Outdated Show resolved Hide resolved
examples/ERP/plot_classify_EEG_quantum.py Outdated Show resolved Hide resolved
pyriemann_qiskit/classification.py Outdated Show resolved Hide resolved
pyriemann_qiskit/classification.py Outdated Show resolved Hide resolved
pyriemann_qiskit/classification.py Outdated Show resolved Hide resolved
examples/ERP/plot_classify_EEG_quantum.py Outdated Show resolved Hide resolved
examples/ERP/plot_classify_EEG_quantum.py Outdated Show resolved Hide resolved
examples/ERP/plot_classify_EEG_quantum.py Outdated Show resolved Hide resolved
# Time complexity of quantum algorithm depends on the number of trials and
# the number of elements inside the covariance matrices
# Thus we reduce elements number by using restrictive spatial filtering
sf = XdawnCovariances(nfilter=1)
Copy link
Member

Choose a reason for hiding this comment

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

Once again, it should be at least nfilter=2, otherwise covariance matrices are reduced to scalars.

Copy link
Collaborator Author

@gcattan gcattan Oct 20, 2021

Choose a reason for hiding this comment

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

In the case of Xdawn, n_channels = nfilter x nclasses if I do not mistake.

Please, see my previous comment below:
#2 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I think you are right: output matrices are 2x2. However, it remains a rough dimension reduction.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, there is this limitation on the feature dimension (24 when running on the local simulator).
I will try to do something similar to my last answer in the previous comment for nfilter = 2 and 3.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Here is the result of nfilter=2, with NaiveDimRed vs PCA (top = classical, bottom = quantum).

Figure_1

The balanced accuracy is very low in quantum as compared to nfilter=1. This is likely due to the fact that the feature dimension increases (from 10 to 18) but the number of measurements stays the same (1024).
But just to give an order, the example displayed in the above figure took 1 day and half to run on my local machine. It would be necessary to use a remote backend to achieve higher computation, but I do not see how we can integrate this in a github action.

That said, the question remains interesting, maybe it would be relevant to add an example of optimization with GridSearchCV on a fewer number of data?


if __cvxpy__:
classifiers.append(QuanticVQC())
classifiers.append(QuanticSVM(quantum=False))
Copy link
Member

Choose a reason for hiding this comment

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

What are the hyper-parameters of QSVM? Is it a linear or a RBF QSVM?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There are two cases.

  • When quantum=False, it uses SVC with RBF kernel.
  • When quantum=True, it uses a QuantumKernel, which is parameterized using a feature map (which entangles the data into a quantum state).

Copy link
Member

Choose a reason for hiding this comment

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

Ok, but what are the hyper-parameters for QuanticSVM(quantum=False)?
You should use the same as SVC.

And when quantum is True, what are the hyper-parameters of the QuantumKernel?

Copy link
Collaborator Author

@gcattan gcattan Oct 20, 2021

Choose a reason for hiding this comment

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

For both there is something like this:

  • transform the labels 0/1 into -1/1
  • get the kernel matrix:
    • for quantum=False, that is call sklearn.rbf_kernel with gamma = None (hence default is 1/nb_features)
    • for quantum=True, that is call QSVM.get_kernel_matrix with feature_map=ZZFeatureMap and enforce_spd=True (set negative eigen values to zero if kernel matrix is not positive semi-definite) as parameters
  • optimize the kernel matrix, with similar parameters:
    • number of iteration=500
    • normalization of the output = L2 Norm of y
    • L2 norm regularization factor = 0.001 (C?)

I changed gamma = auto (i.e 1/n_feature according to the doc), and C = 0.001. The figure below show how QuanticSVM(quantum=False) compares to SVC(gamma='auto', C = 0.001):

image

Copy link
Member

Choose a reason for hiding this comment

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

Ok. Like SVC, QuanticSVM should allow to set SVM hyper-parameters in its init.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, you are right. Do you want me also to take this work in a follow-up issue?

Copy link
Member

Choose a reason for hiding this comment

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

Is it a huge modification of code?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

At first sight, I would say that gamma (for quantum=False), feature_map and L2 regularization (for quantum=True) are easy to access. Same thing for the number of shots, the optimizer and the variational form (for VQC) which might be relevant to expose.
Other parameters: enforce_spd, number of iteration, output normalization and L2 regularisaiton (for quantum=False) might be harder to access.

gcattan and others added 16 commits October 20, 2021 10:42
Co-authored-by: Quentin Barthélemy <q.barthelemy@gmail.com>
Co-authored-by: Quentin Barthélemy <q.barthelemy@gmail.com>
Co-authored-by: Quentin Barthélemy <q.barthelemy@gmail.com>
Co-authored-by: Quentin Barthélemy <q.barthelemy@gmail.com>
Co-authored-by: Quentin Barthélemy <q.barthelemy@gmail.com>
Co-authored-by: Quentin Barthélemy <q.barthelemy@gmail.com>
Co-authored-by: Quentin Barthélemy <q.barthelemy@gmail.com>
Co-authored-by: Quentin Barthélemy <q.barthelemy@gmail.com>
Co-authored-by: Quentin Barthélemy <q.barthelemy@gmail.com>
gcattan and others added 2 commits October 23, 2021 13:07
Co-authored-by: Quentin Barthélemy <q.barthelemy@gmail.com>
Copy link
Member

@qbarthelemy qbarthelemy left a comment

Choose a reason for hiding this comment

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

Ok for me, because code has been deeply refactored.
The review of this PR has been very time-consuming for me: in the future, I am not sure to have enough time to continue to review such PRs.

@sylvchev , I let you review, approve and squash+merge.
And can you deploy the documentation on RTD?

@gcattan
Copy link
Collaborator Author

gcattan commented Oct 23, 2021

@qbarthelemy
Thank you for your hard work on this PR. Your feedback was very valuable and I am happy we achieved this work together.
Let me ensure you that next PRs will be much much smaller with limited scopes and functionalities.

@sylvchev
Copy link
Member

Thanks @gcattan, we could merge this PR. Thanks a lot @qbarthelemy for your careful reading!
I'll take care of putting the docs on RTD.

@sylvchev sylvchev merged commit cbe99f6 into pyRiemann:main Nov 16, 2021
@gcattan gcattan deleted the gc/quantum branch November 19, 2021 14:04
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