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

[UnitaryHack] Implementation of Nelder Mead Optimizer #104

Closed
wants to merge 19 commits into from

Conversation

Gopal-Dahale
Copy link
Contributor

@Gopal-Dahale Gopal-Dahale commented Jun 12, 2023

Purpose

Implemented the Nelder Mead Optimizer.

Details

The optimizer along with tests. Also added the jupyter notebook used to test the optimizer. I have used 200 epochs instead of 100 as the optimizer required more epochs for convergence. This PR resolves #85.

Results

I ran two tests with adaptive equal to True and False for 200 epochs.

  1. adaptive=False, final output is 0.98555, 0.01445
  1. adaptive=True, final output is 0.96625, 0.03375

@dimkart
Copy link
Contributor

dimkart commented Jun 12, 2023

@Gopal-Dahale Hi and thanks for this. There are some linting errors, could you fix them?

@Gopal-Dahale
Copy link
Contributor Author

Hi @dimkart, I have formatted the code and ran the two commands locally and both of them now give 0:

  1. flake8 lambeq --max-doc-length=74
  2. flake8 lambeq --count --isolated --select=E9,F63,F7,F82 --show-source --statistics

@Gopal-Dahale
Copy link
Contributor Author

Hi @dimkart, I see type check with mypy has failed. I'll fix it. Meanwhile, is there documentation on how to build and test lambeq locally so that I don't have to rely on GitHub actions until I fix them all locally?

@dimkart
Copy link
Contributor

dimkart commented Jun 12, 2023

Hi @dimkart, I see type check with mypy has failed. I'll fix it. Meanwhile, is there documentation on how to build and test lambeq locally so that I don't have to rely on GitHub actions until I fix them all locally?

Unfortunately there is no documentation for that, but running pytest and mypy locally should work. Check the github actions (build_test.yml) to see the command lines we use for any special options or exclusion of files.

@Gopal-Dahale
Copy link
Contributor Author

Thanks. I have fixed the mypy issue and now it gives Success: no issues found in 58 source files.

@dimkart
Copy link
Contributor

dimkart commented Jun 12, 2023

@Gopal-Dahale Thanks, tests are now passing. This look already very good, and tomorrow we'll have a closer look at the code and we'll run the notebook to check performance. If you have already tried to run the notebook you might post here a screenshot of the trainer output for comparison. Keep an eye here for new comments :)

Copy link
Collaborator

@ianyfan ianyfan left a comment

Choose a reason for hiding this comment

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

Thanks for your submission. I've had a skim through and it looks good so far, I just have a few comments about things that jumped out at me immediately. I would need more time to look through it in detail.

lambeq/training/nelder_mead_optimizer.py Outdated Show resolved Hide resolved
lambeq/training/nelder_mead_optimizer.py Outdated Show resolved Hide resolved
self.fsim[j] = self.func(
diagrams, targets, self.sim[j]
)
except Exception:
Copy link
Contributor

Choose a reason for hiding this comment

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

Also here, it would be better to catch a more specific Exception

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have updated it with a RuntimeError.

- Using a RuntimeError exception in except block
- Rename self.func to self.objective_func (descriptive)
- Updated tests according to renaming
Copy link
Contributor

@Thommy257 Thommy257 left a comment

Choose a reason for hiding this comment

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

I've left a couple of comments, but in general it looks really great! Good work :)

lambeq/training/nelder_mead_optimizer.py Outdated Show resolved Hide resolved
self.fsim[k] = self.objective_func(diagrams,
targets,
self.sim[k])
except RuntimeError:
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm wondering why the objective function would triggers this error... Is there a reason for that or do we have a bug?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This handles the case when the objective function does not return scalar values. I have updated the code for the objective function.

Copy link
Collaborator

Choose a reason for hiding this comment

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

If it's just used within this class, I think it would be clearer to define a custom exception and raise/catch since it is more specific

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean something like this?

class CustomError(RuntimeError):
    pass

Any suggestion for the name?

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe smething like LossEvalError? @ianyfan?

lambeq/training/nelder_mead_optimizer.py Outdated Show resolved Hide resolved
lambeq/training/nelder_mead_optimizer.py Outdated Show resolved Hide resolved
lambeq/training/nelder_mead_optimizer.py Outdated Show resolved Hide resolved
lambeq/training/nelder_mead_optimizer.py Outdated Show resolved Hide resolved
lambeq/training/nelder_mead_optimizer.py Outdated Show resolved Hide resolved
lambeq/training/nelder_mead_optimizer.py Show resolved Hide resolved
lambeq/training/nelder_mead_optimizer.py Show resolved Hide resolved
lambeq/training/nelder_mead_optimizer.py Outdated Show resolved Hide resolved
- The objective fun is updated. It now checks whether the result is scalar or not and raises error if not which is handled by try except block
- Added maxfev hyperparam for maximum function evaluations
- Fixed typos and code style
- Removed redundant code lines
- Update state dict
@Gopal-Dahale
Copy link
Contributor Author

Hi, @Thommy257 @ianyfan can you resolve the conversations?

@dimkart
Copy link
Contributor

dimkart commented Jun 13, 2023

@Gopal-Dahale Hi, thanks for taking the time to respond to all comments -- the conversations will be resolved tomorrow by Ian and Thomas.

@Thommy257
Copy link
Contributor

Let's remove the notebook as well

Copy link
Collaborator

@ianyfan ianyfan left a comment

Choose a reason for hiding this comment

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

Hi, sorry for the late review. Again, this all looks very good, but I've left some comments, mainly about code/docstring style, so you only need to address them if you get the chance to.

lambeq/training/nelder_mead_optimizer.py Outdated Show resolved Hide resolved
lambeq/training/nelder_mead_optimizer.py Outdated Show resolved Hide resolved
heuristic search approach, which means that it can sometimes
converge to non-stationary points or suboptimal solutions.

This implementation is based heavily scipy's optimize.minimize. See
Copy link
Collaborator

Choose a reason for hiding this comment

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

based heavily on SciPy's `optimize.minimize` function.

model: QuantumModel,
hyperparams: dict[str, float],
loss_fn: Callable[[Any, Any], float],
bounds: ArrayLike | None = None,
Copy link
Collaborator

Choose a reason for hiding this comment

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

We don't need the extra comma at the end here

lambeq/training/nelder_mead_optimizer.py Outdated Show resolved Hide resolved
lambeq/training/nelder_mead_optimizer.py Outdated Show resolved Hide resolved
lambeq/training/nelder_mead_optimizer.py Outdated Show resolved Hide resolved
lambeq/training/nelder_mead_optimizer.py Outdated Show resolved Hide resolved
lambeq/training/nelder_mead_optimizer.py Outdated Show resolved Hide resolved
lambeq/training/nelder_mead_optimizer.py Outdated Show resolved Hide resolved
@Gopal-Dahale
Copy link
Contributor Author

Hi @ianyfan, I had updated the code as per your comments. Can you resolve the conversations?

Copy link
Contributor

@dimkart dimkart left a comment

Choose a reason for hiding this comment

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

Thanks for this, good work! The issue will be assigned to you, however the PR will be merged a little later, after a new (already planned) new release.

@ianyfan ianyfan closed this in ec2ae88 Jul 28, 2023
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.

UnitaryHACK: Implement and test gradient free optimizer for circuit models
4 participants