# How does one perform amplitude encoding using only unitary gates?

How does one perform amplitude encoding using only unitary gates ?

Could you show me a concrete example ?

In amplitude encoding, we encode data into the amplitudes of a quantum state. So, dataset is represented as a normalized classical $$2^n$$-dimensional vector, which can be thought of as a quantum state of $$n$$ qubits. Then, we create the quantum circuit that prepares this state.

As an example, assume that your dataset contains two points:

$$D = \{(1, 3), (0, 2)\}$$ We write it as a vector $$V = [1, 3, 0, 2]$$ Then we normalize it $$V' = \frac{1}{\sqrt{14}}[1, 3, 0, 2]$$ This is a two-qubit quantum state which can be written as: $$|V\rangle = \frac{1}{\sqrt{14}}(|00\rangle + 3|01\rangle + 2|11\rangle)$$ So, we can embed it as follows:

from qiskit import QuantumCircuit
import numpy as np

state = (1 / np.sqrt(14)) * np.array([1, 3, 0, 2])
num_qubits = 2
circ = QuantumCircuit(num_qubits)
circ.prepare_state(state, [0, 1])


And to draw the circuit:

circ.decompose(reps = 4).draw('mpl')


• Thank you ! Can you explain what decompose does exactly ? How did you choose reps=4 ?
– Duen
Commented Mar 7, 2023 at 7:41
• If your circuit contains blocks that is composed of sub-blocks or basis gates, decompose expands these blocks. It does this one level only (shallow decompose). To repeat this operation, set reps to the number of times the circuit should be decomposed. I chose $4$ by trail and error. Commented Mar 7, 2023 at 10:56

The prepare_state method is not a standard method in Qiskit anymore, and it has been removed in recent versions.

To prepare an initial quantum state in Qiskit, you can use the initialize method, which takes a state vector as an input and prepares the qubits in the circuit according to that state vector.

Here is above example using the initialize method in Qiskit:

from qiskit import QuantumCircuit

import numpy as np

state = (1 / np.sqrt(14)) * np.array([1, 3, 0, 2])

num_qubits = 2

circ = QuantumCircuit(num_qubits)

circ.initialize(state, [0, 1])