How does one perform amplitude encoding using only unitary gates ?
Could you show me a concrete example ?
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')
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])