Giter Site home page Giter Site logo

Comments (11)

adekusar-drl avatar adekusar-drl commented on July 29, 2024 1

No worries at all, I did not say it would be easy. I don't know to what extent you understand implementation of VQE, so let me get to basics. Feel free to skip if it is clear.

First, let's take a look at VQE. The callback is invoked in _energy_evaluation, there are four parameters are passed to the callback: the evaluation count, the optimizer parameters for the variational form, the evaluated mean and the evaluated standard deviation. Now we know what kind of parameters are passed to the callback in VQE. This may help to determine what parameters we need in our case. Next step, let's see where _energy_evaluation is called. Well, actually it is not called directly, but rather used as a cost function in the parameters of the call of find_minimum, which, in turn, is called from compute_minimum_eigenvalue and this is a top level method invoked by a user. OK, now a top level hierarchy of the VQE calls should be more or less clear.

Let's take a took at this hierarchy from a user side. When an instance of VQE is created and a callback is passed to the constructor, it is saved on the instance level. Then a user calls compute_minimum_eigenvalue, internally find_minimum is called and then the magic happens. In the find_minimum method we call optimizer.optimize() and pass a reference to _energy_evaluation as a cost function (cost_fn) as I mentioned earlier. An optimizer from VQE, as the name suggests, optimizes parameters in way to find a minimum of the cost function. That means the optimizer computes cost function value several times. Now, let's recall what the cost function is, it is _energy_evaluation where the callback is actually invoked. So now it becomes clear the meaning of the parameters of the callback. The evaluation count is just a number of cost function computations, parameters are the values are used to compute cost function, mean and deviation are the cost function value and deviation.

Now, let's see where we have a similar construction in the NNs. An obvious example is NeuralNetworkClassifier.fit() where we call optimizer.optimize() and provide a reference to an objective function (well, a synonym to cost function). There's a similar place in NeuralNetworkRegressor, but let's keep it aside for a while. So, basically we need to pass a callback to the classifier, save it, and then make use of it somewhere in the objective (cost) function. Unfortunately, we don't have a single definition of the objection function, but rather a few of them in objective_functions.py. The base class for objective functions is ObjectiveFunction and there's a method objective where we compute a value of the objective (cost) function. So, basically this is the place where we should invoke the callback in our case. Callback parameters? Well, in this method we only have weights (they are parameters in case of VQE) and the computed value itself. I don't think we need evaluation count, it can be easily computed by a user by tracking now many times the callback is invoked. The problem is that the objective functions defined in QML are stateless and I'd like to keep them stateless. Hence, the idea is to introduce a similar to _energy_evaluation method in the classifier (or more likely in TrainableModel) where the actual objective function and the callback are invoked. The problem is multiplied by two since we nave a classifier and a regressor. Common logic/methods should be pulled up to the TrainableModel level. This will allow us to generalize a callback mechanism across both classifier and regressor.

I hope I did not mess up somewhere in my writing and hope it helps. Let me know if you have questions.

from qiskit-machine-learning.

adekusar-drl avatar adekusar-drl commented on July 29, 2024 1

This is what I would definitely like to avoid: mixing up math stuff in the objective functions and the algorithm itself in the models.

from qiskit-machine-learning.

darshkaushik avatar darshkaushik commented on July 29, 2024

@adekusar-drl I am interested to fix this issue.
I suppose the changes are to be made in CircuitQNN or any of the base classes?

This callback may make use of the callback interface of optimizers.

Also, the callback is available only for some specific optimizers and is not a feature of the class Optimizers I think.

from qiskit-machine-learning.

adekusar-drl avatar adekusar-drl commented on July 29, 2024

I guess the changes should go to TrainableModel or the subclasses. VQE class may be a good example to look at, it has a callback parameter. Anyways, this issue requires additional investigation to have a proper design to implement.

from qiskit-machine-learning.

darshkaushik avatar darshkaushik commented on July 29, 2024

@adekusar-drl excuse my inactivity, was a little overwhelmed with NeuralNetwork and its subclasses.

I guess the changes should go to TrainableModel or the subclasses.

I thought NeuralNetwork or its subclasses should be changed?

VQE class may be a good example to look at, it has a callback parameter.

Got some idea from there, but still unclear what specific parameters should callback contain.

Anyways, this issue requires additional investigation to have a proper design to implement.

Do you have anything in mind, or any of the other contributors, I need some help here.

from qiskit-machine-learning.

darshkaushik avatar darshkaushik commented on July 29, 2024

I highly appreciate the extensive explanation.

Then a user calls compute_minimum_eigenvalue, internally find_minimum is called and then the magic happens. In the find_minimum method we call optimizer.optimize() and pass a reference to _energy_evaluation as a cost function (cost_fn) as I mentioned earlier.

