Should I declare Hadamard gate before calling the Basis Encoding method

The problem transform from github discussion :

At first, thanks for the developer’s reply. However, I am afraid that the problem still exists in some cases.

for example.
I attempt to encode 2 data examples that have 3 dimensions into a quantum state. (x1=[0, 1, 1], x2 = [1, 1, 0] )

So, the resulting circuit maybe

0 -------ID----------X--------- 1
0 -------X-----------X--------- 0
0 -------X-----------ID-------- 1

In the above result, their have 2 problem →

  1. PauliX gate can not make the superposition, so the coefficient may not reflect the probability of quantum states, namely the probability to measure the x1 as well as x2 is zero. Furthermore, we are only able to measure the result x = [1, 0, 1] which is not the original data point.

  2. In the single data example, the PauliX may produce the suitable result (as the developer reply). For example, x = [1, 0, 0, 1] just flip bit 0 and 3, then done~
    However, the double PauliX gate will produce the Identity transformation in the corresponding wire.
    For more specific, suppose x1= [0, 0, 1], x2 = [0, 1, 0], x3 = [0, 1, 1]
    While we encode the 3 data example, the result will be x = [0, 0, 0]…
    Due to the last 2 bit transformed by PauliX twices without superposition.

============================================

By the way, in the tfq tutorial mnist, which use the independent circuit to perform the Basis Encoding (then everything make sense like the developer’s reply – just flip bit by PauliX).

However, I found that Pennylane stack the data examples into only 1 circuit as the following blog plot!!

source : All about Data Encoding for Quantum Machine Learning | by Baijayanta Roy | Jul, 2021 | DataDrivenInvestor

Any suggestion will be appreciate !!

Hi @1111! Thank you very much for your question and for providing so much detail. We are looking into it and will be back soon with an answer!

1 Like

Thanks for PennyLane developer support~~
I’m look forward to get any suggestion or answer ~

Hi 1111,

Thanks for the question and welcome to the forum!

BasisEmbedding will enode classical data into a quantum basis s this can be used diectly to encode your two states without the need for PauliX gates:


classical_data = [[0, 1, 1],  [1, 1, 0]]
quantum_basis = [BasisEmbedding(data, wires = range(3)) for data in classical_data ] 

which returns quantum_basis as

[BasisEmbedding([0, 1, 1], wires=[0, 1, 2]),
 BasisEmbedding([1, 1, 0], wires=[0, 1, 2])]

Since a superposition state is a normalised linear combination of basis states this cannot be directly embedded using this method. A Hadamard operation would be a good option to create a superposition state as you have suggested. This highlights a subtle difference between basis embedding and state preparation.

Please let us know if you have any more questions!

1 Like

Many thanks for your reply.
Maybe I have a little confusion about the difference between state preparation and basis embedding.

I thought the basis embedding is one of the methods to prepare the basis encoding ( QuantumComputingPatterns. )

So, can we prepare the basis encoding by directly using the BasisEmbedding function? or there exists the other function in Pennylane that offer such functionality?

Hi 1111,

You are correct in thinking that basis embedding is one of the methods to prepare the basis encoding. Indeed, basis embedding associates each classical input number with a computational basis state of a qubit system.

Pennylane’s BasisEmbedding class will do exactly this using the intermediate step of translating a classical number into a binary representation e.g 5 = [1, 0, 1]. We would then pass this binary representation to BasisEmbedding as done so above.

Embedding a basis in this way is generally an initial step for quantum machine learning. Similar to how classical input is only acceptable to the neural network in the form of embedding vectors. The same is true for quantum neural networks; they require embedded input before the next steps of the machine learning algorithm can be performed.

Quantum information processing can be performed on embedded data such as applying Hadamard gates to create superposition states or applying CNOT gates introduce entanglement . This allows for the quantum machine learning.

Please let us know if you have further questions!

1 Like

Very thanks for your introduction. Now, I’m clear about the related concepts ~~ !!

1 Like

Great, let us know if you have any more questions :slight_smile:

1 Like