Quantum circuit diagram conventions
Reading quantum circuit diagrams#
In a quantum circuit, time flows from left to right. Quantum gates are ordered chronologically, with the left-most gate applied first to the qubits. A circuit diagram has four core elements:
- Qubit register — displayed as horizontal lines, each representing a qubit. The top line is register 0, the second is register 1, and so on.
- Quantum gate — quantum operations represented as a box on the line, analogous to a classical logic gate. A Hadamard operation, for example, is drawn as a labeled box.
- Controlled gate — acts on two or more qubits. A CNOT gate, for instance, is drawn with a black circle for the control qubit and a crossed circle for the target qubit.
- Measurement operation — represented by a meter symbol, taking a qubit register as input and outputting classical information.
Applying quantum gates#
Because time flows from left to right, the left-most gate is applied first. For example, a circuit with gates A, then B, then C (left to right) implements the unitary matrix CBA — matrix multiplication obeys the opposite convention (right-most applied first), so it's important not to confuse the two notations.
Inputs and outputs#
The wires entering a gate represent the qubits input to it; the wires exiting represent the qubits output from it. The number of inputs always equals the number of outputs, because quantum operations are unitary and therefore reversible.
Multi-qubit operations#
Multi-qubit circuit diagrams follow similar conventions to single-qubit ones. Abstract circuit diagrams let you describe complicated quantum algorithms at a high level without compiling them down to fundamental gates — you can get intuition about the data flow of a large algorithm without understanding every subroutine's details.
Controlled gates#
Controlled gates are two-qubit gates that apply a single-qubit gate to a target qubit only if a control qubit is in a specific state. In circuit diagrams, a black circle denotes the control qubit and a vertical wire denotes the unitary applied when the control qubit is 1. The special case where the applied gate is X is the CNOT gate. Q# provides methods to automatically generate the controlled version of an operation:
operation PrepareSuperposition(qubit : Qubit) : Unit
is Ctl { // Auto-generate the controlled specialization of the operation
H(qubit);
}Classically controlled gates#
Quantum gates can also be applied after a measurement, where the measurement result acts as a classical control bit — the gate is applied only if the classical control bit equals 1.
Measurement operator#
A measurement operation takes a qubit register, measures it, and outputs the result as classical information. It's denoted by a meter symbol, with a solid line as input (the qubit) and a double line as output (classical information). In Q#, the Measure operator implements this.
Example: entangling two qubits#
The unitary transformation CNOT₀₁(H⊗1) applied to the state |00⟩ produces a maximally entangled two-qubit state — the same Bell pair built step by step in Create your first Q# program. This gate sequence is of fundamental significance to quantum computing, and operations of this or greater complexity are ubiquitous in quantum algorithms and quantum error correction.
Example: quantum teleportation#
Quantum teleportation is one of the best algorithms for illustrating circuit components — it's a protocol that transmits a quantum state from one qubit to another, using a shared entangled state between sender and receiver plus classical communication.
Conventionally, the sender is Alice, the receiver is Bob, and the qubit to be teleported is the message qubit. The protocol:
- Register
q0is the message qubit,q1is Alice's qubit,q2is Bob's qubit. The message qubit is in an unknown state; Alice's and Bob's qubits start at |0⟩. - A Hadamard gate is applied to Alice's qubit, putting it into an even superposition.
- A CNOT gate entangles Alice's and Bob's qubits — they now share an entangled state.
- A CNOT gate is applied to the message qubit and Alice's qubit, producing a three-qubit entangled state.
- A Hadamard gate is applied to the message qubit.
- Alice measures her two qubits and tells the results to Bob (two classical bits: 00, 01, 10, or 11).
- Classically controlled Pauli gates X and Z are applied to Bob's qubit depending on those results — Bob's qubit now holds the original message qubit's state.