Docs Playground
dmvjs / ket GitHub @kirkelliott / ket npm

ket

Quantum computing in JavaScript · npm i @kirkelliott/ket · zero dependencies · 1,308 tests

QAOA Max-Cut — Quantum Optimization

A 4-cycle graph has two optimal bipartitions: {0,2} vs {1,3} and {1,3} vs {0,2}, each cutting all 4 edges. QAOA p=1 finds them — the two dominant peaks are the correct answers.

0123measurement outcomes (all 16 states)26%010126%1010{0, 2}{1, 3}
const edges = [[0,1],[1,2],[2,3],[3,0]] const circuit = qaoa(4, edges, [Math.PI / 4], [0.15 * Math.PI]) circuit.exactProbs() // → { '0101': 26.5%, '1010': 26.5%, … }
Expected cut: 2.95 edges Random baseline: 2.0 edges Optimal: 4.0 edges Top states: '1010' 26.5% · '0101' 26.5%
Circuit Drawing API — toSVG() · blochSphere(q)

Circuit SVG toSVG()

Self-contained SVG string — no external fonts or stylesheets. Same column layout as draw(). Embed directly in HTML, save to a file, or use in notebooks.

q0: q1: H
new Circuit(2).h(0).cnot(0, 1).toSVG() // → self-contained SVG string
Format: SVG string Background: self-contained Also: toLatex()

Bloch Sphere blochSphere(q)

300×300 SVG for a single qubit's state using cavalier projection. Partial-traces over all other qubits — works on any circuit, including entangled states.

|0⟩ |1⟩ |+⟩ |i⟩
|0⟩
ground state
|0⟩ |1⟩ |+⟩ |i⟩
|+⟩
H gate
|0⟩ |1⟩ |+⟩ |i⟩
|1⟩
X gate
|0⟩ |1⟩ |+⟩ |i⟩
|−⟩
H · Z
|0⟩ |1⟩ |+⟩ |i⟩
|+i⟩
H · S
|0⟩ |1⟩ |+⟩ |i⟩
|T⟩
H · T
Grover's Search — Hardware Fidelity

The same 3-qubit Grover circuit (target: |101⟩) run across six backends with 4,096 shots each. Lower gate error rates preserve the quadratic amplitude advantage. All results use published device noise models — not real hardware runs.

Circuit toSVG()

q0: q1: q2: H H H X H H X H H H X X X H H X X X H H H X H H X H H H X X X H H X X X H H H
Ideal
Statevector · no noise
Ideal (all 8 states)95%101
94.53%
P(|101⟩) — target state
BEST
IonQ Forte-1
Trapped-Ion · 36 qubits
IonQ Forte-1 (all 8 states)94%101
93.90%
P(|101⟩) — target state
IonQ Aria-1
Trapped-Ion · 25 qubits
IonQ Aria-1 (all 8 states)93%101
93.04%
P(|101⟩) — target state
IonQ Harmony
Trapped-Ion · 11 qubits
IonQ Harmony (all 8 states)90%101
90.06%
P(|101⟩) — target state
IBM Torino
Superconducting · 133 qubits
IBM Torino (all 8 states)92%101
91.72%
P(|101⟩) — target state
IBM Sherbrooke
Superconducting · 127 qubits
IBM Sherbrooke (all 8 states)91%101
90.72%
P(|101⟩) — target state
1,024-Qubit GHZ State CliffordSim

Stabilizer Tableau Heatmap Gottesman-Knill

A 1,024-qubit GHZ state built with one H gate + 1,023 CNOTs. Classically infeasible with statevector simulation (21024 amplitudes), but the Gottesman-Knill theorem lets us track it exactly in O(n²) space. Each row is a stabilizer generator; each column is a qubit.

Row 0 (bright stripe): X⊗¹⁰²⁴ — all 1,024 qubits correlated via X.  ·  Rows 1–1023 (diagonal): ZZ at adjacent qubit positions.

Qubits: 1,024 Gates: 1,024 (1H + 1,023 CNOT) Build time: 6ms Stabilizers: 1,024 generators

Scalability

O(n²) per gate — statevector needs 2n complex numbers, impossible beyond ~50 qubits.

QubitsTimeStatevector RAM
64<1ms >16 PB
128<1ms > universe
256<1ms > universe
5121ms > universe
1,0245ms > universe
2,04823ms > universe
4,096106ms > universe

Measurement Verification

Every qubit collapses to the same value — perfect quantum correlation across all 1,024 qubits.

|ψ⟩ = (1/√2)(|000…0⟩ + |111…1⟩) ← 1,024 qubits → Qubit 0 measured: 1 Qubit 511 measured: 1 (same) Qubit 1023 measured: 1 (same)
1,024
qubits — all correlated
6ms
to build & run
Matrix Product State Trajectories runMps()

The trajectory-MPS backend simulates noisy circuits at 127+ qubits by representing the quantum state as a product of rank-3 tensors. Bond dimension χ tracks entanglement, not qubit count — a 127-qubit GHZ state maintains χ = 2 throughout, making it as cheap as a 3-qubit simulation. Noisy trajectories are embarrassingly parallel: pass workers: N for N× speedup.

GHZ Fidelity vs Qubit Count noise model

P(|00…0⟩ + |11…1⟩) for an n-qubit GHZ state under depolarizing noise. Bond dimension stays at χ = 2 for all n — fidelity decays from gate errors only, not simulation approximation.

0%25%50%75%100%0255075100127IonQ Forte-1IBM SherbrookeGHZ qubits (n)
IBM Sherbrooke: p₂ = 7.4 × 10⁻³ IonQ Forte-1: p₂ ≈ 10⁻³ Model: depolarizing channel · gate errors only

