Lecture 3 - Quantum Algorithm and State Preparation

Lecture 3 - Quantum Algorithm and State Preparation

Updated 27 Nov 2026

1. Introduction

Quantum Algorithm

  • A quantum algorithm is a circuit of quantum logic gates for manipulating qubit-based information to achieve desired computation.

Quantum algorithm like a electronic circuit, but it just only a program

Output=desired probability distribution over all solution candidates\text{Output} = \text{desired probability distribution over all solution candidates}

    1. Preparation: convert input into quantum states, superpositions, and entanglement
    1. Computation: via state manipulation, conditions, loops, reflections, and phase estimation
    1. Measurement: measure the probability distribution for each quantum state

Caption — Figure 1: Overview of a quantum algorithm. A row of qubits, all initialized to ∣0⟩|0\rangle, passes through a "STATE PREPARATION" block, then a "STEP-BY-STEP INSTRUCTIONS" (computation) block, and finally each wire ends at a Detector — the measurement stage.

💡 Analogy: Think of a quantum algorithm like a recipe with exactly three phases: (1) get your ingredients into the right starting mix (preparation), (2) follow the steps that transform them (computation), and (3) taste the final dish and report what you got (measurement) — except "tasting" here means sampling from a probability distribution, not reading off one guaranteed answer.

PennyLane

  • Popular library for quantum computing in Python, developed by Xanadu
  • Features:
    • Easy to use with a low learning curve
    • Support for CPU-based and GPU-based quantum simulation
    • Plugins for actual QPUs, e.g. IBM Qiskit, Amazon Braket, and Xanadu
    • Support for automatic JIT compilation of quantum algorithms in the entire quantum-classical workflow
  • The URL is https://pennylane.ai/

Basic installation

pip install pennylane

On a CUDA-enabled machine

pip install custatevec_cu12
pip install pennylane-lightning-gpu

On CUDA Tensor Networks

pip install cutensornet-cu12
pip install pennylane-lightning-tensor

Docker

docker pull pennylaneai/pennylane:latest-lightning-qubit

Example Code

import pennylane as qml              # Quantum computing
from pennylane import numpy as np    # Classical computing
 
# Quantum device with two wires (i.e. variables)
dev = qml.device('default.qubit', wires=[0, 1]) # connect to real quantum computer
 
dev = qml.device('default.qubit', wires=["q1", "google", "9arm", "3arm"])
 
# This is our simple quantum algorithm
@qml.qnode(dev)
def circuit(theta: float):
 
   # State preparation
   qml.H(wires=[0])             # Hadamard gate on qubit-0
 
   # Computation
   qml.RY(theta, wires=[0])     # Rotate qubit-0 on Axis-Y by theta
   qml.CNOT(wires=[0,1])        # Controlled NOT on qubit-1 with control qubit-0
 
   # Measurement
   return qml.state()
 
# Let's run our quantum algorithm
print(circuit(np.pi / 6))

Code Skeleton

Header

import pennylane as qml
from pennylane import numpy as np

Quantum device

dev = qml.device('default.qubit', wires=...)

Several quantum devices are also available.

  • default.qubit: Simple simulator
  • default.mixed: Mixed-state simulator
  • lightning.qubit: C++-based simulator
  • default.tensor: Tensor Networks

Quantum algorithm = quantum node

  • Preparation = variable initialization
  • Computation = sequence of logic gates
  • Measurement = collapsing quantum states to a bitstring
@qml.qnode(dev)
def circuit(args):
    # State preparation
    ......
 
    # Computation
    ......
 
    # Measurement
    return ......

2. Clifford Set/1

Clifford Set of Quantum Logic Gates

These gates are very stable, we don’t need the big refrigerator to freeze them (room temperature is okay). Non-clifford needs refrigerator.

  • A set of basic quantum logic gates
    • Fault-tolerant: Hardware implementation of these gates can significantly tolerate noise and quantum decoherence
    • Self-inversible: Applying a quantum operator twice on a state results in the original one
    • Efficiently simulatable: They can be emulated in polynomial time by probabilistic classical computers
  • The main purpose of the Clifford set is state preparation
  • A quantum algorithm that consists of only Clifford gates is sometimes called a stabilizer circuit due to its stability
  • In this chapter, we will divide them into three levels:

Table 1: Clifford set of quantum logic gates

Level 1Level 2Level 3
NOTPauli XSwap
HadamardPauli YControlled Z
Phase shiftPauli Z
Controlled NOT

💡 Analogy: "Fault-tolerant, self-inversible, efficiently simulatable" is basically physics' way of saying "sturdy, reversible, and cheap to double-check on a normal computer" — which is exactly why these particular gates are the trusted toolkit for the fiddly job of state preparation, before the "real," harder-to-simulate computation begins.

Level 1: NOT Gate

  • NOT gate converts ∣0⟩|0\rangle to ∣1⟩|1\rangle and vice versa, similar to logical NOT

X=∣xˉ⟩⟨x∣=∣1⟩⟨0∣+∣0⟩⟨1∣=[0110]\boxed{\mathbf{X} = |\bar x\rangle\langle x| = |1\rangle\langle0| + |0\rangle\langle1| = \begin{bmatrix}0&1\\1&0\end{bmatrix}}

Circuit:

(Figure 2: NOT gate)

📝 Instructor's annotation:
The eigenvectors of X\mathbf{X} directly under the "self-inversible" line: The eigenvectors of X are: ∣0⟩=[10], ∣1⟩=[01]\text{The eigenvectors of } \mathbf{X} \text{ are: } \quad |0\rangle=\begin{bmatrix}1\\0\end{bmatrix},\ |1\rangle=\begin{bmatrix}0\\1\end{bmatrix} and bracketed the two together, labeled "computational basis ∣0⟩|0\rangle and ∣1⟩|1\rangle." — i.e. X\mathbf{X}'s own eigenvectors are nothing exotic, just the two states you already know.

  • X\mathbf{X} is self-inversible: XX∣x⟩=∣x⟩\mathbf{XX}|x\rangle = |x\rangle
  • X\mathbf{X} is often used for preparing ∣1⟩|1\rangle
  • qml.X(wires=…)

  • อ่อ พอจะเริ่ม get ทุกอย่างแล้ว 5555, X\mathbf{X} คือจริง ๆ แล้วตัวมันเป็น Matrix ที่ apply ไปแล้วจะได้ค่าออกมาเป็น output แต่การจะสร้างอันนี้ออกมาได้ก็จาก state mapping ไง ว่าเราต้องการอะไรออกมาเป็นอะไร

Level 1: Hadamard Gate

📝 Instructor's annotation: next to the slide title, wrote a phonetic pronunciation guide — [HAH-də-mar]— for "Hadamard."

  • Hadamard gate converts pure states to superpositions and vice versa

H=∣+⟩⟨0∣+∣−⟩⟨1∣=12[111−1]\boxed{\mathbf{H} = |+\rangle\langle0|+|-\rangle\langle1| = \frac{1}{\sqrt2}\begin{bmatrix}1&1\\1&-1\end{bmatrix}}

📝 "State mapping:" 
∣0⟩↦∣+⟩|0\rangle \mapsto |+\rangle
∣1⟩↦∣−⟩|1\rangle \mapsto |-\rangle

  • H\mathbf{H} is self-inversible: HH∣x⟩=∣x⟩\mathbf{HH}|x\rangle = |x\rangle
  • Hadamard gate is often used for preparing a superposition
  • qml.H(wires=…)
  • Let's define the superposition bases

∣+⟩=∣0⟩+∣1⟩2=[1212]\vert+\rangle = \frac{\vert0\rangle + \vert1\rangle}{\sqrt{2}} = \begin{bmatrix} \frac{1}{\sqrt{2}} \\ \frac{1}{\sqrt{2}} \end{bmatrix}
∣−⟩=∣0⟩−∣1⟩2=[12−12]\vert-\rangle = \frac{\vert0\rangle - \vert1\rangle}{\sqrt{2}} = \begin{bmatrix} \frac{1}{\sqrt{2}} \\ -\frac{1}{\sqrt{2}} \end{bmatrix}

📝 Instructor's annotation: bracketed both formulas together and labeled them "Eigenvectors of H."

Circuit:

(Figure 3: Hadamard gate)

