Add reinterpret for PauliOperators and Tableaux/Stabilizers/etc - #740
Add reinterpret for PauliOperators and Tableaux/Stabilizers/etc#740arnavk23 wants to merge 11 commits into
Conversation
Benchmark Results (Julia v1)Time benchmarks
Memory benchmarks
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #740 +/- ##
=======================================
Coverage 74.12% 74.12%
=======================================
Files 111 113 +2
Lines 7791 7838 +47
=======================================
+ Hits 5775 5810 +35
- Misses 2016 2028 +12 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
I reviewed this after reading through the comments on the earlier version in #620. The main open issues I see are below, with concrete examples.
using QuantumClifford
t = zero(Tableau, 3, 64)
# Existing invariant from QuantumClifford:
@assert size(t.xzs, 1) == QuantumClifford._nchunks(nqubits(t), eltype(t.xzs))
# PR 740 expects this instead, so normal tableaus fail:
@assert size(t.xzs, 1) != 2 * QuantumClifford._nchunks(nqubits(t), eltype(t.xzs))
reinterpret(UInt8, t) # should work, but currently throwsThis also breaks
t = zero(Tableau, 3, 64)
try
reinterpret(UInt8, t)
@test true
catch e
# This makes a broken valid reinterpret pass the test.
reinterpret_error_matches(e, "Unable to reinterpret tableau storage")
endThese should instead assert success directly: t2 = reinterpret(UInt8, t)
t3 = reinterpret(eltype(t.xzs), t2)
@test t == t3Only invalid alignment/size cases should use
using LinearAlgebra
using QuantumClifford
t = fastcolumn(zero(Tableau, 3, 7))
@assert t.xzs isa Adjoint
@show size(t.xzs) # logical tableau storage axes
@show size(parent(t.xzs)) # parent axes are reversed
reinterpret(UInt8, t) # should preserve fast-column behavior, but line 13 reads parent axes incorrectlyThis is one of the cases explicitly requested in the #620 discussion: the tests should prove both
t = zero(Tableau, 2, 64)
t2 = reinterpret(UInt8, t)
t2.xzs[1, 1] = 0xff
# For reinterpret-like behavior, mutating t2 should affect t's backing storage.
# The copy at src/reinterpret.jl:37 prevents that.
@test !all(iszero, t.xzs)One of the main points from #620 was that this should be allocation-free and should reuse the same backing memory where possible.
pf = PauliFrame(3, 4, 2)
new_frame = reinterpret(UInt8, pf.frame)
# PR code effectively does this:
typeof(pf.frame)(tab(new_frame))
# But typeof(pf.frame) still encodes the old tableau/xzs storage type.
# The result should use new_frame directly, not force the old frame type.The test currently hides this by accepting conversion errors: reinterpret_error_matches(e, (
"Unable to reinterpret pauliframe storage",
"Unable to reinterpret tableau storage",
"Cannot `convert`",
))I was not able to run local Julia probes to completion because the temporary checkout environment was not instantiated and -- Reviewed with OpenAI Codex CLI, GPT-5-based coding agent. |
|
@arnavk23 , it seems this PR is very different from the #620 that HA and I had reviewed some time ago. If that is indeed the case and we are not misremembering, please close this PR and consider reopening the older one or resubmitting the older one from a newly named branch. The branch of the old one seems to be deleted. |
|
@arnavk23 , is it possible to explicitly bring back the previous version that was already reviewed. Otherwise we have to review it again, which is a lot of work and I probably will struggle to find the motivation to repeat work that was already done. |
I have done just that, Stefan but wanted to go through all the comments once more on this code, specially the llm reply to see if the reviewed version has any faults before I asked you or HA for review. |
|
I meant doing that in some machine-verifiable form, e.g. seeing that this has the same hash or same branch or reviving the previous PR. Otherwise we still need to put in a lot of work to do a full review because there is no proof that this is actually exactly the same code as the one we had review already. |
That can be seen in pr #696 , the pr is on a different issue but it contains all the information of the reinterpret branch before commits focused on the issue. Can be seen at 652d50a |
|
@Krastanov, just as a curiosity. Is there any reason that From my understanding, the underlying implementation details should be transparent to any project or user the employs the library. In so far as they are concerned, we could be storing states on actual quantum hardware and they would be none the wiser. |
|
@Hamiltonian-Action , I agree with your logic, this is not a super important feature. It is a well defined feature that is not unreasonable to exist, but it would not see wide use -- as such, if it gets implemented and reviewed, great, we can merge it, but probably it would not be a high priority to review for me. |
|
@Krastanov this is safe to review and merge. All the files (except |
Supersedes #620 (closed due to the git merge issues)
Please address only one topic or issue per pull request! Many small PRs are much easier to review and merge than one large PR.
If this is your first submission to this organization and you are not a developer known in the Julia ecosystem, do not use LLMs -- we need to trust you first before we trust the LLM under your control.
If you want to submit an unfinished piece of work in order to get comments and discuss, please mark the pull request as a draft and ping the repository maintainer.
Before merging, all changes and new functionality should be marked in the CHANGELOG file, but feel free to just leave your CHANGELOG notes in the PR description, to avoid merge conflicts with other requests modifying that file. The maintainer will add these CHANGELOG notes for you if you do so.
Before considering your pull request ready for review and merging, make sure that all of the following are completed (please keep the clecklist as part of your PR):
If you are submitting for a bug bounty:
If possible, keep your git history not too wild (rebase and squash commits, keep commits small and semantically separated) so that review is easier.