# Superposition on a subset of integers [duplicate]

Assuming I have $$n$$ qubits and I want to create a superposition out of a subset of integers:$$k∈\{1,...,2^n\},$$ how can I create a circuit that creates a uniform superposition of, for example, $$k = 3$$ and what would this look like? All the qubits are initialized in the state $$|0\rangle$$.

$$|\psi\rangle = \frac{1}{\sqrt{k}}\sum_{j=0}^{k-1}|j\rangle$$

Fixing $$k$$ to 3, 5, 7, etc. Can someone give me guidance on how to scale this approach?

• How do you determine which values are in your subset? Is there some sort of easy to computer function? Or is it just an arbitrarily awkward list? Is it a large subset, or a small one? Sep 19, 2023 at 14:46
• The subset follows the summation I posted, e.g. I can pick any number of qubits, say n=3. Then I fix any subset of the bits, e.g. k=3. Based on this, I'd want to create the following state: 1/sqrt(3)*(|000> + |001> + |011>). If I fix k = 5, I build a new circuit to get the following state: 1/sqrt(5)*(|000> + |001> + |010> + |011> + |100>) Sep 19, 2023 at 14:56
• @CraigGidney I appreciate the link but my knowledge of quantum circuits is too little to benefit from understanding this. Sep 20, 2023 at 7:23

Perhaps the easiest way is to find $$n=\lceil\log_2(k+1)\rceil$$. Take $$n$$ qubits in the state $$|0\rangle$$ and apply Hadamard to each of them.
If $$k+1$$ was a power of 2, you're done! If not, get an ancilla, and implement some logic that flips the ancilla to 1 if the number stored in your set of $$n$$ qubits is bigger than $$k-1$$. Finally, measure the ancilla. If it's $$|0\rangle$$, you're done. This happens with a probability of at least 50%. If not, throw everything away and start again. On average, you require fewer than 2 goes to prepare your state.
How does the logic work for setting the ancilla? Worst case, you've got a list of numbers $$k$$ to $$2^n-1$$. For each of them, do an $$n$$-controlled-not to detect the presence of that specific number, and target the ancilla if that number is present. But you can save quite a lot of logic with a bit of care. For example, if you don't want either $$2^n-1$$ or $$2^n-2$$, you can do a multi-controlled-not off the $$n-1$$ most significant bits, and ignore the least significant bit.
• Well you're asking me about a method that is not the method I was suggesting... (mine does not require fancy angles). Nevertheless, what you usually end up doing is grouping terms on a single qubit $|0\rangle|\psi\rangle+|1\rangle|\phi\rangle$. Once you normalise the states, you get coefficients in front of the two terms, and those are basically the values of $\cos\frac{\theta}{2}$ and $\sin\frac{\theta}{2}$ that you're trying to get from a rotation. Sep 20, 2023 at 8:08