ไอ่ตัว +,−+,- อันนี้ไงที่คือ Superposition เป็นสอง state ในเวลาเดียวกัน เรา define มันเป็นบวกกันไปเลย5555

💡 Analogy: H\mathbf{H} is the quantum coin-flipper: feed it a definite ∣0⟩|0\rangle or ∣1⟩|1\rangle and it spreads the state into an even 50/50 superposition (with a −- sign on the ∣1⟩|1\rangle term when starting from ∣1⟩|1\rangle). Do it twice and — like flipping a coin, then "un-flipping" it — you land back where you started.

This demonstrates that the Hadamard gate is unitary and self-inverse (H2=IH^2=I), meaning applying it twice returns the qubit to its initial state.

H∣+⟩=H(∣0⟩+∣1⟩2)=12H∣0⟩+12H∣1⟩=12∣+⟩+12∣−⟩=12(∣0⟩+∣1⟩2)+12(∣0⟩−∣1⟩2)=∣0⟩+∣1⟩2+∣0⟩−∣1⟩2=∣0⟩+∣1⟩+∣0⟩−∣1⟩2=2∣0⟩2=∣0⟩\begin{aligned} H|+\rangle &= H\left(\frac{|0\rangle + |1\rangle}{\sqrt{2}}\right) \\[8pt] &= \frac{1}{\sqrt{2}} H|0\rangle + \frac{1}{\sqrt{2}} H|1\rangle \\[8pt] &= \frac{1}{\sqrt{2}} |+\rangle + \frac{1}{\sqrt{2}} |-\rangle \\[8pt] &= \frac{1}{\sqrt{2}} \left( \frac{|0\rangle + |1\rangle}{\sqrt{2}} \right) + \frac{1}{\sqrt{2}} \left( \frac{|0\rangle - |1\rangle}{\sqrt{2}} \right) \\[8pt] &= \frac{|0\rangle + |1\rangle}{2} + \frac{|0\rangle - |1\rangle}{2} \\[8pt] &= \frac{|0\rangle + |1\rangle + |0\rangle - |1\rangle}{2} \\[8pt] &= \frac{2|0\rangle}{2} \\[8pt] &= |0\rangle \end{aligned}

Level 1: Phase Shift Gate

  • Phase shift gate converts real numbers to imaginary numbers and vice versa

S=∣0⟩⟨0∣+(i∣1⟩)⟨1∣=[100i]\boxed{\mathbf{S} = |0\rangle\langle0| + \big(i|1\rangle\big)\langle1| = \begin{bmatrix}1&0\\0&i\end{bmatrix}}

"State mapping:"
∣0⟩↦∣0⟩|0\rangle \mapsto |0\rangle
∣1⟩↦i∣1⟩|1\rangle \mapsto i|1\rangle = ∣1⟩↦cisπ2∣1⟩|1\rangle \mapsto \text{cis}\frac{\pi}{2}|1\rangle (change the phase of ket 1, to become π2\frac{\pi}{2}, 90°)

  • SS\mathbf{SS} is equivalent to sign flipping: SS∣0⟩=∣0⟩\mathbf{SS}|0\rangle=|0\rangle, SS∣1⟩=−∣1⟩\mathbf{SS}|1\rangle=-|1\rangle

  • qml.S(wires=…)

  • Let's define the circular bases

∣+i⟩=∣0⟩+i∣1⟩2∣−i⟩=∣0⟩−i∣1⟩2|{+i}\rangle = \frac{|0\rangle+i|1\rangle}{\sqrt2} \qquad |{-i}\rangle = \frac{|0\rangle-i|1\rangle}{\sqrt2}

📝 Instructor's annotation: bracketed both formulas together and labeled them "Eigenvectors of S."

Circuit:

(Figure 4: Phase shift gate)

Level 1: Controlled NOT

Meaning na krub: If the 1st qubit is one, you flip the 2nd qubit
(take first qubit 1st control)

  • Controlled NOT is a condition tester, where the second qubit is bit-flipped if the first qubit is ∣1⟩|1\rangle

CNOT=∣0,x⟩⟨0,x∣+∣1,xˉ⟩⟨1,x∣=[1000010000010010]\boxed{\text{CNOT} = |0,x\rangle\langle0,x| + |1,\bar x\rangle\langle1,x| = \begin{bmatrix}1&0&0&0\\0&1&0&0\\0&0&0&1\\0&0&1&0\end{bmatrix}}

x∈{0,1}x\in\{0,1\}

"State mapping:"
∣0,0⟩↦∣0,0⟩|0,0\rangle\mapsto|0,0\rangle
∣0,1⟩↦∣0,1⟩|0,1\rangle\mapsto|0,1\rangle
∣1,0⟩↦∣1,1⟩|1,0\rangle\mapsto|1,1\rangle
∣1,1⟩↦∣1,0⟩|1,1\rangle\mapsto|1,0\rangle #MidtermExam question, will look something like this

  • CNOT is equivalent to logical XOR ⊕\oplus: CNOT=∣x,(x⊕y)⟩⟨x,y∣\text{CNOT} = |x,(x\oplus y)\rangle\langle x,y|
  • CNOT combined with H\mathbf{H} creates various forms of entanglement
  • CNOT is also self-inversible: CNOT CNOT ∣x,y⟩=∣x,y⟩\text{CNOT}\ \text{CNOT}\ |x,y\rangle = |x,y\rangle
  • qml.CNOT(wires=…)

Circuit: qubit ∣x⟩|x\rangle (the control) passes through unchanged; qubit ∣y⟩|y\rangle (the target) comes out as ∣x⊕y⟩|x\oplus y\rangle. (Figure 5: Controlled NOT gate)

dark circle mean control (if it’s 1), ส่วน oplus ข้างหลัง หมายถึง bit flip ซะ

On the #MidtermExam, he might put the control on the down, and bit flip on the above, don’t be fooled by that (he mentioned suspiciously, that it might actually end up on the actual exam)

Also one more notation is that above may have blank (white) circle means that control is zero


ก็คือถ้าเป็น 0 ให้เปลี่ยน Bit ต่อมาละ - มันก็กลับมุมมองอะไรงี้แหละ

∣00⟩↦∣01⟩∣01⟩↦∣00⟩∣10⟩↦∣10⟩∣11⟩↦∣11⟩\begin{aligned} |00\rangle\mapsto|01\rangle \\ |01\rangle\mapsto|00\rangle \\ |10\rangle\mapsto|10\rangle \\ |11\rangle\mapsto|11\rangle \\ \end{aligned}

💡 Analogy: CNOT is a quantum "if–then" switch — qubit xx is the control (left untouched), qubit yy is the target. If the control reads 11, flip the target; if it reads 00, leave it alone. Paired with a Hadamard on the control, it's the standard recipe for cooking up entanglement.

Summary of Level 1

Table 2: Basic Clifford gates

GatesDescriptionsSymbols = Formulae
NOT gateBit-flip the input qubit, similar to the logical NOT.X=∣xˉ⟩⟨x∣\mathbf{X} = \vert\bar{x}\rangle\langle x\vert
Hadamard gateConvert pure ↔ superposition on the input qubit.H=∣+⟩⟨0∣+∣−⟩⟨1∣\mathbf{H} = \vert+\rangle\langle 0\vert + \vert-\rangle\langle 1\vert
Phase shift gateConvert real ↔ imaginary if the input qubit is 1.S=∣0⟩⟨0∣+(i∣1⟩)⟨1∣\mathbf{S} = \vert 0\rangle\langle 0\vert + (i\vert 1\rangle)\langle 1\vert
CNOT gateBit-flip the second qubit if the first one is 1. Create an entangled state when used with the Hadamard gate.CNOT=∣0,x⟩⟨0,x∣+∣1,xˉ⟩⟨1,x∣\mathbf{CNOT} = \vert 0, x\rangle\langle 0, x\vert + \vert 1, \bar{x}\rangle\langle 1, x\vert

Special Unitary Group

  • Special unitary group SU(n)\mathbf{SU}(n): the set of n×nn\times n complex matrices that are unitary and special
  • Formal definition:

SU(n)={U∈Cn×n∣UU∗=I and det⁡(U)=1}\boxed{\mathbf{SU}(n) = \{\mathbf{U}\in\mathbb{C}^{n\times n} \mid \mathbf{UU}^{*}=\mathbf{I} \text{ and } \det(\mathbf{U})=1\}}
Table 3: Clifford set and their SU groups

