-
Notifications
You must be signed in to change notification settings - Fork 60
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
Documenting custom VQC optimizations #298
Conversation
Codecov Report
@@ Coverage Diff @@
## master #298 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 55 54 -1
Lines 10664 10695 +31
=========================================
+ Hits 10664 10695 +31
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for implementing this. I fully agree with the proposed approach of not modifying the VQE model but rather importing the optimizers module and using the custom loss directly there. It is very clean from the user perspective and it is better to keep the VQE as in most cases it refers to Hamiltonian optimization which is how we implement it.
One comment is that I would add a test for the proposed VQC optimization script. We can use the same code you added in the examples, just add it to pytest and use a regressions .out
file to check the results, like we do in the VQE and QAOA tests. In terms of structure, we could probably merge test_vqe.py
and test_qaoa.py
to a single test_variational.py
and add the VQC test there. I would probably update this in the unit test refactoring anyway.
doc/source/advancedexamples.rst
Outdated
@@ -445,6 +466,51 @@ be written using :class:`qibo.base.gates.VariationalLayer` as follows: | |||
circuit.add(gates.CZ(0, nqubits - 1)) | |||
circuit.add((gates.RY(i, theta) for i in range(nqubits))) | |||
|
|||
.. _vqc-example: | |||
|
|||
How to write a custom VQC optimization? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I may be wrong in this but I was a bit confused about the VQC acronym for a few moments, so I would change this to
How to write a custom VQC optimization? | |
How to write a custom variational circuit optimization? |
because I think the VQC acronym is not as commonly used as the VQE.
In the text we could write explicitly "Variational Quantum Circuit (VQC)" for the first time and just use VQC after that.
doc/source/advancedexamples.rst
Outdated
# custom loss function which returns numpy object | ||
def myloss(parameters, circuit, target): | ||
circuit.set_parameters(parameters) | ||
return np.square(np.sum(circuit()-target)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that this will return a complex number for the loss because circuit()
will be complex. A more appropriate loss would be
return np.sum(np.square(np.abs(circuit() - target)))
Even better we could use the fidelity which is the common measure of distance between quantum states:
return 1 - np.abs(np.conj(target).dot(circuit()))
Documenting custom VQC optimizations
Following our discussion last week, I have tested different approaches to make the VQE more general to accept a generic variational quantum circuit optimization. After some tests, I am not fully convinced that a new object is really needed, so in this PR I document how to write a custom VQC and some minimal code cleanup.