Lesson 21 · 75 min · core
Controlled Gates: CNOT, CZ, SWAP
A controlled gate applies a single-qubit unitary to a target qubit exactly when the control qubit is : . This lesson proves that is unitary, shows that is symmetric in its two qubits, converts into with two Hadamards, reverses the direction of a CNOT with four Hadamards, builds SWAP from three CNOTs, explains phase kickback , and constructs an arbitrary controlled- from two CNOTs and single-qubit gates through the decomposition with .
Prerequisites: Rotation Gates & ZYZ Euler Decomposition, Tensor (Kronecker) Products
Objectives and key results
Write a controlled- gate in projector form and in block form , and prove that it is unitary using only projector algebra.
Prove that is symmetric in control and target and derive from .
Exchange the roles of control and target with .
Prove on basis states and extend it to all states by linearity.
Compute the phase kickback and its generalisation to eigenvectors of .
Build any controlled single-qubit gate from two CNOTs and single-qubit rotations with the construction, verifying and step by step.
- formula · Controlled-U
- identity · CZ and CNOT
- identity · Reversed CNOT
- identity · SWAP from CNOTs
- identity · Phase kickback
- theorem · ABC construction
Controlled-U in block form#
So far every gate acted on one qubit. A controlled gate makes the action on one qubit (the target) depend on the computational-basis value of another qubit (the control). Throughout, qubit 0 is the most significant bit, so with on qubit 0 and on qubit 1 has index , and the basis order is . We write and for the two computational-basis projectors, and for the CNOT with control and target ; plain means .
Definition
Controlled-U gate
For a single-qubit unitary , the controlled- gate with control qubit 0 and target qubit 1 is the two-qubit operator . It leaves unchanged and maps to . The special cases and are the controlled-NOT and the controlled- .
Block form of
- Step 1:An outer product is the column times the row .
- Step 2:The Kronecker product places a copy of , scaled by , in block ; here each block is and denotes the zero block.
- Step 3:Add the two block matrices. With qubit 0 as the most significant bit, the upper-left block acts on (control ) and the lower-right block on (control ).
- Step 4:Apply to each term, with and .
- Step 5:Now and : the target receives exactly when the control is . Compactly, for .
Result
is unitary
If is a unitary matrix, then is a unitary matrix, and its inverse is .
Proof (7 steps)
- Step 1:The adjoint is additive and satisfies ; the projectors and are Hermitian.
- Step 2:Orthonormality of the computational basis gives , , ; completeness gives .
- Step 3:Substitute the adjoint from the first line.
- Step 4:Expand the product of two sums into four terms and apply the mixed-product rule to each.
- Step 5:, , , and because is unitary. The cross terms vanish because the two branches act on orthogonal control states.
- Step 6:The tensor product is bilinear, and .
- Step 7:The same expansion in the opposite order uses . Both products are the identity, so is unitary and its inverse is the controlled-.
∎
Unitary verified: (max deviation 0.0e+0)
Unitary verified: (max deviation 0.0e+0)
and on basis states
- Step 1:For , and ; for , and , i.e. . Here is addition modulo (XOR).
- Step 2:Use with : the target bit is XORed with the control bit.
- Step 3:The four cases of the previous line. Column of the matrix is the image of basis state , which reproduces the permutation matrix shown above.
- Step 4:, so for .
- Step 5:The same rule with : only , where , acquires the sign , which is the diagonal .
Result
Circuit · 2 qubits
Preparing with on qubit 0 and then applying gives : the statevector panel shows amplitude on index .
A CNOT on a superposed control. Basis inputs only exercise the truth table. Move the slider of the statevector view below through the circuit on qubit 0, then : position shows the input, the state after the Hadamard and the output, with the gates applied so far highlighted in the diagram. Predict the three amplitude columns from the derivation first; all amplitudes stay real and non-negative, so every nonzero amplitude has phase .
The state after each operation of ,
- Step 1:Every qubit starts in ; amplitudes are listed in the order .
- Step 2:acts on qubit 0, the most significant bit, so the weight moves to indices and .
- Step 3:Linearity with and : the branch with control flips its target, the other is left alone.
- Step 4:A product state would need while ; but forces (then ) or (then ). The output is therefore not a product state: it is entangled, the Bell state of the next lesson.
Result
Quick check
Which state is ?
CZ is symmetric; CNOT from CZ#
does not distinguish control from target
. Hence the controlled- with control 0 and target 1 equals the controlled- with control 1 and target 0, and .
Proof (5 steps)
- Step 1:Kronecker products of diagonal matrices are diagonal: , with , , .
- Step 2:Add the two diagonals entry by entry.
- Step 3:The same rule with the factors in the other order: and .
- Step 4:Add the diagonals. The operator is, by definition, the controlled- with control on qubit 1 and target on qubit 0, so both orientations give the same matrix.
- Step 5:Equivalent formulation with basis labels: , , and . The two operators agree on a basis, hence everywhere by linearity.
∎
The matrix display below prints with its rows and columns labelled (qubit 0 first) and lists its properties. A real diagonal matrix with entries is Hermitian and unitary at once; its trace is , its determinant , and its eigenvalues are the diagonal entries , with the single eigenvector . Nothing in this list refers to which qubit is the control, which is the symmetry just proved.
, block by block
- Step 1:The Kronecker product has in both diagonal blocks and off the diagonal.
- Step 2:Block-diagonal matrices multiply block by block: the block is and the block is ; every off-diagonal block contains a factor .
- Step 3:Both identities were proved in the Hadamard lesson: follows from , and .
- Step 4:Insert the two blocks; this is the block form of .
- Step 5:Entry-level check of the lower-right block: left multiplication by the diagonal negates the last row of ; then times the block gives . The upper-left block is .
- Step 6:The same computation in projector form, using the mixed-product rule: conjugating the target of a controlled gate conjugates , for .
- Step 7:Conversely, multiply both sides of by on the left and right, and use .
Result
Circuit · 2 qubits
The sandwich on qubit 1, , on qubit 1. The unitary panel shows exactly the CNOT permutation matrix, with no global phase: the target Hadamards turn the phase flip into the bit flip .
Quick check
Which circuit implements exactly, using one ?
Exchanging control and target#
The CNOT with control on qubit 1 and target on qubit 0 is : on basis states . Its columns are the images , , , . On hardware with a fixed CNOT direction the reversed gate is obtained from the forward one by Hadamards on both qubits.
Unitary verified: (max deviation 0.0e+0)
Reversing a CNOT with four Hadamards
. In the Hadamard basis the roles of control and target are exchanged.
Proof (6 steps)
- Step 1:Mixed-product rule: , and likewise in the other order. Gates on different qubits commute.
- Step 2:Substitute from the previous section, and split the outer as in the first line (left factor , right factor ).
- Step 3:on both sides of .
- Step 4:Use the symmetric form proved above, then the mixed-product rule on each term.
- Step 5:and . The result applies to qubit 0 exactly when qubit 1 is .
- Step 6:Spot check on a basis state with , : the target qubit 0 is flipped because the control qubit 1 is set, matching the second column of the matrix.
∎
Circuit · 2 qubits
, then , then . The unitary panel shows the permutation matrix (columns exchanged), exactly and with no global phase.
The display below computes the unitary of the one-gate circuit directly with the simulator and prints it with basis labels. Read its columns: and , while and are fixed. Its properties coincide with those of : Hermitian and unitary, trace (two fixed basis states), determinant (a permutation matrix of a single transposition), eigenvalues . The eigenvector is : target qubit 0 in , control qubit 1 in , a first glimpse of the phase kickback below.
SWAP from three CNOTs#
Definition
SWAP gate
The SWAP gate exchanges the states of two qubits: for , extended linearly, so that for all single-qubit states. It fixes and and exchanges .
Unitary verified: (max deviation 0.0e+0)
Applying , then , then implements the SWAP gate exactly: satisfies for every two-qubit state .
Proof (7 steps)
- Step 1:Addition modulo on is commutative and associative, is neutral, and every bit is its own inverse.
- Step 2:First gate: control qubit 0, target qubit 1.
- Step 3:Second gate: control qubit 1 (value ), target qubit 0, so qubit 0 becomes ; by associativity and this is .
- Step 4:Third gate: control qubit 0 (now ), target qubit 1, which becomes .
- Step 5:The operator for the wire order is the product (the first gate acts first); the three lines above evaluate it on every basis state.
- Step 6:Every two-qubit state is a linear combination ; both and are linear and agree on the basis, so they agree everywhere: .
- Step 7:For a product state , regrouping the sum by bilinearity of shows that superpositions, not only basis labels, are exchanged.
∎
The three CNOTs on the four basis states
- Step 1:Every control is , so nothing happens.
- Step 2:The first control (qubit 0) is ; then qubit 1 is and flips qubit 0; then qubit 0 is and flips qubit 1.
- Step 3:Qubit 0 is and flips qubit 1; then qubit 1 is and flips qubit 0 back to ; finally qubit 0 is , so the last CNOT does nothing.
- Step 4:Qubit 1 is flipped to ; then the control qubit 1 is ; finally qubit 0 is and flips qubit 1 back to .
Result
Circuit · 2 qubits
Three alternating CNOTs. The unitary panel shows the SWAP permutation matrix exactly: rows and columns and exchanged.
Circuit · 2 qubits
Preparing and applying the three CNOTs moves the excitation to qubit 1: the output is , amplitude on index .
Quick check
What does the two-gate circuit followed by implement?
Phase kickback#
Phase kickback
For , with . More generally, if , then : the target is unchanged and the eigenvalue phase is attached to the control.
Proof (6 steps)
- Step 1:is linear and exchanges and ; so is an eigenvector of with eigenvalue .
- Step 2:Block-form rule with and .
- Step 3:For , ; for , the first line.
- Step 4:A scalar can be moved across the tensor product: . The sign can be read as belonging to the control.
- Step 5:Direct check of the case with the basis table , .
- Step 6:The same three steps for any controlled- and eigenvector , using . The CNOT case is , , .
∎
- Step 1:Expand the tensor product by bilinearity; the amplitude vector is .
- Step 2:Apply term by term: and are unchanged, and .
- Step 3:Reorder the terms by index; the coefficients factor as .
- Step 4:The same result from the kickback proposition and linearity: the branch picks up .
- Step 5:The target qubit is untouched, while the control has been flipped in the basis, exactly as if had acted on it. This is the reversed-CNOT theorem seen on one input.
Result
Circuit · 2 qubits
on qubit 0 and then on qubit 1 prepare ; the CNOT then outputs , with amplitudes on .
Quick check
After acts on , what is the amplitude of ? Give a decimal.
Any controlled-U from two CNOTs#
The ZYZ decomposition from the rotation-gates lesson writes every single-qubit unitary as with real , where . Two facts about rotations are used repeatedly below: rotations about the same axis add, , because and are exponentials of commuting matrices; and . The construction that follows, due to Barenco et al., builds from two CNOTs, single-qubit gates on the target, and one phase gate on the control.
and
For every real , conjugation by reverses rotations about the and axes: and .
Proof (6 steps)
- Step 1:Pauli products and , and .
- Step 2:Pauli products and , and .
- Step 3:Conjugation is linear; and from the first line.
- Step 4:Cosine is even and sine is odd: , .
- Step 5:The same two steps with .
- Step 6:Matrix check: conjugating a diagonal matrix by exchanges its two diagonal entries.
∎
The decomposition
Let . Define , and . Then and .
Proof (7 steps)
- Step 1:Write out the three definitions in order.
- Step 2:Adjacent rotations about the same axis add: gives , and .
- Step 3:Add the two angles once more.
- Step 4:Insert between the two factors of , then apply the lemma to each: and .
- Step 5:Substitute , the previous line for , and .
- Step 6:Add adjacent same-axis angles: and .
- Step 7:Multiply by the scalar and compare with the ZYZ form of .
∎
Controlled- from two CNOTs
With as in the theorem, the circuit that applies to the target, then , then to the target, then , then to the target, together with the phase gate on the control, implements exactly.
Proof (6 steps)
- Step 1:The operator is the product of the gates in reverse wire order. acts on the control only and is diagonal there, so it commutes with both CNOTs and may be placed anywhere on the control wire.
- Step 2:Insert and use the mixed-product rule on each term: .
- Step 3:Expand the product: the cross terms contain and vanish, while and ; the last factor appends to both branches.
- Step 4:The theorem gives : when the control is the target sees , then , then , whose product is the identity.
- Step 5:and , because is diagonal with entries and .
- Step 6:The theorem gives . Without the circuit would implement , which differs from by a relative, observable phase between the two control branches.
∎
Worked example: controlled-
- Step 1:The Hadamard lesson computed , so ; inserting puts it in ZYZ form.
- Step 2:Substitute into the definitions: , , .
- Step 3:Add the same-axis angles.
- Step 4:The lemma turns into ; then add the angles and use the matrix product from the Hadamard lesson.
- Step 5:The control phase gate for is the gate; without it the circuit would give .
- Step 6:The circuit ; ; , ; ; on the target, with on the control, has block form by the corollary. On the wire is applied as first, because .
Result
Circuit · 2 qubits
The circuit for controlled-. The unitary panel shows exactly: the identity block confirms , and the lower-right block confirms .
Quick check
How many CNOT gates does the construction use to implement an arbitrary controlled single-qubit gate ?
Numerical verification and lab#
The script below rebuilds the identities of this lesson from the simulator's matrices and checks them entry by entry to : the projector form of CNOT, the CZ-to-CNOT conversion, the reversed CNOT, the three-CNOT SWAP, and the construction for the sample angles , , . It also evaluates the process fidelity of the two-CNOT circuit , which the lab uses as a diagnostic.
import { HADAMARD, I2, PAULI_X, SWAP_GATE, circuitUnitary, matrix, ryMatrix, rzMatrix } from '@/lib/quantum-simulator';
const tol = 1e-12;
const P0 = matrix.fromRows([[1, 0], [0, 0]]);
const P1 = matrix.fromRows([[0, 0], [0, 1]]);
// Engine unitaries of single two-qubit gates (qubit 0 = control = most significant bit)
const CNOT = circuitUnitary({ version: 1, numQubits: 2, numClbits: 0, ops: [{ kind: 'gate', gate: 'CNOT', targets: [0, 1] }] });
const CZ = circuitUnitary({ version: 1, numQubits: 2, numClbits: 0, ops: [{ kind: 'gate', gate: 'CZ', targets: [0, 1] }] });
// Block form C-U = |0><0| (x) I + |1><1| (x) U
console.log('CNOT = P0 x I + P1 x X :', matrix.approxEqual(matrix.add(matrix.kron(P0, I2), matrix.kron(P1, PAULI_X)), CNOT, tol));
console.log('CNOT is unitary :', matrix.isUnitary(CNOT, tol));
// CNOT from CZ, and the reversed CNOT
const IH = matrix.kron(I2, HADAMARD);
const HH = matrix.kron(HADAMARD, HADAMARD);
const CNOT10 = matrix.add(matrix.kron(I2, P0), matrix.kron(PAULI_X, P1));
console.log('(I x H) CZ (I x H) = CNOT :', matrix.approxEqual(matrix.mulAll(IH, CZ, IH), CNOT, tol));
console.log('(H x H) CNOT (H x H) = CNOT10 :', matrix.approxEqual(matrix.mulAll(HH, CNOT, HH), CNOT10, tol));
// SWAP from three CNOTs, and the two-CNOT near miss
console.log('CNOT CNOT10 CNOT = SWAP :', matrix.approxEqual(matrix.mulAll(CNOT, CNOT10, CNOT), SWAP_GATE, tol));
const V = matrix.mul(CNOT10, CNOT);
const tr = matrix.trace(matrix.mul(matrix.adjoint(SWAP_GATE), V));
console.log('two CNOTs: |Tr(SWAP^dag V)| / 4 =', (Math.hypot(tr.re, tr.im) / 4).toFixed(4));
// ABC construction for beta = 0.3, gamma = 1.1, delta = -0.7
const beta = 0.3, gamma = 1.1, delta = -0.7;
const A = matrix.mul(rzMatrix(beta), ryMatrix(gamma / 2));
const B = matrix.mul(ryMatrix(-gamma / 2), rzMatrix(-(delta + beta) / 2));
const C = rzMatrix((delta - beta) / 2);
const zyz = matrix.mulAll(rzMatrix(beta), ryMatrix(gamma), rzMatrix(delta));
console.log('ABC = I :', matrix.approxEqual(matrix.mulAll(A, B, C), I2, tol));
console.log('AXBXC = RZ(b) RY(g) RZ(d) :', matrix.approxEqual(matrix.mulAll(A, PAULI_X, B, PAULI_X, C), zyz, tol));Expected output
CNOT = P0 x I + P1 x X : true CNOT is unitary : true (I x H) CZ (I x H) = CNOT : true (H x H) CNOT (H x H) = CNOT10 : true CNOT CNOT10 CNOT = SWAP : true two CNOTs: |Tr(SWAP^dag V)| / 4 = 0.5000 ABC = I : true AXBXC = RZ(b) RY(g) RZ(d) : true
References
- M. A. Nielsen and I. L. Chuang, Quantum Computation and Quantum Information, 10th anniversary ed., Cambridge University Press (2010), §4.3 (controlled operations, Corollary 4.2 and the CNOT identities of Exercises 4.17 and 4.20).doi:10.1017/CBO9780511976667
- A. Barenco, C. H. Bennett, R. Cleve, D. P. DiVincenzo, N. Margolus, P. Shor, T. Sleator, J. A. Smolin and H. Weinfurter, Elementary gates for quantum computation, Physical Review A 52, 3457–3467 (1995).doi:10.1103/PhysRevA.52.3457
Hands-on labs
SWAP from three CNOTs
1 step · auto-gradedOpen labBuild the two-qubit SWAP gate, which exchanges the states of the two wires, using only CNOT gates and at most three of them.