Skip to content

Fix high-impact compiler, backend, and algorithm regressions - #14

Open
wuzp15 wants to merge 59 commits into
SpinQTech:mainfrom
wuzp15:codex/fix-native-optimizer-semantics
Open

Fix high-impact compiler, backend, and algorithm regressions#14
wuzp15 wants to merge 59 commits into
SpinQTech:mainfrom
wuzp15:codex/fix-native-optimizer-semantics

Conversation

@wuzp15

@wuzp15 wuzp15 commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR fixes independently reproduced correctness and robustness failures across compilation, circuit differentiation, variational algorithms, simulators, QASM, and cloud assembly. Each defect is isolated in its own commit and guarded by a focused regression test.

Compilation and IR

  • preserve IR connectivity, conditions, and qubit operand order during rewrites
  • disable optimizer transformations that are not semantics-preserving across their advertised input domain
  • correctly lower controlled CY, nested controlled CX, arbitrary controlled matrix gates, and nested controlled single-qubit gates
  • preserve relative/global phase when an existing decomposition is placed under another control
  • decompose symbolic controlled rotations before parameter shift, instead of applying a two-eigenvalue rule to a three-eigenvalue controlled generator
  • broadcast OpenQASM registers elementwise, reject size mismatches, serialize live optimized definitions, and preserve numeric/symbolic expressions
  • avoid redefining bundled QELIB gates and correct bundled crx

Autograd and measurement

  • defer matrix metadata evaluation for trainable controlled gates
  • differentiate parameters through composite gate definitions with the correct chain rule
  • provide one VJP per circuit argument and make qgrad return one gradient per argument
  • fix Torch dense-Hamiltonian conversion and transformed-parameter shift coefficients
  • preserve batched Torch Pauli expectations instead of returning only batch zero
  • align Pauli-string characters with circuit qubit order for asymmetric multi-qubit terms
  • isolate appended Pauli measurements from definition subgraphs and classical-register leaves
  • remove a NumPy scalar-conversion path that would break Torch probabilities on newer NumPy

Variational algorithms

  • allow documented list Hamiltonians with the default adjoint method by converting only the QLayer measurement copy
  • initialize QAOA qubit count for custom problem ansatzes and validate its width
  • initialize VQE depth when explicit deterministic parameters or a parameter shape are supplied
  • scale QAOA evolution by Hamiltonian coefficients and support identity-only and commuting mixed-Pauli terms
  • reject noncommuting terms in the generated default QAOA problem ansatz, whose sequential product would otherwise be order-dependent; callers can provide an explicit custom problem ansatz
  • write optimizer endpoints back to parameters

Simulators and cloud backend

  • fix controlled-Y phases, complex Torch state-vector validation, exact count allocation, and weighted random readings
  • safely execute numeric optimized callees in native and Torch backends
  • preserve semantic gate operand order during repeated cloud DAG substitutions (including separated CCX decompositions)
  • reject unsupported cloud conditions and preserve configured host/task flags and task reconstruction

Impact

Before these fixes, valid programs could silently produce incorrect states, phases, expectation values, gradients, QASM, cloud circuits, or optimizer outputs. Several failures were silent scientific errors: asymmetric Pauli terms acted on the wrong qubit, controlled-rotation parameter shift returned the wrong derivative, nested controls introduced observable relative phase, batched expectation values lost rows, and repeated cloud substitutions could swap a CCX target with a control.

Unsupported noncommuting default-QAOA synthesis now fails closed instead of returning an order-dependent approximation without an approximation contract.

Regression tests

The files under tests/ are permanent unit/regression tests, not disposable reproduction artifacts. Each is a minimized executable reproduction and should merge with its corresponding fix.

Build and test the installed package outside the source tree so the local spinqit/ directory does not shadow the compiled native extension:

python -m pip install --no-build-isolation .
repo_root=$PWD
(cd /tmp && python -m unittest discover -v -s "$repo_root/tests" -p 'test_*.py')

Validation

  • fresh source package built and installed with Python 3.9
  • installed-package suite: 70/70 tests passed
  • compiler process sweep: 15 fixed/numeric single-qubit bases, two and three nested controls, Basic optimization levels 0-3, Torch, QASM round-trip, and SQC-25 cloud assembly; no failures
  • Basic/QASM/cloud process fidelity: 1.000000000000 throughout; worst Torch fidelity: 0.999998971820
  • autograd oracle sweep: Torch multi-argument dense/list, SpinQ adjoint/parameter-shift dense/list, controlled phase at levels 0-3, batched VJPs, and nested controlled rotations; worst observed error below 1e-7
  • variational sweep: 36 VQE/QAOA list/sparse, adjoint/parameter-shift, custom-ansatz, optimizer-endpoint, Torch-backprop, and explicit-parameter cases passed
  • earlier optimizer differential sweep: 3,000/3,000 state comparisons passed; minimum fidelity 0.9999999999999989
  • random 4x4 MatrixGate sweep: 400/400 passed across optimization levels 0-3; minimum fidelity 0.9999999999999993
  • python -m pip check and git diff --check passed

@wuzp15
wuzp15 marked this pull request as ready for review July 22, 2026 09:08
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.

1 participant