Skip to content

Infer qubit number from STIM program#162

Merged
david-pl merged 5 commits into
mainfrom
david/stim-no-qubit
Jul 14, 2026
Merged

Infer qubit number from STIM program#162
david-pl merged 5 commits into
mainfrom
david/stim-no-qubit

Conversation

@david-pl

Copy link
Copy Markdown
Collaborator

One more nice convenience thing when running STIM programs.

  • Passing in n_qubits in the python wrapper (sample_stim and GeneralizedTableau.sample) is now optional and inferred from the program if omitted.
  • The rust side and GeneralizedTableau.do still require it (even though the inference is exposed, so you could construct the stim program first and infer from that), because of the compile-time values that depend on the qubit number.

FYI, @rafaelha: is this okay or should we somehow have GeneralizedTableau.do also omit the qubit number? It's a little trickier, but I guess we could just add a hack.

Copilot AI review requested due to automatic review settings June 25, 2026 13:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds a qubit-count inference path for running validated Stim programs from the Python API, so callers can omit n_qubits in common cases while keeping the Rust backend’s explicit sizing requirements.

Changes:

  • Added StimProgram.num_qubits (Python-visible) backed by a new ExtendedProgram::num_qubits() AST helper in stim-parser.
  • Made n_qubits optional in ppvm.sample_stim and GeneralizedTableau.sample, inferring it from prog.num_qubits when omitted.
  • Added Python and Rust tests covering qubit-count inference and edge cases (annotations ignored, repeats handled).

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
ppvm-python/test/generalized_tableau/test_stim.py Adds tests for StimProgram.num_qubits and for omitting n_qubits in sampling APIs.
ppvm-python/src/ppvm/generalized_tableau.py Makes n_qubits optional for sampling and infers from prog.num_qubits.
ppvm-python/src/ppvm/_core.pyi Exposes the new StimProgram.num_qubits property in type stubs.
crates/stim-parser/src/ast/extended.rs Implements ExtendedProgram::num_qubits() via a recursive max-qubit walk, with unit tests.
crates/ppvm-python-native/src/stim_program.rs Exposes num_qubits via a PyO3 #[getter] on StimProgram.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread ppvm-python/src/ppvm/generalized_tableau.py
Comment thread crates/ppvm-python-native/src/stim_program.rs
@github-actions

github-actions Bot commented Jun 25, 2026

Copy link
Copy Markdown
PR Preview Action v1.8.1
Preview removed because the pull request was closed.
2026-07-14 09:38 UTC

Copilot AI review requested due to automatic review settings June 25, 2026 13:52

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Comment thread ppvm-python/src/ppvm/generalized_tableau.py
Comment thread crates/stim-parser/src/ast/extended.rs
@david-pl david-pl force-pushed the david/stim-no-qubit branch from 67f289e to 8e4fa52 Compare June 25, 2026 14:18
Copilot AI review requested due to automatic review settings June 26, 2026 08:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Comment thread crates/stim-parser/src/ast/extended.rs
Comment thread crates/ppvm-python-native/src/stim_program.rs
Comment thread crates/stim-parser/src/ast/extended.rs
Copilot AI review requested due to automatic review settings July 14, 2026 09:32
@david-pl david-pl enabled auto-merge (squash) July 14, 2026 09:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Comment on lines +49 to +51
/// Number of qubits the program operates on: one past the highest qubit
/// index any instruction references (`0` if it touches no qubits). Lets
/// callers size a tableau when no explicit qubit count is given.
Comment on lines +338 to +341
When ``n_qubits`` is ``None`` (the default) the qubit count is inferred
from the program via ``prog.num_qubits`` (one past the highest qubit
index it references), falling back to 1 for a program that touches no
qubits. Pass an explicit ``n_qubits`` to size the tableau larger.
Comment on lines +232 to +238
// Empty program, and an annotation-only program, both touch no qubits.
let empty = ExtendedProgram {
instructions: vec![],
line_map: Arc::new(LineMap::new("")),
};
assert_eq!(empty.num_qubits(), 0);
}
@david-pl david-pl merged commit 8d50344 into main Jul 14, 2026
14 checks passed
@david-pl david-pl deleted the david/stim-no-qubit branch July 14, 2026 09:38
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.

3 participants