When trying V.1.1 in the codebook, I get an error saying the expectation value is not correct (as shown in the picture), which seems to me it is not a syntax / technical problem. I have looked through the StronglyEntanglingLayers document but nothing jumps out for the reason.
It is still quite confusing. The only problem I can possibly see is the shape (L,M,3) of the “weights” argument. But in the exercise it is stated that the the params will match the correct shape.
But just to be safe i reshaped it to (params.shape[0], n_bits, 3) which gave the same result. I do not think it is the “wires” keyword that. caused the problem so I am quite lost
If you focus on the two blocks from the documentation image, do they look the same too? Check the documentation in detail to answer this question, and you’ll find the answer to the challenge!
If it helps, you can try coding a small circuit on a separate notebook and drawing it with qml.draw_mpl().
Good point @twang150 , it’s not super clear in the description that the images are part of the question. Thanks for pointing it out! I’ll se how we can clarify this in the question.
@twang150 we added a note saying “Your implementation should match the images shown above.” I hope this makes it more clear for everyone. Thanks again for helping us improve the Codebook and let us know if you have any further feedback!
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.
"""
##################
# YOUR CODE HERE #
##################
qml.BasicEntanglerLayers(weights=params, wires=range(n_bits))
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.
"""
##################
# YOUR CODE HERE #
##################
qml.StronglyEntanglingLayers(weights=params, wires=range(n_bits))
return qml.expval(observable) The expectation value of the second circuit(strongly_entangling_ansatz(observable,params)) doesn't look quite right. Please debug this code. twang150, I do not get what the pictures did.
Hi!
You are almost there. Take a closer look at the diagram for the Strong Entangler provided by the Codercise and think about the argument ranges in qml.StronglyEntanglingLayers. That’s the part you are missing in your answer.
Hi!
I’m glad you got it. I am not sure about your question, but I believe that you are asking about why it is range three.
And this has to do with what my colleague hinted here Error in Codercise V.1.1 - #4 by CatalinaAlbornoz
Take a look at the gates, the target and control qubits, and count how many qubits are there between the gates. See the difference between those different diagrams she posted.
Good day,
While trying Codercise V.1.1 @daniela.murcillo@CatalinaAlbornoz I got an <Error: ‘PauliZ’ object is not callable./>, can you please help me out, defiantly I missing something out.
Hi!
When you define observable in such way, when qml.expval is called, it would be trying to call the object qml.PauliZ as if it was a function and it isn’t. I see what you are trying to do, but it is incorrect on PennyLane.
A correct way would be to define a vector of observables, with the wires already specified and go over the vector, say observables=[qml.PauliZ(0),qml.PauliZ(1)], and observables[0] and so on.
I just want to remind you that in case you are submitting this codercise, you should keep intact the code provided by the template, and only modified the parts where it says # YOUR CODE HERE #. But it is ok to experiment with examples.
Thanks @daniela.murcillo it helped a lot, I wonder how I missed that.
Thanks for the reminder my bad, everything couldn’t fit in the snapshot, I usually keep the code intact.
Again I think I’m missing something out on the second ansatz, can you please walk me out, code below:
__________________________>> returned prompt:<The expectation value of the second circuit doesn’t look quite right/>
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.
"""
##################
qml.BasicEntanglerLayers(weights=params, wires=range(n_bits))
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.
"""
##################
params=np.random.random(qml.StronglyEntanglingLayers.shape(1,4))
qml.StronglyEntanglingLayers(weights=params, wires=range(n_bits))
return qml.expval(observable)
##################
observable = qml.PauliZ(0)# qml.PauliZ(1),qml.PauliZ(2),qml.PauliZ(3)]
returned prompt:The expectation value of the second circuit doesn’t look quite right.
Thank you in advance for your time and consideration.
Hi!
I’m glad it was helpful.
why are you defining params in the second function? This is not necessary, those are given in the backend of the codercise. Also, think about the argument ranges for the qml.StronglyEntanglingLayers, it needs to be incorporated into the code.