Data encoding on forest.qvm

Hi @avinash_ch! We can’t reconstruct the quantum state from the marginal probability distributions. However, we can go in the reverse order:

  • From the quantum state, we can compute the joint probability distribution of all qubits.

  • From the joint probability distribution, we can compute the marginal probabilities.

For example, consider the following program:

import pennylane as qml
import numpy as np

dev = qml.device("default.qubit", wires = 2)
f = np.array([1,2,3,4])

@qml.qnode(dev)
def marginal_probs(features=None):
    qml.templates.AmplitudeEmbedding(features, wires=[0,1], pad=None, normalize=True)
    return [qml.probs(wires=[0]), qml.probs(wires=[1])]

@qml.qnode(dev)
def joint_probs(features=None):
    qml.templates.AmplitudeEmbedding(features, wires=[0,1], pad=None, normalize=True)
    return qml.probs(wires=[0, 1])

marginals = marginal_probs(features=f)
joint = joint_probs(features=f)

Now, generating the probabilities from the quantum state:

>>> print("Initial state : ", dev.state)
>>> print("Final state : ", dev.state)
>>> print("Joint probabilities: ", joint)
>>> print("State absolute squared: ", np.abs(dev.state)**2)
Initial state :  [1.+0.j 0.+0.j 0.+0.j 0.+0.j]
Final state :  [0.18257419+0.j 0.36514837+0.j 0.54772256+0.j 0.73029674+0.j]
Joint probabilities:  [0.03333333 0.13333333 0.3        0.53333333]
State absolute squared:  [0.03333333 0.13333333 0.3        0.53333333]

Then, getting the marginal probabilities:

>>> print("Marginal probabilites on wire 0: ", marginals[0])
>>> print("Marginal prob 0 from joint: ", np.sum(joint.reshape([2, 2]), axis=1))
Marginal probabilites on wire 0:  [0.16666667 0.83333333]
Marginal prob 0 from joint:  [0.16666667 0.83333333]
>>> print("Marginal probabilites on wire 1: ", marginals[1])
>>> print("Marginal prob 1 from joint: ", np.sum(joint.reshape([2, 2]), axis=0))
Marginal probabilites on wire 1:  [0.33333333 0.66666667]
Marginal prob 1 from joint:  [0.33333333 0.66666667]