Skip to content

feat(maxcut): build the QAOA circuit from textbook gates for visualization - #36

Merged
Trosper3 merged 1 commit into
mainfrom
feature/maxcut-circuit-visualization
Nov 17, 2026
Merged

Trosper3 merged 1 commit into
mainfrom
feature/maxcut-circuit-visualization

Conversation

@wrigjl

@wrigjl wrigjl commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

Summary

Supports the Max Cut QAOA circuit visualization in Redux (ReduxISU/Redux PR to follow, which depends on this one).

  • Hand-built ansatz: qaoa_circuit() builds QAOA from h, cx, rz and rx: a Hadamard layer, then per layer a cx·rz·cx for each edge, with the rz angle γₖ·w/s scaled by the edge's weight, and an rx(2βₖ) on each node. This replaces qaoa_ansatz plus transpiling to rz/sx/cx, which produced long QASM that hid the circuit's structure.
  • Rounds: edge_rounds() splits the edges into rounds that share no node (a greedy edge colouring). Each round's gates touch disjoint qubits, so they sit side by side when drawn.
  • Response: now includes "gammas" and "betas", the optimized angles, one per layer. The README is updated.

Redux's D3 circuit renderer can draw this QASM directly. Its parameters are plain numbers, and every gate is one it knows. Redux_GUI's Q.js converter drops all parameterized gates, which is why the Redux side uses D3.

Same circuit, same results

  • Same state: the hand-built circuit prepares the same state as qaoa_ansatz up to a global phase (within 3×10⁻¹⁵ over random weighted graphs and angles), with the parameters named and ordered identically (β[k], then γ[k]). A new test checks this, so the warm start and the optimizer behave as before.
  • Old vs new on 8 graphs (5–16 nodes): identical answers on 7. On one 16-node graph the new code found a better cut, 111 vs 109. Floating-point differences that small can steer COBYLA differently on large graphs.
  • Faster: no transpile step and fewer gates.
Nodes Old New QASM lines old → new
5 0.08 s 0.04 s 110 → 60
12 0.36 s 0.14 s 280 → 160
16 3.29 s 2.34 s 546 → 386

Testing

  • New tests: qaoa_circuit matches qaoa_ansatz on three graphs; edge_rounds gives matchings that cover every edge exactly once; the response carries one γ and one β per layer, and the QASM uses only h, cx, rz, rx, barrier and measure.
  • 89 tests pass on 3.13 and 3.12, with 99% coverage of maxcut_quantum.py. ruff and black are clean.
  • End to end with Redux's new visualization: the default 5-node instance draws in 21 columns, with the four layer overlays.

🤖 Generated with Claude Code

…ation

Build the QAOA ansatz directly from h, cx, rz and rx instead of
qaoa_ansatz + transpile: a Hadamard layer, then per layer a cx-rz-cx
for each edge (the rz angle scaled by the edge's weight) and an rx on
each node. The returned qasm now reads as the circuit that ran, about
a third shorter than the transpiled rz/sx/cx output, with numeric
parameters that the Redux D3 circuit renderer can label.

Edges are emitted in rounds that share no node (a greedy edge
colouring), so gates on disjoint qubits sit side by side when drawn.
The circuit prepares the same state as qaoa_ansatz up to a global
phase, with parameters named and ordered the same way, which a new
test checks.

Also return the optimized angles as "gammas" and "betas".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

Redux Build System — CI Report

uv · commit 26ddd6d

Operation Status Summary Time
audit ✅ no known vulnerabilities 8.8s
format-check ✅ all files formatted 0.5s
lint ✅ 0 issues 0.0s
typecheck ⏭️ not implemented —
unit-test · 3.12 ✅ 89 passed · coverage 88% 83.9s
unit-test · 3.13 ✅ 89 passed · coverage 88% 67.1s
build ✅ built local/quantumsolver:ci · 582MB 17.1s
integration-test ✅ /health ready · 7 passed 13.0s
push ⏭️ only on push to main —

Overall: ✅ 7 passed · 0 failed · 2 skipped

@Trosper3 Trosper3 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks Jason. The hand-built circuit matching qaoa_ansatz (same state, same parameter order) is a nice check, and it's faster too. Grouping edges that share no node keeps each layer's edge gates together, then the mixer gates, which is what the Redux picture's layer boxes (ReduxISU/Redux#1026) depend on. I merged #34, #35 and this together locally: no conflicts, and the Max Cut tests pass on 3.13 and 3.14. Merge this before Redux#1026.

@Trosper3
Trosper3 merged commit cd1756d into main Nov 17, 2026
4 checks passed
@Trosper3
Trosper3 deleted the feature/maxcut-circuit-visualization branch November 17, 2026 23:01
Trosper3 pushed a commit to ReduxISU/Redux that referenced this pull request Nov 17, 2026
MaxCutQAOAVisualization draws the QAOA circuit the quantum service
optimized for the graph with the D3 circuit renderer: one wire per
node, named after it, the Hadamard layer, a U_C(γk) overlay over each
cost layer (a CNOT-RZ-CNOT per edge) and a U_B(βk) overlay over each
mixer layer (an RX per qubit), then measurement. Angles are rounded for
display and the sampler's barrier is left out. The solved view adds the
cut, the measured bits, the cut weight, the angles and the seed. A
failure of the quantum service is a 503 solver_unavailable, as for the
solver (#625).

QasmD3Scheduler now reads parameters in the invariant culture (a
decimal-comma culture misread "0.5") and understands the multiples of
pi Qiskit writes for exact angles ("pi/2", "-3*pi/4").

Requires ReduxISU/quantumsolver#36 for the circuit's textbook gates and
its gammas and betas.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants