# QubitStateVector pennylane-qiskit error

**URL:** <https://discuss.pennylane.ai/t/qubitstatevector-pennylane-qiskit-error/246>\
**Category:** PennyLane Qiskit\
**Created:** [October 16, 2019, 9:17am UTC](https://discuss.pennylane.ai/t/qubitstatevector-pennylane-qiskit-error/246 "2019-10-16T09:17:27Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![RicardoGaGu](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/ricardogagu/32/87_2.png) [@RicardoGaGu](https://discuss.pennylane.ai/u/RicardoGaGu)\
**Post date:** [October 16, 2019, 9:17am UTC](https://discuss.pennylane.ai/t/qubitstatevector-pennylane-qiskit-error/246/1 "2019-10-16T09:17:27Z")

</div>

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.

```auto
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** :

```auto
QiskitError: 'Sum of amplitudes-squared does not equal one.'

```

I have obviously checked that this is True:

```auto
math.isclose(sum(np.absolute(x) ** 2), 1.0,abs_tol=1e-7)

```

could it be qiskit’s tolerance eps value?

**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?

Thank you very much pennylane community!

---

<div class="post-metadata">

**Author:** ![josh](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/josh/32/100_2.png) [@josh](https://discuss.pennylane.ai/u/josh)\
**Post date:** [October 16, 2019, 9:57pm UTC](https://discuss.pennylane.ai/t/qubitstatevector-pennylane-qiskit-error/246/2 "2019-10-16T21:57:15Z")

</div>

Hi @RicardoGaGu!

To extract the probabilities, you can use the [`dev.probabilities()`](https://pennylane-qiskit.readthedocs.io/en/latest/code/qiskit_device.html#pennylane_qiskit.qiskit_device.QiskitDevice.probabilities) method:

```python
import pennylane as qml

dev = qml.device("qiskit.aer", wires=3)

@qml.qnode(dev)
def test(x):
    qml.RX(x, wires = [0])
    qml.CNOT(wires=[0, 1])
    qml.RX(x, wires=[2])
    return qml.expval(qml.PauliZ(0))

results = test(0.54)
# get the marginal probability of wires 0 and 1
prob = dev.probabilities(wires=[0, 1])

```

Printing the probability:

```python
>>> print(prob)
OrderedDict([((0, 0), 0.9326171875),
             ((0, 1), 0.0),
             ((1, 0), 0.0),
             ((1, 1), 0.0673828125)])

```

Note that this method returns an ordered dictionary, so you can extract the values as a list or by using the event key:

```python
>>> list(prob.values())                                                                                                                                                                                       
[0.9296875, 0.0, 0.0, 0.0703125]
>>> prob[(1, 1)]
0.0703125

```

* * *

In the case of the Qiskit error, it looks like this might be a tolerance issue with Qiskit. I will investigate further just to make sure.

---

<div class="post-metadata">

**Author:** ![RicardoGaGu](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/ricardogagu/32/87_2.png) [@RicardoGaGu](https://discuss.pennylane.ai/u/RicardoGaGu)\
**Post date:** [October 21, 2019, 11:26am UTC](https://discuss.pennylane.ai/t/qubitstatevector-pennylane-qiskit-error/246/3 "2019-10-21T11:26:32Z")

</div>

HI @josh, have you had time to look into the tolerance issue? Thanks for the heads up on the .probabilities() method!

Thanks,

Ricardo

---

<div class="post-metadata">

**Author:** ![antalszava](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/antalszava/32/171_2.png) [@antalszava](https://discuss.pennylane.ai/u/antalszava)\
**Post date:** [October 25, 2019, 10:18pm UTC](https://discuss.pennylane.ai/t/qubitstatevector-pennylane-qiskit-error/246/4 "2019-10-25T22:18:19Z")

</div>

Hi @RicardoGaGu, thanks for your question!

In the `qiskit/extensions/quantum_initializer/initializer.py` file the `_EPS = 1e-10` is defined.

Therefore later the following part raises the error:

```auto
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:

```auto
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))

```

---

<div class="post-metadata">

**Author:** ![vijpandaturtle](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/vijpandaturtle/32/156_2.png) [@vijpandaturtle](https://discuss.pennylane.ai/u/vijpandaturtle)\
**Post date:** [February 14, 2020, 4:00pm UTC](https://discuss.pennylane.ai/t/qubitstatevector-pennylane-qiskit-error/246/5 "2020-02-14T16:00:28Z")

</div>

@josh i am having the same issue but with the default.qubit device. what do you suggest i do ?

“ValueError: Sum of amplitudes-squared does not equal one.”

I am trying to push a bunch of numpy arrays through a loop.

```
  num_qubits = 12
  dev = qml.device('default.qubit',wires=num_qubits)
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
   init_params = random_layers_uniform(n_layers=3, 
    n_wires=num_qubits, seed=2)
   
   @qml.qnode(dev, interface='torch')
def circuit(x=None, weights=None):
    AmplitudeEmbedding(x, list(range(num_qubits)), pad=0., normalize=True)
    RandomLayers(weights=weights, wires=list(range(num_qubits)))
    return qml.expval(qml.PauliZ(0))
```

---

<div class="post-metadata">

**Author:** ![vijpandaturtle](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/vijpandaturtle/32/156_2.png) [@vijpandaturtle](https://discuss.pennylane.ai/u/vijpandaturtle)\
**Post date:** [February 18, 2020, 7:11am UTC](https://discuss.pennylane.ai/t/qubitstatevector-pennylane-qiskit-error/246/6 "2020-02-18T07:11:09Z")

</div>

Hi, just a gentle reminder i am really stuck here and require help. Is there a specific procedure to encode features into amplitude embeddings ?

---

<div class="post-metadata">

**Author:** ![antalszava](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/antalszava/32/171_2.png) [@antalszava](https://discuss.pennylane.ai/u/antalszava)\
**Post date:** [February 20, 2020, 12:45am UTC](https://discuss.pennylane.ai/t/qubitstatevector-pennylane-qiskit-error/246/8 "2020-02-20T00:45:59Z")

</div>

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](https://pennylane.readthedocs.io/en/stable/code/api/pennylane.templates.embeddings.AmplitudeEmbedding.html?highlight=amplitudeembedding#pennylane.templates.embeddings.AmplitudeEmbedding).

---

<div class="post-metadata">

**Author:** ![antalszava](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/antalszava/32/171_2.png) [@antalszava](https://discuss.pennylane.ai/u/antalszava)\
**Post date:** [February 20, 2020, 12:54am UTC](https://discuss.pennylane.ai/t/qubitstatevector-pennylane-qiskit-error/246/9 "2020-02-20T00:54:41Z")

</div>

Could you also make sure that you are using the latest version of PennyLane (`v0.8.0`) and send the output that you get for `qml.about()`? 😊

---

<div class="post-metadata">

**Author:** ![vijpandaturtle](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/vijpandaturtle/32/156_2.png) [@vijpandaturtle](https://discuss.pennylane.ai/u/vijpandaturtle)\
**Post date:** [February 24, 2020, 3:06am UTC](https://discuss.pennylane.ai/t/qubitstatevector-pennylane-qiskit-error/246/10 "2020-02-24T03:06:38Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![antalszava](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/antalszava/32/171_2.png) [@antalszava](https://discuss.pennylane.ai/u/antalszava)\
**Post date:** [February 24, 2020, 5:45pm UTC](https://discuss.pennylane.ai/t/qubitstatevector-pennylane-qiskit-error/246/11 "2020-02-24T17:45:00Z")

</div>

Hi @vijpandaturtle,

Thanks for the details! This seems to be an error related to the tolerance value used with `AmplitudeEmbedding`.

A [new addition by @Maria\_Schuld](https://github.com/XanaduAI/pennylane/pull/519) solves this issue by adjusting the tolerance to the value used at other parts of the codebase.

If you check out the master branch of PennyLane, then your problem should be solved now. 😊

Thanks so much for raising this issue!

---

<div class="post-metadata">

**Author:** ![vijpandaturtle](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/vijpandaturtle/32/156_2.png) [@vijpandaturtle](https://discuss.pennylane.ai/u/vijpandaturtle)\
**Post date:** [February 25, 2020, 3:02am UTC](https://discuss.pennylane.ai/t/qubitstatevector-pennylane-qiskit-error/246/12 "2020-02-25T03:02:46Z")

</div>

Thank you for solving the issue 😁

---

<div class="post-metadata">

**Author:** ![vijpandaturtle](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/vijpandaturtle/32/156_2.png) [@vijpandaturtle](https://discuss.pennylane.ai/u/vijpandaturtle)\
**Post date:** [February 26, 2020, 8:00am UTC](https://discuss.pennylane.ai/t/qubitstatevector-pennylane-qiskit-error/246/13 "2020-02-26T08:00:36Z")

</div>

@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 ?

---

<div class="post-metadata">

**Author:** ![antalszava](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/antalszava/32/171_2.png) [@antalszava](https://discuss.pennylane.ai/u/antalszava)\
**Post date:** [February 26, 2020, 10:40pm UTC](https://discuss.pennylane.ai/t/qubitstatevector-pennylane-qiskit-error/246/14 "2020-02-26T22:40:26Z")

</div>

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? 🙂

---

<div class="post-metadata">

**Author:** ![vijpandaturtle](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/vijpandaturtle/32/156_2.png) [@vijpandaturtle](https://discuss.pennylane.ai/u/vijpandaturtle)\
**Post date:** [February 27, 2020, 2:21am UTC](https://discuss.pennylane.ai/t/qubitstatevector-pennylane-qiskit-error/246/15 "2020-02-27T02:21:41Z")

</div>

@antalszava okay in that case I may be doing something wrong  
here is my qml.about()

Name: PennyLane  
Version: 0.9.0.dev0  
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.  
Home-page: [https://github.com/XanaduAI/pennylane](https://github.com/XanaduAI/pennylane)  
Author: None  
Author-email: None  
License: Apache License 2.0  
Location: c:\users\dorian-hp1\anaconda3\envs\quantum\lib\site-packages  
Requires: numpy, autograd, semantic-version, scipy, appdirs, toml, networkx  
Required-by: PennyLane-qiskit, PennyLane-Forest  
Platform info: Windows-10-10.0.18362-SP0  
Python version: 3.6.10  
Numpy version: 1.18.1  
Scipy version: 1.4.1  
Installed devices:

- default.gaussian (PennyLane-0.9.0.dev0)
- default.qubit (PennyLane-0.9.0.dev0)
- default.tensor (PennyLane-0.9.0.dev0)
- default.tensor.tf (PennyLane-0.9.0.dev0)
- qiskit.aer (PennyLane-qiskit-0.8.0)
- qiskit.basicaer (PennyLane-qiskit-0.8.0)
- qiskit.ibmq (PennyLane-qiskit-0.8.0)
- forest.numpy\_wavefunction (PennyLane-Forest-0.8.0)
- forest.qpu (PennyLane-Forest-0.8.0)
- forest.qvm (PennyLane-Forest-0.8.0)
- forest.wavefunction (PennyLane-Forest-0.8.0)

there are some devices that have been updated to the latest version and some haven’t. this correct ?  
code for my device

```
   dev = qml.device('qiskit.aer', wires=num_qubits, backend = 
                                         qasm_simulator')
  
   device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

```

QiskitError: ‘Sum of amplitudes-squared does not equal one.’

---

<div class="post-metadata">

**Author:** ![antalszava](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/antalszava/32/171_2.png) [@antalszava](https://discuss.pennylane.ai/u/antalszava)\
**Post date:** [February 28, 2020, 3:23am UTC](https://discuss.pennylane.ai/t/qubitstatevector-pennylane-qiskit-error/246/16 "2020-02-28T03:23:17Z")

</div>

@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!

---

<div class="post-metadata">

**Author:** ![vijpandaturtle](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/vijpandaturtle/32/156_2.png) [@vijpandaturtle](https://discuss.pennylane.ai/u/vijpandaturtle)\
**Post date:** [February 28, 2020, 3:40am UTC](https://discuss.pennylane.ai/t/qubitstatevector-pennylane-qiskit-error/246/17 "2020-02-28T03:40:29Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![antalszava](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/antalszava/32/171_2.png) [@antalszava](https://discuss.pennylane.ai/u/antalszava)\
**Post date:** [February 28, 2020, 5:35am UTC](https://discuss.pennylane.ai/t/qubitstatevector-pennylane-qiskit-error/246/18 "2020-02-28T05:35:30Z")

</div>

@vijpandaturtle I see!

One other thing you could try is to create a new `conda` environment and see if the problem persists. This can be done with the following commands:

```
conda create -n pennylane_1 python=3.6
conda activate pennylane_1
pip install numpy
pip install scipy
cd pennylane/
pip install -e .
pip install pennylane-qiskit
pip freeze | grep PennyLane
python this_is_my_script.py

```

For sure, we’ll let you know once the release is out! 😊

---

<div class="post-metadata">

**Author:** ![antalszava](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/antalszava/32/171_2.png) [@antalszava](https://discuss.pennylane.ai/u/antalszava)\
**Post date:** [February 29, 2020, 5:17am UTC](https://discuss.pennylane.ai/t/qubitstatevector-pennylane-qiskit-error/246/19 "2020-02-29T05:17:07Z")

</div>

@vijpandaturtle: PennyLane 0.8.1 is live on PyPi! 😊

---

<div class="post-metadata">

**Author:** ![vijpandaturtle](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/vijpandaturtle/32/156_2.png) [@vijpandaturtle](https://discuss.pennylane.ai/u/vijpandaturtle)\
**Post date:** [February 29, 2020, 7:37am UTC](https://discuss.pennylane.ai/t/qubitstatevector-pennylane-qiskit-error/246/20 "2020-02-29T07:37:26Z")

</div>

@antalszava could not fix the error.  
i even pulled pennylane-qiskit from github and run pip install e .

---

<div class="post-metadata">

**Author:** ![antalszava](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/antalszava/32/171_2.png) [@antalszava](https://discuss.pennylane.ai/u/antalszava)\
**Post date:** [March 2, 2020, 6:00pm UTC](https://discuss.pennylane.ai/t/qubitstatevector-pennylane-qiskit-error/246/21 "2020-03-02T18:00:39Z")

</div>

@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](https://u.strawberryfields.ai/slack), such that we can discuss further details more quickly. 😊

[Next page](https://discuss.pennylane.ai/t/qubitstatevector-pennylane-qiskit-error/246.md?page=2)