127-Qubit IBM Sherbrooke χ = 2

Statevector simulation of a 127-qubit circuit would require 2127 complex amplitudes — roughly 1020× more RAM than all data ever created. MPS stores O(n · χ²) numbers — linear in qubit count.

import { Circuit, DEVICES } from '@kirkelliott/ket' let c = new Circuit(127).h(0) for (let i = 0; i < 126; i++) c = c.cnot(i, i+1) const result = c.runMps({ shots: 256, seed: 42, noise: DEVICES['ibm_sherbrooke'].noise, }) // → { '000…0': 2.3%, '111…1': 2.1%, … }
127
qubits · noisy · χ = 2
O(n)
memory — not O(2ⁿ)

Scalability — GHZ with Noise

256 shots, IBM Sherbrooke noise model, single-threaded. Time scales linearly with n because χ = 2 throughout the GHZ circuit.

QubitsTimeχStatevector RAM
104ms 216 KB
207ms 216 MB
5017ms 2>16 PB
10033ms 2> universe
12741ms 2> universe

Multi-Core Speedup

Each noisy trajectory is an independent simulation — distribute across OS threads with workers: N. Requires built bundle and Node.js ≥ 22.

circuit.runMps({ shots: 1024, noise: DEVICES['forte-1'].noise, workers: 4, // ~4× wall-time speedup seed: 42, })
speedup · 4 workers
O(n·χ²)
memory — linear in n
Entanglement Fingerprint — bondEntropies()

Von Neumann entropy S(b) at each bond cut — the entanglement structure of a quantum state at a glance. Different circuits leave distinct fingerprints: GHZ saturates every bond, Bell pairs show sharp on/off alternation, brickwork builds bulk entanglement peaked in the middle.

GHZ(8)
Maximum entanglement — every bond saturated at S = 1
GHZ(8) — maximally entangled0.00.51.0bond
ghz(8).bondEntropies() // → [1.00, 1.00, 1.00, 1.00, 1.00, 1.00, 1.00]
Bell Pairs ⊗4
Strictly local entanglement — alternating S = 1, 0
Bell pairs — alternating entanglement0.00.51.0bond
// 4 independent Bell pairs // → [1.00, 0.00, 1.00, 0.00, 1.00, 0.00, 1.00]
Brickwork Circuit
Bulk entanglement — CFT-like bell-curve profile
Brickwork — bulk entanglement0.00.51.0bond
// Ry init + 2 CNOT layers // → [0.13, 0.32, 0.26, 0.40, 0.19, 0.12, 0.02]
11ms
GHZ(1024).bondEntropies()
O(n·χ²)
time + memory
// 1024 qubits, runs in 11ms makeGhz(1024).bondEntropies() // → [1, 1, 1, …, 1] // 1023 bonds // Compare circuit families at a glance circuit.bondEntropies() // O(n·χ²) // → entropy profile → physics insight
VQE at Scale — 12-Qubit Heisenberg XXX minimizeMps()

Variational quantum eigensolver on the 12-qubit antiferromagnetic Heisenberg chain (H = Σ XiXi+1 + YiYi+1 + ZiZi+1) using MPS-based parameter-shift gradients. A 12-qubit statevector needs 128 KB — manageable — but 50+ qubits needs petabytes. MPS VQE scales to hundreds of qubits at low entanglement.

Energy Convergence MPS gradient

Parameter-shift gradients computed via MPS — same accuracy as statevector but O(n·χ²) memory. Each step: 2 × 36 MPS evaluations.

6.35-2.58-11.51-20.44020406080gradient descent step
Qubits: 12 Params: 36 Steps: 80 Initial E: 6.354 Final E: -18.4590 Wall time: 811ms

Ground State area law

Bond entropies of the optimized ground state — the area-law profile confirms the Heisenberg chain has limited entanglement, making MPS the ideal representation.

Heisenberg ground state — area law0.00.51.0bond
import { minimizeMps, realAmplitudes } from '@kirkelliott/ket' const ansatz = realAmplitudes(12, 2) const result = minimizeMps(ansatz, heisenbergH, params, { maxBond: 8, // MPS bond dimension }) // → { energy: -18.4590, converged: … }
12
qubits · open chain
-18.459
VQE ground energy
0.8s
total · JavaScript
O(n·χ²)
scales to 100+ qubits
Variational Quantum Eigensolver — Transverse Ising

4-qubit open-chain Ising Hamiltonian: H = −Σ ZᵢZᵢ₊₁ − 0.5·Σ Xᵢ. RealAmplitudes ansatz (12 params), parameter-shift gradient descent.

Energy Convergence converged

Each step computes 2×12 = 24 exact circuit evaluations via the parameter-shift rule.

-3.13-3.22-3.31-3.41020406080gradient descent step
Steps: 80 Initial E: -3.1291 Final E: -3.386299 Δ: 8.2%

Ground State Found

Transverse Ising model at J = g — the quantum critical point. VQE locates the ground state without diagonalizing the full Hamiltonian.

H = −Z₀Z₁ − Z₁Z₂ − Z₂Z₃ − 0.5(X₀ + X₁ + X₂ + X₃) Ansatz: RealAmplitudes(n=4, reps=2) Params: 12 · Steps: 80 · LR: 0.12 E₀ (VQE): -3.38629900 E₀ (exact): -3.42703409 Error: 4.07e-2
-3.38630
VQE · exact: -3.42703
12
variational parameters
ket — quantum circuits in JavaScript
Built by @dmvjs
dmvjs/ket on GitHub → npm install @kirkelliott/ket