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

Documenting custom VQC optimizations #298

Merged
merged 5 commits into from
Dec 22, 2020
Merged

Documenting custom VQC optimizations #298

merged 5 commits into from
Dec 22, 2020

Conversation

scarrazza
Copy link
Member

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.

@scarrazza scarrazza changed the base branch from master to tf240 December 15, 2020 20:22
@codecov
Copy link

codecov bot commented Dec 15, 2020

Codecov Report

Merging #298 (9eea1cf) into master (2a5fbe8) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #298   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           55        54    -1     
  Lines        10664     10695   +31     
=========================================
+ Hits         10664     10695   +31     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/qibo/evolution.py 100.00% <100.00%> (ø)
src/qibo/models.py 100.00% <100.00%> (ø)
src/qibo/optimizers.py 100.00% <100.00%> (ø)
src/qibo/tensorflow/cgates.py 100.00% <100.00%> (ø)
src/qibo/tensorflow/circuit.py 100.00% <100.00%> (ø)
...m_operators/python/ops/qibo_tf_custom_operators.py 100.00% <100.00%> (ø)
...ators/python/ops/qibo_tf_custom_operators_grads.py 100.00% <100.00%> (ø)
src/qibo/tensorflow/distcircuit.py 100.00% <100.00%> (ø)
src/qibo/tests/test_custom_operators.py 100.00% <100.00%> (ø)
src/qibo/tests/test_variational.py 100.00% <100.00%> (ø)
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2a5fbe8...9eea1cf. Read the comment docs.

Copy link
Member

@stavros11 stavros11 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 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.

@@ -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?
Copy link
Member

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

Suggested change
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.

# custom loss function which returns numpy object
def myloss(parameters, circuit, target):
circuit.set_parameters(parameters)
return np.square(np.sum(circuit()-target))
Copy link
Member

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()))

@scarrazza scarrazza changed the base branch from tf240 to master December 21, 2020 18:53
@scarrazza scarrazza merged commit f1a3490 into master Dec 22, 2020
@scarrazza scarrazza deleted the vqeloss branch February 6, 2021 13:44
scarrazza added a commit that referenced this pull request Nov 1, 2022
Documenting custom VQC optimizations
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.

2 participants