Skip to content

feat(core): quantum memory layout for quantum data types#134

Closed
Adithyaphani wants to merge 1 commit into
hhat-lang:dev/rust_impl/pre0.3beta/arch_scaffoldfrom
Adithyaphani:feat/quantum-memory-layout-113
Closed

feat(core): quantum memory layout for quantum data types#134
Adithyaphani wants to merge 1 commit into
hhat-lang:dev/rust_impl/pre0.3beta/arch_scaffoldfrom
Adithyaphani:feat/quantum-memory-layout-113

Conversation

@Adithyaphani

@Adithyaphani Adithyaphani commented Jun 10, 2026

Copy link
Copy Markdown

Closes #113

Adds layout/quantum.rs to hhat_core — the quantum-side counterpart to the classical
TypeLayout / LayoutCache machinery. Classical layouts count bytes and insert
alignment 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, sums
    their qubit counts linearly, tracks per-member qubit_offset for Q3L register naming.
  • QuantumEnumLayout — always 1 qubit; asserts exactly 2 variants at layout time
    (first = |0⟩, second = |1⟩).
  • QuantumTypeLayout — unified Primitive | Struct | Enum with a single .qubits().
  • QuantumInstruction — minimal stub carrying ancilla_qubits: u32.
  • QuantumLayoutCacheHashMap<Ty, QuantumTypeLayout> so each type is computed once
    across 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 Q3L
    backend needs.

Files changed:

rust/hhat_lang/hhat_core/src/layout/quantum.rs   (new)
rust/hhat_lang/hhat_core/src/layout/mod.rs       (+1 line: pub mod quantum;)

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 QuantumProgram with instructions and cast.


Generative AI/LLM disclosure

Code and Logic Architecture/Design:

  • The code and logic architecture/design contain no generative AI/LLM
  • The code and logic architecture/design are partially performed by generative AI/LLM
    • 30% performed by the author(s)
  • The code and logic architecture/design are fully performed by generative AI/LLM

Code content:

  • The code contains no generative AI/LLM work
  • The code is partially written by generative AI/LLM
    • 25% performed by the author(s)
  • The code is fully written by generative AI/LLM

Code review:

  • The code review contains no generative AI/LLM
  • The code review is partially done by generative AI/LLM
  • The code review is fully done by generative AI/LLM

Code tests:

  • The code tests contain no generative AI/LLM
  • The code tests are partially written by generative AI/LLM
    • 30% performed by the author(s)
  • The code tests are fully written by generative AI/LLM

@Adithyaphani

Copy link
Copy Markdown
Author

@Doomsk looking forward to your response on this PR and happy to make further changes if needed.

@Doomsk

Doomsk commented Jun 10, 2026

Copy link
Copy Markdown
Member

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.
No LLM response.

@Adithyaphani
Adithyaphani changed the base branch from main to dev/rust_impl/pre0.3beta/arch_scaffold June 10, 2026 09:16
@Adithyaphani

Copy link
Copy Markdown
Author

@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.

@Adithyaphani
Adithyaphani force-pushed the feat/quantum-memory-layout-113 branch from 9ea1fcb to fdc7b99 Compare June 10, 2026 09:26

@Doomsk Doomsk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +109 to +113
///
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct QuantumEnumLayout {
pub qubits: u32,
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain this without using LLM?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Comment on lines +185 to +186

impl QuantumLayoutCache {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the method names adopted in the classical version when handling same purpose methods.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Doomsk Renamed get_or_insert to layout_of and compute_and_store to insert_layout to follow the same naming as the classical LayoutCache.

Comment on lines +248 to +258
///
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,
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain this struct without using LLM?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Comment on lines +280 to +283
///
pub fn cast_attribute(&mut self) {
self.classical_register_bits += 1;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when and why is it used for? Explain without using LLM.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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 @@
//!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quantum struct types can contain classical types as their members (fields).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

@Adithyaphani

Copy link
Copy Markdown
Author

@Doomsk thanks for your swift response. I think most of unnecessary files are added due to cargo command . I will make necessary changes.

@Adithyaphani
Adithyaphani force-pushed the feat/quantum-memory-layout-113 branch from fdc7b99 to 40bf97b Compare June 10, 2026 20:16
@Adithyaphani

Copy link
Copy Markdown
Author

@Doomsk made the following changes.

Branch fix:
Reset to upstream base, only our commit is there now.

PartialEq/Eq:
Removed — tests only check .qubits (u32) directly, struct comparison wasn't needed.

Method names:
Renamed get_or_insert → layout_of, compute_and_store → insert_layout to match LayoutCache.
Classical members in quantum struct:
Code already handles it — only quantum fields are counted for qubits, classical fields stay under LayoutCache. Updated the doc comment to say this clearly.

QuantumProgram:
Holds the three numbers the backend needs before allocating anything for a quantum variable — qubits from the type, ancilla qubits gates need, and classical bits for measurements.

cast_attribute:
Called once per quantum field that gets measured. Each measurement needs one classical bit. The counter tells the backend how wide the classical register should be.

Looking forward to your response and happy to make further changes if needed.

@Doomsk

Doomsk commented Jun 10, 2026

Copy link
Copy Markdown
Member

I am closing this PR. According to the AI policies of this repo, no fully automated PRs are allowed.

@Doomsk Doomsk closed this Jun 10, 2026
@github-project-automation github-project-automation Bot moved this from Backlog to Done in H-hat lang kanban Jun 10, 2026
@Adithyaphani
Adithyaphani deleted the feat/quantum-memory-layout-113 branch June 10, 2026 20:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[IMPL] Add quantum programs memory layout for quantum data

2 participants