Learn Pennylane - 10 Code Examples & CST Typing Practice Test
PennyLane is an open-source Python library for differentiable programming of quantum computers. It enables hybrid quantum-classical machine learning workflows, automatic differentiation, and optimization across multiple quantum hardware platforms.
View all 10 Pennylane code examples →
Learn PENNYLANE with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
PennyLane Simple Quantum Circuit
import pennylane as qml
from pennylane import numpy as np
# Define a 2-qubit device
dev = qml.device('default.qubit', wires=2)
# Define a quantum circuit
@qml.qnode(dev)
def circuit(params):
qml.RX(params[0], wires=0)
qml.RY(params[1], wires=1)
qml.CNOT(wires=[0,1])
return qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliZ(1))
# Evaluate the circuit
params = np.array([0.1, 0.2])
print(circuit(params))
A minimal PennyLane example defining a 2-qubit quantum circuit, applying gates, and computing expectation values.
PennyLane Bell State Circuit
import pennylane as qml
dev = qml.device('default.qubit', wires=2)
@qml.qnode(dev)
def bell_state():
hadamard(0)
qml.CNOT(wires=[0,1])
return qml.state()
print(bell_state())
Creates a Bell state using a Hadamard and CNOT gate.
PennyLane GHZ State Circuit
import pennylane as qml
dev = qml.device('default.qubit', wires=3)
@qml.qnode(dev)
def ghz_state():
qml.Hadamard(wires=0)
qml.CNOT(wires=[0,1])
qml.CNOT(wires=[0,2])
return qml.state()
print(ghz_state())
Generates a 3-qubit GHZ state.
PennyLane Quantum Teleportation
import pennylane as qml
from pennylane import numpy as np
dev = qml.device('default.qubit', wires=3)
@qml.qnode(dev)
def teleport(msg_state):
qml.QubitStateVector(msg_state, wires=0)
qml.Hadamard(wires=1)
qml.CNOT(wires=[1,2])
qml.CNOT(wires=[0,1])
qml.Hadamard(wires=0)
return qml.state()
msg_state = [1,0]
print(teleport(msg_state))
Implements the quantum teleportation protocol.
PennyLane Variational Circuit Example
import pennylane as qml
from pennylane import numpy as np
dev = qml.device('default.qubit', wires=2)
@qml.qnode(dev)
def var_circuit(params):
qml.RX(params[0], wires=0)
qml.RY(params[1], wires=1)
qml.CNOT(wires=[0,1])
return qml.expval(qml.PauliZ(0))
params = np.array([0.5, 0.3])
print(var_circuit(params))
A simple variational quantum circuit using RX and RY rotations.
PennyLane Controlled Phase Gate
import pennylane as qml
dev = qml.device('default.qubit', wires=2)
@qml.qnode(dev)
def cphase_example():
qml.Hadamard(wires=0)
qml.CPhase(np.pi/2, wires=[0,1])
return qml.state()
print(cphase_example())
Applies a controlled phase rotation between two qubits.
PennyLane Swap Test
import pennylane as qml
from pennylane import numpy as np
dev = qml.device('default.qubit', wires=3)
@qml.qnode(dev)
def swap_test(state1, state2):
qml.Hadamard(wires=0)
qml.QubitStateVector(state1, wires=1)
qml.QubitStateVector(state2, wires=2)
qml.CSWAP(wires=[0,1,2])
qml.Hadamard(wires=0)
return qml.expval(qml.PauliZ(0))
state1 = [1,0]
state2 = [0,1]
print(swap_test(state1, state2))
Performs a swap test to compute the overlap of two quantum states.
PennyLane Gradient Computation
import pennylane as qml
from pennylane import numpy as np
dev = qml.device('default.qubit', wires=1)
@qml.qnode(dev)
def circuit(x):
qml.RX(x, wires=0)
return qml.expval(qml.PauliZ(0))
x = 0.5
grad_fn = qml.grad(circuit)
print(grad_fn(x))
Computes the gradient of a simple variational circuit.
PennyLane Quantum Fourier Transform
import pennylane as qml
import numpy as np
dev = qml.device('default.qubit', wires=3)
def qft3():
qml.Hadamard(wires=0)
qml.CPhase(np.pi/2, wires=[0,1])
qml.CPhase(np.pi/4, wires=[0,2])
qml.Hadamard(wires=1)
qml.CPhase(np.pi/2, wires=[1,2])
qml.Hadamard(wires=2)
@qml.qnode(dev)
def qft_circuit():
qft3()
return qml.state()
print(qft_circuit())
Implements a simple 3-qubit quantum Fourier transform.
PennyLane Quantum Circuit with Measurement
import pennylane as qml
dev = qml.device('default.qubit', wires=2)
@qml.qnode(dev)
def measure_circuit():
qml.Hadamard(wires=0)
qml.CNOT(wires=[0,1])
return qml.sample()
print(measure_circuit())
Applies gates and measures qubits in the computational basis.
Frequently Asked Questions about Pennylane
What is Pennylane?
PennyLane is an open-source Python library for differentiable programming of quantum computers. It enables hybrid quantum-classical machine learning workflows, automatic differentiation, and optimization across multiple quantum hardware platforms.
What are the primary use cases for Pennylane?
Developing hybrid quantum-classical machine learning models. Simulating quantum circuits and computing gradients with automatic differentiation. Running variational algorithms such as VQE and QAOA. Integrating with classical ML frameworks like TensorFlow, PyTorch, and JAX. Executing quantum programs on hardware from multiple vendors
What are the strengths of Pennylane?
Seamless integration with classical ML frameworks. Automatic differentiation for hybrid quantum-classical models. Flexible device-agnostic design for multiple quantum backends. Active community and strong documentation. Rapid prototyping for research and experimentation
What are the limitations of Pennylane?
Simulation of large circuits is computationally intensive. Performance depends on the backend and hardware availability. Requires familiarity with quantum computing and ML frameworks. Less low-level control compared to SDKs like Qiskit or Forest. Certain advanced features may require multiple plugins
How can I practice Pennylane typing speed?
CodeSpeedTest offers 10+ real Pennylane code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.