Quantum GatesGroups
NOT gateSU(2)\mathbf{SU}(2)
Hadamard gateSU(2)\mathbf{SU}(2)
Phase shift gateSU(2)\mathbf{SU}(2)
CNOT gateSU(4)\mathbf{SU}(4)
  • Why mentioning this?
    • Unitary (UU∗=I\mathbf{UU}^{*}=\mathbf{I}): All of these matrices are quantum operators
    • Special (det⁡(U)=1\det(\mathbf{U})=1): All of these matrices do not change the global phase

Quantum Algorithm: Example 1


Caption — Figure 6: "Just a quantum algorithm" — a 2-wire circuit (H\mathbf{H} on qubit 1, then X\mathbf{X} and S\mathbf{S} on qubit 2, then a CNOT joining both), with each intermediate state (∣0⟩⊗∣0⟩→∣+⟩⊗∣0⟩→∣+⟩⊗∣1⟩→∣+⟩⊗i∣1⟩→i∣Φ+⟩|0\rangle\otimes|0\rangle \to |+\rangle\otimes|0\rangle \to |+\rangle\otimes|1\rangle \to |+\rangle\otimes i|1\rangle \to i|\Phi^{+}\rangle) labeled directly above the wires at the point in the circuit where it occurs.

  • Step 0: The initial state is always ∣0⟩⊗N|0\rangle^{\otimes N}. ∣s(0)⟩=∣0⟩⊗∣0⟩=∣00⟩\left|s^{(0)}\right\rangle = |0\rangle\otimes|0\rangle=|00\rangle
  • Step 1: Apply the Hadamard gate on the first qubit. ∣s(1)⟩=(H∣0⟩)⊗∣0⟩=∣+⟩⊗∣0⟩\left|s^{(1)}\right\rangle = (\mathbf{H}|0\rangle)\otimes|0\rangle = |+\rangle\otimes|0\rangle
  • Step 2: Apply the NOT gate on the second qubit. ∣s(2)⟩=∣+⟩⊗(X∣0⟩)=∣+⟩⊗∣1⟩\left|s^{(2)}\right\rangle = |+\rangle\otimes(\mathbf{X}|0\rangle) = |+\rangle\otimes|1\rangle
  • Step 3: Apply the phase shift gate on the second qubit; ∣1⟩|1\rangle yields i∣1⟩i|1\rangle. ∣s(3)⟩=∣+⟩⊗(S∣1⟩)=∣+⟩⊗i∣1⟩\left|s^{(3)}\right\rangle = |+\rangle\otimes(\mathbf{S}|1\rangle) = |+\rangle\otimes i|1\rangle
  • Step 4: To apply CNOT, we first realize the tensor product of its inputs. ∣+⟩⊗i∣1⟩=∣0⟩+∣1⟩2⊗i∣1⟩=i2(∣01⟩+∣11⟩)\begin{aligned} |+\rangle\otimes i|1\rangle &= \frac{|0\rangle+|1\rangle}{\sqrt2}\otimes i|1\rangle \\ &= \frac{i}{\sqrt2}\big(|01\rangle+|11\rangle\big) \end{aligned}
    • Trick: if all quantum gates are single qubit, don’t have to expand
    • But when muti-qubit showed up, you need to expand #MidtermExam (maybe this is the hard question he mentioned about)
  • Step 4 (cont'd): Applying CNOT on i2(∣01⟩+∣11⟩)\dfrac{i}{\sqrt2}(|01\rangle+|11\rangle) yields ∣s(4)⟩=CNOT(i2(∣01⟩+∣11⟩))=i2(∣01⟩+∣10⟩)=i∣Φ+⟩\begin{aligned} \left|s^{(4)}\right\rangle &= \text{CNOT}\left(\frac{i}{\sqrt2}\big(|01\rangle+|11\rangle\big)\right) \\ &= \frac{i}{\sqrt2}\big(|01\rangle+|10\rangle\big) \\ &= i|\Phi^{+}\rangle \end{aligned}
    Note that the output now becomes an entangled state. The entanglement is denoted with a bidirectional arrow between the two qubits. (ถ้า reduce มาถึง bell state ขนาดนี้ก็สุดยอด แต่ติดไว้ข้างบนพอก็ได้)

Example 1 in PennyLane

import pennylane as qml
from pennylane import numpy as np
 
dev = qml.device('default.qubit', wires=[0, 1])
 
@qml.qnode(dev)
def circuit():
    # State preparation
    qml.H(wires=[0])
    qml.X(wires=[1])
    qml.S(wires=[1])
    qml.CNOT(wires=[0, 1])
    # Measurement
    return qml.state()
 
print(circuit())
import pennylane as qml
from pennylane import numpy as np
 
dev = qml.device('default.qubit', wires=['q1', 'q2'])
 
@qml.qnode(dev)
def circuit():
    # State preparation
    qml.H(wires=['q1'])
    qml.X(wires=['q2'])
    qml.S(wires=['q2'])
    qml.CNOT(wires=['q1', 'q2'])
    # Measurement
    return qml.state()
 
print(circuit())

Result: i∣Φ+⟩=i2(∣01⟩+∣10⟩)=[0i2i20]⊤i|\Phi^{+}\rangle = \dfrac{i}{\sqrt2}(|01\rangle+|10\rangle) = \begin{bmatrix}0 & \frac{i}{\sqrt2} & \frac{i}{\sqrt2} & 0\end{bmatrix}^{\top}

[0.+0.j 0.+0.707j 0.+0.707j 0.+0.j]

Hadamard Gate and For-Loop

  • Hadamard gate can be used as a for-loop
    ∣s(0)⟩=∣000⟩∣s(1)⟩=(H∣0⟩)⊗∣00⟩=∣+⟩⊗∣00⟩∣s(2)⟩=∣+⟩⊗(H∣0⟩)⊗∣0⟩=∣+⟩⊗∣+⟩⊗∣0⟩∣s(3)⟩=∣+⟩⊗∣+⟩⊗(H∣0⟩)=∣+⟩⊗∣+⟩⊗∣+⟩∣s(4)⟩=U(∣+⟩⊗∣+⟩⊗∣+⟩)=∑k=07U∣B3(k)⟩\begin{aligned} \left|s^{(0)}\right\rangle &= |000\rangle \\ \left|s^{(1)}\right\rangle &= (\mathbf{H}|0\rangle)\otimes|00\rangle = |+\rangle\otimes|00\rangle \\ \left|s^{(2)}\right\rangle &= |+\rangle\otimes(\mathbf{H}|0\rangle)\otimes|0\rangle = |+\rangle\otimes|+\rangle\otimes|0\rangle \\ \left|s^{(3)}\right\rangle &= |+\rangle\otimes|+\rangle\otimes(\mathbf{H}|0\rangle) = |+\rangle\otimes|+\rangle\otimes|+\rangle \\ \left|s^{(4)}\right\rangle &= \mathbf{U}\big(|+\rangle\otimes|+\rangle\otimes|+\rangle\big) \\ &= \sum_{k=0}^{7}\mathbf{U}|\mathbb{B}_3(k)\rangle \end{aligned}
∣s(0)⟩=∣000⟩∣s(1)⟩=(H∣0⟩)⊗∣00⟩=∣+⟩⊗∣00⟩∣s(2)⟩=∣+⟩⊗(H∣0⟩)⊗∣0⟩=∣+⟩⊗∣+⟩⊗∣0⟩∣s(3)⟩=∣+⟩⊗∣+⟩⊗(H∣0⟩)=∣+⟩⊗∣+⟩⊗∣+⟩∣0⟩+∣1⟩2⊗∣0⟩+∣1⟩2⊗∣0⟩+∣1⟩2∣s(4)⟩=U(∣+⟩⊗∣+⟩⊗∣+⟩)=18(∣000⟩+∣001⟩+∣010⟩+…+∣111⟩)=∑k=07U∣B3(k)⟩\begin{aligned} \left|s^{(0)}\right\rangle &= |000\rangle \\[8pt] \left|s^{(1)}\right\rangle &= (\mathbf{H}|0\rangle)\otimes|00\rangle = |+\rangle\otimes|00\rangle \\[8pt] \left|s^{(2)}\right\rangle &= |+\rangle\otimes(\mathbf{H}|0\rangle)\otimes|0\rangle = |+\rangle\otimes|+\rangle\otimes|0\rangle \\[8pt] \left|s^{(3)}\right\rangle &= |+\rangle\otimes|+\rangle\otimes(\mathbf{H}|0\rangle) \\[8pt] &= |+\rangle\otimes|+\rangle\otimes|+\rangle \color{red}{\quad \frac{|0\rangle+|1\rangle}{\sqrt{2}} \otimes \frac{|0\rangle+|1\rangle}{\sqrt{2}} \otimes \frac{|0\rangle+|1\rangle}{\sqrt{2}}} \\[8pt] \left|s^{(4)}\right\rangle &= \mathbf{U}\big(|+\rangle\otimes|+\rangle\otimes|+\rangle\big) \color{red}{\quad = \frac{1}{\sqrt{8}} \Big( |000\rangle + |001\rangle + |010\rangle + \ldots + |111\rangle \Big)} \\[8pt] &= \sum_{k=0}^{7}\mathbf{U}|\mathbb{B}_3(k)\rangle \end{aligned}
  • H⊗N∣0⟩⊗N\mathbf{H}^{\otimes N}|0\rangle^{\otimes N} is equivalent to 2N2^N iterations in classical computing


Caption — Figure 7: Quantum parallelism via superposition. Three qubits each pass through their own H\mathbf{H} gate (staggered across the circuit) before all three feed into a general operator U\mathbf{U} at the end, illustrating how H⊗3∣000⟩\mathbf{H}^{\otimes 3}|000\rangle produces an equal superposition over all 23=82^3=8 basis states, so that applying U\mathbf{U} once effectively applies it to all 8 candidates "in parallel."

We don’t actually need a for loop, we need H gate. We apply U in parallel without actually do threading. WOWWW! We need QPU - We can convert all classical program into quantum algorithm

💡 Analogy: This is the heart of quantum parallelism: instead of writing a classical for-loop that runs U\mathbf{U} once per candidate (2N2^N separate runs), NN Hadamard gates create a superposition of all2N2^N candidates simultaneously, and a single application of U\mathbf{U} touches all of them at once — the loop happens "in superposition" rather than in time.

Exercise 3.1 — State Preparation

Design quantum algorithms that prepare the following states. Develop PennyLane code to verify each of them. This is what we’ll see in the exam #MidtermExam (He said 100%, pay mroe attention) - You’ll be asked to create a quantum algorithm that prepares these states.

  1. ∣+⟩|+\rangle
  2. ∣01⟩|01\rangle
  3. ∣100⟩|100\rangle
  4. i∣1⟩i|1\rangle
  5. −∣1⟩-|1\rangle
  6. ∣−⟩⊗∣+⟩⊗∣−⟩|-\rangle\otimes|+\rangle\otimes|-\rangle #Important
  7. ∣Ψ+⟩|\Psi^{+}\rangle
  8. ∣Ψ−⟩|\Psi^{-}\rangle
  9. ∣1010⟩|1010\rangle
  10. ∣10⟩⊗∣+⟩⊗∣01⟩|10\rangle\otimes|+\rangle\otimes|01\rangle
  11. ∣Φ+⟩|\Phi^{+}\rangle
  12. ∣Φ−⟩|\Phi^{-}\rangle
  13. −∣0⟩-|0\rangle
  14. ∣01⟩⊗∣Ψ+⟩|01\rangle\otimes|\Psi^{+}\rangle
  15. ∣Ψ+⟩⊗∣Φ−⟩|\Psi^{+}\rangle\otimes|\Phi^{-}\rangle
  16. (−∣0⟩)⊗∣1⟩(-|0\rangle)\otimes|1\rangle
  17. ∣00⟩⊗∣+⟩⊗∣−⟩|00\rangle\otimes|+\rangle\otimes|-\rangle
  18. −∣+⟩-|+\rangle
  19. −∣−⟩-|-\rangle
  20. ∣0⟩⊗(−i∣1⟩)|0\rangle\otimes(-i|1\rangle)
  21. ∣+⟩⊗∣Ψ+⟩|+\rangle\otimes|\Psi^{+}\rangle
  22. (−i∣+⟩)⊗∣−⟩(-i|+\rangle)\otimes|-\rangle

Operator Augmentation

  • If we have a quantum operator

Q=∑k=1K∣ϕk⟩⟨ψk∣\mathbf{Q} = \sum_{k=1}^{K}|\phi_k\rangle\langle\psi_k|

we can augment Q\mathbf{Q} with trivial qubits by
I⊗M⊗Q=∑k=1K∣x1,…,xM,ϕk⟩⟨x1,…,xM,ψk∣\boxed{\mathbf{I}^{\otimes M}\otimes\mathbf{Q} = \sum_{k=1}^{K}|x_1,\dots,x_M,\phi_k\rangle\langle x_1,\dots,x_M,\psi_k|} Q⊗I⊗M=∑k=1K∣ϕk,x1,…,xM⟩⟨ψk,x1,…,xM∣\boxed{\mathbf{Q}\otimes\mathbf{I}^{\otimes M} = \sum_{k=1}^{K}|\phi_k,x_1,\dots,x_M\rangle\langle\psi_k,x_1,\dots,x_M|}

  • This is useful for accommodating all entangled qubits, some of which are not involved with the operator
  • Examples:
    I⊗X=∣x,yˉ⟩⟨x,y∣X⊗I=∣xˉ,y⟩⟨x,y∣\mathbf{I}\otimes\mathbf{X} = |x,\bar y\rangle\langle x,y| \qquad \mathbf{X}\otimes\mathbf{I} = |\bar x,y\rangle\langle x,y| I⊗CNOT=∣x,y,y⊕z⟩⟨x,y,z∣CNOT⊗I=∣x,x⊕y,z⟩⟨x,y,z∣\mathbf{I}\otimes\text{CNOT} = |x,y,y\oplus z\rangle\langle x,y,z| \qquad \text{CNOT}\otimes\mathbf{I} = |x,x\oplus y,z\rangle\langle x,y,z| I⊗2⊗H=∣x,y,+⟩⟨x,y,0∣+∣x,y,−⟩⟨x,y,1∣\mathbf{I}^{\otimes2}\otimes\mathbf{H} = |x,y,+\rangle\langle x,y,0| + |x,y,-\rangle\langle x,y,1| I⊗H⊗I=∣x,+,y⟩⟨x,0,y∣+∣x,−,y⟩⟨x,1,y∣\mathbf{I}\otimes\mathbf{H}\otimes\mathbf{I} = |x,+,y\rangle\langle x,0,y| + |x,-,y\rangle\langle x,1,y|

💡 Analogy: Augmentation is just "padding" an operator with identity wires it doesn't act on — like writing a function that only touches variables y and z but still has to pass x through untouched so the whole tuple (x, y, z) stays well-formed.

Operator Augmentation and Tensor Product

  • Important property of tensor product
    (⨂k=1NAk)(⨂k=1NBk)=⨂k=1NAkBk\boxed{\left(\bigotimes_{k=1}^{N}\mathbf{A}_k\right)\left(\bigotimes_{k=1}^{N}\mathbf{B}_k\right) = \bigotimes_{k=1}^{N}\mathbf{A}_k\mathbf{B}_k}

It applies backward เห็นป้ะ ๆ B ก่อน A นะ เพราะเวลาเราเขียนเนี่ยสุดท้าย ∣x⟩|x\rangle จะมาวางท้ายสุด ดังนั้นเราจะ applies B\mathbf{B} ก่อน ถูกป้ะ

  • Examples
    (X⊗Y)(I⊗Z)=XI⊗YZ(\mathbf{X}\otimes\mathbf{Y})(\mathbf{I}\otimes\mathbf{Z}) = \mathbf{XI}\otimes\mathbf{YZ} (H⊗H)(X⊗Y)(Y⊗Z)=HXY⊗HYZ(\mathbf{H}\otimes\mathbf{H})(\mathbf{X}\otimes\mathbf{Y})(\mathbf{Y}\otimes\mathbf{Z}) = \mathbf{HXY}\otimes\mathbf{HYZ} (H⊗H⊗X)(H⊗X⊗Y)(H⊗X⊗Z)=H3⊗HX2⊗XYZ(\mathbf{H}\otimes\mathbf{H}\otimes\mathbf{X})(\mathbf{H}\otimes\mathbf{X}\otimes\mathbf{Y})(\mathbf{H}\otimes\mathbf{X}\otimes\mathbf{Z}) = \mathbf{H}^3\otimes\mathbf{HX}^2\otimes\mathbf{XYZ}

  • This property is useful for Pauli decomposition (decomposing a large matrix into Pauli axes: X, Y, Z) and computing the Hamiltonian matrix (taught in Chapter 7 afterward)

Quantum Algorithm: Example 2

#MidtermExam Example of the midterm exam question!! And you have to write a python program to validate or something as well.


 Caption — Figure 8: "Another quantum algorithm" — a 3-wire circuit (H\mathbf{H} then CNOT on qubits 1–2, X\mathbf{X} on qubit 2, CNOT on qubits 2–3, S\mathbf{S} on qubit 3), with each intermediate state labeled above the wires: ∣0⟩⊗3→∣+⟩⊗∣00⟩→∣Ψ+⟩⊗∣0⟩→∣Φ+⟩⊗∣0⟩→∣011⟩+∣100⟩2→i∣011⟩+∣100⟩2|0\rangle^{\otimes3} \to |{+}\rangle\otimes|00\rangle \to |\Psi^{+}\rangle\otimes|0\rangle \to |\Phi^{+}\rangle\otimes|0\rangle \to \frac{|011\rangle+|100\rangle}{\sqrt2} \to \frac{i|011\rangle+|100\rangle}{\sqrt2}.

  • Step 0: The initial state is ∣0⟩⊗N|0\rangle^{\otimes N}. ∣s(0)⟩=∣0⟩⊗∣0⟩⊗∣0⟩\left|s^{(0)}\right\rangle = |0\rangle\otimes|0\rangle\otimes|0\rangle
  • Step 1: Apply the Hadamard gate on the first qubit. ∣s(1)⟩=(H∣0⟩)⊗∣0⟩⊗∣0⟩=∣+⟩⊗∣0⟩⊗∣0⟩\left|s^{(1)}\right\rangle = (\mathbf{H}|0\rangle)\otimes|0\rangle\otimes|0\rangle = |+\rangle\otimes|0\rangle\otimes|0\rangle
  • Step 2: Apply the CNOT gate on the first and second qubits. ∣s(2)⟩=(CNOT(∣+⟩⊗∣0⟩))⊗∣0⟩=(CNOT,∣00⟩+∣10⟩2)⊗∣0⟩=∣00⟩+∣11⟩2⊗∣0⟩\begin{aligned} \left|s^{(2)}\right\rangle &= \big(\text{CNOT}(|+\rangle\otimes|0\rangle)\big)\otimes|0\rangle \\ &= \left(\text{CNOT},\frac{|00\rangle+|10\rangle}{\sqrt2}\right)\otimes|0\rangle \\ &= \frac{|00\rangle+|11\rangle}{\sqrt2}\otimes|0\rangle \end{aligned} The result is an entangled state, so a bidirectional arrow is shown.
  • Step 3: Apply the NOT gate on the second qubit. Since it is entangled with the first qubit, we have to augment the NOT gate. I⊗X=∣x,yˉ⟩⟨x,y∣\mathbf{I}\otimes\mathbf{X} = |x,\bar y\rangle\langle x,y| Note that the first qubit is preserved while the second one is flipped. ∣s(3)⟩=((I⊗X)∣00⟩+∣11⟩2)⊗∣0⟩=∣01⟩+∣10⟩2⊗∣0⟩\begin{aligned} \left|s^{(3)}\right\rangle &= \big((\mathbf{I}\otimes\mathbf{X})\frac{|00\rangle+|11\rangle}{\sqrt2}\big)\otimes|0\rangle \\ &= \frac{|01\rangle+|10\rangle}{\sqrt2}\otimes|0\rangle \end{aligned}
  • Step 4: Apply the CNOT gate on the second and third qubits. Since the second qubit is entangled with the first one, we augment the CNOT gate. I⊗CNOT=∣x,y,y⊕z⟩⟨x,y,z∣\mathbf{I}\otimes\text{CNOT} = |x,y,y\oplus z\rangle\langle x,y,z| Note that the first qubit is preserved. ∣s(4)⟩=(I⊗CNOT)(∣01⟩+∣10⟩2⊗∣0⟩)=(I⊗CNOT),∣010⟩+∣100⟩2=∣011⟩+∣100⟩2\begin{aligned} \left|s^{(4)}\right\rangle &= (\mathbf{I}\otimes\text{CNOT})\left(\frac{|01\rangle+|10\rangle}{\sqrt2}\otimes|0\rangle\right) \\ &= (\mathbf{I}\otimes\text{CNOT}),\frac{|010\rangle+|100\rangle}{\sqrt2} \\ &= \frac{|011\rangle+|100\rangle}{\sqrt2} \end{aligned} Note that it results in another entangled state — that is why it's marked with a bidirectional arrow across all three qubits.
  • Step 5: Apply the phase shift gate on the third qubit. Since it is entangled with the first and second ones, we have to augment the gate. I⊗2⊗S=∣x,y,0⟩⟨x,y,0∣+i∣x,y,1⟩⟨x,y,1∣\mathbf{I}^{\otimes2}\otimes\mathbf{S} = |x,y,0\rangle\langle x,y,0| + i|x,y,1\rangle\langle x,y,1| Note that the first and second qubits are preserved. ∣s(5)⟩=(I⊗2⊗S)(∣011⟩+∣100⟩2)=i∣011⟩+∣100⟩2\begin{aligned} \left|s^{(5)}\right\rangle &= (\mathbf{I}^{\otimes2}\otimes\mathbf{S})\left(\frac{|011\rangle+|100\rangle}{\sqrt2}\right) \\ &= \frac{i|011\rangle+|100\rangle}{\sqrt2} \end{aligned}

Example 2 in PennyLane

import pennylane as qml
from pennylane import numpy as np
 
dev = qml.device('default.qubit', wires=[0,1,2])
 
@qml.qnode(dev)
def circuit():
    # State preparation
    qml.H(wires=[0])
    qml.CNOT(wires=[0,1])
    qml.X(wires=[1])
    qml.CNOT(wires=[1,2])
    qml.S(wires=[2])
 
    # Measurement
    return qml.state()
 
print(circuit())

Result:

[0. 0. 0. 0.707j 0.707 0. 0. 0.]

Creating GHZ State

 Caption — Figure 9: Hadamard gate and a series of CNOT gates can be used to create the GHZ state. Qubit 1 passes through H\mathbf{H}, then a chain of CNOTs runs from qubit 1 as control into qubit 2 as target, qubit 2 as control into qubit 3 as target, and so on down to qubit NN — each CNOT propagating the superposition one qubit further along the chain.

📝 Instructor's annotation: the printed formula under the figure rendered as a broken/garbled string in the PDF; the instructor rewrote it clearly by hand: ∣GHZN⟩=∣0⟩⊗N+∣1⟩⊗N2\boxed{|\text{GHZ}_N\rangle = \frac{|0\rangle^{\otimes N}+|1\rangle^{\otimes N}}{\sqrt2}}

  • He explicitly we should try creating ∣GHZ3⟩|\text{GHZ}_3\rangle, ∣GHZ4⟩|\text{GHZ}_4\rangle maybe on #MidtermExam???

Exercise 3.2 — Entanglement Preparation

Design quantum algorithms that prepare the following states. Develop PennyLane code to verify each of them.

  1. ∣Ψ+⟩|\Psi^{+}\rangle
  2. ∣Φ+⟩|\Phi^{+}\rangle
  3. 12(∣000⟩+∣111⟩)\frac{1}{\sqrt2}(|000\rangle+|111\rangle)
  4. 12(∣100⟩+∣011⟩)\frac{1}{\sqrt2}(|100\rangle+|011\rangle)
  5. 12(∣0011⟩+∣1100⟩)\frac{1}{\sqrt2}(|0011\rangle+|1100\rangle)
  6. 12(∣100⟩−∣011⟩)\frac{1}{\sqrt2}(|100\rangle-|011\rangle)
  7. 12(∣100⟩−i∣011⟩)\frac{1}{\sqrt2}(|100\rangle-i|011\rangle)
  8. 12(i∣100⟩−∣011⟩)\frac{1}{\sqrt2}(i|100\rangle-|011\rangle)
  9. 12(i∣101⟩+∣010⟩)⊗∣Ψ−⟩\frac{1}{\sqrt2}(i|101\rangle+|010\rangle)\otimes|\Psi^{-}\rangle

Answer the following questions.

  1. Let's define the adjoint shift gate S∗=∣0⟩⟨0∣−(i∣1⟩)⟨1∣\mathbf{S}^* = |0\rangle\langle0| - (i|1\rangle)\langle1| Show that SSS∣x⟩=S∗∣x⟩\mathbf{SSS}|x\rangle = \mathbf{S}^{*}|x\rangle for any quantum state ∣x⟩|x\rangle.
  2. Show that I⊗M⊗U=I2M⊗UU⊗I⊗M=U⊗I2M\mathbf{I}^{\otimes M}\otimes\mathbf{U} = \mathbf{I}_{2^M}\otimes\mathbf{U} \qquad \mathbf{U}\otimes\mathbf{I}^{\otimes M} = \mathbf{U}\otimes\mathbf{I}_{2^M} where IK\mathbf{I}_K is the K×KK\times K identity matrix.

3. Clifford Set/2

Level 2: Pauli-X Gate

  • Pauli gate rotates the qubit for phase π\pi on one of the axes: X, Y, and Z
  • Pauli X flips the qubit

∣0⟩↦∣1⟩∣1⟩↦∣0⟩|0\rangle\mapsto|1\rangle \qquad |1\rangle\mapsto|0\rangle

which is equivalent to rotation on Axis X

X=∣1⟩⟨0∣+∣0⟩⟨1∣=[0110]\boxed{\mathbf{X} = |1\rangle\langle0|+|0\rangle\langle1| = \begin{bmatrix}0&1\\1&0\end{bmatrix}}

  • X\mathbf{X} is self-inversible: XX∣x⟩=∣x⟩\mathbf{XX}|x\rangle=|x\rangle
  • qml.X(wires=…)


Caption — Figures 10–12: Pauli X, Y, and Z shown as rotations by π\pi on the phase circle (axes labeled pos real +1+1, neg real −1-1, pos img +i+i, neg img −i-i, with Bit 0 at top and Bit 1 at bottom): Pauli X rotates around the real axis (swapping Bit 0 ↔ Bit 1), Pauli Y rotates around the imaginary axis (swapping bits and flipping through ±i\pm i), Pauli Z rotates in place around the vertical bit-axis (keeping the bit but flipping its sign).

Level 2: Pauli-Y Gate

  • Pauli Y flips the qubit, flips the sign, and flips the phase
    • Flip แม่งทุกอย่างเลย

∣0⟩↦i∣1⟩∣1⟩↦−i∣0⟩|0\rangle\mapsto i|1\rangle \qquad |1\rangle\mapsto-i|0\rangle

which is equivalent to rotation on Axis Y

Y=(i∣1⟩)⟨0∣+(−i∣0⟩)⟨1∣=[0−ii0]\boxed{\mathbf{Y} = (i|1\rangle)\langle0|+(-i|0\rangle)\langle1| = \begin{bmatrix}0&-i\\i&0\end{bmatrix}}

  • Y\mathbf{Y} is self-inversible: YY∣x⟩=∣x⟩\mathbf{YY}|x\rangle=|x\rangle
  • qml.Y(wires=…)

💡 Analogy: X\mathbf{X} flips which basis state you're in. Z\mathbf{Z} flips the sign but leaves you in the same basis state. Y\mathbf{Y} does both at once, and picks up a factor of ±i\pm i along the way — it's genuinely "X\mathbf{X} and Z\mathbf{Z} combined" (you'll prove Y=iXZ\mathbf{Y}=i\mathbf{XZ} in Exercise 3.3).

Level 2: Pauli-Z Gate

  • Pauli Z keeps the qubit and flips the sign

∣0⟩↦∣0⟩∣1⟩↦−∣1⟩|0\rangle\mapsto|0\rangle \qquad |1\rangle\mapsto-|1\rangle

which is equivalent to rotation on Axis Z

Z=∣0⟩⟨0∣+(−∣1⟩)⟨1∣=[100−1]\boxed{\mathbf{Z} = |0\rangle\langle0|+(-|1\rangle)\langle1| = \begin{bmatrix}1&0\\0&-1\end{bmatrix}}

  • Z\mathbf{Z} is self-inversible: ZZ∣x⟩=∣x⟩\mathbf{ZZ}|x\rangle=|x\rangle
  • qml.Z(wires=…)

Summary of Level 2

Table 4: Pauli gates. All of them belong to the SU(2)\mathbf{SU}(2) group.

Gates∣0⟩\vert0\rangle∣1⟩\vert1\rangleFlip qubit?Flip sign?Flip phase?
X∣1⟩\vert{}1\rangle∣0⟩\vert{}0\rangleYNN
Yi∣1⟩i\vert{}1\rangle−i∣0⟩-i\vert{}0\rangleYYY
Z∣0⟩\vert{}0\rangle−∣1⟩-\vert{}1\rangleNYN

Circuits:
∣0⟩→ X ∣1⟩, ∣1⟩→ X ∣0⟩∣0⟩→ Y i∣1⟩, ∣1⟩→ Y −i∣0⟩∣0⟩→ Z ∣0⟩, ∣1⟩→ Z −∣1⟩|0\rangle \xrightarrow{\ \mathbf{X}\ } |1\rangle,\ |1\rangle \xrightarrow{\ \mathbf{X}\ } |0\rangle \qquad\quad |0\rangle \xrightarrow{\ \mathbf{Y}\ } i|1\rangle,\ |1\rangle \xrightarrow{\ \mathbf{Y}\ } {-i}|0\rangle \qquad\quad |0\rangle \xrightarrow{\ \mathbf{Z}\ } |0\rangle,\ |1\rangle \xrightarrow{\ \mathbf{Z}\ } {-}|1\rangle(Figures 13, 14, 15: Pauli X, Y, Z)

Generalized Operator Augmentation

  • For any quantum operator Q\mathbf{Q}, we can augment it with trivial qubits by

Qp1,…,pM(N)=∑k=1K∣x1′,…,xN′⟩⟨x1,…,xN∣\boxed{\mathbf{Q}^{(N)}_{p_1,\dots,p_M} = \sum_{k=1}^{K}|x_1',\dots,x_N'\rangle\langle x_1,\dots,x_N|}
where the output mixed state is specified by the indices p1,…,pMp_1,\dots,p_M, i.e.

∣xp1′,…,xpM′⟩=Q∣xp1,…,xpM⟩|x_{p_1}',\dots,x_{p_M}'\rangle = \mathbf{Q}|x_{p_1},\dots,x_{p_M}\rangle
while the remaining input qubits are unchanged, i.e. xpj′=xpjx_{p_j}'=x_{p_j} for all indices j∉{p1,…,pM}j\notin\{p_1,\dots,p_M\}

📝 Instructor's annotation: drew an arrow from the superscript NN to the note **"→ NN qubits in total,"**and an arrow from the subscript p1,…,pMp_1,\dots,p_M to the note "only apply to these qubits" — a compact key for reading the notation: the superscript is the total system size, the subscript is which qubits the operator actually touches.

  • Examples:
    X3(3)=∣x,y,1⟩⟨x,y,0∣+∣x,y,0⟩⟨x,y,1∣\mathbf{X}_3^{(3)} = |x,y,1\rangle\langle x,y,0| + |x,y,0\rangle\langle x,y,1| Z2(3)=∣x,0,z⟩⟨x,0,z∣+(−1)∣x,1,z⟩⟨x,1,z∣\mathbf{Z}_2^{(3)} = |x,0,z\rangle\langle x,0,z| + (-1)|x,1,z\rangle\langle x,1,z| H1(3)=∣+,y,z⟩⟨0,y,z∣+∣−,y,z⟩⟨1,y,z∣\mathbf{H}_1^{(3)} = |+,y,z\rangle\langle0,y,z| + |-,y,z\rangle\langle1,y,z| CNOT1,3(4)=∣0,x,y,z⟩⟨0,x,y,z∣+∣1,x,yˉ,z⟩⟨1,x,y,z∣\text{CNOT}_{1,3}^{(4)} = |0,x,y,z\rangle\langle0,x,y,z| + |1,x,\bar y,z\rangle\langle1,x,y,z|

💡 Analogy: This is a more precise version of the "augmentation" from Clifford Set/1: instead of always padding identities on the outside (left/right), Qp1,…,pM(N)\mathbf{Q}^{(N)}_{p_1,\dots,p_M} lets the operator reach into any subset of positions in an NN-qubit register — like indexing directly into specific array slots instead of only ever operating on a fixed prefix or suffix.

Quantum Algorithm: Example 3

  • Step 0: The initial state is ∣0⟩⊗N|0\rangle^{\otimes N}. ∣s(0)⟩=∣0⟩⊗∣0⟩⊗∣0⟩⊗∣0⟩\left|s^{(0)}\right\rangle = |0\rangle\otimes|0\rangle\otimes|0\rangle\otimes|0\rangle
  • Step 1: We recognize two entangled states caused by the Hadamard and CNOT gates. ∣s(1)⟩=12(∣00⟩+∣11⟩)⊗(∣00⟩+∣11⟩)\left|s^{(1)}\right\rangle = \frac{1}{2}\big(|00\rangle+|11\rangle\big)\otimes\big(|00\rangle+|11\rangle\big)
  • Step 2: We apply the X and Z gates on the first and latter pairs of qubits. ∣s(2)⟩=(I⊗X⊗I⊗Z)∣s(1)⟩=12(∣01⟩+∣10⟩)⊗(∣00⟩−∣11⟩)\begin{aligned} \left|s^{(2)}\right\rangle &= (\mathbf{I}\otimes\mathbf{X}\otimes\mathbf{I}\otimes\mathbf{Z})\left|s^{(1)}\right\rangle \\ &= \frac{1}{2}\big(|01\rangle+|10\rangle\big)\otimes\big(|00\rangle-|11\rangle\big) \end{aligned}
  • Step 2 (cont'd): We expand the tensor product of ∣s(2)⟩\left|s^{(2)}\right\rangle ∣s(2)⟩=12(∣0100⟩−∣0111⟩+∣1000⟩−∣1011⟩)\left|s^{(2)}\right\rangle = \frac{1}{2}\big(|0100\rangle-|0111\rangle+|1000\rangle-|1011\rangle\big)
  • Step 3: The CNOT gate for the 2nd and 4th qubits is simply a CNOT gate with the 1st and 3rd qubits augmented to it. CNOT2,4(4)=∣v,x,y,x⊕z⟩⟨v,x,y,z∣\text{CNOT}_{2,4}^{(4)} = |v,x,y,x\oplus z\rangle\langle v,x,y,z| Observe that vv and yy are introduced as dummy variables.
  • Step 3 (cont'd): Applying this gate on ∣s(2)⟩\left|s^{(2)}\right\rangle yields CNOT2,4(4)∣s(2)⟩=12(∣0101⟩−∣0110⟩+∣1000⟩−∣1011⟩)\text{CNOT}_{2,4}^{(4)}\left|s^{(2)}\right\rangle = \frac{1}{2}\big(|0101\rangle-|0110\rangle+|1000\rangle-|1011\rangle\big) which is no longer entangled.

Caption — Figure 16: "Yet another quantum algorithm" — a 4-wire circuit: H\mathbf{H} + CNOT create a Bell pair on qubits 1–2, another H\mathbf{H} + CNOT create a second Bell pair on qubits 3–4, then X\mathbf{X} acts on qubit 2 and Z\mathbf{Z} on qubit 4, and finally a CNOT links qubit 2 (control) to qubit 4 (target) — the step that disentangles the whole system back into a product state.

Exercise 3.3 — Pauli Gates

Design quantum algorithms that prepare the following states. Develop PennyLane code to verify each of them. #MidtermExam (In the midterm exam, you’ll see something like this)

  • More of #MidtermExam
    • quantum algorithm, show me step by step, the quantum state in each step.
    • given a quantum state, created with a quantum algorithm.
  1. ∣Φ+⟩|\Phi^{+}\rangle
  2. i2(∣01⟩−∣10⟩)\frac{i}{\sqrt2}(|01\rangle-|10\rangle)
  3. ∣Ψ−⟩|\Psi^{-}\rangle
  4. i2(−∣00⟩+∣11⟩)\frac{i}{\sqrt2}(-|00\rangle+|11\rangle) [Use Y.]
  5. 12(∣011⟩+∣100⟩)\frac{1}{\sqrt2}(|011\rangle+|100\rangle) [Use Z.]
  6. 12(−∣010⟩−i∣101⟩)\frac{1}{\sqrt2}(-|010\rangle-i|101\rangle) [Use Y.]
  7. i2(∣0100⟩−∣1011⟩)\frac{i}{\sqrt2}(|0100\rangle-|1011\rangle) [Use X, Y, Z.]
  8. i2(∣0101⟩−∣0110⟩−∣1000⟩+∣1011⟩)\frac{i}{2}(|0101\rangle-|0110\rangle-|1000\rangle+|1011\rangle) [Use Y and Z.]

Answer the following questions.

  1. Show that Z∣x⟩=HXH∣x⟩\mathbf{Z}|x\rangle=\mathbf{HXH}|x\rangle. It means that Z\mathbf{Z} can be emulated by the Hadamard gate and the NOT gate.

  2. Show that Y∣x⟩=iXZ∣x⟩\mathbf{Y}|x\rangle=i\mathbf{XZ}|x\rangle.

    💡 This one's description on the slide reuses the wording from Q9 ("emulated by the Hadamard gate and the NOT gate"), but the actual formula uses X\mathbf{X} and Z\mathbf{Z} — so read it as: Y\mathbf{Y} can be emulated by the X\mathbf{X} and Z\mathbf{Z} gates (up to the factor of ii).

  3. Show that −iXYZ=I-i\mathbf{XYZ}=\mathbf{I}.

  4. Show that for any single-qubit quantum operator Q\mathbf{Q}, Qj(N)=I⊗j−1⊗Q⊗I⊗N−j\mathbf{Q}_j^{(N)} = \mathbf{I}^{\otimes j-1}\otimes\mathbf{Q}\otimes\mathbf{I}^{\otimes N-j}

4. Clifford Set/3

Level 3: SWAP Gate

  • Swap: We can trivially swap the places of two qubits for ease of processing

SWAP=∣y,x⟩⟨x,y∣=[1000001001000001]\boxed{\text{SWAP} = |y,x\rangle\langle x,y| = \begin{bmatrix}1&0&0&0\\0&0&1&0\\0&1&0&0\\0&0&0&1\end{bmatrix}}

"State mapping:"
∣00⟩↦∣00⟩\vert 00 \rangle \mapsto \vert 00 \rangle
∣01⟩↦∣10⟩\vert 01 \rangle \mapsto \vert 10 \rangle
∣10⟩↦∣01⟩\vert 10 \rangle \mapsto \vert 01 \rangle
∣11⟩↦∣11⟩\vert 11 \rangle \mapsto \vert 11 \rangle

SWAP=∣00⟩⟨00∣+∣10⟩⟨01∣+∣01⟩⟨10∣+∣11⟩⟨11∣\text{SWAP} = |00\rangle\langle00|+|10\rangle\langle01|+|01\rangle\langle10|+|11\rangle\langle11|

  • This is useful for rearranging the qubits before passing them to a quantum gate
  • qml.SWAP(wires=…)
  • Example: SWAP(∣01⟩−∣10⟩2)=∣10⟩−∣01⟩2\text{SWAP}\left(\frac{|01\rangle-|10\rangle}{\sqrt2}\right) = \frac{|10\rangle-|01\rangle}{\sqrt2}
  • SWAP is self-inversible: SWAP SWAP ∣x,y⟩=∣x,y⟩\text{SWAP}\ \text{SWAP}\ |x,y\rangle = |x,y\rangle

Circuit: qubit ∣x⟩|x\rangle and qubit ∣y⟩|y\rangle simply trade places.

(Figure 17: Swap gate)

Level 3: Controlled Z Gate

  • Controlled Z gate keeps the qubit and flips the sign if the control qubit is ∣1⟩|1\rangle

CZ=(−1)x⋅y∣xy⟩⟨xy∣=[100001000010000−1]\boxed{\text{CZ} = (-1)^{x\cdot y}|xy\rangle\langle xy| = \begin{bmatrix}1&0&0&0\\0&1&0&0\\0&0&1&0\\0&0&0&-1\end{bmatrix}}

"State mapping:"
∣00⟩↦∣00⟩\vert 00 \rangle \mapsto \vert 00 \rangle
∣01⟩↦∣01⟩\vert 01 \rangle \mapsto \vert 01 \rangle
∣10⟩↦∣10⟩\vert 10 \rangle \mapsto \vert 10 \rangle
∣11⟩↦−∣11⟩\vert 11 \rangle \mapsto -\vert 11 \rangle

CZ=∣00⟩⟨00∣+∣01⟩⟨01∣+∣10⟩⟨10∣+(−1)∣11⟩⟨11∣\text{CZ} = |00\rangle\langle00|+|01\rangle\langle01|+|10\rangle\langle10|+(-1)|11\rangle\langle11|

  • CZ is self-inversible: CZ CZ ∣x,y⟩=∣x,y⟩\text{CZ}\ \text{CZ}\ |x,y\rangle = |x,y\rangle
  • qml.CZ(wires=…)
  • Example: CZ(∣01⟩−∣10⟩2)=∣01⟩−∣11⟩2\text{CZ}\left(\frac{|01\rangle-|10\rangle}{\sqrt2}\right) = \frac{|01\rangle-|11\rangle}{\sqrt2}

Circuit: control qubit ∣x⟩|x\rangle passes through unchanged; target qubit ∣y⟩|y\rangle comes out as (−1)xy∣y⟩(-1)^{xy}|y\rangle.

(Figure 18: Controlled Z gate)

Exercise 3.4 — Swap and Controlled Z

Answer the following questions.

  1. Let's define the Controlled Y gate CY\mathbf{CY} from the following state mapping: ∣0x⟩↦∣0x⟩∣10⟩↦i∣10⟩∣11⟩↦−i∣11⟩|0x\rangle\mapsto|0x\rangle \qquad |10\rangle\mapsto i|10\rangle \qquad |11\rangle\mapsto-i|11\rangle Explain why CY\mathbf{CY} is not self-inversible.
  2. Compute SWAP2,3(4)∣s⟩\text{SWAP}_{2,3}^{(4)}|s\rangle, where ∣s⟩=12(∣00⟩+∣11⟩)⊗(∣01⟩+∣10⟩)|s\rangle = \frac{1}{2}\big(|00\rangle+|11\rangle\big)\otimes\big(|01\rangle+|10\rangle\big)
  3. Create 12(∣0⟩⊗N+(−1)N+1∣1⟩⊗N)\dfrac{1}{\sqrt2}\big(|0\rangle^{\otimes N}+(-1)^{N+1}|1\rangle^{\otimes N}\big), where N≥1N\geq1, using only H\mathbf{H} and CZ\text{CZ}.
  4. Show that CZ=(I⊗H),CNOT,(I⊗H)\text{CZ} = (\mathbf{I}\otimes\mathbf{H}),\text{CNOT},(\mathbf{I}\otimes\mathbf{H}). It means that CZ can be emulated by the H\mathbf{H} and CNOT gates.
  5. Show that SWAP=CNOT1,2(2) CNOT2,1(2) CNOT1,2(2)\text{SWAP} = \text{CNOT}_{1,2}^{(2)}\ \text{CNOT}_{2,1}^{(2)}\ \text{CNOT}_{1,2}^{(2)} It means that SWAP can be emulated only by the CNOT gate.
  6. Design a quantum operator L\mathbf{L} that rotates a mixed state of NN qubits to the left by one bit. E.g. L∣101100⟩=∣011001⟩\mathbf{L}|101100\rangle=|011001\rangle. Then convert L\mathbf{L} to a quantum algorithm.

5. Circuit Visualization

  • We can visualize a quantum circuit in two formats: text and graphics

Text Format

  • qml.draw(circuit)(params)
  • This instruction displays the given circuit after applying the given parameters on it
@qml.qnode(dev)
def circuit(theta: float):
    qml.H(wires=[0])
    qml.RY(theta, wires=[0])
    qml.CNOT(wires=[0,1])
    return qml.state()
 
print(qml.draw(circuit)(np.pi / 6))

(Figure 19: String representation of a quantum circuit)

0: ──H──RY(0.52)─╭●─┤  State
1: ──────────────╰X─┤  State

Graphical Format

  • qml.draw_mpl(circuit)(params)
@qml.qnode(dev)
def circuit(theta: float):
    qml.H(wires=[0])
    qml.RY(theta, wires=[0])
    qml.CNOT(wires=[0,1])
    return qml.state()
 
fig, ax = qml.draw_mpl(circuit)(np.pi / 6)
fig.savefig('circuit.png')

(Figure 20: Graphical representation of a quantum circuit — the same H→RY(θ)→CNOT\mathbf{H}\to\mathbf{RY}(\theta)\to\text{CNOT} circuit as above, rendered as a matplotlib figure with boxed gate symbols and a measurement icon on each wire, and saved to circuit.png.)


Conclusion

Table 5: Single-qubit gates

| Gates | Symbols | From ∣0⟩|0\rangle | From ∣1⟩|1\rangle|
|---|---|---|---|
| NOT | X | ∣1⟩|1\rangle | ∣0⟩|0\rangle|
| Hadamard | H | ∣0⟩+∣1⟩2\frac{|0\rangle+|1\rangle}{\sqrt2} | ∣0⟩−∣1⟩2\frac{|0\rangle-|1\rangle}{\sqrt2}|
| Phase shift | S | ∣0⟩|0\rangle | i∣1⟩i|1\rangle|
| Pauli-X | X | ∣1⟩|1\rangle | ∣0⟩|0\rangle|
| Pauli-Y | Y | i∣1⟩i|1\rangle | −i∣0⟩-i|0\rangle|
| Pauli-Z | Z | ∣0⟩|0\rangle | −∣1⟩-|1\rangle |

Two-qubit gates (Figure 21)

GatesSymbolsDescriptions
CNOTCNOT$
SwapSWAP$
CZCZ$
  • Quantum algorithm = a circuit of Clifford (and other) gates for state preparation, computation, and measurement
  • Operator augmentation (I⊗M⊗Q\mathbf{I}^{\otimes M}\otimes\mathbf{Q}, Q⊗I⊗M\mathbf{Q}\otimes\mathbf{I}^{\otimes M}, and the generalized Qp1,…,pM(N)\mathbf{Q}^{(N)}_{p_1,\dots,p_M}) lets a gate defined on a few qubits act correctly inside a larger, possibly entangled register — see Operator Augmentation and Generalized Operator Augmentation above for the full definitions and worked examples.

Target Learning Outcomes

Conceptual Understanding of Quantum Circuits

  • Define the three primary stages of a quantum algorithm: state preparation, computation through gate manipulation, and measurement.
  • Explain the characteristics of the Clifford gate set, including fault-tolerance, self-inversibility, and efficient classical simulability.

Quantum Gate Mastery

  • Apply Level 1 and 2 gates (Hadamard, Phase Shift, and Pauli X, Y, Z) to transform pure states into superpositions or modify quantum phases.
  • Differentiate between the effects of Pauli X, Y, and Z gates in terms of qubit flipping, sign flipping, and phase changes.
  • Utilize multi-qubit gates (CNOT, SWAP, CZ) to create entanglement or rearrange qubit order for processing.

Mathematical and Algebraic Application

  • Calculate the state of a quantum system after a series of gate applications using tensor products and matrix multiplication.
  • Perform operator augmentation to describe the action of a gate on specific indices within an N-qubit state.
  • Prove gate equivalencies through algebraic manipulation.

Computational Implementation

  • Construct functional quantum circuits using the PennyLane library, including device initialization and QNode definition.
  • Visualize quantum circuits in both text and graphical formats to verify algorithm structure.
  • Develop algorithms to prepare specific quantum states, such as Bell states or GHZ states.