# How to construct a IBM Quantum Experience circuit for the following state transformation?

Please help me in building IBM Quantum Experience circuit for: $$M|0\rangle = \frac{1}{2}(|0\rangle+|1\rangle+|2\rangle+|3\rangle)$$

Edit: Is it possible to make a circuit for a general transformation $$M|0\rangle = \frac{1}{\sqrt{m}}\sum_{i=0}^{m-1}|i\rangle$$ acting on $$\log m$$ qubits initialized to $$|0\rangle$$?

• Simply put Hadamard gates on two qubits and that's it. – Martin Vesely Feb 12 '20 at 19:22
• @MartinVesely can we give circuit for a general transformation $M|0\rangle = \frac{1}{\sqrt{m}}\sum_{i=0}^{m-1}|i\rangle$ by initializing $log~m$ qubits to $|0\rangle$. – Adam Levine Feb 13 '20 at 3:10

It is possible to prepeare a state

$$|\psi\rangle = \frac{1}{\sqrt{m}}\sum_{i=0}^{m-1}|i\rangle,$$

where $$m = 2^n$$ and $$n$$ is a number of qubits, by application of operator $$\otimes H ^{n} = H_{q_0} \otimes H_{q_1} \otimes \dots \otimes H_{q_{n-1}}$$, i.e. Hadamard gate is applied on each qubit involved. As a results, you will get $$n=\log{m}$$ qubits in superposition described by uniform probability distribution. It is a generalization of construction provided in answer by met927

EDIT: based on comment by Adam Levine: "what if for example $$m=5$$?"

It that case you have to prepare entangled state and the approach is a little bit more intricated. For $$m=5$$ you have to prepare state (I switched to more common convention with states expressed in binary numbers instead of decimal):

$$|\psi\rangle = \frac{1}{\sqrt{5}}(|000\rangle + |001\rangle + |010\rangle+|011\rangle+|100\rangle),$$

i.e. three qubits state where $$|101\rangle$$, $$|110\rangle$$ and $$|111\rangle$$ have zero probability.

To prepare an arbitrary state, you can employ approach presented in this article: Transformation of quantum states using uniformly controlled rotations.

Based on the article, a circuit preparing state $$|\psi\rangle$$ is this: Here you can see results provided by IBM Q simulator: Apparently, the circuit prepared desired state with uniformly distributed values from $$|000\rangle$$ to $$|100\rangle$$.

EDIT 2: here is a code in QASM of the circuit above:

OPENQASM 2.0;
include "qelib1.inc";

qreg q;
creg c;

ry(0.927) q;

ry(pi/4) q;
cx q,q;
ry(pi/4) q;
cx q,q;

ry(pi/2) q;
cx q,q;
ry(-pi/4) q;
cx q,q;
ry(pi/4) q;
cx q,q;
cx q,q;

measure q -> c;
measure q -> c;
measure q -> c;

• If $m \neq 2^{n}$ for example $m=5$, what should be the approach? – Adam Levine Feb 13 '20 at 5:49
• @AdamLevine: Good question, see expanded answer. – Martin Vesely Feb 13 '20 at 7:16
• Please share code for the above circuit – Adam Levine Feb 13 '20 at 9:38
• Dear Martin Vesely, following the paper - " Transformation of quantum states using uniformly controlled rotations." did you solve it by pen and paper? In paper, $|a\rangle \mapsto |e_{1}\rangle$ is given (this is not applicable for our transformation since it is already in $|e_{1}\rangle$ form), but how do I convert $|e_{1}\rangle \mapsto |b\rangle$ (where $|b\rangle$ is the state we require)? – Adam Levine Feb 14 '20 at 15:17
The circuit you have described is formed of 2 qubits, both in an equal superposition. This can be achieved by applying the H gate to both qubits, as this puts each qubit into superposition and takes us up to 4 possible states. In the IBM Quantum Experience, this circuit would look like 