Skip to content

[Circuit renderer] Support user-provided input sizes for circuit rendering - #3658

Open
Dima Fedoriaka (fedimser) wants to merge 8 commits into
mainfrom
fedimser/array-sizes
Open

[Circuit renderer] Support user-provided input sizes for circuit rendering#3658
Dima Fedoriaka (fedimser) wants to merge 8 commits into
mainfrom
fedimser/array-sizes

Conversation

@fedimser

@fedimser Dima Fedoriaka (fedimser) commented Aug 26, 2026

Copy link
Copy Markdown
Contributor
  • Now operation can have annotation like @CircuitRenderingOptions(inputSizes=[...]), which means that if a circuit is rendered for this operation, Qubit[]-typed inputs will have size as specified in inputSizes array.
  • Semantics of the inputSizes:
    • Values from inputSizes are assigned to each dimension of params in declaration order. For example, if args are Qubit[][], Qubit[], Qubit[][] and input_sizes=[1,2,3,4,5], then for rendering we will alocate arrays Qubit[1][2], Qubit[3], Qubit[4][5].
    • If less values provided then necessary, remaining dimensions default to 2.
    • If more values provided than necessary,extra values are ignored.
  • Added validation thatthe total number of qubits to render does not exceed 1000. It's the same limit we have in TypeScript renderer, but this check in Rust allows to prevent crashing circuit generator due to allocating to many qubits.
  • Resolves Allow array sizes other than 2 in circuit rendering #2069
  • This also affects syntax for "hide box" feature, now it's @CircuitRenderingOptions(hideBox=true).

Example

@CircuitRenderingOptions(inputSizes=[3,4])
operation PairwiseCNOT(controls: Qubit[], targets: Qubit[]) : Unit {
    for c in controls {
        for t in targets {
            CNOT(c, t);
        }
    }
}
image

@fedimser Dima Fedoriaka (fedimser) changed the title [DRAFT] [Circuit renderer] Input sizes. [Circuit renderer] Input sizes. Aug 27, 2026
@fedimser
Dima Fedoriaka (fedimser) marked this pull request as ready for review August 27, 2026 17:33
@github-actions

Copy link
Copy Markdown

Change in memory usage detected by benchmark.

Memory Report for 6e24b73

Test This Branch On Main Difference
compile core + standard lib 26692272 bytes 26691819 bytes 453 bytes

@fedimser Dima Fedoriaka (fedimser) changed the title [Circuit renderer] Input sizes. [Circuit renderer] Support user-provided input sizes for circuit rendering Aug 27, 2026

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

This PR extends the Q# compiler’s circuit rendering pipeline to support user-specified qubit-array input lengths via a new inputSizes option on @CircuitRenderingOptions(...), flowing from frontend attribute parsing through HIR/FIR lowering and into circuit entry-expression generation.

Changes:

  • Added input_sizes: Option<Vec<u32>> to CircuitRenderingOptions in both HIR and FIR, and plumbed it through HIR→FIR lowering.
  • Implemented parsing of inputSizes=... in frontend lowering (;-separated list, positive integers).
  • Updated circuit entry-expression generation to allocate/slice qubits based on the configured sizes, with new unit tests covering single/multiple/missing/extra sizes.

Reviewed changes

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

Show a summary per file
File Description
source/compiler/qsc_lowerer/src/lib.rs Propagates input_sizes when lowering CircuitRenderingOptions from HIR to FIR.
source/compiler/qsc_hir/src/hir.rs Adds input_sizes to HIR options and documents inputSizes semantics.
source/compiler/qsc_frontend/src/lower.rs Parses inputSizes from @CircuitRenderingOptions(...) strings into HIR.
source/compiler/qsc_frontend/src/lower/tests.rs Adds a frontend lowering test asserting inputSizes is parsed into HIR attrs.
source/compiler/qsc_fir/src/fir.rs Adds input_sizes to FIR CircuitRenderingOptions.
source/compiler/qsc_circuit/src/operations.rs Applies inputSizes to qubit-array parameters and uses sizes in entry-expression generation.
source/compiler/qsc_circuit/src/operations/tests.rs Adds circuit entry-expression tests for size application behavior.
source/compiler/qsc_circuit/src/builder/tests.rs Updates a QubitParam test literal to include the new size field.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread source/compiler/qsc_frontend/src/lower.rs Outdated
Comment thread source/compiler/qsc_circuit/src/operations.rs
@swernli

Copy link
Copy Markdown
Contributor

I would prefer array syntax for the list of sizes rather than semicolon separated integers. So "inputSizes=3;4" becomes inputSizes=[3,4]. It also removes the need for quotes.

@fedimser

Copy link
Copy Markdown
Contributor Author

I would prefer array syntax for the list of sizes rather than semicolon separated integers. So "inputSizes=3;4" becomes inputSizes=[3,4]. It also removes the need for quotes.

But the comma is already reserved for key-value pair separator! So you can do this:
@CircuitRenderingOptions("hideBox=true,inputSizes=3;4")

Sure, we can support syntax @CircuitRenderingOptions("hideBox=true,inputSizes=[3,4]") but I would need to write a parser that would understand that comma within brackets is not key-value pair separator.

@swernli

Copy link
Copy Markdown
Contributor

I would prefer array syntax for the list of sizes rather than semicolon separated integers. So "inputSizes=3;4" becomes inputSizes=[3,4]. It also removes the need for quotes.

But the comma is already reserved for key-value pair separator! So you can do this:

@CircuitRenderingOptions("hideBox=true,inputSizes=3;4")

Sure, we can support syntax @CircuitRenderingOptions("hideBox=true,inputSizes=[3,4]") but I would need to write a parser that would understand that comma within brackets is not key-value pair separator.

I think maybe there is something deeper going on here, because you keep using quotes and mentioned a needing a parser. If you skip the quotes, you can make use of the existing Q# parser that should already handle all of this for you. Let me try to put together an example to show what I mean.

@github-actions

Copy link
Copy Markdown

Change in memory usage detected by benchmark.

Memory Report for 54e86c5

Test This Branch On Main Difference
compile core + standard lib 26692272 bytes 26691819 bytes 453 bytes

@github-actions

Copy link
Copy Markdown

Change in memory usage detected by benchmark.

Memory Report for 62272f9

Test This Branch On Main Difference
compile core + standard lib 26692272 bytes 26691819 bytes 453 bytes

@github-actions

Copy link
Copy Markdown

Change in memory usage detected by benchmark.

Memory Report for b607e1a

Test This Branch On Main Difference
compile core + standard lib 26692272 bytes 26691819 bytes 453 bytes

@github-actions

Copy link
Copy Markdown

Change in memory usage detected by benchmark.

Memory Report for b0b67af

Test This Branch On Main Difference
compile core + standard lib 26692272 bytes 26691819 bytes 453 bytes

@github-actions

Copy link
Copy Markdown

Change in memory usage detected by benchmark.

Memory Report for bed3110

Test This Branch On Main Difference
compile core + standard lib 26692272 bytes 26691819 bytes 453 bytes

@github-actions

Copy link
Copy Markdown

Change in memory usage detected by benchmark.

Memory Report for d56c1ee

Test This Branch On Main Difference
compile core + standard lib 26692272 bytes 26691819 bytes 453 bytes

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 10 out of 10 changed files in this pull request and generated 1 comment.

Comment on lines +1459 to 1460
The argument is a string containing comma-separated `key=value` pairs. Keys are case-insensitive.

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.

Allow array sizes other than 2 in circuit rendering

3 participants