# How to create superposition of all bitstrings with exactly two ones on 4 qubits?

On a quantum circuit, how would I create an equal superposition of the states:

$$|\psi\rangle=|0011\rangle + |0101\rangle + |0110\rangle + |1001\rangle + |1010\rangle + |1100\rangle.$$

• – glS
Commented Nov 7, 2022 at 7:21

The desired state may be rewritten$$^1$$ as $$|\psi\rangle=|0\rangle\otimes|\overline{W}\rangle+|1\rangle\otimes|W\rangle$$ where $$|W\rangle=|001\rangle+|010\rangle+|100\rangle$$ and $$|\overline{W}\rangle=|110\rangle+|101\rangle+|011\rangle=XXX|W\rangle$$.

Thus, we can obtain $$|\psi\rangle$$ in three steps. First, initialize four qubits $$1$$,$$2$$,$$3$$ and $$4$$ to $$|0000\rangle$$. Next, apply Hadamard to qubit $$1$$ and a circuit$$^2$$ that creates the W state to qubits $$2$$, $$3$$ and $$4$$. Then, apply $$X$$ to qubits $$2$$, $$3$$ and $$4$$. Finally, execute three controlled-NOT gates, each controlled by qubit $$1$$ and having qubits $$2$$, $$3$$ and $$4$$ as the target, respectively.

$$^1$$ As in the question, we ignore normalization.

$$^2$$ See here for an example of such a circuit.

• And the magic angle in the circuit of Footnote 2 is $cos^{-1}(-1/3)$ Commented Nov 8, 2022 at 2:32

While @Adam-Zalman's answer above is probably optimal, you can use Qiskit to get an answer to any state vector initialization problem.

from qiskit import QuantumCircuit
import numpy as np

qc = QuantumCircuit(4)
# Create the desired state vector
vector = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0]) / np.sqrt(6)
qc.prepare_state(vector)
# So we can see what the final circuit really looks like:
qc = qc.decompose(reps=10)
qc.draw(fold=-1)


If you need to just quickly get the qubits into a particular state so that you can work on doing something else, this is the right solution. If the goal is to reach a particular solution optimally and understand how you got there, the other answer is best.

[Note: prepare_state() is the same as initialize(), except it doesn't zero the wires at the start. The result is a unitary gate.]