from qiskit.quantum_info import Statevector
from util import zero, one
Appendix: Binary Notation#
The notation
\[
\overline{j} = j_{n-1} \dots j_0
\]
converts the number \(j\) into its binary expansion (in little Endian) in \(n\) bits, i.e.,
\[\begin{align*}
j & = j_{n-1} 2^{n-1} + j_{n-2} 2^{n-2} + \dots + j_0 2^0 \,.
\end{align*}\]
def to_binary(num_qubits: int, j: int) -> list[int]:
return [int(x) for x in f"{j:0{num_qubits}b}"]
The notation
\[
|\overline{j}\rangle = |j_{n-1} \dots j_0 \rangle
\]
thus indicates a computational basis in \(n\) qubits. As an example,
\[
|\overline{6}\rangle = |110\rangle
\]
for \(n = 3\).
def idx_to_basis(num_qubits: int, j: int) -> Statevector:
vecs = [zero if x == 0 else one for x in to_binary(num_qubits, j)]
acc = vecs[-1]
for v in vecs[-2::-1]:
acc = v ^ acc
return acc
idx_to_basis(3, 0).draw("latex")
\[ |000\rangle\]
idx_to_basis(3, 1).draw("latex")
\[ |001\rangle\]
idx_to_basis(3, 2).draw("latex")
\[ |010\rangle\]
idx_to_basis(3, 3).draw("latex")
\[ |011\rangle\]
idx_to_basis(3, 4).draw("latex")
\[ |100\rangle\]
idx_to_basis(3, 5).draw("latex")
\[ |101\rangle\]
idx_to_basis(3, 6).draw("latex")
\[ |110\rangle\]
idx_to_basis(3, 7).draw("latex")
\[ |111\rangle\]