This repository has been archived by the owner on May 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 108
change the VQE examples to stop using the deprecated qiskit.algorithms #3493
Closed
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Co-authored-by: Elena Peña Tapia <[email protected]>
1ucian0
requested review from
techtolentino,
eddybrando and
y4izus
as code owners
August 8, 2023 11:14
ElePT
reviewed
Aug 8, 2023
Comment on lines
36
to
53
hamiltonian = SparsePauliOp.from_list( | ||
[("YZ", 0.3980), ("ZI", -0.3980), ("ZZ", -0.0113), ("XX", 0.1810)] | ||
) | ||
|
||
# Define VQE ansatz, initial point and cost function | ||
from qiskit.circuit.library import EfficientSU2 | ||
ansatz = EfficientSU2(hamiltonian.num_qubits) | ||
initial_point = [0]*16 | ||
def cost_function(params, ansatz, hamiltonian, estimator): | ||
energy = estimator.run(ansatz, hamiltonian, parameter_values=params).result().values[0] | ||
return energy | ||
|
||
# Run VQE using SciPy minimizer routine | ||
from scipy.optimize import minimize | ||
result = minimize(cost_function, initial_point, args=(ansatz, hamiltonian, estimator), method="cobyla") | ||
|
||
# Print minimum eigenvalue | ||
print(result.fun) |
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.
Now that I see the side-by-side comparison with the old example, I am thinking it might make sense to actually keep the hamiltonian and ansatz from the old example (might be easier with those familiar with the code). In that case, the snippet would look like this:
Suggested change
hamiltonian = SparsePauliOp.from_list( | |
[("YZ", 0.3980), ("ZI", -0.3980), ("ZZ", -0.0113), ("XX", 0.1810)] | |
) | |
# Define VQE ansatz, initial point and cost function | |
from qiskit.circuit.library import EfficientSU2 | |
ansatz = EfficientSU2(hamiltonian.num_qubits) | |
initial_point = [0]*16 | |
def cost_function(params, ansatz, hamiltonian, estimator): | |
energy = estimator.run(ansatz, hamiltonian, parameter_values=params).result().values[0] | |
return energy | |
# Run VQE using SciPy minimizer routine | |
from scipy.optimize import minimize | |
result = minimize(cost_function, initial_point, args=(ansatz, hamiltonian, estimator), method="cobyla") | |
# Print minimum eigenvalue | |
print(result.fun) | |
hamiltonian = SparsePauliOp.from_list( | |
("II", -1.052373245772859), | |
("IZ", 0.39793742484318045), | |
("ZI", -0.39793742484318045), | |
("ZZ", -0.01128010425623538), | |
("XX", 0.18093119978423156) | |
]) | |
# Define VQE ansatz, initial point and cost function | |
from qiskit.circuit.library import TwoLocal | |
ansatz = TwoLocal(num_qubits=2, rotation_blocks="ry", entanglement_blocks="cz") | |
initial_point = initial_point = [0] * 8 | |
def cost_function(params, ansatz, hamiltonian, estimator): | |
energy = estimator.run(ansatz, hamiltonian, parameter_values=params).result().values[0] | |
return energy | |
# Run VQE using SciPy minimizer routine | |
from scipy.optimize import minimize | |
result = minimize(cost_function, initial_point, args=(ansatz, hamiltonian, estimator), method="cobyla") | |
# Print minimum eigenvalue | |
print(result.fun) |
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.
Suggestion added to 6164255 Thanks!
Closed in favor of #3531 , which PRs from upstream |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Changes
Fixes #3487
Co-authored-by: @ElePT
As
qiskit.algorithms
is deprecated, the VQE examples in the landing page needs to be refactor.Implementation details
How to read this PR
The change was implemented with a script. So, if you are ok with one of the entries, you are ok with all of them. They are literally the same.
Screenshots