Examples¶
Every example below ships in the repository and runs out of the box. Rust
examples run with cargo run -p <crate> --example <name>; Python examples
run with python <script> after building the bindings.
Start here¶
Bell state / GHZ (Rust)¶
use simq::QuantumCircuit;
fn main() {
let mut qc = QuantumCircuit::new(3);
qc.h(0).cnot(0, 1).cnot(1, 2);
let result = qc.simulate_with_shots(1024).unwrap();
println!("{:?}", result.measurements.unwrap().sorted());
// [("000", ~512), ("111", ~512)]
}
Fluent API tour¶
cargo run -p simq --example fluent_api
Covers the whole builder surface: gates, inspection, ASCII rendering, error handling, exact probabilities.
Variational algorithms¶
Minimal VQE with gradient descent¶
cargo run -p simq --example vqe_fluent
Optimizes the one-parameter ansatz RY(θ)|0⟩ against H = Z and converges to energy −1 at θ = π. The whole loop:
use simq::{PauliObservable, PauliString, QuantumCircuit};
fn energy(theta: f64, hamiltonian: &PauliObservable) -> f64 {
let mut qc = QuantumCircuit::new(1);
qc.ry(theta, 0);
qc.expectation_value(hamiltonian).expect("simulation failed")
}
fn main() {
let hamiltonian = PauliObservable::from_pauli_string(
PauliString::from_str("Z").unwrap(), 1.0);
let (mut theta, lr, eps) = (0.5, 0.4, 1e-6);
for _ in 0..40 {
let grad = (energy(theta + eps, &hamiltonian)
- energy(theta - eps, &hamiltonian)) / (2.0 * eps);
theta -= lr * grad;
}
println!("theta = {theta:.6}, energy = {:.8}", energy(theta, &hamiltonian));
}
Full-scale VQE and QAOA¶
Example |
Command |
|---|---|
H₂ molecule ground state |
|
MaxCut with QAOA |
|
Comprehensive QAOA |
|
Optimizer comparison (L-BFGS vs Nelder–Mead vs GD) |
|
Convergence monitoring |
|
Gradient method fallback |
|
States, measurement, and observables¶
Example |
Command |
|---|---|
Quantum teleportation |
|
Pauli observables |
|
Computational-basis measurement |
|
Sparse states |
|
Adaptive sparse↔dense conversion |
|
Copy-on-write state branching |
|
Efficient sampling |
|
Circuit tooling¶
Example |
Command |
|---|---|
Const-generic circuit builder |
|
ASCII circuit rendering |
|
Bloch sphere |
|
Circuit debugger |
|
Validation |
|
Serialization |
|
Parameter tracking |
|
Gates and compiler¶
Example |
Command |
|---|---|
Custom gates tutorial |
|
Gate matrices |
|
Compile-time lookup tables |
|
Optimization pipeline |
|
Gate fusion |
|
Diagonal gate optimization |
|
(See the compiler guide for the full list of compiler demos.)
Python examples¶
Located in simq-py/examples/:
Script |
What it shows |
|---|---|
|
End-to-end tour of the Python API |
|
Circuit construction and simulation |
|
Parameterized gates and sweeps |
|
Noise channels and hardware models |
|
Complete VQE optimization loop |
cd simq-py
maturin develop --release
python examples/vqe_example.py