Repository navigation
Make the simulator, matrices and vectors keep k and stop mutating (#84, #82, #87) - #97
Merged
Merged
Conversation
This was referenced Oct 17, 2026
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
#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
force-pushed
the
fix/84-82-87-simulator
branch
from
October 18, 2026 21:42
28e73a5 to
cf2957c
Compare
ondrik
approved these changes
Oct 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
cli/simulator.py(cli/simulator.py crashes or prints wrong values #84):simulate_matrixlooked gates up asMatrixmethods, which exist only for some gates. 15 of the 26 gates,idandmeasurecrashed.-m/--measaccepted any integer and silently gave an all-zero post-measurement state.-v matrix -c FiveTupleprinted the matrix without its 1/√2^k scale.Matrixconstructor setk = 0, sotranspose,dagger,+and-lostk.density_from_vectordropped it too (|+⟩⟨+| became all ones).conjugate()changed it in place.Vector.copydroppedname,boundandn.Complex / ComplexreturnedNone;FiveTuple / intraised.Vector.__eq__comparedksyntactically, so the same state with anotherkwas unequal.parse_filecalls, andmeasured_qubitshad one copy of each qubit per input vector. After refactor: replace Logger with logging module and fix timings (#86, #87 F41) #94 is merged, closes Reported timings and simulator stats are wrong #87.Fix:
simulate_matrixbuilds 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 keepsk, and itsreprshows it.choices,Simulator, andVector.measure.Matrix:k;density_from_vectorscales a FiveTuple result by2k;+and-raise for FiveTuple operands with differentk;conjugate()return a new matrix, and a FiveTuple matrix takes only integer scalars.Vector.copycopies the object and its elements; FiveTuple vectors compare by value (rescale_with).Complex / Complexis implemented, andFiveTuple / intworks.Simulator.reset_stats()runs on every parse.measured_qubitsholds each qubit once, in the order of its first measurement, and nothing withno_measurement.Tests: 95 new, in
test_matrix.py,test_simulator.pyandtest_small_fixes.py:simulate_matrixchecked against Qiskit for every gate, operand order and both representations;kand rejects other outcomes;khandling;Vector.copy, value equality, division.The full suite passes locally on this branch (579 tests).