In addition to qubit registers, circuits contain operations (Op). All operations has to reference register qubits by their index.

Currently, Scotty supports three different types of operations: CircuitConnector, Gate, and Measure.

CircuitConnector is a convenient wrapper that lets you add arbitrary circuits in the middle of your circuit.

val toffoliNot = Circuit(CCNOT(0, 2, 3), X(3))

Circuit(H(0), X(2), CircuitConnector(toffoliNot), X(0))

This is very useful when your circuit is defined elsewhere dynamically or you have an algorithm stored in another method that can’t be easily added with Circuit.combine.

Measure is a one-qubit operation that forces the superposition to collapse at the end of the circuit execution. Scotty simulator uses the deferred measurement principle meaning that the simulator doesn’t perform any measurements until all operations (besides Measure itself) are done executing.

RunAndMeasure returns Collapsed but run returns a generic State. If you don’t know whether your circuit contains any Measure operations you’ll have to pattern match on the return type:

QuantumSimulator().run(circuit) match {
  case s: Superposition => println("not measured")
  case s: Collapsed => println("measured")

Gate is another Op type and it deserves its own section.