Here, can you please link the line where find_minimum is called internally, I cannot seem to connect it's function call from compute_minimum_eigenvalue.

The problem is that the objective functions defined in QML are stateless and I'd like to keep them stateless.

I cannot say that I understand this completely. If you think I need to do some reading for this, let me know.

Hence, the idea is to introduce a similar to _energy_evaluation method in the classifier (or more likely in TrainableModel) where the actual objective function and the callback are invoked. The problem is multiplied by two since we nave a classifier and a regressor. Common logic/methods should be pulled up to the TrainableModel level. This will allow us to generalize a callback mechanism across both classifier and regressor.

I mistook NeuralNetworkClassifier and NeuralNetworkRegressor classes for the NeuralNetwork class (which is not a descendant of TrainableModel) due to the title of the issue. But thanks for clearing that out.

Although I always wanted to contribute to qiskit, the advocate program was definitely encouragement I needed. Don't know if I would be able to solve this issue before the deadline, but I am getting to learn a lot about this repo.

from qiskit-machine-learning.

adekusar-drl avatar adekusar-drl commented on July 29, 2024

Then a user calls compute_minimum_eigenvalue, internally find_minimum is called and then the magic happens. In the find_minimum method we call optimizer.optimize() and pass a reference to _energy_evaluation as a cost function (cost_fn) as I mentioned earlier.

Here, can you please link the line where find_minimum is called internally, I cannot seem to connect it's function call from compute_minimum_eigenvalue.

My bad, VQE was updated on 17/06 and I had an old version of Terra locally. Basically they eliminated find_minimum and moved all that code of find_minimum from VariationalAlgorithm to VQE. Indeed, now compute_minimum_eigenvalue calls optimizer.optimize directly.

The method _energy_evaluation is also refactored and now it is a nested method in get_energy_evaluation. Well, looks slightly different now, but in general, it does the same.

The problem is that the objective functions defined in QML are stateless and I'd like to keep them stateless.

I cannot say that I understand this completely. If you think I need to do some reading for this, let me know.

Stateless means that an object does not keep internally any variable, any other object. So, calling a method of such object with the same parameter values must yield the same output.

Hence, the idea is to introduce a similar to _energy_evaluation method in the classifier (or more likely in TrainableModel) where the actual objective function and the callback are invoked. The problem is multiplied by two since we nave a classifier and a regressor. Common logic/methods should be pulled up to the TrainableModel level. This will allow us to generalize a callback mechanism across both classifier and regressor.

I mistook NeuralNetworkClassifier and NeuralNetworkRegressor classes for the NeuralNetwork class (which is not a descendant of TrainableModel) due to the title of the issue. But thanks for clearing that out.

Yeah, probably the title is not great. By NNs I usually mean NNs themselves and everything around them, so this may be misleading, sorry.

Although I always wanted to contribute to qiskit, the advocate program was definitely encouragement I needed. Don't know if I would be able to solve this issue before the deadline, but I am getting to learn a lot about this repo.

We don't have a strict deadline for this issue now.

from qiskit-machine-learning.

darshkaushik avatar darshkaushik commented on July 29, 2024

@adekusar-drl these are the changes I am working on (would commit some changes by tomorrow hopefully):

  • Adding callback as a Callable argument in __init__() of NeuralNetworkClassifier and NeuralNetworkRegressor. Callback contains weights of the objective function and the computed value.

  • Adding a member variable _callback which will be initialized by callback.

  • Adding objective_evaluation (analogous to energy_evaluation) method to NeuralNetworkClassifier and NeuralNetworkRegressor (as of now cannot think of how to do it from TrainableModel, let me know if you have any suggestions). This function will invoke the objective function and append the values using _callback().

  • Adding get_objective_evaluation (analogous to get_energy_evaluation) method which returns the method objective_evaluation, which will be passed to _optimizer.optimize() in fit() as the argument objective_funtion.

I hope I am not missing anything. Let me know.

We don't have a strict deadline for this issue now.

Oh, I meant that I hope to finish this before the deadline of the advocate program (July 15) as I don't want to miss the mentorship opportunity.

from qiskit-machine-learning.

adekusar-drl avatar adekusar-drl commented on July 29, 2024

A few thoughts:

  • objective_evaluation (or may be a better name), I guess, can be pulled up to TrainableModel.
  • a dedicated get_objective_evaluation, likely, is not required. It may have just a single line with a return statement.

from qiskit-machine-learning.

darshkaushik avatar darshkaushik commented on July 29, 2024

what if the implementation of ObjectiveFunction is changed to take an argument callback which when not None would call callback(...) ?

from qiskit-machine-learning.

darshkaushik avatar darshkaushik commented on July 29, 2024
  • objective_evaluation (or may be a better name), I guess, can be pulled up to TrainableModel.

@adekusar-drl Have tried to do this, let me know what you think.

from qiskit-machine-learning.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.