QubitStateVector pennylane-qiskit error

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

I have obviously checked that this is True:

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!

Hi @RicardoGaGu!

To extract the probabilities, you can use the dev.probabilities() method:

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:

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

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

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

Thanks,

Ricardo

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:

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