I have the following code where I’m trying to encode a vector of 4 features into a 2-qubit quantum state on the qiskit.aer simulator backend.
import pennylane as qml
from pennylane import numpy as np
import pennylane_qiskit as pqis
import math,qiskit
dev = qml.device('qiskit.aer', wires=2, backend = 'statevector_simulator')
x = np.array([0.53896774,0.79503606,0.27826503,0.]) # n qubits to encode 2^n features
#x = np.array([1,0,0,0])
@qml.qnode(dev)
def test(x):
qml.QubitStateVector(x,wires = [0,1])
return qml.expval(qml.PauliZ(0))
print(test(x))
This outputs the following error:
QiskitError: 'Sum of amplitudes-squared does not equal one.'
Qiskit version 0.9.0 Pennylane qiskit plugin 0.5.1
Also I’m getting the expectation value of the Pauli Z opeartor of the first qubit , but I’m rather interested on the marginal probabilities of quantum state, is there a way to extract that from the quantum node dev?
In the qiskit/extensions/quantum_initializer/initializer.py file the _EPS = 1e-10 is defined.
Therefore later the following part raises the error:
if not math.isclose(sum(np.absolute(params) ** 2), 1.0, abs_tol=_EPS):
raise QiskitError("Sum of amplitudes-squared does not equal one.")
It might be worth posting this as an issue for Qiskit repository.
In the meantime, you may go for using PennyLane’s default_qubit device:
import pennylane as qml
from pennylane import numpy as np
dev = qml.device('default.qubit', wires=2)
x = np.array([0.53896774,0.79503606,0.27826503,0.]) # n qubits to encode 2^n features
@qml.qnode(dev)
def test(x):
qml.QubitStateVector(x,wires = [0,1])
return qml.expval(qml.PauliZ(0))
print(test(x))
Hi @vijpandaturtle, thanks for your question and for your patience!
Just to get a little bit more of an idea of what is happening, could you help out with including more details on the traceback that you are getting?
Also, it would be helpful to see what arguments you are passing to circuit, such that we can track down which part is giving the error (the shape of x could also be helpful if you wouldn’t like to include the real value of x).
General details related to AmplitudeEmbedding can be found at this page.
@antalszava sorry for the late reply
Yes I have the latest version of pennylane
based on the above code I posted there are 2 arguments I have, one is a torch tensor of around 27 decimal values, second is the params defined (random_layers_uniform)
Unfortunately, I am unable to post my exact input values right now.
This is the exact error i am getting
c:\users\owner\anaconda3\envs\quantum\lib\site-packages\pennylane\plugins\default_qubit.py in apply_state_vector(self, input_state, wires)
143 “”"
144 if not np.isclose(np.linalg.norm(input_state, 2), 1.0, atol=tolerance):
–> 145 raise ValueError(“Sum of amplitudes-squared does not equal one.”)
146
147 n_state_vector = input_state.shape[0]
ValueError: Sum of amplitudes-squared does not equal one.
I used 12 qubits because my input size is likely to be variable. But do you think using such a large amount of qubits could lead to this issue ?
Thanks
@antalszava since the default.qubit simulator is really slow I wanted to try with other quantum simulators. however, when I try to use the qiskit aer device i run into the same tolerance issue. has this been solved yet ?
Hi @vijpandaturtle, the fix has been merged into the master branch (it was not yet included in a release).
If the traceback that you are getting is exactly the same as before, then it is coming from PennyLane core (and not the PennyLane-Qiskit plugin). In this case, could you double-check that you were using the same PennyLane distribution in both cases?
(Note that if you change directories, it might be that a different PennyLane version will be used.)
Locally I gave it a try, and it worked for me when running with dafult.qubit and then simply changing to qiskit.aer.
If this would still not be coming around, could you share your PennyLane-Qiskit version, please?
@vijpandaturtle thanks for the info! I did a reset of PennyLane to the commit where this problem was still around and now I’m also getting a QiskitError.
Could you please check that:
you did a git pull for PennyLane on the master branch and that
you ran pip install -e . from the directory where you have PennyLane cloned?
After this, could you also include what your output for pip freeze | grep PennyLane is?
It should look something like this: -e git+https://github.com/XanaduAI/pennylane.git@a76140d842298845fa3623d3f5612af9eedf8d07#egg=PennyLane
where a76140d842298845fa3623d3f5612af9eedf8d07 is specific for the commit that you have currently.
(Also, just to double-check if you run the same script, but with dev = qml.device('default.qubit', wires=num_qubits), then no error was raised, correct?)
Let me know how these go! There will be soon a new minor release for PennyLane including the fix for this issue, so that will for sure solve your problem!
@antalszava yes I have pulled the master branch and logged the commit history. a76140d842298845fa3623d3f5612af9eedf8d07 this is the latest commit I believe so my lib is up to date i guess
the issue is that my quantum circuit is being called multiple times aka i’m passing in many vectors and generating outputs. so my circuit is too slow for me to find out if it is not creating an issue for any of my input vectors.
Which is why i wanted to try the aer device. Having said that, so far no issues with default.qubit !
Could you let me know whenever the release is out ? And thank you so much for the reply.
@vijpandaturtle, now PennyLane-Qiskit 0.8.1 is also live on PyPi!
In this release, the requirement for PennyLane has been increased to the version which solved the normalization issue (0.8.1).
Let me know if you are still experiencing problems! If so, we could also take the discussion to our Slack channel, such that we can discuss further details more quickly.