Skip to content
This repository has been archived by the owner on May 17, 2024. It is now read-only.

change the VQE examples to stop using the deprecated qiskit.algorithms #3493

Closed
wants to merge 3 commits into from

Conversation

1ucian0
Copy link
Member

@1ucian0 1ucian0 commented Aug 8, 2023

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

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)
Copy link
Contributor

@ElePT ElePT Aug 8, 2023

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)

Copy link
Member Author

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!

@1ucian0
Copy link
Member Author

1ucian0 commented Sep 6, 2023

Closed in favor of #3531 , which PRs from upstream

@1ucian0 1ucian0 closed this Sep 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: Run VQE examples in the landing page widget use the deprecated qiskit.algorithms
3 participants