# How to create quantum circuits from scratch

I am doing self-study at the moment using primarily the book: Quantum Computing a Gentle Introduction by Eleanor Rieffel and Wolfgang Polak.

Getting through the earlier chapters and exercises went quite well (fortunately the earlier chapters had plenty of examples), however I got stuck on the 5th chapter on quantum circuits. Although I understand the concepts the authors present, perhaps due to a lack of examples, I have trouble applying said concepts to the exercises.

The exercises I have trouble with (and where I can't find a solution or thorough/ introductory explanation for) are the following:

$$\\$$

Questions:

Design a circuit for creating: $$\left| W_n \right> = \frac{1}{\sqrt{n}}(\left| 0 \dots 001 \right> + \left| 0 \dots 010 \right> + \left| 0\dots 100 \right>) + \cdots + \left| 1\dots 000 \right>)$$ from $$\left| 0 \dots 000 \right>$$

And design a circuit for creating "the Hardy state": $$\frac{1}{\sqrt{12}}(3\left| 00 \right> + \left| 01 \right> + \left| 10 \right> + \left| 11 \right>)$$

$$\\$$

Can somebody point me in the right direction or refer me to some literature/ tutorials so I can grasp these kind of exercises better?

$$\\$$

Perhaps a related question: Tips and tricks for constructing circuits to generate arbitrary quantum states

• I'm not familiar with the Hardy state, but can you check what you've written? It's not normalised (and kind of trivial), so I'm guessing it's not what you intended. For the W-state, you probably want to check out this question. – DaftWullie Jul 22 at 13:23
• You're right, I made some typos. I've edited them, now they are correct/ normalized. And thank you! – Joery Jul 22 at 13:26
• By the way, this paper arxiv.org/abs/quant-ph/0104030 gives a general technique to construct arbitrary quantum states. – Paradox Jul 23 at 14:18

As DaftWullie pointed out, the question about $$W_n$$ has an excellent collection of answers here.

For the Hardy state question (and a lot of other tasks like it), you can approach it as follows.

• Start with the $$|0...0\rangle$$ state.
• Start by putting the first qubit "in the right state", which is a state $$(\alpha |0\rangle + \beta |1\rangle) \otimes |0...0\rangle$$, where $$\alpha$$ and $$\beta$$ are the relative weights of all basis states which start with 0 and with 1, respectively. For Hardy state specifically, two basis states start with 0: $$\frac{1}{\sqrt{12}}(3\left| 00 \right> + \left| 01 \right>)$$ and two basis states start with 1: $$\frac{1}{\sqrt{12}}(\left| 10 \right> + \left| 11 \right>)$$; their relative weights are just the sums of squares of their amplitudes: $$\frac{9}{12} + \frac{1}{12} = \frac{10}{12}$$ and $$\frac{1}{12} + \frac{1}{12} = \frac{2}{12}$$, respectively. So you'll need to put the first qubit in the state $$(\sqrt{\frac{10}{12}} |0\rangle + \sqrt{\frac{2}{12}} |1\rangle)$$ using $$R_y$$ gate.
• Continue by putting the second qubit in the right state, applying controlled $$R_y$$ gates with the first qubit as the control. To get the first two terms right, you need to convert the term $$\sqrt{\frac{10}{12}} |0\rangle \otimes |0\rangle$$ into the term $$\frac{1}{\sqrt{12}}(3\left| 00 \right> + \left| 01 \right>)$$, which is the same as convert normal state $$|0\rangle \otimes |0\rangle$$ into $$\frac{1}{\sqrt{10}}(3\left| 00 \right> + \left| 01 \right>)$$ without affecting the state $$|1\rangle \otimes |0\rangle$$ (note the renormalization when switching from terms of a larger expression to standalone states!) To do this, you can do a 0-controlled $$R_y$$ with the first qubit as control and the second qubit as target.
• If you have more qubits, you will continue doing this, using more control qubits to make your rotations more and more specific.

You can see this paper by Shende, Bullock and Markov if you want a more formal and less ad-hoc explanation.

• Your answer is great! I think I got stuck because I tried doing this in a top-down approach, i.e., starting from the final state and trying to find a decomposition and gates towards the base state. This and the paper are great help, thanks! – Joery Jul 23 at 8:28

You can simplify "produce a state" problems by breaking them into three parts:

1. Prepare the collection of magnitudes you'll need, without worrying about phase or which state has which magnitude.
2. Fix the phases.
3. Fix the ordering.

Now consider the Hardy state. What are the magnitudes that we need to make? We need one instance of $$3/\sqrt{12}$$ and three instances of instance of $$1/\sqrt{12}$$. We can make them one at a time, by having a "remaining amplitude" state that we keep splitting off of.

We start with all the amplitude in one state with an excitation on the left, $$\ell_0 |1000...00\rangle$$ where $$\ell_0=1$$. What we want to do is move the excitation rightward while leaving behind the desired magnitudes. So to start we want to leave behind the magnitude $$3/\sqrt{12}$$. We can do that with a controlled $$R_y(\theta_0)$$ operation, where the control is the leftmost qubit and the target is the qubit just to its right. By picking just the right value for $$\theta$$, this will result in the state $$3/\sqrt{12}|1000...00\rangle + \ell_1 |1100...00\rangle$$. We then CNOT the second qubit back onto the first qubit to get to $$\ell_1|1000...00\rangle + 3/\sqrt{12} |0100...00\rangle$$. Next we want to pull off $$1/\sqrt{12}$$. We perform another $$R_y$$ controlled by the leftmost qubit followed by a backwards CNOT, but this time with the target is the qubit third from the left. By picking the perfect $$\theta_1$$ we will produce the state $$\ell_2|1000...00\rangle + 3/\sqrt{12}|0100...00\rangle + 1/\sqrt{12} \ell_2 |0010...00\rangle$$. And you just keep doing this until you've got all the amplitudes you need, conveniently addressed by individual qubits being excited.

Now you want to fix any incorrect phases produced by the Y rotations. For the Hardy state this is easy, because all the phases are positive. In general you target each qubit position $$k$$ with an $$R_z(\phi_k)$$ operation with appropriately chosen $$\phi_k$$ values, and that will get the phases right.

Now we want to get the ordering right. The easiest way to do this is to have some extra qubits that are your output qubits and, for each of the qubits we've prepared so far and each of the output qubits, either add a CNOT between the two or don't. For example, if the state with amplitude $$3/\sqrt{12}$$ is supposed to be a $$|11\rangle$$, then we need to CNOT from our leftmost qubit onto both of the output qubits. Then we need to uncompute the leftmost qubit by using a many-controlled NOT operation. There should be one control for each output qubit, and the control's type (qubit-must-be-on vs qubit-must-be-off) is determined by whether or not you toggled the qubit.

Applying these steps produces an inefficient, but correct, circuit for creating a Hardy state. You can open the circuit in Quirk:

If you want to produce a state without using so much workspace, the task gets harder. But you can still follow the magnitudes then phases then ordering pattern. Also, there are cleverer ways to prepare magnitude sets that have nice patterns. For example, when only one amplitude is different from the others, one round of partial amplitude amplification may be enough to prepare the state.