Comments (11)
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.
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.
@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.
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.
@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.
I highly appreciate the extensive explanation.
Then a user calls
compute_minimum_eigenvalue
, internallyfind_minimum
is called and then the magic happens. In thefind_minimum
method we calloptimizer.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 inTrainableModel
) 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 theTrainableModel
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.
Then a user calls
compute_minimum_eigenvalue
, internallyfind_minimum
is called and then the magic happens. In thefind_minimum
method we calloptimizer.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 fromcompute_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 inTrainableModel
) 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 theTrainableModel
level. This will allow us to generalize a callback mechanism across both classifier and regressor.I mistook
NeuralNetworkClassifier
andNeuralNetworkRegressor
classes for theNeuralNetwork
class (which is not a descendant ofTrainableModel
) 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.
@adekusar-drl these are the changes I am working on (would commit some changes by tomorrow hopefully):
-
Adding
callback
as aCallable
argument in__init__
() ofNeuralNetworkClassifier
andNeuralNetworkRegressor
. Callback contains weights of the objective function and the computed value. -
Adding a member variable
_callback
which will be initialized bycallback
. -
Adding
objective_evaluation
(analogous toenergy_evaluation
) method toNeuralNetworkClassifier
andNeuralNetworkRegressor
(as of now cannot think of how to do it fromTrainableModel
, let me know if you have any suggestions). This function will invoke theobjective function
and append the values using_callback()
. -
Adding
get_objective_evaluation
(analogous toget_energy_evaluation
) method which returns the methodobjective_evaluation
, which will be passed to_optimizer.optimize()
infit()
as the argumentobjective_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.
A few thoughts:
objective_evaluation
(or may be a better name), I guess, can be pulled up toTrainableModel
.- a dedicated
get_objective_evaluation
, likely, is not required. It may have just a single line with areturn
statement.
from qiskit-machine-learning.
what if the implementation of ObjectiveFunction
is changed to take an argument callback
which when not None
would call callback(...)
?
from qiskit-machine-learning.
objective_evaluation
(or may be a better name), I guess, can be pulled up toTrainableModel
.
@adekusar-drl Have tried to do this, let me know what you think.
from qiskit-machine-learning.
Related Issues (20)
- Enhancement of PyTorch connector HOT 2
- Extend unit test coverage with `Hypothesis` in numerical tests
- Add `jit` compilation to the Torch connector with `thunder`
- Revamp `README.md` with structured information HOT 4
- Set up a security policy (@maintainers)
- Multi-class Classification Problem Using QSVC HOT 3
- Error when testing samples with labels other than {0, 1} in the MNIST dataset. HOT 6
- Revert CI environment to latest PyTorch once UTF bug is fixed
- Binary classification problem using NeuralNetworkClassifier and cross entropy loss HOT 1
- MacOS in CI - macos-latest is now ARM HOT 2
- Link Qiskit 1.0 migration instructions in Readme
- Add support for EstimatorV2 from ibm-qiskit-runtime to run circuits over hardware HOT 1
- Migrate `qiskit_algorithms` following end-of-support HOT 2
- Pinned `torch==2.2.2` breaks CI due to `numpy>=2.0`
- NeuralNetworkClassifier Accuracy Updates HOT 2
- Revert Numpy to the latest version in CI environment once UTF bug in PyTorch is fixed
- Restore mypy checks on Windows and lowest Numpy version
- The return values from SamplerQNN are in the wrong shape.
- Separate parameters for the trainable part and the encoding part in EstimatorQNN
- Mismatching between loss function code and documentation formula
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from qiskit-machine-learning.