Skip to content

Make the simulator, matrices and vectors keep k and stop mutating (#84, #82, #87) - #97

Merged
ondrik merged 1 commit into
masterfrom
fix/84-82-87-simulator
Oct 18, 2026
Merged

ondrik merged 1 commit into
masterfrom
fix/84-82-87-simulator

Conversation

@jkhav

@jkhav jkhav commented Oct 17, 2026 •

Copy link
Copy Markdown
Member

Closes #84 (F30, F71, F77) and #82 (F26, F59–F63). Part of #87 (F65; F41 is in #94).

Second of a stack of four. Stacked on #96, so review only the top commit.

Bugs:

Fix:

  • simulate_matrix builds the matrix from the simulated images of the basis states, so every gate works, and it rejects circuits with measurements (they have no unitary). The matrix keeps k, and its repr shows it.
  • The measurement outcome can be only 0 or 1: argparse choices, Simulator, and Vector.measure.
  • Matrix:
    • the constructor keeps the given k;
    • density_from_vector scales a FiveTuple result by 2k;
    • + and - raise for FiveTuple operands with different k;
    • scalar operations and conjugate() return a new matrix, and a FiveTuple matrix takes only integer scalars.
  • Vector.copy copies the object and its elements; FiveTuple vectors compare by value (rescale_with). Complex / Complex is implemented, and FiveTuple / int works.
  • Simulator.reset_stats() runs on every parse. measured_qubits holds each qubit once, in the order of its first measurement, and nothing with no_measurement.

Tests: 95 new, in test_matrix.py, test_simulator.py and test_small_fixes.py:

  • simulate_matrix checked against Qiskit for every gate, operand order and both representations;
  • the CLI prints k and rejects other outcomes;
  • matrix scalar operations and k handling;
  • repeated simulations, and unique measured qubits;
  • Vector.copy, value equality, division.

The full suite passes locally on this branch (579 tests).

@codecov

codecov Bot commented Oct 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.03922% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/complex/fivetuple.py 0.0% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

Base automatically changed from fix/83-80-gateset-jamiolkowski to master October 18, 2026 21:35
#84, #82, #87)

cli/simulator.py and Simulator:
- simulate_matrix looked gates up as Matrix methods, which exist only
  for some gates, so 15 of the 26 gates, id and measure crashed. It
  now builds the matrix from the simulated images of the basis states,
  so every gate works, and it rejects circuits with measurements (they
  have no unitary).
- the printed matrix shows its k (a FiveTuple matrix without it is off
  by 1/sqrt(2)^k).
- the measurement outcome (-m / meas) can be only 0 or 1; any other
  value gave an all-zero state.
- the stats were accumulated over repeated parse_file calls. They are
  reset on every parse, and measured_qubits holds each qubit once, in
  the order of its first measurement (it had one copy per input
  vector), and nothing with no_measurement.

Matrix:
- scalar multiplication and conjugate() return a new matrix instead of
  changing the operand; a FiveTuple matrix takes only integer scalars;
- the constructor keeps the given k, and density_from_vector scales a
  FiveTuple result by 2k;
- adding or subtracting FiveTuple matrices with different k raises;
- / works (it went through 1/other).

Vector.copy keeps name, bound and n, and FiveTuple vectors compare by
value, so the same state with a different k is equal. Complex / Complex
is implemented, and FiveTuple / int works.
@ondrik
ondrik force-pushed the fix/84-82-87-simulator branch from 28e73a5 to cf2957c Compare October 18, 2026 21:42
@ondrik
ondrik merged commit 2cda9ac into master Oct 18, 2026
4 checks passed
@ondrik
ondrik deleted the fix/84-82-87-simulator branch October 18, 2026 21:46
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.

Reported timings and simulator stats are wrong cli/simulator.py crashes or prints wrong values

2 participants