Superposition and Measurement


A qubit register state of $n$ qubits can be thought of as a complex vector space:

Scotty uses this idea to represent the superposition of all qubit states by introducing a trait called Superposition. It extends State and declares an array of complex numbers with several helpers like qubitCount, probabilities to get a sequence of probabilities for each possible state, and applyGate to apply a gate to the current state.

The State trait itself references a QubitRegister, which is a convenience for the user if they need to get the original state quickly or map qubit labels to the current superposition or the final collapsed state.

Each value in the superposition vector represents a probability for a given state. For example, if you have a vector representing the state of three qubits then the first vector value represents the probability of $\lvert000\rangle$, the second $\lvert001\rangle$…all the way up to $\lvert111\rangle$. In this case there is a total of $2^{3} = 8$ states.

Another possible State is Collapsed, which represents a collapsed state—duh! Collapsed has properties for the original qubit count and the index of the collapsed probability from Superposition. Since the index can be represented as a binary number it’s trivial to generate an actual list of classical bits. For example, index 6 corresponds to the binary number 110, which represents state $\lvert110\rangle$.

Classical bits are represented by a sum type Bit that has two members One and Zero. If your original qubits had labels then the final bits will preserve those labels.

Collapsed can be converted to BinaryRegister, which is a helper container with Bit values representing a collapsed state that maps classical bits directly to the original QubitRegister qubits.

There are two ways to make a measurement and collapse the superposition. The first is to use the runAndMeasure method of QuantumContext. It will run the whole circuit, make a probabilistic measurement of the superposition, and return a collapsed state. The second way is to use the Measure operation that we’ll cover in more detail in the next section.