3
$\begingroup$

How does one perform amplitude encoding using only unitary gates ?

Could you show me a concrete example ?

$\endgroup$

2 Answers 2

7
$\begingroup$

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')

enter image description here

$\endgroup$
2
  • $\begingroup$ Thank you ! Can you explain what decompose does exactly ? How did you choose reps=4 ? $\endgroup$
    – Duen
    Mar 7, 2023 at 7:41
  • $\begingroup$ 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. $\endgroup$ Mar 7, 2023 at 10:56
2
$\begingroup$

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])

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.