This note is part of [[quantum/Practical Quantum Information System]].
> [!info] Course code
> Use the companion repository for this lecture's runnable lab, helper functions, and regression checks:
> - [notebooks/by_concept/quantum_gates_and_circuits.ipynb](https://github.com/montekkundan/quantum-code/blob/main/notebooks/by_concept/quantum_gates_and_circuits.ipynb)
> - [qcourse/states.py](https://github.com/montekkundan/quantum-code/blob/main/qcourse/states.py)
> - [tests/test_expanded_course_primitives.py](https://github.com/montekkundan/quantum-code/blob/main/tests/test_expanded_course_primitives.py)
[TODO: add video - Quantum Gates and Circuits]
## What This Concept Is
Quantum circuits are the working language of the course. A circuit tells you how to prepare qubits, apply transformations, and extract measurement outcomes in a sequence you can run on a simulator or on hardware. If the earlier notes gave you the mathematics, this note gives you the operational syntax.
The key shift is that a circuit is not just a picture. It is a compact description of a linear-algebra pipeline. Every gate corresponds to a structured operation on one or more qubits, and the whole circuit composes those operations in time.
## Foundation Terms You Need First
A [[quantum/Glossary#Qubit|qubit]] is the system being acted on. A [[quantum/Glossary#Unitary|unitary]] gate is an allowed ideal closed-system transformation. A [[quantum/Glossary#Born rule|measurement]] step converts the quantum state into classical outcome statistics.
It also helps to keep "state" and "circuit" separate in your head. The state is the object being transformed. The circuit is the recipe describing which transformations happen and in what order.
## How The Idea Actually Works
Single-qubit gates such as $X$, $Y$, $Z$, and $H$ change the amplitudes and phases of a one-qubit state. Multi-qubit gates, especially controlled gates, are where the circuit model becomes much more interesting. They let one qubit's basis component influence another qubit's evolution, which is how many entangling operations are built.
Composition matters. If you apply gate $A$ and then gate $B$, the resulting transformation is not in general the same as applying $B$ and then $A$. That non-commutativity is one reason circuit order carries real computational meaning.
Measurement also has to be placed carefully. If you measure too early, you destroy coherence that later gates might have used. This is why many quantum algorithms delay measurement until the end. The circuit is designed to let interference do useful work before classical information is extracted.
The circuit model is especially powerful because it gives you a common language across frameworks. Qiskit, Q#, CUDA-Q, and other SDKs differ in syntax, but the underlying gate logic is the same. Learning that shared logic is what makes tool-switching manageable later.
## Why It Matters
- It turns the abstract state-update rules into executable objects.
- It is the main practical surface for labs, protocols, and algorithms.
- It is the bridge from mathematical understanding to hardware and software implementation.
## Related Questions
> [!question] Why does circuit order matter?
>> [!answer] Matrix multiplication is generally noncommutative. Applying $H$ then $Z$ can produce a different state from applying $Z$ then $H$, so a circuit's time order is part of the computation.
> [!question] Why do many algorithms delay measurement until the end?
>> [!answer] Early measurement can destroy the coherence needed for later interference. Many algorithms first arrange amplitudes so wrong answers cancel and useful structure survives, then measure after that interference has done its work.
> [!question] Why should you learn circuits before choosing a software framework?
>> [!answer] Qiskit, Q#, CUDA-Q, PennyLane, and other tools have different syntax, but the underlying circuit ideas are shared: state preparation, gate composition, controlled operations, and measurement.
## Additional Study Notes
**Check yourself: phase gates affect only selected branches** The phase gate $P=\begin{bmatrix}1&0\\0&i\end{bmatrix}$ leaves a qubit's $|0\rangle$ component alone and multiplies its $|1\rangle$ component by $i$. In a two-qubit state, apply that factor only to the basis terms where the target qubit is $1$.
**Check yourself: trace a circuit by basis amplitudes** For a two-qubit circuit, write the state as amplitudes on $|00\rangle, |01\rangle, |10\rangle, |11\rangle$ after each gate. This prevents sign and phase mistakes when Hadamards, controlled gates, and phase gates appear together.
**Question: Why is matching an intermediate circuit state easier if you keep the basis order fixed?** A fixed basis order turns each gate step into a predictable update of four amplitudes. If you keep changing notation or reordering terms, it becomes much easier to confuse a phase on $|01\rangle$ with a phase on $|10\rangle$.
## Study Checks
Use these after the explanation, not before it.
### Quick Checks
- MCQ: Which matrix is the Hadamard gate? A. $\begin{bmatrix}0&1\\1&0\end{bmatrix}$ B. $\frac{1}{\sqrt2}\begin{bmatrix}1&1\\1&-1\end{bmatrix}$ C. $\begin{bmatrix}1&0\\0&i\end{bmatrix}$ D. $\begin{bmatrix}1&0\\0&-1\end{bmatrix}$ **Answer:** B. (1/sqrt(2)) [[1, 1], [1, -1]].
- What does it mean for a gate to be unitary? **Answer:** Its inverse is its conjugate transpose, so U dagger U = I. It preserves inner products and state normalization.
- MCQ: Which operation is not generally unitary? A. X gate B. Hadamard gate C. Measurement D. Phase gate **Answer:** C. Measurement.
- MCQ: Which tool is the primary circuit-lab path in this course? A. Qiskit B. Excel C. LaTeX only D. Git alone **Answer:** A. Qiskit.
- MCQ: What should every serious notebook rely on instead of hiding all logic inline? A. Untested handwritten cells only B. Tested helpers in qcourse/ C. Screenshots only D. Hardware queues only **Answer:** B. Tested helpers in qcourse/.
### Oral Exam Anchors
> [!question] Oral exam anchor
> Explain why a quantum gate must be unitary in the closed-system model.
A good answer should say:
Closed-system quantum evolution must preserve normalization and inner products. If two states are evolved by U, then probabilities remain well-defined only if ||U|psi>|| = |||psi>|| for every |psi>. This is equivalent to U dagger U = I in finite dimension, so U is unitary.
Unitarity also implies reversibility. This is why circuit descriptions of quantum algorithms often need to compute, copy classical reversible information into work registers, and then uncompute garbage. If unwanted workspace remains entangled with the answer register, the final interference step can fail.
## Practical Lab
Build a reusable gate notebook here because you will reuse it throughout the course.
- Implement X, Y, Z, H, S, T, and at least one controlled gate.
- Show one example where gate order changes the output.
- Add one small repeated-measurement or repeated-projection experiment connected to the Zeno-style intuition.
## Homework
Use the homework to connect circuit notation back to state evolution.
- Apply a short sequence of gates to an input state and compute the output by hand.
- Explain why unitary evolution is reversible while measurement is not.
- Draw or describe a circuit that prepares a target one-qubit state from $|0\rangle$.
## References
- Scott Aaronson, [Introduction to Quantum Information Science](https://www.scottaaronson.com/qclec.pdf), Lecture 4.
- [[Resources]]
- IBM Quantum Learning, [circuit building resources](https://quantum.cloud.ibm.com/learning/en).
- Microsoft Learn, [Azure Quantum documentation](https://learn.microsoft.com/en-us/azure/quantum/).