Learn Forest-sdk - 10 Code Examples & CST Typing Practice Test
Forest SDK is Rigetti’s quantum software development kit that enables writing, simulating, compiling, and executing quantum programs using the Quil instruction language.
View all 10 Forest-sdk code examples →
Learn FOREST-SDK with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
Forest SDK Simple Quantum Circuit
from pyquil import Program, get_qc
from pyquil.gates import H, CNOT, MEASURE
# Create a program
p = Program()
ro = p.declare('ro', 'BIT', 2)
# Apply gates
p += H(0)
p += CNOT(0, 1)
# Measure qubits
p += MEASURE(0, ro[0])
p += MEASURE(1, ro[1])
# Run on QVM simulator
qc = get_qc('2q-qvm')
result = qc.run(p)
print(result)
A minimal example using Forest SDK to create a 2-qubit circuit, apply gates, and simulate measurements.
Forest SDK Bell State Circuit
from pyquil import Program, get_qc
from pyquil.gates import H, CNOT, MEASURE
p = Program()
ro = p.declare('ro', 'BIT', 2)
p += H(0)
p += CNOT(0,1)
p += MEASURE(0, ro[0])
p += MEASURE(1, ro[1])
qc = get_qc('2q-qvm')
result = qc.run(p)
print('Bell state result:', result)
Create a Bell state using 2 qubits and measure them.
Forest SDK GHZ State Circuit
from pyquil import Program, get_qc
from pyquil.gates import H, CNOT, MEASURE
p = Program()
ro = p.declare('ro', 'BIT', 3)
p += H(0)
p += CNOT(0,1)
p += CNOT(0,2)
p += MEASURE(0, ro[0])
p += MEASURE(1, ro[1])
p += MEASURE(2, ro[2])
qc = get_qc('3q-qvm')
result = qc.run(p)
print('GHZ state result:', result)
Create a 3-qubit GHZ state and measure all qubits.
Forest SDK Superposition Example
from pyquil import Program, get_qc
from pyquil.gates import H, MEASURE
p = Program()
ro = p.declare('ro', 'BIT', 1)
p += H(0)
p += MEASURE(0, ro[0])
qc = get_qc('1q-qvm')
result = qc.run(p)
print('Superposition result:', result)
Create a superposition on a single qubit.
Forest SDK Quantum Teleportation Example
from pyquil import Program, get_qc
from pyquil.gates import H, CNOT, MEASURE, X, Z
p = Program()
ro = p.declare('ro', 'BIT', 3)
p += H(1)
p += CNOT(1,2)
p += CNOT(0,1)
p += H(0)
p += MEASURE(0, ro[0])
p += MEASURE(1, ro[1])
p += X(2).controlled(ro[1])
p += Z(2).controlled(ro[0])
p += MEASURE(2, ro[2])
qc = get_qc('3q-qvm')
result = qc.run(p)
print('Teleportation result:', result)
Minimal teleportation protocol using 3 qubits.
Forest SDK Quantum Fourier Transform Example
from pyquil import Program, get_qc
from pyquil.gates import H, CPHASE, MEASURE
import numpy as np
p = Program()
ro = p.declare('ro', 'BIT', 2)
p += H(0)
p += CPHASE(np.pi/2, 0,1)
p += H(1)
p += MEASURE(0, ro[0])
p += MEASURE(1, ro[1])
qc = get_qc('2q-qvm')
result = qc.run(p)
print('QFT result:', result)
Minimal 2-qubit QFT demonstration.
Forest SDK Grover's Algorithm Example
from pyquil import Program, get_qc
from pyquil.gates import H, Z, X, CNOT, MEASURE
p = Program()
ro = p.declare('ro','BIT',2)
p += H(0)
p += H(1)
p += Z(0)
p += H(0)
p += H(1)
p += MEASURE(0,ro[0])
p += MEASURE(1,ro[1])
qc = get_qc('2q-qvm')
result = qc.run(p)
print('Grover result:', result)
Minimal Grover search on 2 qubits.
Forest SDK Deutsch-Jozsa Algorithm Example
from pyquil import Program, get_qc
from pyquil.gates import H, CNOT, MEASURE
p = Program()
ro = p.declare('ro','BIT',1)
p += H(0)
p += H(1)
p += CNOT(0,1)
p += H(0)
p += MEASURE(0, ro[0])
qc = get_qc('2q-qvm')
result = qc.run(p)
print('Deutsch-Jozsa result:', result)
Implementing Deutsch-Jozsa algorithm for 2 qubits.
Forest SDK Variational Circuit Example
from pyquil import Program, get_qc
from pyquil.gates import RX, RY, CNOT, MEASURE
p = Program()
ro = p.declare('ro','BIT',2)
p += RX(0.5,0)
p += RY(1.2,1)
p += CNOT(0,1)
p += MEASURE(0,ro[0])
p += MEASURE(1,ro[1])
qc = get_qc('2q-qvm')
result = qc.run(p)
print('Variational circuit result:', result)
Minimal variational circuit using RX and RY rotations.
Forest SDK Random Circuit Example
from pyquil import Program, get_qc
from pyquil.gates import H, X, CNOT, MEASURE
import random
p = Program()
ro = p.declare('ro','BIT',2)
for _ in range(3):
p += random.choice([H(0), X(0), CNOT(0,1)]) p += random.choice([H(1), X(1), CNOT(1,0)])
p += MEASURE(0,ro[0])
p += MEASURE(1,ro[1])
qc = get_qc('2q-qvm')
result = qc.run(p)
print('Random circuit result:', result)
Randomly apply H, X, and CNOT gates to a 2-qubit system.
Frequently Asked Questions about Forest-sdk
What is Forest-sdk?
Forest SDK is Rigetti’s quantum software development kit that enables writing, simulating, compiling, and executing quantum programs using the Quil instruction language.
What are the primary use cases for Forest-sdk?
Constructing quantum programs using Quil via Python (pyQuil). Simulating quantum circuits using the QVM (Quantum Virtual Machine). Compiling Quil programs for different architectures with quilc. Running quantum programs on Rigetti QPUs through QCS. Developing hybrid algorithms (quantum + classical) for optimization, chemistry, or machine learning
What are the strengths of Forest-sdk?
Flexible hybrid quantum‑classical programming model. Strong compiler for Quil with optimization. Simulation capabilities with QVM before running on real hardware. Scalability via cloud access to real quantum processors. Open‑source components (pyQuil, quilc, etc.) with active documentation :contentReference[oaicite:1]{index=1}
What are the limitations of Forest-sdk?
Requires registration and access to QCS for hardware runs. Classical simulation (QVM) becomes expensive for many qubits. Hardware noise and limited qubit connectivity on current QPUs. Learning curve for Quil language and pyQuil API. Less ecosystem maturity compared to some more popular SDKs
How can I practice Forest-sdk typing speed?
CodeSpeedTest offers 10+ real Forest-sdk code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.