`qml.probs` returns values greater than zero

Hello everyone! :slight_smile:

While using qml.AmplitudeEmbedding and qml.probs(), I encountered a strange situation. When I execute the code below, the circuit gives me a different result depending on the dtype of pennylane.numpy.tensor.tensor (that’s the only difference I noticed).

import pennylane as qml
from pennylane import numpy as np

dev = qml.device("default.qubit", wires=4)

@qml.qnode(dev)
def circuit(image):
    qml.AmplitudeEmbedding(features=image, wires=range(4), normalize=True)

    return qml.probs(wires = range(4))

test = np.array([8,  33,  38,  21,  38, 106, 114,  53,  46,  70,  75,  54,  30, 53,  54,  36])
display(test)
display(type(test))
display(circuit(test))

display(X_train[0])
display(type(X_train[0]))
display(circuit(X_train[0]))

Running the above code:

tensor([  8,  33,  38,  21,  38, 106, 114,  53,  46,  70,  75,  54,  30,
         53,  54,  36], requires_grad=True)

pennylane.numpy.tensor.tensor

tensor([0.00116362, 0.01979964, 0.02625407, 0.00801804, 0.02625407,
        0.20428719, 0.23628661, 0.0510718 , 0.03847203, 0.08908929,
        0.10227087, 0.05301722, 0.01636334, 0.0510718 , 0.05301722,
        0.02356321], requires_grad=True)

tensor([  8,  33,  38,  21,  38, 106, 114,  53,  46,  70,  75,  54,  30,
         53,  54,  36], dtype=uint8, requires_grad=True)

pennylane.numpy.tensor.tensor

tensor([0.02825607, 0.4807947 , 0.63752759, 0.19470199, 0.63752759,
        4.9607064 , 5.73774834, 1.2401766 , 0.93421634, 2.16335541,
        2.48344371, 1.28741722, 0.39735099, 1.2401766 , 1.28741722,
        0.57218543], requires_grad=True)

test variable is the dtype('int32').I’m using qml.version = ‘0.36.0’.

Should it be like that? Should qml.probs only returns values from 0 to 1?

Thank you for your help :slight_smile:

Hi @Martyna_Czuba , you’re right.

The dtype is causing problems here. You can set it specifically by using the dtype keyword argument as shown below. Do you want to open the bug report yourself? If not I can open it. Let me know.

test = np.array([8, 33, 38, 21, 38, 106, 114, 53, 46, 70, 75, 54, 30, 53, 54, 36], dtype='int32')

And thanks again for reporting this!

I’ve reported the Bug.

1 Like

Thank you @Martyna_Czuba ! :raised_hands:

Hi @Martyna_Czuba, it seems that one of our functions is having an overflow with uint8. One quick fix that you can add in your code in case you can’t change the dtype is dividing your data by 256. Eg: x/256

I tried this with your test data and it works. Let me know if you have any issues with this! We’re still looking into options for fixing this more permanently.

test3 = np.array([8,  33,  38,  21,  38, 106, 114,  53,  46,  70,  75,  54,  30, 53,  54,  36],dtype='uint8')/256

Hi @CatalinaAlbornoz, thx you for the quick respons and the suggestion. I handled it calmly. This bug isn’t holding me back in any way. I mannually change the dtype during dataset generation to ensure it’s correct.
I’m glad you’ll take care of the bug :slight_smile:

I’m glad to hear @Martyna_Czuba!

Thanks again for reporting this. You’re helping us make PennyLane better :heart:

Hi @Martyna_Czuba ,
I wanted to let you know that this issue has been fixed in this Pull Request! The fix is now available in the development version of PennyLane and it will be in the next stable version (v0.38).

1 Like

@CatalinaAlbornoz, thank you for your work. It’s wonderful to hear that. :slight_smile:

1 Like