# Data encoding for a real datasets

**URL:** <https://discuss.pennylane.ai/t/data-encoding-for-a-real-datasets/1090>\
**Category:** PennyLane Help\
**Created:** [June 6, 2021, 2:23pm UTC](https://discuss.pennylane.ai/t/data-encoding-for-a-real-datasets/1090 "2021-06-06T14:23:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SuFong\_Chien](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/sufong_chien/32/170_2.png) [@SuFong\_Chien](https://discuss.pennylane.ai/u/SuFong_Chien)\
**Post date:** [June 6, 2021, 2:23pm UTC](https://discuss.pennylane.ai/t/data-encoding-for-a-real-datasets/1090/1 "2021-06-06T14:23:44Z")

</div>

def get\_angles(x):

```
beta0 = 2 * np.arcsin(np.sqrt(x[1] **2) / np.sqrt(x[0]** 2 + x[1] ** 2 + 1e-12))
beta1 = 2 * np.arcsin(np.sqrt(x[3] **2) / np.sqrt(x[2]** 2 + x[3] ** 2 + 1e-12))
beta2 = 2 * np.arcsin(
    np.sqrt(x[2] **2 + x[3]** 2)
    / np.sqrt(x[0] **2 + x[1]** 2 + x[2] **2 + x[3]** 2)
)

return np.array([beta2, -beta1 / 2, beta1 / 2, -beta0 / 2, beta0 / 2])

```

May I know why e choose np.sqrt(x[1] \*\* 2) / np.sqrt(x[0] \*\* 2 + x[1] \*\* 2 + 1e-12), not np.sqrt(x[0] \*\* 2) / np.sqrt(x[0] \*\* 2 + x[1] \*\* 2 + 1e-12)? Similarly for beta1, x[3] -\> x[4] .

For beta2, can we set np.sqrt(x[0] \*\* 2 + x[4] \*\* 2)  
/ np.sqrt(x[0] \*\* 2 + x[1] \*\* 2 + x[2] \*\* 2 + x[3] \*\* 2) and etc?

Any mathematical explanation here?

This is 4 data in a vector using two qubits. Says, I have a datesets with 8 elements in a vector. It means that I can use method above to represent the data. What about I use 3 qubits to have 2^3 states to represent my data? If hardware is not the issue, what is the merit between these two techniques? Thank you very much.

---

<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:** [June 8, 2021, 1:09am UTC](https://discuss.pennylane.ai/t/data-encoding-for-a-real-datasets/1090/2 "2021-06-08T01:09:41Z")

</div>

Hi @SuFong_Chien,

Thanks for the question! 🙂

> Any mathematical explanation here?

The techniques used here are part of the [[1] Mottonen state preparation](https://arxiv.org/pdf/quant-ph/0407010.pdf) and its modified version for positive vectors only from [Schuld and Petruccione (2018)](https://link.springer.com/book/10.1007/978-3-319-96424-9).

It’s worth noting, that the `x` vector describes the statevector:

\boldsymbol{x} = x\_0|00\rangle+x\_1|01\rangle+x\_2|10\rangle+x\_3|11\rangle

As such, for example, as noted in the State preparation part of [1] (below equation (5)), we would like to zero out the values for the |1\rangle state on qubit n. To achieve this, we specifically pick the 2j-1 components of \boldsymbol{x} where j=1,2 (note that the paper uses 2j as the index in a\_{2j} is done from 1 as opposed to our code where we index from 0).

For two qubits, the uniformly controlled Y rotations correspond to controlled Y rotation gates. These are used in the tutorial. When going for higher qubit numbers, we would then apply the pattern described for uniformly controlled Y rotations on n qubits.

> If hardware is not the issue, what is the merit between these two techniques?

Not sure if I grasp this, could you elaborate on this question?

---

<div class="post-metadata">

**Author:** ![Maria\_Schuld](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/maria_schuld/32/166_2.png) [@Maria\_Schuld](https://discuss.pennylane.ai/u/Maria_Schuld)\
**Post date:** [June 8, 2021, 7:49am UTC](https://discuss.pennylane.ai/t/data-encoding-for-a-real-datasets/1090/3 "2021-06-08T07:49:49Z")

</div>

Just to add my two cents to @antalszava’s great reply (since I wrote the demo), if I remember correctly this was just how the equation worked out in this particular case. It seems to work, but let us know if you suspect a bug…

In general, we could just use the [AmplitudeEmbedding](https://pennylane.readthedocs.io/en/stable/code/api/pennylane.templates.embeddings.AmplitudeEmbedding.html) template here, but this was more explicit. If you use the template, you should be able to encode _any_ normalised vector, even if it has negative entries.

I’m also not sure what you mean by “two techniques” - what is the second one? In simulators, amplitude embedding can be implemented more efficiently than computing angles for a lengthy circuit, since we can just set the initial state vector to the desired values. [AmplitudeEmbedding](https://pennylane.readthedocs.io/en/stable/code/api/pennylane.templates.embeddings.AmplitudeEmbedding.html) will do exactly that when run on a simulator device like `default.qubit`, and it will revert to the circuit decomposition (similar to the one in this example) which is called [MottonenStatePreparation](https://pennylane.readthedocs.io/en/stable/code/api/pennylane.templates.state_preparations.MottonenStatePreparation.html) if used on hardware.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![SuFong\_Chien](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/sufong_chien/32/170_2.png) [@SuFong\_Chien](https://discuss.pennylane.ai/u/SuFong_Chien)\
**Post date:** [June 8, 2021, 10:55am UTC](https://discuss.pennylane.ai/t/data-encoding-for-a-real-datasets/1090/4 "2021-06-08T10:55:13Z")

</div>

Dear all

I am sorry that I shouldn;t use 'two techniques" that causes confusion.  
My question is that if I have input data [a1,a2, a3, a4, a5, a6, a7, a8]. If I want to have state preparation in real quantum computer for these input data for input neurons, I may have two options, i.e., the first is solution is creating two different states a1|00\>+ a2|01\>+a3|10\> +a4|11\> and a5|00\>+ a6|01\>,+a7|10\> +a8|11\>

The second solution is very straightforward i.e., a1|000\>+ a2|001\>+a3|010\> +a4|011\>+a5|100\> + a6|101\> +a7|110\> + a8|111\>

By looking at these two solutions, what are the advantages and disadanvetges for these data encodings? (Supposing we have no problem with the hardware)

I am thinking to code my data for IBM composer if you can give the amplitude encoding example for the composer, it will help lots. Thank you.

---

<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:** [June 9, 2021, 1:41am UTC](https://discuss.pennylane.ai/t/data-encoding-for-a-real-datasets/1090/5 "2021-06-09T01:41:51Z")

</div>

Hi @SuFong_Chien,

I see that’s a good question! Frankly, I’m not sure which one could be more beneficial in your case, maybe it’s worth checking on a simulator beforehand? There are many aspects to consider, e.g., the accuracy, the depth of the circuits to simulate, queuing with the real hardware, etc.

> I am thinking to code my data for IBM composer if you can give the amplitude encoding example for the composer, it will help lots

Not entirely sure about this, could perhaps the documentation part that Maria linked help here? It contains an example of using `qml.AmplitudeEmbedding`. Once the circuit looks good, the device can then be switched by specifying the [IBMQ device](https://pennylaneqiskit.readthedocs.io/en/latest/devices/ibmq.html) from PennyLane-Qiskit. Alternatively, let us know where it would be useful to have a more detailed example as help.
