# How does one create the unitary sending $|0\rangle$ into a target quantum state?

The Hadamard gate allows us to construct an equal superposition of states. If one wants to construct an arbitrary superposition e.g. $$\alpha\vert 0\rangle + \beta\vert 1\rangle + ..$$, how does one create the corresponding unitary?

Such a unitary should act on $$\vert 0\rangle$$ and output the desired state. Also, in more generality, it should take in $$\vert 0\rangle^{\otimes n}$$ and output some coherent superposition state of my choice.

I ask this question since many quantum algorithms encode a classical vector $$(x_1, x_2,... x_n)$$ into a quantum state $$\sum_i x_i\vert i\rangle$$ but this step itself seems hard to achieve.

If you want to generate a state $$|\psi\rangle$$ from $$|0\rangle$$, you can use any unitary whose first column is $$|\psi\rangle$$ (assuming you are using a convention in which $$|0\rangle$$ represents the first row/column, as is usually done).

More generally, if you are looking for a unitary $$U$$ such that $$U|\psi\rangle=|\phi\rangle$$ for some given $$|\psi\rangle$$ and $$|\phi\rangle$$, you can choose any $$U$$ of the form $$U = |\phi\rangle\!\langle\psi| + U'$$ where $$U'=\sum_j |\phi_j\rangle\!\langle \psi_j|$$ for any choice of orthonormal bases $$\{|\phi_j\rangle\}_j$$ and $$\{|\psi_j\rangle\}_j$$ which span the spaces $$|\phi\rangle_\perp$$ and $$|\psi\rangle_\perp$$, respectively.

To create arbitrary one qubit state you can use combination of $$Rz$$ and $$Ry$$ gates. $$Ry(\theta)$$ gate takes state $$|0\rangle$$ to $$\cos(\theta/2)|0\rangle + \sin(\theta/2)|1\rangle$$. By setting angle $$\theta$$ you achive desired probabilities of states $$|0\rangle$$ and $$|1\rangle$$. If you now apply $$Rz(\varphi)$$ gate you get state $$\mathrm{e}^{-i\varphi/2}\cos(\theta/2)|0\rangle + \mathrm{e}^{i\varphi/2}\sin(\theta/2)|1\rangle$$ which is equivalent to $$\cos(\theta/2)|0\rangle + \mathrm{e}^{i\varphi}\sin(\theta/2)|1\rangle$$ because global phase can be neglected. Hence by setting angles $$\varphi$$ you can set any phase you want. Overall, the state above is the most general description of a qubit. So, this method allows you to prepare one qubit in any state you want.

Note that on IBM Q you can employ gate $$U3$$ instead.

To prepare multiqubit state is more difficult. You can follow a method in this article: Transformation of quantum states using uniformly controlled rotations.

In Qiskit, you can use a method initialize to set a quantum circuit input state:

quantumState = [
1 / math.sqrt(2) * complex(1, 0),
1 / 2 * complex(1, 1)]

q = QuantumRegister(1, name = 'q')
c = ClassicalRegister(1, name = 'c')

circuit = QuantumCircuit(q,c)

circuit.initialize(quantumState, [q])


In this case, one qubit state is prepared, however, matrix quantumState can be for example

[1 / math.sqrt(2), 0, 0, 1 / math.sqrt(2)]


for Bell state $$\frac{1}{\sqrt{2}}(|00\rangle+|11\rangle)$$.

Short answer: Really good question! We know how to do it in exponential time (details below) but there is a lot of ongoing research on how to do it in polynomial time.

Longer answer: If you want to use gates, you can always encode it in exponentially many time steps using a combination of controlled $$R_x, R_y$$ and $$R_z$$ gates. As an example, try creating the state $$\sqrt{\frac{1}{8}}|00\rangle + \sqrt{\frac{1}{8}}|01\rangle + \sqrt{\frac{5}{8}}|10\rangle + \sqrt{\frac{1}{8}}|11\rangle$$ using only $$R_y$$ and controlled-$$R_y$$ gates. However for a general case with more quibts this can quickly turn into a very long circuit! There are some ways to load the right state in polynomial time given certain assumptions about the state. But this is an ongoing research question that many of us are working on! For a few ideas out there you can see e.g. the following references: