Standard Gates

Quantum gates are the meat of quantum algorithms. Each Gate is an Op with several basic methods. There are two generic types of gates defined with TargetGate or ControlGate traits. The former is a gate applied to at least one qubit. All it needs is a qubit index and a gate matrix. The latter provides an abstraction for controlled quantum gates.

All target gates can be parametric depending on the gate. Gate parameters are supposed to be listed first followed by qubit indexes:

SomeGate(param1, param2, index1, index2, ...)

Gate parameters are expected to be individual method parameters and indexes are represented by variable arguments (varargs).

Scotty provides a set of standard gates that matches gates in the Quil gate set.

Identity Gate

I is a TargetGate identity gate that doesn’t change the state.

Pauli Gates

Pauli gates X, Y, and Z are TargetGate gates that perform rotations around the three axes.

Hadamard Gate

The Hadamard gate H is a TargetGate gate that sets basis state measurement probabilities to 50%.

Rotation Gates

RX, RY, and RZ are parametric TargetGate gates. They each take a $\theta$ angle in radians as the first parameter and a qubit index as the second parameter:

RX(Math.PI / 3, 0)

This will rotate qubit 0 around the x axis by $\frac{\pi}{3}$ radians.

CNOT, CCNOT, and CZ Gates

CNOT, CCNOT (Toffoli gate), and CZ are controlled quantum gates that implement the ControlGate trait. As part of the Scotty simulator implementation, you can apply those gates to any qubits in your circuit:

Circuit(X(5), CNOT(5, 2))


The SWAP gate implements QubitSwap, which is a target gate acting on two qubits. It swaps two qubits and can be controlled by a Controlled gate:

Controlled(1, SWAP(2, 4))

Simulator implementation is flexible enough to have target qubits be set in any order. Also, the control qubit can be in between the two target gates if you need it to be there.

CSWAP is a shorthand gate for a controlled SWAP gate.

ISWAP swaps the state of two qubits, applying an i phase to the first qubit when it’s in the $\lvert1\rangle$ state and an i phase to the second qubit when it’ i’s in the $\lvert0\rangle$ state.

PSWAP swaps the state of two qubits, applying a custom phase to the first qubit when it’s in the $\lvert1\rangle$ state and a custom phase to the second qubit when it’ i’s in the $\lvert0\rangle$ state.

Phase Shift Gates

PHASE is a parametric TargetGate gate that leaves $\lvert0\rangle$ unchanged and maps $\lvert1\rangle$ to $e^{i\phi}\lvert1\rangle$.

CPHASE, CPHASE00, CPHASE10, and CPHASE01 are controlled phase gates that act on states $\lvert11\rangle$, $\lvert00\rangle$, $\lvert10\rangle$, and $\lvert01\rangle$ accordingly.

S and T Gates

S is a phase gate that represents a 90 degree rotation around the z-axis. T gate is related to the S gate through $S = T^2$.

How Are Gates Implemented

The simulator applies gates to the state sequentially without any circuit optimizations (for now). It means that if you have a state consisting of three qubits and you apply a NOT gate to the second qubit then the total gate matrix becomes a tensor product of three gates:

Controlled and swap gates work differently and generate their matrices dynamically based on the order and the number of provided qubits.