-
Notifications
You must be signed in to change notification settings - Fork 184
Just executor #5028
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
beckykd
wants to merge
356
commits into
main
Choose a base branch
from
bd-just-executor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Just executor #5028
Changes from 250 commits
Commits
Show all changes
356 commits
Select commit
Hold shift + click to select a range
838aad5
changes from review
beckykd 05794d2
There is no precedence section!
beckykd 7cceeda
Merge branch 'main' into bd-new-toc
beckykd e8f53a7
links should have the full path
beckykd d11b950
Merge branch 'bd-new-toc' of https://github.com/Qiskit/documentation …
beckykd 5a9020e
Move stuff around
beckykd 72957ee
Apply suggestions from code review
beckykd 1a0fce3
merge conflicts
beckykd 2a32a94
Merge branch 'main' of https://github.com/Qiskit/documentation into b…
beckykd 745a26c
links
beckykd d07207e
squeaky clean
beckykd 51dcbfc
Marry pubs and broadcasting topics
beckykd 23d6da9
Update _toc.json
beckykd daae80d
Clean up the options overview
beckykd dfe2005
fix twisties
beckykd 3339461
Merge branch 'main' of https://github.com/Qiskit/documentation into b…
beckykd ff86adb
Merge branch 'main' of https://github.com/Qiskit/documentation into b…
beckykd 2963cf5
untangle toc
beckykd 085d209
AC comments
beckykd b282918
title mismatch
beckykd 460a27c
Capitalization
beckykd 2a3d781
Merge branch 'main' of https://github.com/Qiskit/documentation into b…
beckykd 4b68a9c
links
beckykd feb7505
Something fun
beckykd e565670
typo
beckykd b915ddb
Add PEA example
beckykd dd5f006
links
beckykd 5a0d8c3
yarghhh
beckykd 41e909f
remove unused images
beckykd 6675bd4
Merge branch 'main' of https://github.com/Qiskit/documentation into b…
beckykd 9bdfbe3
Merge branch 'main' of https://github.com/Qiskit/documentation into b…
beckykd ca41e55
Pull in all updates, make copies of files previously modified
beckykd 7a97913
put modified filkes in a different folder
beckykd aaa4c78
updates from previous work
beckykd 88064be
Use full link path
beckykd ed28c49
Move periods outside links
beckykd d6893e8
Fix TOC
beckykd f3e8b90
fix links
beckykd 64529b3
links
beckykd 7942159
remove file
beckykd eb393e9
broken link
beckykd 8e49e8f
link
beckykd 8ceec65
replace pubs-broadcasting with primitives-input-output
beckykd 1578621
linx
beckykd 05786cb
edits
beckykd bd9f3bb
link
beckykd db6e971
fix the bot
beckykd 800bbf2
fix
beckykd 35bc9ae
Merge branch 'main' into bd-new-toc
beckykd 87a1a9b
edits
beckykd 14f6a34
Merge branch 'bd-new-toc' of https://github.com/Qiskit/documentation …
beckykd b532dd0
Merge branch 'main' into bd-new-toc
beckykd e4e74fe
no executor in qiskit
beckykd e5e656e
clean up input output topics
beckykd 4dc0136
squeaky clean!
beckykd 932f805
fix notebook tester
beckykd 50a4f24
Apply suggestions from code review
beckykd 46f0be9
change to notebook for testing
beckykd bd23a6d
Merge branch 'bd-new-toc' of https://github.com/Qiskit/documentation …
beckykd 2acb0ec
review comments
beckykd 0ff98e1
review comments
beckykd 031a6aa
finish updates from reviews
beckykd 2e61549
Merge branch 'main' of https://github.com/Qiskit/documentation into b…
beckykd 5144be3
execution modes
beckykd 1f04c79
unused
beckykd 8469233
title mismatches
beckykd dfc664d
Fix code
beckykd ff65393
spelling
beckykd c7c974e
Merge branch 'main' into bd-new-toc
abbycross 7dce5d9
./fix
abbycross 4fae164
Merge branch 'main' of https://github.com/Qiskit/documentation into b…
beckykd 0eed48f
tox e fix
beckykd ccab48d
Merge branch 'bd-new-toc' of https://github.com/Qiskit/documentation …
beckykd 448ff1a
fix notebook code LIKE A BOSS
beckykd 81fdff2
merge
beckykd 83668e1
Squeaky clean!
beckykd df3152a
update executor options info
beckykd b82ab34
links
beckykd b864abb
update notebook so it runs
beckykd 5519fdf
Squeaky clean - again?
beckykd 212ce10
last readthrough
beckykd b06d448
typo
abbycross 7c0a419
squeaky clean again
beckykd 6da51a1
Merge branch 'bd-new-toc' of https://github.com/Qiskit/documentation …
beckykd c638a97
Make sure sampler-examples notebook runs on a test device.
beckykd b8bde93
move more to test system
beckykd 0426af7
more notebooks can be simulated
beckykd 8e239a3
Apply suggestions from code review
beckykd d8aaaa9
Merge branch 'main' of https://github.com/Qiskit/documentation into b…
beckykd 450119f
broadcasting topic needs pseudocode
beckykd e32c7aa
take out i/o
beckykd bccf5a1
maybe cards
beckykd cb28e20
move backend out of Runtime
beckykd ccc2994
no links in cards
beckykd 7da5730
quick shortening
beckykd c469abe
change cards
beckykd 207aad2
Merge branch 'main' into bd-new-toc
beckykd 94048bd
cards
beckykd 43afce3
Merge branch 'bd-new-toc' of https://github.com/Qiskit/documentation …
beckykd 3e76e18
comments
beckykd 166c242
comments
beckykd ce28ed3
links
beckykd a169b0f
tox e fix
beckykd 8c784f7
execution-spans
beckykd eb59897
execution spans
beckykd b2b879e
typo
beckykd d0c052a
tox e fix
beckykd a8088dd
edits
beckykd e502ccd
fix notebook
beckykd 2622697
fix execution span example
beckykd 711f878
squeaky
beckykd c4d90db
edits
beckykd 8f41abf
Update docs/guides/get-started-with-estimator.ipynb
beckykd 00e8738
Update docs/guides/qiskit-runtime-primitives.mdx
beckykd c6b4684
Just one more little change
beckykd 0b9fd4e
Merge branch 'bd-new-toc' of https://github.com/Qiskit/documentation …
beckykd 925a7d9
change examples to use batch
beckykd 5a26684
links
beckykd 1c4335a
title mismatch
beckykd 360f84a
I'm losing my mind
beckykd f1e1759
spelling
beckykd e72f790
spell
beckykd 289dd5b
squeaky clean
beckykd 91b6830
code fix
beckykd 0e5e85d
squeaky
beckykd b8de0d4
edits
beckykd 97666e8
only one table
beckykd 6a36568
edits
beckykd 95c23a7
squeaky
beckykd 029778c
add one more link
beckykd c1b9bec
Add example
beckykd 7d261b0
fix titles
beckykd a429c98
Fix metadata
beckykd ea6c3a8
run again
beckykd df582d3
rerun notebook
beckykd ad52710
delete unused imates
beckykd e4449af
Update get-started-with-executor.ipynb
beckykd 5f3c95e
update example
beckykd 4b6c5cc
don't need to import numpy
beckykd f5f1e45
squeaky clean
beckykd 1022f5c
Merge branch 'main' into bd-new-toc
abbycross 1674a2b
Merge branch 'main' of https://github.com/Qiskit/documentation into b…
beckykd 8194a9e
tox -e fix
abbycross 79991f9
Merge branch 'bd-new-toc' of https://github.com/Qiskit/documentation …
abbycross 4824906
Merge branch 'main' of https://github.com/Qiskit/documentation into b…
beckykd b9a614b
Merge branch 'main' of https://github.com/Qiskit/documentation into b…
beckykd 94c5ca1
Apply suggestions from code review
beckykd bb93560
Apply suggestions from code review
beckykd b9c9bda
Review changes
beckykd 30cc721
Merge branch 'bd-new-toc' of https://github.com/Qiskit/documentation …
beckykd 2be0be6
Apply suggestions from code review
beckykd 188777b
Apply suggestions from code review
beckykd f35e115
Revamp get started
beckykd 43c9011
Merge branch 'bd-new-toc' of https://github.com/Qiskit/documentation …
beckykd 1008b86
Apply suggestions from code review
beckykd 3bb9ea8
Comments from Sam
beckykd cedf9fb
Merge branch 'bd-new-toc' of https://github.com/Qiskit/documentation …
beckykd dbf74dd
squeaky
beckykd 9c03fbe
circuit transpilation settings submits jobs
beckykd 1ced9dd
Update docs/guides/executor-input-output.mdx
beckykd 7799ec0
Update docs/guides/executor-input-output.mdx
beckykd 31846be
Merge branch 'main' of https://github.com/Qiskit/documentation into b…
beckykd 9c06842
get rid of executor
beckykd 310d1ca
fix links
beckykd f2a6d91
links
beckykd 83ceafe
update all the files
beckykd c6a5578
fix link
beckykd 4399f2d
remove executor images
beckykd 741efbb
ignore title mismatches
beckykd a277513
update setup files
beckykd 176b2b3
Merge branch 'main' of https://github.com/Qiskit/documentation into b…
beckykd 4a9b51b
Merge branch 'main' of https://github.com/Qiskit/documentation into b…
beckykd 237d70d
Update qiskit_bot.yaml
beckykd f7f6290
Merge branch 'bd-new-toc' of https://github.com/Qiskit/documentation …
beckykd 9814670
remove unwanted file
beckykd 033096b
duplicate entry
beckykd fca12f3
Merge branch 'bd-new-toc' of https://github.com/Qiskit/documentation …
beckykd 2ff6a6d
add owner
beckykd 5dc436e
Apply suggestions from code review
beckykd dd2eb41
remove duplicated code
beckykd 62baf4d
run notebook
beckykd ce3fe14
Merge branch 'main' of https://github.com/Qiskit/documentation into b…
beckykd 65adba5
unused
beckykd ce0ca45
fix bot
beckykd 95d84aa
update notebook testing
beckykd 1c99ec3
Apply suggestions from code review
beckykd df2a478
executor is new
beckykd 118fd5a
Merge branch 'bd-just-executor' of https://github.com/Qiskit/document…
beckykd 3bdb988
change to notebook and run
beckykd e7179bb
Merge branch 'main' of https://github.com/Qiskit/documentation into b…
beckykd 963bd6e
tox e fix
beckykd e8a3b03
clarify more
beckykd 1c4b578
get the Get started topic running
beckykd 2d7e95e
link formatting
beckykd 6648396
Merge branch 'main' into bd-just-executor
beckykd 21b91c6
add "template circuit"
beckykd 17c2577
Merge branch 'main' into bd-just-executor
beckykd 213f77f
review comments
beckykd f894a08
Merge branch 'bd-just-executor' of https://github.com/Qiskit/document…
beckykd c86aa68
spelling
beckykd 77d98ea
Fix spelling?
beckykd dae066a
fix qiskit bot
beckykd 6a561a6
clean
beckykd a86d61c
Samplex inputs
beckykd 651d82e
rerun to fix
beckykd c46e8b8
clean
beckykd 99f7db7
./fix
abbycross 0b20914
review comments
beckykd 3cfe8fe
Add install instruction
beckykd aae106f
spelling
beckykd 056d876
run tox
beckykd 671c919
Merge branch 'main' into bd-just-executor
abbycross f892298
./fix
abbycross 9f1b46d
combine the two remove-cell blocks
abbycross 7ec7593
address 5061
beckykd ba5f2c6
rerun notebook
beckykd 7554fcb
tox e fix
beckykd 2ea4ed3
Update docs/guides/error-mitigation-and-suppression-techniques.ipynb
beckykd cfeb205
Update docs/guides/get-started-with-executor.ipynb
beckykd 8aa161d
Update docs/guides/executor-input-output.ipynb
beckykd f054c50
Update docs/guides/executor-input-output.ipynb
beckykd d8ec628
Update docs/guides/executor-input-output.ipynb
beckykd 5de0775
review comments
beckykd d652260
Merge branch 'bd-just-executor' of https://github.com/Qiskit/document…
beckykd a49336c
two parameter values
beckykd fc555fb
show samplomatic version
beckykd c67ddbf
import samplomatic as part of the code
beckykd 4a59660
make purty
beckykd c767891
add samplomatic install where needed
beckykd e6cbc58
don't explicitly import samplomatic
beckykd 24b89aa
Update docs/guides/get-started-with-executor.ipynb
beckykd 97bb635
Update docs/guides/executor-options.ipynb
beckykd 15ab18e
Update docs/guides/executor-input-output.ipynb
beckykd d1e2ec3
Merge branch 'main' into bd-just-executor
abbycross 23e1e76
tox e fix
beckykd f7de8fd
Merge branch 'main' of https://github.com/Qiskit/documentation into b…
beckykd 0bc7b48
merge conflicts
beckykd 5f7d8c4
fix example
beckykd fe44dfc
rerun examples file
beckykd 2c4f929
unused images
beckykd b0d667a
jessie udpates
beckykd 54bcaf3
tox
beckykd 16bb882
tox
beckykd 3b66f25
Merge branch 'main' into bd-just-executor
abbycross 6008c71
./fix
abbycross 4d341d3
Update docs/guides/executor-broadcasting.mdx
beckykd 55aeed3
final countdown
beckykd d72cd4c
Merge branch 'bd-just-executor' of https://github.com/Qiskit/document…
beckykd c2aaa5a
Merge branch 'main' into bd-just-executor
abbycross 11741e8
Update save-jobs.ipynb
abbycross File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,337 @@ | ||
| --- | ||
| title: Executor broadcasting | ||
| description: How the Executor handles array inputs and outputs using broadcasting semantics | ||
|
|
||
| --- | ||
| {/* cspell:ignore broadcastable */} | ||
|
|
||
| # Executor broadcasting | ||
|
|
||
| The data provided to the Executor primitive can be arranged in a variety of shapes to provide flexibility in a workload through broadcasting. This guide explains how the Executor handles array inputs and outputs using broadcasting semantics. Understanding these concepts will help you efficiently sweep over parameter values, combine | ||
| multiple configurations, and interpret the shape of returned data. | ||
|
|
||
| <Admonition type="note"> | ||
| The examples in this topic cannot be run on their own. They assume you have defined appropriate circuits, used the Samplomatic pass manager to add boxes and annotations, and used the Samplomatic `build` method to get a template circuit and samplex for each code block, as necessary. | ||
| </Admonition> | ||
|
|
||
| <span id="quickstart"></span> | ||
| ## Quickstart example | ||
|
|
||
| This example demonstrates the core idea. It creates a parametric circuit and and five different parameter configurations. The executor runs all five configurations and returns data organized by configuration, with one result per classical register in each quantum program item. | ||
|
|
||
| The rest of this guide refers back to this example to explain how this works and how to build more complex sweeps, including Samplomatic-based randomization and inputs. | ||
|
|
||
| ```python | ||
| import numpy as np | ||
| from qiskit.circuit import Parameter, QuantumCircuit | ||
| from qiskit_ibm_runtime import QiskitRuntimeService, Executor | ||
| from qiskit_ibm_runtime.quantum_program import QuantumProgram | ||
| from qiskit.transpiler import generate_preset_pass_manager | ||
|
|
||
| # A circuit with 2 parameters | ||
| # This circuit is used throughout the rest of this guide. | ||
| circuit = QuantumCircuit(4) | ||
| circuit.rx(Parameter("a"), 0) | ||
| circuit.rx(Parameter("b"), 1) | ||
| circuit.h(2) | ||
| circuit.cx(2, 3) | ||
| circuit.measure_all() | ||
|
|
||
| # 5 different parameter configurations (shape: 5 configurations × 2 parameters) | ||
| parameter_values = np.linspace(0, np.pi, 10).reshape(5, 2) | ||
|
|
||
| # Initialize the service and choose a backend | ||
| service = QiskitRuntimeService() | ||
| backend = service.least_busy(operational=True, simulator=False) | ||
|
|
||
| # Transpile to ISA circuit | ||
| preset_pass_manager = generate_preset_pass_manager( | ||
| backend=backend, | ||
| optimization_level=3, | ||
| ) | ||
| isa_circuit = preset_pass_manager.run(circuit) | ||
|
|
||
| # This program is used throughout the rest of this guide. | ||
| program = QuantumProgram(shots=1024) | ||
| program.append_circuit_item(isa_circuit, circuit_arguments=parameter_values) | ||
|
|
||
| # initialize an Executor with default options | ||
| executor = Executor(mode=backend) | ||
|
|
||
| # Run and get results | ||
| result = executor.run(program).result() | ||
|
|
||
| # result is a list with one entry per program item | ||
| # result[0] is a dict mapping classical register names to data arrays | ||
| # Output bool arrays have shape (5, 1024, 4) | ||
| # 5 = number of parameter configurations | ||
| # 1024 = number of shots | ||
| # 4 = bits in the classical register | ||
| result[0]["meas"] | ||
| ``` | ||
|
|
||
| ## Intrinsic and extrinsic axes | ||
|
|
||
| Broadcasting only applies to _extrinsic_ axes. The intrinsic axes are always preserved as specified. | ||
|
|
||
| - **Intrinsic axes** (rightmost): Determined by the data type. For example, if your circuit | ||
| has three parameters, then parameter values require three numbers, giving an intrinsic shape | ||
| of `(3,)`. | ||
|
|
||
| - **Extrinsic axes** (leftmost): Your sweep dimensions. These define how many configurations you | ||
| want to run. | ||
|
|
||
| | Input type | Intrinsic shape | Example full shape | | ||
| |----------------------------------------|---------------|---------------------------------------------------| | ||
| | Parameter values (n parameters) | (n,) | (5, 3) for five configurations and three parameters | | ||
| | Scalar inputs (for example, noise scale) | () | (4,) for four configurations | | ||
| | Observables (if applicable) | varies | Depends on observable type | | ||
|
|
||
| ### Example | ||
|
|
||
| Consider a circuit with two parameters that you want to sweep over a 4x3 grid of configurations, varying parameter values and a noise scale factor: | ||
|
|
||
| ```python | ||
| import numpy as np | ||
|
|
||
| # Parameter values: 4 configurations along axis 0, intrinsic shape (2,) | ||
| # Full shape: (4, 1, 2) - the "1" allows broadcasting with noise_scale | ||
| parameter_values = np.array([ | ||
| [[0.1, 0.2]], | ||
| [[0.3, 0.4]], | ||
| [[0.5, 0.6]], | ||
| [[0.7, 0.8]], | ||
| ]) # shape (4, 1, 2) | ||
|
|
||
| # Noise scale: 3 configurations, intrinsic shape () (scalar) | ||
| # Full shape: (3,) | ||
| noise_scale = np.array([0.8, 1.0, 1.2]) # shape (3,) | ||
|
|
||
| # Extrinsic shapes: (4, 1) and (3,) → broadcast to (4, 3) | ||
| # Result: 12 total configurations in a 4×3 grid | ||
| program.append_samplex_item( | ||
| template_circuit, | ||
| samplex=samplex, | ||
| samplex_arguments={ | ||
| "parameter_values": parameter_values, | ||
| "noise_scales.mod_ref1": noise_scale, | ||
| }, | ||
| ) | ||
| ``` | ||
|
|
||
| The shapes are as follows: | ||
|
|
||
| | Input | Full shape | Extrinsic shape | Intrinsic shape | | ||
| |------------------|----------|---------------|---------------| | ||
| | `parameter_values` | (4, 1, 2) | (4, 1) | (2,) | | ||
| | `noise_scale` | (3,) | (3,) | () | | ||
| | Broadcast | None | (4, 3) | None | | ||
|
|
||
| ## Output array shapes | ||
|
|
||
| Output arrays follow the same extrinsic/intrinsic pattern: | ||
|
|
||
| - **Extrinsic shape:** Matches the broadcast shape of all inputs | ||
| - **Intrinsic shape:** Determined by the output type | ||
|
|
||
| The most common output is bitstring data from measurements, which is | ||
| formatted as an array of boolean values: | ||
|
|
||
| | Output type | Intrinsic shape | Description | | ||
| |-----------------------|----------------------|--------------------------------| | ||
| | Classical register data | (num_shots, creg_size) | Bitstring data from measurements | | ||
|
|
||
| ### Example | ||
|
|
||
| If you provide inputs with extrinsic shapes `(4, 1)` and `(3,)`, the broadcast extrinsic | ||
| shape is `(4, 3)`. The following code uses a circuit with 1024 shots and a 4-bit classical register (as defined in the previously defined [Quickstart](#quickstart) example): | ||
|
beckykd marked this conversation as resolved.
Outdated
|
||
|
|
||
| ```python | ||
| # Input extrinsic shapes: (4, 1) and (3,) → (4, 3) | ||
| # Output for classical register "meas": | ||
| # extrinsic: (4, 3) | ||
| # intrinsic: (1024, 4) - shots × bits | ||
| # full shape: (4, 3, 1024, 4) | ||
|
|
||
| result = executor.run(program).result() | ||
| meas_data = result[0]["meas"] # result[0] for first program item | ||
| print(meas_data.shape) # (4, 3, 1024, 4) | ||
|
|
||
| # Access a specific configuration | ||
| config_2_1 = meas_data[2, 1, :, :] # shape (1024, 4) | ||
| ``` | ||
| <Admonition type="note"> | ||
| Each configuration runs the full shot count specified in the quantum program. Shots are **not** divided among configurations. For example, if you request 1024 shots and have 10 configurations, each configuration runs 1024 shots (10,240 total shots executed). | ||
| </Admonition> | ||
|
|
||
| ## Randomization and the `shape` parameter | ||
|
|
||
| When using a [samplex](/docs/guides/directed-execution-model), each element of the extrinsic shape corresponds to an independent circuit | ||
| execution. The samplex typically injects randomness (for example, gate twirling) into each | ||
| execution, so even without explicitly requesting multiple randomizations, each element | ||
| receives a random realization. | ||
|
|
||
| You can use the `shape` parameter to augment the extrinsic shape for the item, effectively | ||
| adding axes that correspond specifically to randomizing the same configuration many times. It must be | ||
| broadcastable from the shape implicit in your `samplex_arguments`. Axes where `shape` exceeds | ||
| the implicit shape enumerate additional independent randomizations. | ||
|
|
||
| ### No explicit randomization axes | ||
|
|
||
| If you omit `shape` (or set it to match your input shapes), you get one execution per | ||
| input configuration. Each execution is still randomized by the samplex, but with only a | ||
| single random realization you don't benefit from averaging over multiple randomizations. | ||
|
|
||
| <Admonition type="note"> | ||
| If you're accustomed to enabling twirling with a simple flag like `twirling=True`, note that | ||
| the Executor requires you to explicitly request multiple randomizations with the `shape` argument to | ||
| allow your post-processing routines to get the benefits of averaging over multiple | ||
| randomizations. A single randomization (the default when `shape` is omitted) applies | ||
| random gates but typically offers no advantage over running the base circuit without | ||
| randomization. | ||
| </Admonition> | ||
|
|
||
| The following example demonstrates the default behavior: | ||
|
|
||
| ```python | ||
| program.append_samplex_item( | ||
| template_circuit, | ||
| samplex=samplex, | ||
| samplex_arguments={ | ||
| "parameter_values": np.random.rand(10, 2), # extrinsic (10,) | ||
| }, | ||
| # shape defaults to (10,) - one randomized execution per config | ||
| ) | ||
| # Output shape for "meas": (10, num_shots, creg_size) | ||
| ``` | ||
|
|
||
| ### Single randomization axis | ||
|
|
||
| To run multiple randomizations per configuration, extend the shape with additional axes. | ||
| For example, the following code runs 20 randomizations for each of 10 parameter configurations: | ||
|
|
||
| ```python | ||
| program.append_samplex_item( | ||
| template_circuit, | ||
| samplex=samplex, | ||
| samplex_arguments={ | ||
| "parameter_values": np.random.rand(10, 2), # extrinsic (10,) | ||
| }, | ||
| shape=(20, 10), # 20 randomizations × 10 configurations | ||
| ) | ||
| # Output shape for "meas": (20, 10, num_shots, creg_size) | ||
| ``` | ||
|
|
||
| ### Multiple randomization axes | ||
|
|
||
| You can organize randomizations into a multi-dimensional grid. This is useful for structured | ||
| analysis, for example, separating randomizations by type or grouping them for statistical processing. | ||
|
|
||
| Here, the input extrinsic shape `(10,)` broadcasts to the requested shape `(2, 14, 10)`, | ||
| with axes 0 and 1 filled by independent randomizations. | ||
|
|
||
| ```python | ||
| program.append_samplex_item( | ||
| template_circuit, | ||
| samplex=samplex, | ||
| samplex_arguments={ | ||
| "parameter_values": np.random.rand(10, 2), # extrinsic (10,) | ||
| }, | ||
| # 2×14=28 randomizations per configuration, 10 configurations | ||
| # Or you could set shape=(28, 10) for the same effect | ||
| shape=(2, 14, 10), | ||
| ) | ||
| # Output shape for "meas": (2, 14, 10, num_shots, creg_size) | ||
| ``` | ||
|
|
||
| ### How `shape` and input shapes interact | ||
|
|
||
| The `shape` parameter must be broadcastable *from* your input extrinsic shapes. This means: | ||
|
|
||
| - Input shapes with size-1 dimensions can expand to match `shape`. | ||
| - Input shapes must align from the right with `shape`. | ||
| - Axes in `shape` that exceed the input dimensions enumerate randomizations. | ||
|
|
||
| Note that `shape` can contain size-1 dimensions | ||
| that expand to match input dimensions, as is illustrated in the last row of the following table. | ||
|
|
||
| Examples: | ||
|
|
||
| | Input extrinsic | Shape | Result | | ||
| |---------------|----------|-----------------------------------------------------------------| | ||
| | (10,) | (10,) | 10 configurations, 1 randomization each | | ||
| | (10,) | (5, 10) | 10 configurations, 5 randomizations each | | ||
| | (10,) | (2, 3, 10) | 10 configurations, 2×3=6 randomizations each | | ||
| | (4, 1) | (4, 5) | 4 configurations, 5 randomizations each | | ||
| | (4, 3) | (2, 4, 3) | 4×3=12 configurations, 2 randomizations each | | ||
| | (4, 3) | (2, 1, 3) | 4×3=12 configurations, 2 randomizations each (the 1 expands to 4) | | ||
|
|
||
|
|
||
|
|
||
| ### Index into results | ||
|
|
||
|
|
||
| With randomization axes, you can index into specific randomization/parameter combinations: | ||
|
|
||
| ```python | ||
| # Using shape=(2, 14, 10) with input extrinsic shape (10,), and | ||
| # 1024 shots and 4 classical registers. | ||
| result = executor.run(program).result() | ||
| meas_data = result[0]["meas"] # shape (2, 14, 10, 1024, 4) | ||
|
|
||
| # Get all shots for randomization (0, 7) and parameter config 3 | ||
| specific = meas_data[0, 7, 3, :, :] # shape (1024, 4) | ||
|
|
||
| # Average over all randomizations for parameter config 5 on bit 2 | ||
| averaged = meas_data[:, :, 5, :, 2].mean(axis=(0, 1)) | ||
| ``` | ||
|
|
||
| ## Common patterns | ||
|
|
||
| ### Sweep a single parameter | ||
| Use code like the following to sweep one parameter while holding others fixed: | ||
|
|
||
| ```python | ||
| # Circuit has 2 parameters, sweep first one over 20 values | ||
| sweep_values = np.linspace(0, 2*np.pi, 20) | ||
|
|
||
| parameter_values = np.column_stack([ | ||
| sweep_values, | ||
| np.full(20, 0.5), | ||
| ]) # shape (20, 2) | ||
| ``` | ||
|
|
||
| ### Creating a 2D grid sweep | ||
| To create a grid over three parameters: | ||
|
|
||
| ```python | ||
| # Sweep param 0 over 10 values, param 1 over 8 values, param 2 fixed | ||
| p0 = np.linspace(0, np.pi, 10)[:, np.newaxis, np.newaxis] # (10, 1, 1) | ||
| p1 = np.linspace(0, np.pi, 8)[np.newaxis, :, np.newaxis] # (1, 8, 1) | ||
| p2 = np.array([[[0.5]]]) # (1, 1, 1) | ||
|
|
||
| parameter_values = np.broadcast_arrays(p0, p1, p2) | ||
| parameter_values = np.stack(parameter_values, axis=-1).squeeze() # (10, 8, 3) | ||
|
|
||
| # Extrinsic shape: (10, 8), intrinsic shape: (3,) | ||
| ``` | ||
|
|
||
| ### Combining multiple inputs | ||
|
|
||
| When combining inputs with different intrinsic shapes, align extrinsic dimensions using | ||
| size-1 axes: | ||
|
|
||
| ```python | ||
| # 4 parameter configurations, 3 noise scales → 4×3 = 12 total configurations | ||
| parameter_values = np.random.rand(4, 1, 2) # extrinsic (4, 1), intrinsic (2,) | ||
| noise_scale = np.array([0.8, 1.0, 1.2]) # extrinsic (3,), intrinsic () | ||
|
|
||
| # Broadcasted extrinsic shape: (4, 3) | ||
| ``` | ||
|
|
||
| ## Next steps | ||
|
|
||
| <Admonition type="tip" title="Recommendations"> | ||
| - Review the [broadcasting](/docs/guides/primitive-input-output#broadcasting) overview. | ||
| - Understand [Executor inputs and outputs](/docs/guides/executor-input-output). | ||
| </Admonition> | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.