Encoding in real space

Hi, is there is a way to confine encoded amplitudes (MottonenStatePreparation) to real space? Naively, if the input vector contains negative real numbers, they seem to encoded in imaginary numbers, which is a problem when I try to read (‘decode’) the statevector later (i.e. if I take the absolute amplitude of the complex number, I lose the sign of the original numbers from the input vector).

Sorry if I have not phrased the question properly, thank you.

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

@qml.qnode(dev)
def encode(f, wires):
      qml.templates.MottonenStatePreparation(f, wires=wires)
      return qml.probs(wires=list(range(3)))

state= [1,1,1,1,-1,-1,-1,-1]/np.sqrt(8)
print('state \n',state)
encode(f=state,wires=list(range(3)))
print('encode \n',dev.state)
print(qml.draw(encode)(f=state,wires=list(range(3))))

Hi @leongfy, very good question. It seems that making this work is not trivial.

This tutorial might be useful though.

@Maria_Schuld, do you have any thoughts on this?

Hey @leongfy,

You are totally right, it is a property of the MottonenStatePreparation algorithm that amplitude vectors are prepared up to a global phase factor (which is unmeasurable in quantum mechanics as you may know, but can cause quite a lot of headaches in simulations). For example, [0+i*a1, 0+i*a2] and [0i+a1, 0i+a2] are the same quantum state up to the global phase (-i).

Programmatically, there are two things you can do: either you find a state preparation algorithm that uses a different global phase (which, I can assure you, takes a bit of time because they are quite nasty to pick apart), or you use QubitStateVector for simulators to set your desired state as the initial state:

import pennylane as qml
from pennylane import numpy as np

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

@qml.qnode(dev)
def encode(f, wires):
      qml.QubitStateVector(f, wires=wires)
      return qml.state()

state= ([1,1,1,1,-1,-1,-1,-1]/np.sqrt(8))
res = encode(f=state, wires=list(range(3)))
print(np.allclose(res, state)) # is True

The second solution is simple and fast, but some devices and differentiation methods may not work with this state preparation routine.

Lastly, I am not 100% sure why the global phase is worrying you? Even if the negative signs are encoded in the real part of an amplitude, taking the absolute square will annihilate the sign? I think the idea of amplitude encoding is more that you do some processing afterwards that uses the signs… just curious :slight_smile:

Thank you very much for your clear explanation!