Superposition and Measurement

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

Scotty uses this idea to represent the superposition of all qubits by introducing a trait called Superposition. It extends State and takes an array of floats (Vector) with several helpers:

case class Superposition(register: QubitRegister, state: Vector) extends State {
  lazy val qubitCount: Int

  def applyGate(gate: Gate): Unit

  def combine(sp: Superposition): Superposition

  def toHumanString: String

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.

As mentioned previously, state amplitudes are complex numbers represented by a pair of floats. If you have a vector representing the state of, say, three qubits then the first two float values represents the probability of $\lvert000\rangle$, the second two $\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 the 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 BitRegister, which is a helper container with Bit values representing a collapsed state that maps classical bits directly to the original QubitRegister qubits. You can also convert Collapsed to a binary string with toBinary.

There are two ways to make a measurement and collapse the superposition. The first is to use the runAndMeasure method from the 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.