feat(core): quantum memory layout for quantum data types#134
Conversation
|
@Doomsk looking forward to your response on this PR and happy to make further changes if needed. |
|
Hi @Adithyaphani thank you for this PR. Please make sure to properly fill in the checks at the AI disclaimer before I proceed with the review, and use the arch_scaffold branch instead of the main branch as the target branch to merge this PR. |
|
@Doomsk I fixed the target branch and corrected the AI Disclosure part .Looking forward to your response and review. Happy to make further changes if needed. |
9ea1fcb to
fdc7b99
Compare
Doomsk
left a comment
There was a problem hiding this comment.
Thank you @Adithyaphani . Please fix this PR branch. There are too many unnecessary changes coming from other branches/commits that are unrelated with the issue.
| /// | ||
| #[derive(Clone, Debug, PartialEq, Eq)] | ||
| pub struct QuantumEnumLayout { | ||
| pub qubits: u32, | ||
| } |
There was a problem hiding this comment.
Can you explain this without using LLM?
There was a problem hiding this comment.
@Doomsk I added those thinking they'd be needed for equality checks in tests, but the tests only compare the .qubits field directly which is just a u32 — not the struct itself. So they were unnecessary and I've removed them in the updated version.
|
|
||
| impl QuantumLayoutCache { |
There was a problem hiding this comment.
Please use the method names adopted in the classical version when handling same purpose methods.
There was a problem hiding this comment.
@Doomsk Renamed get_or_insert to layout_of and compute_and_store to insert_layout to follow the same naming as the classical LayoutCache.
| /// | ||
| pub struct QuantumProgram { | ||
| /// Qubit layout for the variable's declared type. | ||
| pub root_layout: QuantumTypeLayout, | ||
| /// Qubit count coming purely from the type (no ancilla). | ||
| pub data_qubits: u32, | ||
| /// Ancilla qubits accumulated from instructions so far. | ||
| pub ancilla_qubits: u32, | ||
| /// Width of the classical output register (one bit per cast attribute). | ||
| pub classical_register_bits: u32, | ||
| } |
There was a problem hiding this comment.
Can you explain this struct without using LLM?
There was a problem hiding this comment.
@Doomsk The struct collects everything the backend needs to allocate for one quantum variable before it can run — how many qubits its type needs, how many extra ancilla qubits the gates on it will require at runtime, and how many classical bits to reserve for measurements. I separated data and ancilla qubits so the backend can tell apart type-required from gate-required allocations.
| /// | ||
| pub fn cast_attribute(&mut self) { | ||
| self.classical_register_bits += 1; | ||
| } |
There was a problem hiding this comment.
when and why is it used for? Explain without using LLM.
There was a problem hiding this comment.
@Doomsk In H-hat when a quantum attribute gets measured and the result stored classically, that's a cast. Each cast produces one classical bit. cast_attribute() is called once per cast during compilation so by the end the backend knows exactly how wide to make the classical register.
| @@ -0,0 +1,544 @@ | |||
| //! | |||
There was a problem hiding this comment.
Quantum struct types can contain classical types as their members (fields).
There was a problem hiding this comment.
@Doomsk good point — the code already handles : QuantumStructLayout only counts quantum fields since classical fields do not need qubits. Classical fields in a quantum struct are still covered by the classical LayoutCache. Updated the doc comment to make this explicit.
|
@Doomsk thanks for your swift response. I think most of unnecessary files are added due to cargo command . I will make necessary changes. |
fdc7b99 to
40bf97b
Compare
|
@Doomsk made the following changes. Branch fix: PartialEq/Eq: Method names: QuantumProgram: cast_attribute: Looking forward to your response and happy to make further changes if needed. |
|
I am closing this PR. According to the AI policies of this repo, no fully automated PRs are allowed. |
Closes #113
Adds
layout/quantum.rstohhat_core— the quantum-side counterpart to the classicalTypeLayout/LayoutCachemachinery. Classical layouts count bytes and insertalignment padding; quantum layouts count qubits with no padding at all (wasting qubits
is not an option).
What's added:
QuantumPrimitiveLayout— qubit counts:@bool→1,@u2→2,@u3→3,@u4→4,@u8→8.Panics early on classical types rather than silently returning zero.
QuantumStructLayout/QuantumMemberLayout— walks quantum members only, sumstheir qubit counts linearly, tracks per-member
qubit_offsetfor Q3L register naming.QuantumEnumLayout— always 1 qubit; asserts exactly 2 variants at layout time(first = |0⟩, second = |1⟩).
QuantumTypeLayout— unifiedPrimitive | Struct | Enumwith a single.qubits().QuantumInstruction— minimal stub carryingancilla_qubits: u32.QuantumLayoutCache—HashMap<Ty, QuantumTypeLayout>so each type is computed onceacross all quantum variables in a program. Struct entries pre-populate their members
recursively before building their own entry.
QuantumProgram— root layout + accumulated ancilla qubits from instructions +classical register bits from cast attributes.
.total_qubits()is what the Q3Lbackend needs.
Files changed:
Classical layout path is untouched. Array quantum layout is stubbed as
unimplemented!()— not specified in the issue.Tests (20 total): all six categories from the issue — all five primitive types,
simple struct, enum, nested structs (two- and three-level), classical/quantum layout
round-trip, and a full
QuantumProgramwith instructions and cast.Generative AI/LLM disclosure
Code and Logic Architecture/Design:
Code content:
Code review:
Code tests: