Ask here about the “Variational Quantum Eigensolver” Codebook topic from the “Introduction to Variational Quantum Algorithms” module.
Hello,
In codercise V.3.2, is it normal that the QAOA_ising(params,p,cost_h) function contains cost_h as a parameters. Does the backend replace cost_h with build_cost_ising(n_particles, h_list) we define just before ? Also what is the correct formula for the Longitudinal Ising Hamiltonian ?
I believe there are some errors in the indices of the sums, and I can’t manage to find the formula online. I would assume that both sums start from 0 and end in N-1
Thanks
Codercise V1.1 Defining Ansatz
By following the documentation of BasicEntanglerLayers and qml.StronglyEntanglingLayers I complete the code as showing below :
n_bits = 4
dev = qml.device("default.qubit", wires=n_bits)
@qml.qnode(dev)
def basic_entangling_ansatz(observable,params):
"""Applies an ansatz with basic entanglement.
Args:
observable (qml.op): a pennylane operator whose expectation value we want to measure.
params(np.array): an array with the trainable parameters of the ansatz. They have the
shape of `qml.StronglyEntanglingLayers.shape(n_layers=1, n_wires=n_bits)`
Returns:
(np.tensor): a numpy tensor of 1 element corresponding to the expectation value of the given observable.
"""
##################
##################
# Apply Basic Entangler Layers using the provided parameters
qml.BasicEntanglerLayers(weights=params, wires=range(n_bits))
# Measure the expectation value of the provided observable
return qml.expval(observable)
##################
@qml.qnode(dev)
def strongly_entangling_ansatz(observable,params):
"""Applies an ansatz with moderate entanglement.
Args:
observable (qml.op): a pennylane operator whose expectation value we want to measure.
params(np.array): an array with the trainable parameters of the ansatz. They have the
shape of `qml.StronglyEntanglingLayers.shape(n_layers=1, n_wires=n_bits)`
Returns:
(np.tensor): a numpy tensor of 1 element corresponding to the expectation value of the given observable.
"""
##################
##################
# Apply Strongly Entangling Layers using the provided parameters
qml.StronglyEntanglingLayers(weights=params, wires=range(n_bits))
# Measure the expectation value of the provided observable
return qml.expval(observable)
##################
this error is always appear, for StronglyEntagling circuit
The expectation value of the second circuit doesn’t look quite right.
what is my mistakes here, and why using just observable as the expacted value doesn’nt work !!
Hi @yousrabou, I think the conversation here can help you figure out what’s wrong.
Note that the error " The expectation value of the second circuit doesn’t look quite right." reflect issues in the entire circuit, not just the return line.
I hope this helps you!
Hi @yoshypdf !
You’re right that cost_h
is the output of build_cost_ising(n_particles, h_list)
. We’ll update the wording of the codercise to make this clear.
The formula for the Longitudinal Ising Hamiltonian is in the description of the codercise. The second sum should start with i=1. The end of both sums is correct. Note that spin site indices here start at ‘1’ but qubit indices start at ‘0’ so you’ll need to take this into account in your implementation. We’ll update the formula to add i=1 and see if there’s a way of making it clearer.
Let me know if this helps you solve the codercise.
Thanks again for helping us improve the Codebook!