Python API Reference¶
The bloq namespace provides the main workflow: author or load a
BlockGraph, compile it to Bloq, then emit a circuit or lower the program
for VM verification. Import bloq.ir for the concrete types returned by IR
accessors. These are the same native objects, with no conversion layer.
This page groups signatures and generated docstrings by task. For a complete first program, use the quickstart.
Compiled-program inspection types and exchange-format constants.
Use these types when inspecting a bloq.Bloq, matching node or circuit variants,
or writing a custom backend. They are the same native objects returned by program
accessors; importing this namespace adds no wrappers or conversions.
Examples
>>> import bloq
>>> from bloq.ir import CircuitOp, RegionKind
>>> program = bloq.compile(bloq.GalleryItem.X_MEMORY.load(), distance=3)
>>> isinstance(program.template(0).circuit.ops()[0], CircuitOp)
True
>>> program.regions_of(RegionKind.RepeatUntilSuccess)
[]
Compilation¶
compile returns a Bloq program at an odd code distance in 3..=255.
It accepts both local and hierarchical sources through BlockGraph. Pass
validate=True to request a full IR audit after compilation; required local
checks run in either case.
The free compile function shares a process-wide template cache with no
automatic eviction. Use clear_compile_cache between large distance sweeps,
or a CompileContext when a Python object should own a fixed configuration
and private cache. A context can serve concurrent callers. Budget overrides use
limits={"FIELD": count}; None disables that field’s limit.
-
compile(graph: BlockGraph, distance: int =
3, *, prepare_t_with_mpps: bool =False, validate: bool =False, limits: Mapping[str, int | None] | None =None) Bloq¶ Compiles
graphinto a Bloq IR program with a one-shot context. Circuit templates are still cached process-wide, so repeated calls at one distance stay warm.Use
CompileContextwhen one object should own the configuration and cache lifetime instead of sharing the process-global cache.- Parameters:
- graph: BlockGraph¶
The block graph to compile.
- distance: int =
3¶ The odd code distance to compile at, in
3..=255.- prepare_t_with_mpps: bool =
False¶ Use compact T-source preparation with one stabilizer
MPPinstead of MSC-LS cultivation.- validate: bool =
False¶ Explicitly audit the compiled IR using the configured Boolean limits. Defaults to
False.- limits: Mapping[str, int | None] | None =
None¶ Compiler budget overrides keyed by field name. Non-negative counts impose a budget,
Nonedisables it, and omitted fields keep their defaults. See the field list at https://bloqec.com/docs/dev/api/rust/bloq_graph/struct.ModuleCertificationLimits.html.
- Raises:
CompileError – If compilation fails.
InvalidArgumentError – If the distance or a limit field is invalid.
OverflowError – If a limit count is negative or exceeds
usize.
- Emits:
- CompileWarning: Once per advisory the compile recorded — currently a
spatial Hadamard pipe under the fixed-bulk convention, whose effective distance can be lower than the requested code distance.
Examples
>>> import bloq >>> graph = bloq.GalleryItem.X_MEMORY.load() >>> bloq.compile(graph, distance=3) <Bloq nodes=2 edges=1 qubits=17>
func bloq.compile
-
class CompileContext(distance: int =
3, *, prepare_t_with_mpps: bool =False, limits: Mapping[str, int | None] | None =None)¶ A reusable compilation context with a fixed configuration and a private template cache. The free
compilefunction instead shares a process-global cache; use a context when its cache lifetime should follow one Python object.compilereleases the GIL and takes a shared receiver, so one context can serve several threads at once — the template cache is internally synchronized.Examples
>>> import bloq >>> context = bloq.CompileContext(distance=3) >>> program = context.compile(bloq.GalleryItem.X_MEMORY.load()) >>> program.metadata[bloq.ir.CODE_DISTANCE_METADATA_KEY] 3-
compile(graph: BlockGraph, *, validate: bool =
False) Bloq¶ Compiles
graphinto a Bloq IR program.- Parameters:
- graph: BlockGraph¶
The block graph to compile.
- validate: bool =
False¶ Explicitly audit the resulting IR, using this context’s Boolean limits. Defaults to
False; compilation always performs its required local checks.
- Raises:
CompileError – If compilation fails.
- Emits:
- CompileWarning: Once per advisory the compile recorded — currently
a spatial Hadamard pipe under the fixed-bulk convention, whose effective distance can be lower than the requested code distance.
Examples
>>> import bloq >>> context = bloq.CompileContext(distance=3) >>> context.compile(bloq.GalleryItem.X_MEMORY.load()) <Bloq nodes=2 edges=1 qubits=17> >>> context.compile(bloq.GalleryItem.CNOT.load()) # reuses cached templates <Bloq nodes=... edges=... qubits=65>
meth
bloq.CompileContext.compile- distance¶
The fixed code distance used by this context.
attr
bloq.CompileContext.distance- prepare_t_with_mpps¶
Whether this context uses compact T-source MPP preparation.
attr
bloq.CompileContext.prepare_t_with_mpps-
compile(graph: BlockGraph, *, validate: bool =
class bloq.CompileContext
- clear_compile_cache() None¶
Drops every circuit template cached by
compile, releasing their memory.compilecaches templates process-wide and never evicts them, so a process that compiles at many code distances — a distance sweep, say — keeps every distance’s templates resident. Call this between phases of such a run to give the memory back; the nextcompilesimply refills the cache.Compilations already in flight on other threads are unaffected.
Examples
>>> import bloq >>> bloq.compile(bloq.GalleryItem.X_MEMORY.load(), distance=3) <Bloq nodes=2 edges=1 qubits=17> >>> bloq.clear_compile_cache() >>> bloq.compile(bloq.GalleryItem.X_MEMORY.load(), distance=3) # recompiles <Bloq nodes=2 edges=1 qubits=17>
func bloq.clear_compile_cache
- is_valid_distance(distance: int) bool¶
Whether
distanceis a code distance this compiler accepts: odd and in3..=255.Use it to filter a sweep before compiling. Negative and arbitrarily large integers return
False; non-integers raiseTypeError.- Parameters:
- distance: int¶
The candidate code distance.
Examples
>>> import bloq >>> [d for d in (1, 2, 3, 4, 1000) if bloq.is_valid_distance(d)] [3]
func bloq.is_valid_distance
Clifford proxies¶
Clifford proxies replace T resources with perfect input ports and pin selective sites to measurement arms. Supply the pins explicitly, or use the random helper with a seed for reproducibility. These programs support circuit-distance analysis; they do not verify the original non-Clifford computation or replace structural branch selection.
-
compile_clifford_proxy(graph: BlockGraph, pins: collections.abc.Sequence[bool], distance: int =
3, *, prepare_t_with_mpps: bool =False, limits: Mapping[str, int | None] | None =None) Bloq¶ Compiles
graphwith every dynamic element replaced by a Clifford stand-in — a distance oracle, not a semantics oracle. Each selective block is statically pinned to one measurement arm by the correspondingpinsbit (deterministic block order, one bit per selective site); T blocks become perfect input ports. The result suits Stim distance searches such asshortest_graphlike_error.pinscarries one bit per selective site in deterministic block order; pass an empty list for a graph with no selective blocks.Both this function and
compile_random_clifford_proxyaccept simple and hierarchical block graphs. The detector-slice proxy remains an editor view and retains T regions, so it is not a Stim distance-search circuit.- Parameters:
- graph: BlockGraph¶
The block graph to compile.
- pins: collections.abc.Sequence[bool]¶
One bit per selective site in deterministic block order, pinning each to a measurement arm; empty when the graph has no selective blocks.
- distance: int =
3¶ The odd code distance to compile at, in
3..=255.- prepare_t_with_mpps: bool =
False¶ Use compact T-source preparation with one stabilizer
MPPinstead of MSC-LS cultivation.- limits: Mapping[str, int | None] | None =
None¶ Compiler budget overrides; non-negative counts impose a budget,
Nonedisables it, and omitted fields keep their defaults.
- Raises:
CompileError – If compilation fails.
InvalidArgumentError – If the distance or a limit field is invalid.
OverflowError – If a limit count is negative or exceeds
usize.
- Emits:
- CompileWarning: Once per advisory the compile recorded — currently a
spatial Hadamard pipe under the fixed-bulk convention, whose effective distance can be lower than the requested code distance.
Examples
>>> import bloq >>> graph = bloq.GalleryItem.X_MEMORY.load() >>> bloq.compile_clifford_proxy(graph, [], distance=3).quantum_node_count 1
func bloq.compile_clifford_proxy
-
compile_random_clifford_proxy(graph: BlockGraph, distance: int =
3, *, prepare_t_with_mpps: bool =False, seed: int | None =None, limits: Mapping[str, int | None] | None =None) Bloq¶ Compiles one random static Clifford proxy path. Shared selective controls are sampled jointly; the seed is stored under
CLIFFORD_PROXY_SEED_METADATA_KEY.- Parameters:
- graph: BlockGraph¶
The block graph to compile.
- distance: int =
3¶ The odd code distance to compile at, in
3..=255.- prepare_t_with_mpps: bool =
False¶ T preparation option; proxy T blocks remain ports.
- seed: int | None =
None¶ Reproducible path seed;
Nonegenerates one.- limits: Mapping[str, int | None] | None =
None¶ Compiler budget overrides; non-negative counts impose a budget,
Nonedisables it, and omitted fields keep their defaults.
func bloq.compile_random_clifford_proxy
Source authoring¶
BlockGraph owns source geometry, actions, and module definitions. Its BLOG
load/save operations retain authored hierarchy; compilation consumes that same
graph representation. Call flatten() only when an independent flat projection
is needed, such as for an explicit port-filling experiment.
Use ActionDag and expressions for named measurements, feedback, and control.
GalleryItem.load() supplies reusable examples. The
BLOG format describes the corresponding source syntax.
- class BlockGraph¶
A graph of surface code blocks connected by pipes.
Blocks sit at integer lattice positions; pipes are lattice-surgery connections between adjacent blocks. Build one incrementally with
add_block/add_pipe, load a ready-made graph fromGalleryItem, or parse.blogtext withparse_blog.Examples
>>> from bloq import BlockGraph, Block, Pipe >>> graph = BlockGraph() >>> graph.add_block(Block((0, 0, 0), "ZXZ")) (0, 0, 0) >>> graph.add_block(Block((0, 0, 1), "ZXZ")) (0, 0, 1) >>> graph.add_pipe(Pipe((0, 0, 0), "+Z")) >>> graph.block_count, graph.pipe_count (2, 1) >>> graph.validate()-
static definition(name: str, body: BlockGraph, *, inputs: dict[str, tuple[int, int, int]] | None =
None, outputs: dict[str, tuple[int, int, int]] | None =None, bit_inputs: collections.abc.Sequence[str] | None =None, bit_outputs: dict[str, Expr | str] | None =None, actions: collections.abc.Sequence[Action] | None =None, resources: Mapping[str, str] | None =None) BlockGraph¶ Copies a graph into a reusable module definition. Omitted interface arguments retain the body’s existing declarations. Quantum port dictionaries retain insertion order; positions name local Port blocks. Supplying inputs or outputs replaces the quantum interface. Child instances and connections are retained. Validate the completed hierarchy with
from_definitions.- Parameters:
- name: str¶
Definition name; use
mainfor the executable root.- body: BlockGraph¶
Graph containing local geometry, actions, and child instances.
- inputs: dict[str, tuple[int, int, int]] | None =
None¶ Names and positions of quantum input Ports.
- outputs: dict[str, tuple[int, int, int]] | None =
None¶ Names and positions of quantum output Ports.
- bit_inputs: collections.abc.Sequence[str] | None =
None¶ Classical names supplied by the parent.
- bit_outputs: dict[str, Expr | str] | None =
None¶ Exported expressions or local/child value names.
- actions: collections.abc.Sequence[Action] | None =
None¶ Optional replacement actions, using the declared bit inputs.
- resources: Mapping[str, str] | None =
None¶ Quantum port resource types; unspecified ports use
data.
- Raises:
InvalidArgumentError – If a resource names an undeclared port.
BlockGraphError – If classical input declarations are invalid.
meth
bloq.BlockGraph.definition-
static from_definitions(definitions: collections.abc.Sequence[BlockGraph], *, limits: Mapping[str, int | None] | None =
None) BlockGraph¶ Assembles module definitions into a validated hierarchy rooted at
main. Each definition is stored once; input graphs remain independent. Definitions must be local graphs without their own helper libraries.- Parameters:
- definitions: collections.abc.Sequence[BlockGraph]¶
Reusable definitions and one executable
main.- limits: Mapping[str, int | None] | None =
None¶ Source-analysis and expansion budget overrides.
- Raises:
BlockGraphError – If names, interfaces, seams, geometry, or dependencies are invalid.
meth
bloq.BlockGraph.from_definitions-
static from_text(text: str, *, limits: Mapping[str, int | None] | None =
None) BlockGraph¶ Parses
.blogtext into a block graph, retaining module definitions, instances, interfaces, and connections. This is the same native constructor as Rust’sBlockGraph::from_text. Useload()to resolve imports relative to a file.- Parameters:
- Raises:
ParseError – If
textis not valid.blog.InvalidArgumentError – If a limit field is unknown.
OverflowError – If a limit count is negative or exceeds
usize.
meth
bloq.BlockGraph.from_text-
static load(path: str | os.PathLike | pathlib.Path, *, limits: Mapping[str, int | None] | None =
None) BlockGraph¶ Loads a
.blogfile through Rust’s nativeBlockGraph::loadconstructor, resolving module imports relative to each file.Like
from_text, retains the module hierarchy. Both simple and hierarchical graphs are accepted bycompile.- Parameters:
- Raises:
OSError – If the root file or an imported file cannot be read.
ParseError – If BLOG parsing or module resolution fails.
BlockGraphError – If graph structure is invalid.
InvalidArgumentError – If a limit field is unknown.
OverflowError – If a limit count is negative or exceeds
usize.
meth
bloq.BlockGraph.load- action_graph() ActionDag¶
Returns an independent snapshot of the current source action DAG.
Local structural edits refresh the cached DAG.
is_analyzedsays whether correlation support dependencies are present. For child instances, callflatten()explicitly to inspect the combined action program.
meth
bloq.BlockGraph.action_graph- actions() list[Action]¶
The graph’s actions as structured
Actionvalues;str(action)renders the BLOG statement (positions, not block ids).
meth
bloq.BlockGraph.actions- add_action(action: Action) None¶
Appends one structured action, rederiving measurement surfaces and the action DAG.
- Parameters:
- Raises:
BlockGraphError – If action semantics, measurement-surface derivation, or the resulting dependency DAG is invalid.
meth
bloq.BlockGraph.add_action- add_actions_from_text(text: str) None¶
Appends actions parsed from BLOG action-statement text, e.g.
"m = measure 0 -> +Z". Block IDs refer toget_block_idvalues.- Parameters:
- text: str¶
BLOG action-statement text; numeric block IDs refer to
get_block_idvalues.
- Raises:
ParseError – If
textis not valid action-statement syntax.BlockGraphError – If action semantics, measurement-surface derivation, or the resulting dependency DAG is invalid.
Examples
>>> from bloq import BlockGraph, Block >>> graph = BlockGraph() >>> graph.add_block(Block((0, 0, 0), "ZXZ")) (0, 0, 0) >>> graph.add_actions_from_text("m = measure 0") >>> [str(a) for a in graph.actions()] ['m = measure [0, 0, 0]']
meth
bloq.BlockGraph.add_actions_from_text- add_block(block: Block) tuple[int, int, int]¶
Adds a block, returning its position. Raises
BlockGraphErrorif the position is occupied.
meth
bloq.BlockGraph.add_block-
add_branches(branches: collections.abc.Sequence[Branch], *, pipes: collections.abc.Sequence[Pipe] | None =
None, actions: collections.abc.Sequence[Action] | None =None) None¶ Attaches branches, shared seams, and additional actions in one edit. Each branch supplies its own resolve condition. Existing actions remain. The graph is unchanged if geometry or action validation fails.
- Parameters:
- branches: collections.abc.Sequence[Branch]¶
Named branches with explicit false and true arms.
- pipes: collections.abc.Sequence[Pipe] | None =
None¶ Shared pipes joining arm boundaries to the common graph or other branches. They are stored once, outside the arms.
- actions: collections.abc.Sequence[Action] | None =
None¶ Additional actions, including measurements used by selectors.
- Raises:
BlockGraphError – If geometry, interfaces, names, or actions are invalid.
meth
bloq.BlockGraph.add_branches-
add_instance(name: str, definition: str, translation: tuple[int, int, int] =
(0, 0, 0), *, rotation: tuple[UDirection | str, int] | None =None) None¶ Places a named child definition, rotating before translating. The completed hierarchy checks names, geometry, and supported orientations.
- Parameters:
- name: str¶
Instance name in this parent.
- definition: str¶
Reusable definition name.
- translation: tuple[int, int, int] =
(0, 0, 0)¶ Placement in the parent’s lattice coordinates.
- rotation: tuple[UDirection | str, int] | None =
None¶ Optional axis and angle in degrees, such as
("Z", 180).
- Raises:
InvalidArgumentError – If the axis or rotation angle is invalid.
meth
bloq.BlockGraph.add_instance- add_pipe(pipe: Pipe) None¶
Adds a pipe. Raises
BlockGraphErrorif an endpoint is missing or the pipe already exists.
meth
bloq.BlockGraph.add_pipe-
analyze_action_graph(*, limits: Mapping[str, int | None] | None =
None) ActionDag¶ Derives an independent DAG including correlation support dependencies.
Accepts local graphs and authored module hierarchies without changing the source. Hierarchies use the compiler’s module linker and correlation composition.
owners()identifies each action’s definition and instance. Continuing branches use guarded causal readout planning.- Parameters:
- limits: Mapping[str, int | None] | None =
None¶ Source-analysis budget overrides, as accepted by
from_text.
- limits: Mapping[str, int | None] | None =
- Raises:
BlockGraphError – If module validation, readout analysis or resource limits fail.
InvalidArgumentError – If a limit field is unknown.
OverflowError – If a limit count is negative or exceeds
usize.
Examples
>>> import bloq >>> source = bloq.GalleryItem.T_GATE.load() >>> dag = source.analyze_action_graph() >>> dag.is_analyzed True
meth
bloq.BlockGraph.analyze_action_graph- bind(source: str, target: str) None¶
Supplies a parent value or child bit output to a child classical input. For example,
bind("read.result", "correct.flip")creates a dependency between those instances. The completed hierarchy validates the binding.- Raises:
InvalidArgumentError – If a qualified endpoint is malformed.
meth
bloq.BlockGraph.bind- bind_modules(source: str, target: str) None¶
Binds a parent value or child bit output to a child’s classical input. Replaces the current binding and removes an unused exposed parent input.
- Raises:
BlockGraphError – If values or dependencies are invalid; the graph is unchanged.
meth
bloq.BlockGraph.bind_modules- blocks() list[Block]¶
Blocks authored in this definition’s local body, excluding child instances.
meth
bloq.BlockGraph.blocks- can_place_block(block: Block) bool¶
Returns
Truewhenblockcan be placed without overlap.
meth
bloq.BlockGraph.can_place_block- clear_actions() None¶
Removes all actions from the graph.
meth
bloq.BlockGraph.clear_actions-
connect(source: tuple[int, int, int] | str, target: tuple[int, int, int] | str, *, hadamard: bool =
False) None¶ Connects a child quantum output to an input or a parent-local block. Endpoints are local position tuples or strings such as
gate.q_out. At least one endpoint must belong to an instance. Connections do not route geometry; the completed hierarchy checks placement and boundary bases.- Raises:
InvalidArgumentError – If endpoint syntax is invalid or both endpoints are local.
meth
bloq.BlockGraph.connect-
connect_modules(output: str, input: str, *, align: bool =
True, compact: bool =True, hadamard: bool =False) int¶ Aligns and docks named child ports, joining all other touching compatible ports. The input instance’s connected group moves together. Endpoint Hadamards are preserved; obstructed compaction retains connection cubes.
- Parameters:
- output: str¶
Child output name, such as
child0.control_out.- input: str¶
Child input name, such as
child1.control_in.- align: bool =
True¶ Align the input group’s selected port to the output.
- compact: bool =
True¶ Prefer direct seams over parent connection cubes.
- hadamard: bool =
False¶ Extra basis change at the selected seam.
- Returns:
Number of port pairs joined.
- Raises:
BlockGraphError – If ports, types, faces, or placement are invalid; the graph is unchanged.
meth
bloq.BlockGraph.connect_modules- degree(pos: tuple[int, int, int]) int¶
The number of pipes attached to the block at
pos.- Parameters:
- pos: tuple[int, int, int]¶
The
(x, y, z)lattice position.
meth
bloq.BlockGraph.degree-
export_gltf(path: str | os.PathLike | pathlib.Path, pipe_length: float =
2.0, stabilizer: StabilizerGenerator | collections.abc.Sequence[int] | None =None, pop_faces_at_directions: collections.abc.Sequence[Direction | str] | None =None, pop_faces_at_blocks: collections.abc.Sequence[tuple[tuple[int, int, int], Direction | str]] | None =None, pop_faces_at_pipes: collections.abc.Sequence[tuple[tuple[int, int, int], tuple[int, int, int], Direction | str]] | None =None, module_view: bool =False) None¶ Exports the graph as a
.gltfscene, optionally overlaying a stabilizer row. Setmodule_view=Trueto color an authored hierarchy by definition.- Parameters:
- path: str | os.PathLike | pathlib.Path¶
Filesystem path to write the
.gltffile to.- pipe_length: float =
2.0¶ Rendered pipe length in scene units. Defaults to 2.0.
- module_view: bool =
False¶ Color by module definition without changing the hierarchy. Cannot be combined with a stabilizer overlay.
- stabilizer: StabilizerGenerator | collections.abc.Sequence[int] | None =
None¶ Optional generator, or indices of logical generators whose phase-free product supplies the displayed correlation support.
- pop_faces_at_directions: collections.abc.Sequence[Direction | str] | None =
None¶ Signed lattice directions whose outward faces are removed, for example
["-Y"].- pop_faces_at_blocks: collections.abc.Sequence[tuple[tuple[int, int, int], Direction | str]] | None =
None¶ (position, face_direction)pairs selecting individual block faces.- pop_faces_at_pipes: collections.abc.Sequence[tuple[tuple[int, int, int], tuple[int, int, int], Direction | str]] | None =
None¶ (u, v, face_direction)triples selecting individual pipe faces.
- Raises:
OSError – If the file cannot be written.
meth
bloq.BlockGraph.export_gltf-
export_html_viewer(path: str | os.PathLike | pathlib.Path, pipe_length: float =
2.0, stabilizer: StabilizerGenerator | collections.abc.Sequence[int] | None =None, pop_faces_at_directions: collections.abc.Sequence[Direction | str] | None =None, pop_faces_at_blocks: collections.abc.Sequence[tuple[tuple[int, int, int], Direction | str]] | None =None, pop_faces_at_pipes: collections.abc.Sequence[tuple[tuple[int, int, int], tuple[int, int, int], Direction | str]] | None =None, module_view: bool =False) None¶ Exports the graph as a self-contained HTML viewer page (the
model-viewerrenderer is loaded from a CDN, so viewing needs network). Setmodule_view=Truefor the editor’s definition colors and module legend.- Parameters:
- path: str | os.PathLike | pathlib.Path¶
Filesystem path to write the HTML file to.
- pipe_length: float =
2.0¶ Rendered pipe length in scene units. Defaults to 2.0.
- module_view: bool =
False¶ Color by module definition without changing the hierarchy. Cannot be combined with a stabilizer overlay.
- stabilizer: StabilizerGenerator | collections.abc.Sequence[int] | None =
None¶ Optional generator, or indices of logical generators whose phase-free product supplies the displayed correlation support.
- pop_faces_at_directions: collections.abc.Sequence[Direction | str] | None =
None¶ Signed lattice directions whose outward faces are removed, for example
["-Y"].- pop_faces_at_blocks: collections.abc.Sequence[tuple[tuple[int, int, int], Direction | str]] | None =
None¶ (position, face_direction)pairs selecting individual block faces.- pop_faces_at_pipes: collections.abc.Sequence[tuple[tuple[int, int, int], tuple[int, int, int], Direction | str]] | None =
None¶ (u, v, face_direction)triples selecting individual pipe faces.
- Raises:
OSError – If the file cannot be written.
meth
bloq.BlockGraph.export_html_viewer- fill_ports_auto() list[tuple[BlockGraph, list[StabilizerGenerator]]]¶
Builds compatible static boundary fills, returning
(closed_graph, stabilizer_generators)pairs.Port, T, and Selective sites use full compatible boundary support. Variants retain selected measurement records and aliases, and remove feedback and resolves for filled caps.
filled_graphs()is the same thing without the generators, for the common case of only wanting the closed graphs. Callflatten()first for authored module interfaces or child instances.- Raises:
BlockGraphError – If a boundary cannot be filled, or postselection or a structural branch needs an omitted measurement record.
meth
bloq.BlockGraph.fill_ports_auto- filled_graphs() list[BlockGraph]¶
The graphs from
fill_ports_auto(), without their selecting generators. Callflatten()first for authored module interfaces or child instances.- Raises:
BlockGraphError – If the ports cannot be filled.
Examples
>>> import bloq >>> filled = bloq.GalleryItem.BELL_STATE.load().flatten().filled_graphs() >>> all(not graph.is_open for graph in filled) True
meth
bloq.BlockGraph.filled_graphs-
flatten(*, limits: Mapping[str, int | None] | None =
None) BlockGraph¶ Expands module instances into one independent graph for geometry, boundary filling, or flat source analysis. Compilation preserves the hierarchy and does not require this conversion.
- Parameters:
- limits: Mapping[str, int | None] | None =
None¶ Source-expansion budget overrides. Use non-negative counts or
Nonefor unlimited; omitted fields retain their defaults.
- limits: Mapping[str, int | None] | None =
- Raises:
BlockGraphError – If expansion cannot produce a valid flat graph.
meth
bloq.BlockGraph.flatten- flip_xz_basis() BlockGraph¶
Returns a new graph with X/Z bases flipped (topology-preserving). Call
flatten()first for authored module interfaces or child instances.- Raises:
BlockGraphError – If the basis flip cannot be applied.
meth
bloq.BlockGraph.flip_xz_basis- get_block(pos: tuple[int, int, int]) Block | None¶
Returns the block at
pos, orNoneif absent.- Parameters:
- pos: tuple[int, int, int]¶
The
(x, y, z)lattice position.
meth
bloq.BlockGraph.get_block- get_block_id(pos: tuple[int, int, int]) int | None¶
Returns the internal node ID for the block at
pos, orNone.- Parameters:
- pos: tuple[int, int, int]¶
The
(x, y, z)lattice position.
meth
bloq.BlockGraph.get_block_id- get_pipe(u: tuple[int, int, int], v: tuple[int, int, int]) Pipe | None¶
Returns the pipe between
uandv, orNoneif absent.
meth
bloq.BlockGraph.get_pipe- has_actions() bool¶
Trueif the graph carries any actions.
meth
bloq.BlockGraph.has_actions- has_block_at(pos: tuple[int, int, int]) bool¶
Trueif a block occupiespos.- Parameters:
- pos: tuple[int, int, int]¶
The
(x, y, z)lattice position.
meth
bloq.BlockGraph.has_block_at- has_pipe_between(u: tuple[int, int, int], v: tuple[int, int, int]) bool¶
Trueif a pipe connectsuandv.
meth
bloq.BlockGraph.has_pipe_between-
module(name: str, *, limits: Mapping[str, int | None] | None =
None) BlockGraph¶ Extracts a named definition as an independent graph, retaining its reachable helper definitions and naming its executable root
main. The copy can be edited, saved, pickled, and compiled independently.- Parameters:
- Raises:
KeyError – If the definition is absent.
BlockGraphError – If its independent hierarchy cannot be constructed.
meth
bloq.BlockGraph.module- neighbors(pos: tuple[int, int, int]) list[Block]¶
All blocks directly connected to the block at
pos.- Parameters:
- pos: tuple[int, int, int]¶
The
(x, y, z)lattice position.
meth
bloq.BlockGraph.neighbors- occupied_positions() list[tuple[int, int, int]]¶
Local lattice cells reserved by blocks (multi-cell blocks contribute their full footprint).
meth
bloq.BlockGraph.occupied_positions- pipes() list[Pipe]¶
Pipes authored in this definition’s local body, excluding child instances.
meth
bloq.BlockGraph.pipes-
place_module(name: str, definition: str, translation: tuple[int, int, int] | None =
None, *, rotation: tuple[UDirection | str, int] | None =None) tuple[int, int, int]¶ Places a module instance and exposes its quantum and classical interface. With no translation, places it beside the current geometry. The graph must contain the named definition; input graphs remain independent.
- Parameters:
- name: str¶
New instance name.
- definition: str¶
Definition already stored in this graph.
- translation: tuple[int, int, int] | None =
None¶ Optional explicit lattice placement.
- rotation: tuple[UDirection | str, int] | None =
None¶ Optional axis and angle, such as
("Z", 180).
- Returns:
The instance’s translation.
- Raises:
BlockGraphError – If placement, names, or interfaces are invalid; the graph is unchanged.
meth
bloq.BlockGraph.place_module- placement_error(block: Block) str | None¶
Why
blockcannot be placed, orNonewhen it can.The message
add_blockwould have raised, without raising it — for callers reporting the reason rather than just refusing.Examples
>>> import bloq >>> graph = bloq.BlockGraph() >>> graph.add_block(bloq.Block((0, 0, 0), "ZXZ")) (0, 0, 0) >>> graph.placement_error(bloq.Block((0, 0, 1), "ZXZ")) is None True >>> "occupied" in graph.placement_error(bloq.Block((0, 0, 0), "ZXZ")) True
meth
bloq.BlockGraph.placement_error- positions() list[tuple[int, int, int]]¶
Positions of this definition’s local blocks, excluding child instances.
meth
bloq.BlockGraph.positions- project_branches(assignments: collections.abc.Sequence[tuple[tuple[int, int, int], bool]]) BlockGraph¶
Projects every structural branch to a static true or false arm.
The returned graph has no
branchactions and can be passed tostabilizers(). Every branch target must appear exactly once. Callflatten()first for authored module interfaces or child instances.- Parameters:
- assignments: collections.abc.Sequence[tuple[tuple[int, int, int], bool]]¶
(target_position, value)pairs for every branch.
- Raises:
BlockGraphError – If a target is unknown, duplicated, or omitted, or the branch regions are invalid.
meth
bloq.BlockGraph.project_branches-
randomly_resolve_selectives(seed: int | None =
None) tuple[BlockGraph, dict[Block, Block]]¶ Resolves all selective blocks by randomly choosing a measurement basis. Returns
(resolved_graph, replacements)wherereplacementsmaps each original selective block to its resolved block. Deterministic whenseedis given.If any selective is resolved, the returned graph is structure-only and has no actions, including structural branches. Project branches first when a concrete conditional topology is required. Call
flatten()first for authored module interfaces or child instances.- Parameters:
- seed: int | None =
None¶ Optional RNG seed; when given, resolution is deterministic. Defaults to
None(nondeterministic).
- seed: int | None =
meth
bloq.BlockGraph.randomly_resolve_selectives- remove_block(pos: tuple[int, int, int]) Block | None¶
Removes and returns the block at
pos, orNoneif absent.- Parameters:
- pos: tuple[int, int, int]¶
The
(x, y, z)lattice position.
meth
bloq.BlockGraph.remove_block- remove_pipe(u: tuple[int, int, int], v: tuple[int, int, int]) Pipe | None¶
Removes and returns the pipe between
uandv, orNoneif absent.
meth
bloq.BlockGraph.remove_pipe- rotate_about_origin(axis: UDirection | str, quarter_turns: int) BlockGraph¶
Returns a new graph rotated about the origin around
axisbyquarter_turns90-degree steps. Discards actions. Callflatten()first for authored module interfaces or child instances.- Parameters:
- axis: UDirection | str¶
A
UDirectionor its axis letter ("X"/"Y"/"Z").- quarter_turns: int¶
Number of 90-degree steps; may be negative.
- Raises:
InvalidArgumentError – If
axisis invalid or a rotated position exceeds the 32-bit coordinate range.BlockGraphError – If the rotation cannot be applied.
meth
bloq.BlockGraph.rotate_about_origin- save(path: str | os.PathLike | pathlib.Path) None¶
Writes the graph to a
.blogfile.- Parameters:
- path: str | os.PathLike | pathlib.Path¶
Filesystem path to write the
.blogfile to.
- Raises:
OSError – If the file cannot be written.
meth
bloq.BlockGraph.save- set_actions(actions: collections.abc.Sequence[Action]) None¶
Replaces all actions, rederiving measurement surfaces and the action DAG.
- Parameters:
- Raises:
BlockGraphError – If action semantics, measurement-surface derivation, or the resulting dependency DAG is invalid.
meth
bloq.BlockGraph.set_actions- set_block_kind(pos: tuple[int, int, int], kind: BlockKind | str) None¶
Replaces the kind of the block at
pos.- Parameters:
- Raises:
InvalidArgumentError – If
kindis an invalid spelling.BlockGraphError – If no block occupies
pos, or the new kind is incompatible.
meth
bloq.BlockGraph.set_block_kind- set_block_tag(pos: tuple[int, int, int], tag: str) None¶
Replaces the tag of the block at
pos.- Parameters:
- Raises:
BlockGraphError – If no block occupies
pos.
meth
bloq.BlockGraph.set_block_tag- set_pipe_hadamard(u: tuple[int, int, int], v: tuple[int, int, int], hadamard: bool) None¶
Sets whether the pipe between
uandvcarries a Hadamard.- Parameters:
- Raises:
BlockGraphError – If no pipe connects
uandv.
meth
bloq.BlockGraph.set_pipe_hadamard- set_pipe_tag(u: tuple[int, int, int], v: tuple[int, int, int], tag: str) None¶
Replaces the tag of the pipe between
uandv.- Parameters:
- Raises:
BlockGraphError – If no pipe connects
uandv.
meth
bloq.BlockGraph.set_pipe_tag- set_port_role(pos: tuple[int, int, int], role: str) None¶
Assigns a Port’s
"auto","input","output", or"multiplex"role.
meth
bloq.BlockGraph.set_port_role- shift_positions(offset: tuple[int, int, int]) BlockGraph¶
Returns a new graph shifted by
offset. Callflatten()first for authored module interfaces or child instances.- Parameters:
- offset: tuple[int, int, int]¶
The
(x, y, z)amount to shift every position by.
- Raises:
InvalidArgumentError – If a shifted position exceeds the 32-bit coordinate range.
BlockGraphError – If the graph retains module structure.
meth
bloq.BlockGraph.shift_positions- stabilizers() list[StabilizerGenerator]¶
Derives the stabilizer generators of the graph’s ZX diagram. Call
flatten()first if the graph contains child instances.- Raises:
BlockGraphError – If the stabilizers cannot be derived.
Examples
>>> import bloq >>> graph = bloq.GalleryItem.X_MEMORY.load() >>> graph.stabilizers()[0].kind 'logical'
meth
bloq.BlockGraph.stabilizers- to_text() str¶
Serializes the graph to
.blogtext (BLOG 1.0).
meth
bloq.BlockGraph.to_text-
translate_module(name: str, offset: tuple[int, int, int], *, compact: bool =
True) tuple[int, int, int]¶ Translates a connected group and joins touching compatible free ports. Returns the applied offset, including any successful compaction.
- Raises:
BlockGraphError – If geometry or interfaces are invalid; the graph is unchanged.
meth
bloq.BlockGraph.translate_module- validate() None¶
Validates structural constraints and action semantics.
- Raises:
BlockGraphError – If a structural or action-semantic constraint is violated.
meth
bloq.BlockGraph.validate- validate_structure() None¶
Validates structural constraints only (skips action semantics).
- Raises:
BlockGraphError – If a structural constraint is violated.
meth
bloq.BlockGraph.validate_structure- with_zero_min_z() BlockGraph¶
Returns a new graph shifted so the minimum z-coordinate is zero. Call
flatten()first for authored module interfaces or child instances.- Raises:
InvalidArgumentError – If normalization exceeds the 32-bit coordinate range.
BlockGraphError – If the graph retains module structure.
meth
bloq.BlockGraph.with_zero_min_z- block_count¶
Number of blocks in this definition’s local body, excluding child instances.
attr
bloq.BlockGraph.block_count- has_module_structure¶
Whether the graph declares module interfaces, instances, or definitions. A leaf with an explicit quantum or classical interface also returns true.
attr
bloq.BlockGraph.has_module_structure- is_clifford¶
Truewhen the graph and its reachable instances contain no T blocks.
attr
bloq.BlockGraph.is_clifford- is_empty¶
Trueif the graph and its reachable instances have no blocks.
attr
bloq.BlockGraph.is_empty- is_open¶
Truewhen the graph contains at least one port.
attr
bloq.BlockGraph.is_open- is_rigid¶
Truewhen every block, including reachable instances, has a statically known kind (no selectives).
attr
bloq.BlockGraph.is_rigid- module_names¶
Names of all graph definitions, including the root.
attr
bloq.BlockGraph.module_names- patch_rotation_count¶
Number of patch-rotation blocks in this definition’s local body.
attr
bloq.BlockGraph.patch_rotation_count- pipe_count¶
Number of pipes in this definition’s local body, excluding child instances.
attr
bloq.BlockGraph.pipe_count- port_count¶
Number of port blocks in this definition’s local body.
attr
bloq.BlockGraph.port_count- selective_count¶
Number of selective blocks in this definition’s local body.
attr
bloq.BlockGraph.selective_count- t_count¶
Number of T blocks in this definition’s local body.
attr
bloq.BlockGraph.t_count- walking_count¶
Number of walking blocks in this definition’s local body.
attr
bloq.BlockGraph.walking_count- y_count¶
Number of Y blocks in this definition’s local body.
attr
bloq.BlockGraph.y_count-
static definition(name: str, body: BlockGraph, *, inputs: dict[str, tuple[int, int, int]] | None =
class bloq.BlockGraph
-
class Block(pos: tuple[int, int, int], kind: BlockKind | str, height: str =
'd', tag: str | None =None, role: str ='auto')¶ A block placed at a 3D lattice position.
The
kindargument accepts aBlockKindor its BLOG spelling ("ZXZ","T", …).Examples
>>> from bloq import Block >>> b = Block((0, 1, 2), "ZXZ") >>> b.pos (0, 1, 2) >>> str(b.kind) 'ZXZ'- height¶
The block’s height as a
k*d + nexpression of the code distance, in the canonical BLOG spelling ("d","2d","d/2","3d+2").
attr
bloq.Block.height- kind¶
The block’s kind.
attr
bloq.Block.kind- pos¶
The block’s
(x, y, z)lattice position.
attr
bloq.Block.pos- role¶
The Port’s
"auto","input","output", or"multiplex"role;Nonefor non-Ports.
attr
bloq.Block.role- tag¶
The block’s tag string, or
None.
attr
bloq.Block.tag
class bloq.Block
- class BlockKind¶
The kind of a block: a cube, a moving block,
Y, a fixed measurement,Port,T, or a selective block. Data-carrying kinds are built via the static constructors.Examples
>>> from bloq import BlockKind >>> k = BlockKind.cube("ZXZ") >>> k.is_cube True >>> tuple(str(b) for b in k.bases) ('Z', 'X', 'Z') >>> BlockKind.t().is_clifford False- static cube(spec: str) BlockKind¶
A cube block from its face-basis spelling, e.g.
"XZZ".- Parameters:
- spec: str¶
The three face bases as an
X/Zstring, e.g."XZZ"(case-insensitive).
- Raises:
InvalidArgumentError – If
specis not a valid cube spelling.
meth
bloq.BlockKind.cube- static measurement(basis: Basis | str) BlockKind¶
A fixed terminal transversal measurement in the X or Z basis.
- Parameters:
- Raises:
InvalidArgumentError – If
basisis not X or Z.
meth
bloq.BlockKind.measurement- static parse(text: str) BlockKind¶
Parses a BLOG kind spelling (
"XZZ","X","Y","Z","PORT","T","XY", …).Walking and patch-rotation kinds carry movement data and cannot be spelled as a single token; build them via
walking/patch_rotation.- Parameters:
- text: str¶
A BLOG kind token, e.g.
"XZZ","X","Y","Z","PORT","T", or"XY".
- Raises:
InvalidArgumentError – If
textis not a valid single-token kind spelling.
meth
bloq.BlockKind.parse- static patch_rotation(basis: Basis | str, movement: tuple[int, int]) BlockKind¶
A patch-rotation block from a basis and an
(x, y)movement of(+/-1, 0)or(0, +/-1).- Parameters:
- Raises:
InvalidArgumentError – If
basisormovementis invalid.
meth
bloq.BlockKind.patch_rotation- static selective(spec: str) BlockKind¶
A selective block from its spelling (
"XY","XZ", or"YZ").- Parameters:
- spec: str¶
The selective spelling:
"XY","XZ", or"YZ".
- Raises:
InvalidArgumentError – If
specis not a valid selective spelling.
meth
bloq.BlockKind.selective- static walking(boundary: str, movement: tuple[int, int]) BlockKind¶
A walking block from its boundary spelling (e.g.
"ZXZ") and an(x, y)movement of(+/-1, 0),(0, +/-1), or(+/-1, +/-1).- Parameters:
- Raises:
InvalidArgumentError – If
boundaryormovementis invalid.
meth
bloq.BlockKind.walking- flip_xz_basis() BlockKind¶
Returns a new kind with X/Z bases flipped.
meth
bloq.BlockKind.flip_xz_basis- bases¶
The
(x, y, z)face bases, when this kind has a fixed basis assignment.
attr
bloq.BlockKind.bases- is_clifford¶
Trueif this kind is Clifford (everything except T).
attr
bloq.BlockKind.is_clifford- is_cube¶
Trueif this is a cube block.
attr
bloq.BlockKind.is_cube- is_dynamic¶
Truefor kinds whose behavior is resolved at runtime (T, selective).
attr
bloq.BlockKind.is_dynamic- is_measurement¶
Trueif this is a fixed terminal measurement block.
attr
bloq.BlockKind.is_measurement- is_patch_rotation¶
Trueif this is a patch-rotation block.
attr
bloq.BlockKind.is_patch_rotation- is_port¶
Trueif this is an open port block.
attr
bloq.BlockKind.is_port- is_selective¶
Trueif this is a selective block.
attr
bloq.BlockKind.is_selective- is_t¶
Trueif this is a T block.
attr
bloq.BlockKind.is_t- is_walking¶
Trueif this is a walking (moving) block.
attr
bloq.BlockKind.is_walking- is_y¶
Trueif this is the Y-basis block.
attr
bloq.BlockKind.is_y- measurement_basis¶
The X or Z basis of a fixed measurement block;
Nonefor other kinds.
attr
bloq.BlockKind.measurement_basis- movement¶
The
(x, y)in-plane step of a walking or patch-rotation kind, as passed towalking/patch_rotation;Nonefor kinds that do not move.Examples
>>> from bloq import BlockKind >>> BlockKind.walking("ZXZ", (1, 0)).movement (1, 0) >>> BlockKind.cube("ZXZ").movement is None True
attr
bloq.BlockKind.movement
class bloq.BlockKind
-
class Pipe(src: tuple[int, int, int], direction: Direction | str, hadamard: bool =
False, tag: str | None =None)¶ A pipe (lattice-surgery connection) from a source block along a direction.
The destination is one lattice step from
srcalongdirection, so a pipe is fully described by its source and direction. Thedirectionargument accepts aDirectionor its BLOG spelling ("+Z", …).Examples
>>> from bloq import Pipe >>> p = Pipe((0, 0, 0), "+Z") >>> p.src, p.dst ((0, 0, 0), (0, 0, 1)) >>> str(p.direction) '+Z'- direction¶
The direction the pipe leaves its source block.
attr
bloq.Pipe.direction- dst¶
The destination block position
(x, y, z).
attr
bloq.Pipe.dst- hadamard¶
Trueif the pipe applies a Hadamard across the seam.
attr
bloq.Pipe.hadamard- src¶
The source block position
(x, y, z).
attr
bloq.Pipe.src- tag¶
The pipe’s tag string, or
None.
attr
bloq.Pipe.tag
class bloq.Pipe
- class Action¶
A classical action between time steps of a block-graph program.
Construct via the staticmethod builders; most mirror BLOG action statements.
branchis the graph-dependent internal form of a named branch resolve.str(action)renders a coordinate form (with positions, not block ids).Examples
>>> from bloq import Action, Expr >>> a = Action.measure((0, 0, 0), "m") >>> a.kind, a.name ('measure', 'm') >>> let = Action.let("q", Expr.var("m1") ^ Expr.var("m2")) >>> str(let) 'q = m1 ^ m2'- static branch(target: tuple[int, int, int], condition: Expr | str) Action¶
Internal structural resolve action for an existing named branch region. Prefer BLOG
resolve <branch-name> if <condition>when authoring one.
meth
bloq.Action.branch- static discard_if(expr: Expr | str) Action¶
discard if <expr>— post-select the shot away whenexpris true.
meth
bloq.Action.discard_if-
static feedback(targets: collections.abc.Sequence[FeedbackTarget | tuple[PauliBasis | str, tuple[int, int, int]]], condition: Expr | str | None =
None) Action¶ feedback <targets> [if <condition>]— apply Pauli feedback, optionally gated. Targets acceptFeedbackTargetinstances or(pauli, pos)tuples.- Parameters:
- targets: collections.abc.Sequence[FeedbackTarget | tuple[PauliBasis | str, tuple[int, int, int]]]¶
The feedback targets;
FeedbackTargetinstances or(pauli, pos)tuples.- condition: Expr | str | None =
None¶ Optional gate expression; an
Expror a variable-namestr. Defaults toNone(always applied).
- Raises:
InvalidArgumentError – If a target’s Pauli letter is invalid.
meth
bloq.Action.feedback- static let(name: str, expr: Expr | str) Action¶
let <name> = <expr>— define a named boolean variable.
meth
bloq.Action.let- static measure(target: MeasureTarget | tuple[int, int, int], name: str) Action¶
<name> = measure <target>— bind a measurement outcome variable.- Parameters:
- target: MeasureTarget | tuple[int, int, int]¶
A
MeasureTargetor a bare(x, y, z)position (a node).- name: str¶
The outcome variable name to bind.
meth
bloq.Action.measure- static resolve(target: tuple[int, int, int], condition: Expr | str) Action¶
resolve <target> if <condition>— resolve a selective block.
meth
bloq.Action.resolve- with_shift(offset: tuple[int, int, int]) Action¶
This action with every position shifted by
offset.- Parameters:
- offset: tuple[int, int, int]¶
The
(x, y, z)amount to shift every position by.
- Raises:
InvalidArgumentError – If a shifted position exceeds the 32-bit coordinate range.
meth
bloq.Action.with_shift- branch_target¶
Internal true-arm key of a structural branch action.
attr
bloq.Action.branch_target- condition¶
the
discard ifpredicate, theresolveorbranchcondition, or the optionalfeedbackgate.- Type:
Guard expression
attr
bloq.Action.condition- expr¶
Defining expression of a
letaction.
attr
bloq.Action.expr- feedback_targets¶
Targets of a
feedbackaction.
attr
bloq.Action.feedback_targets- kind¶
"let","measure","discard_if","resolve","branch", or"feedback".
attr
bloq.Action.kind- name¶
Variable name for
letandmeasureactions.
attr
bloq.Action.name- resolve_target¶
Selective block position of a
resolveaction.
attr
bloq.Action.resolve_target- target¶
Measurement target of a
measureaction.
attr
bloq.Action.target
class bloq.Action
- class ActionDag¶
A snapshot of source action dependencies.
Returned by
BlockGraph.analyze_action_graph(), or byBlockGraph.action_graph()to inspect cached dependencies. Node ids are DAG ordinals indexingactions(). Rendering alone does not analyze the graph.- dependencies() list[tuple[int, int, str]]¶
Returns
(predecessor, consumer, reason)edges in deterministic order.Reasons are
Classical,SelectiveSupport,BranchSupport,FeedbackAnticommutation, orReadoutParity. Ordinals indexactions().
meth
bloq.ActionDag.dependencies- inputs() list[str]¶
Returns the declared external Boolean input names.
meth
bloq.ActionDag.inputs- owners() list[tuple[str, str] | None]¶
Returns
(definition_name, instance_path)ownership for each action.Paths use
__between instances and are empty for the root. Interface bindings belong to their receiving instance. Entries areNonefor standalone flat graphs without authored module ownership. The list has the same ordinal order asactions().
meth
bloq.ActionDag.owners- to_svg() str¶
Renders a self-contained SVG of the stored dependencies.
Solid arrows show variable dependencies. Dashed arrows show dependencies inferred from correlation support. Full action text and dependency reasons are retained. Rendering does not derive missing dependencies or set times.
- Raises:
BlockGraphError – If action syntax, names or dependency cycles are invalid.
meth
bloq.ActionDag.to_svg- is_analyzed¶
Whether correlation support dependencies have been analyzed. An empty action program has nothing to derive.
attr
bloq.ActionDag.is_analyzed
class bloq.ActionDag
- class Branch(name: str, condition: Expr | str, *, on_false: BranchArm, on_true: BranchArm)¶
A named branch with a corrected selector and two explicitly authored arms. Use
BlockGraph.add_branchesto attach it with its shared seams and resolve.- condition¶
Corrected expression selecting the true arm.
attr
bloq.Branch.condition- name¶
Public branch name.
attr
bloq.Branch.name- on_false¶
Geometry selected when the condition is false.
attr
bloq.Branch.on_false- on_true¶
Geometry selected when the condition is true.
attr
bloq.Branch.on_true
class bloq.Branch
-
class BranchArm(blocks: collections.abc.Sequence[Block | tuple[tuple[int, int, int], BlockKind | str]], *, pipes: collections.abc.Sequence[Pipe] | None =
None)¶ Geometry for one branch arm, using coordinates in the parent graph. Blocks accept
bloq.Blockobjects or((x, y, z), kind)pairs. Pipes usebloq.Pipeobjects. External seams are passed once toadd_branches. Geometry is checked when attached to its parent.Examples
>>> import bloq >>> arm = bloq.BranchArm([((0, 0, 1), "Z")]) >>> arm.blocks()[0].pos (0, 0, 1)
class bloq.BranchArm
- class MeasureTarget¶
The target of a measurement action: a node position, or an edge identified by its source position and direction.
Examples
>>> from bloq import MeasureTarget >>> node = MeasureTarget.node((0, 0, 0)) >>> node.is_edge False >>> edge = MeasureTarget.edge((0, 0, 0), "+Z") >>> edge.is_edge, str(edge.direction) (True, '+Z')- static edge(src: tuple[int, int, int], direction: Direction | str) MeasureTarget¶
Measurement of the pipe leaving
srcindirection.- Parameters:
- Raises:
InvalidArgumentError – If
directionis not a valid spelling.
meth
bloq.MeasureTarget.edge- static node(pos: tuple[int, int, int]) MeasureTarget¶
Measurement of the block at
pos.- Parameters:
- pos: tuple[int, int, int]¶
The
(x, y, z)block position to measure.
meth
bloq.MeasureTarget.node- direction¶
Edge direction;
Nonefor node targets.
attr
bloq.MeasureTarget.direction- is_edge¶
Truefor edge targets,Falsefor node targets.
attr
bloq.MeasureTarget.is_edge- pos¶
Node position, or edge source position.
attr
bloq.MeasureTarget.pos
class bloq.MeasureTarget
-
class FeedbackTarget(pauli: PauliBasis | str, pos: tuple[int, int, int], *, direction: Direction | str | None =
None)¶ A Pauli feedback applied to the block at
pos.The
pauliargument accepts aPauliBasisor its"X"/"Y"/"Z"letter.Examples
>>> from bloq import FeedbackTarget >>> ft = FeedbackTarget("X", (0, 0, 0)) >>> str(ft.pauli), ft.pos ('X', (0, 0, 0))- direction¶
The outgoing wire direction, or
Nonefor a node correction.
attr
bloq.FeedbackTarget.direction- pauli¶
The feedback Pauli basis.
attr
bloq.FeedbackTarget.pauli- pos¶
The target block position
(x, y, z).
attr
bloq.FeedbackTarget.pos
class bloq.FeedbackTarget
- class Expr¶
A boolean expression over measurement outcome variables.
Build with
Expr.var("m")(or pass a barestrwherever an expression is expected) and compose with^(xor),&(and),|(or),~(not) — each maps 1:1 to the BLOG expression operators. Python truth testing (bool,if,and,or,not) raisesTypeError: outcomes are unknown until execution. Use the bitwise operators to build expressions.Examples
>>> from bloq import Expr >>> e = Expr.var("m1") ^ Expr.var("m2") >>> e.kind, e.op ('binary', 'xor') >>> [str(o) for o in e.operands()] ['m1', 'm2']- static var(name: str) Expr¶
A named variable reference (a measurement outcome or
letname).- Parameters:
- name: str¶
The variable name (a measurement outcome or
letname).
meth
bloq.Expr.var- kind¶
"var","unary", or"binary".
attr
bloq.Expr.kind- name¶
Variable name (
kind == "var"only).
attr
bloq.Expr.name- op¶
Operator name —
"not","xor","and", or"or"— for unary and binary expressions.
attr
bloq.Expr.op
class bloq.Expr
- class GalleryItem(*values)¶
A built-in example block graph for analysis or compilation.
Members use the compiler’s gallery ids.
Examples
>>> import bloq >>> graph = bloq.GalleryItem.CNOT.load() >>> graph.block_count 10 >>> bloq.GalleryItem("cnot") is bloq.GalleryItem.CNOT True- load() _core.BlockGraph¶
Build this entry’s block graph.
- Returns:
A complete, valid graph for this gallery entry.
- Return type:
Examples
>>> import bloq >>> bloq.GalleryItem.BELL_STATE.load().port_count 2
meth
bloq.GalleryItem.load- source() str¶
The
.blogsource text of this entry.Examples
>>> import bloq >>> bloq.GalleryItem.CNOT.source().startswith("BLOG 1.0") True
meth
bloq.GalleryItem.source-
AND_4T =
'and_4t'¶
attr
bloq.GalleryItem.AND_4T-
BELL_STATE =
'bell_state'¶
attr
bloq.GalleryItem.BELL_STATE-
CCZ_4X3X6 =
'ccz_4x3x6'¶
attr
bloq.GalleryItem.CCZ_4X3X6-
CCZ_4X3X7_TELS =
'ccz_4x3x7_tels'¶
attr
bloq.GalleryItem.CCZ_4X3X7_TELS-
CCZ_GATE_TELEPORT =
'ccz_gate_teleport'¶
attr
bloq.GalleryItem.CCZ_GATE_TELEPORT-
CCZ_INJECTED_AND =
'ccz_injected_and'¶
attr
bloq.GalleryItem.CCZ_INJECTED_AND-
CCZ_INJECTED_MAJ =
'ccz_injected_maj'¶
attr
bloq.GalleryItem.CCZ_INJECTED_MAJ-
CNOT =
'cnot'¶
attr
bloq.GalleryItem.CNOT-
CZ_SPATIAL_H =
'cz_spatial_h'¶
attr
bloq.GalleryItem.CZ_SPATIAL_H-
CZ_TEMPORAL_H =
'cz_temporal_h'¶
attr
bloq.GalleryItem.CZ_TEMPORAL_H-
GHZ =
'ghz'¶
attr
bloq.GalleryItem.GHZ-
GHZ_PATCH_ROTATIONS =
'ghz_patch_rotations'¶
attr
bloq.GalleryItem.GHZ_PATCH_ROTATIONS-
GHZ_SLIDE_THEN_GLIDE =
'ghz_slide_then_glide'¶
attr
bloq.GalleryItem.GHZ_SLIDE_THEN_GLIDE-
MOVE_ROTATION =
'move_rotation'¶
attr
bloq.GalleryItem.MOVE_ROTATION-
ONE_D_YOKED =
'1d-yoked'¶
attr
bloq.GalleryItem.ONE_D_YOKED-
PHASE_GRADIENT =
'phase_gradient'¶
attr
bloq.GalleryItem.PHASE_GRADIENT-
STABILITY =
'stability'¶
attr
bloq.GalleryItem.STABILITY-
STEANE_ENCODING =
'steane_encoding'¶
attr
bloq.GalleryItem.STEANE_ENCODING-
S_GATE =
's_gate'¶
attr
bloq.GalleryItem.S_GATE-
TEN_BIT_ADDER =
'ten_bit_adder'¶
attr
bloq.GalleryItem.TEN_BIT_ADDER-
THREE_BIT_ADDER =
'three_bit_adder'¶
attr
bloq.GalleryItem.THREE_BIT_ADDER-
THREE_CNOTS =
'three_cnots'¶
attr
bloq.GalleryItem.THREE_CNOTS-
THTH =
'thth'¶
attr
bloq.GalleryItem.THTH-
TOFFOLI_FROM_AND_DELAYED_CZ =
'toffoli_from_and_delayed_cz'¶
attr
bloq.GalleryItem.TOFFOLI_FROM_AND_DELAYED_CZ-
T_COMPARISON =
't_comparison'¶
attr
bloq.GalleryItem.T_COMPARISON-
T_GATE =
't_gate'¶
attr
bloq.GalleryItem.T_GATE-
T_WITH_PREPARED_Y =
't_with_prepared_y'¶
attr
bloq.GalleryItem.T_WITH_PREPARED_Y-
UMA =
'uma'¶
attr
bloq.GalleryItem.UMA-
X_MEMORY =
'x_memory'¶
attr
bloq.GalleryItem.X_MEMORY-
Y_MEMORY =
'y_memory'¶
attr
bloq.GalleryItem.Y_MEMORY- property categories : tuple[str, ...]¶
The entry’s categories, for selecting a family of examples.
Examples
>>> import bloq >>> clifford = [g for g in bloq.GalleryItem if "clifford" in g.categories] >>> bloq.GalleryItem.CNOT in clifford True >>> bloq.GalleryItem.T_GATE in clifford False
prop
bloq.GalleryItem.categories- property description : str¶
The human-readable description of this entry.
Examples
>>> import bloq >>> "CNOT" in bloq.GalleryItem.CNOT.description True
prop
bloq.GalleryItem.description- property id : str¶
The gallery id string, as accepted by the CLI and
.blogtooling.Examples
>>> import bloq >>> bloq.GalleryItem.X_MEMORY.id 'x_memory'
prop
bloq.GalleryItem.id
class bloq.GalleryItem
IR programs and graph traversal¶
Bloq owns the physical program, shared templates, and nested control regions.
Loading a text or binary artifact decodes it without a full audit; call
validate() when that audit is required. Structural statistics count stored
region bodies once, independently of how many attempts execution may take.
Node IDs are local to one graph level. Preserve the owning path in walk results
when identifying nested nodes; the same numeric ID can occur in different
regions. Accessors return snapshots, including detached SubGraph values, so
changing an inspected object does not edit the original program. Use the
program’s editing methods for mutations.
- class Bloq¶
A compiled Bloq IR program: a graph of quantum, classical, and region nodes plus a shared template pool. Produced by
compile()or loaded from the.bloqir/.bloqexchange formats.Read the top graph level with
nodes/node/node_ids/edges; nested region bodies hang off theirRegionNodepayloads. Serialize withto_text/to_binary(orsaveto a path) and reload withfrom_text/from_binary(orload), picking the codec by.bloqir(text) or.bloq(binary) extension.Examples
>>> import bloq >>> program = bloq.compile(bloq.GalleryItem.X_MEMORY.load(), distance=3) >>> program <Bloq nodes=2 edges=1 qubits=17> >>> len(program.node_ids()) 2 >>> program.node(0).is_quantum() True >>> program.node(99) is None True >>> len(program.edges()) 1 >>> bloq.Bloq.from_text(program.to_text()).to_binary() == program.to_binary() True >>> bloq.Bloq.from_binary(program.to_binary()).to_binary() == program.to_binary() True- static from_binary(data: bytes) Bloq¶
Decodes a program from the binary
.bloqexchange format.- Parameters:
- data: bytes¶
The binary
.bloqartifact bytes to decode.
- Raises:
BinaryDecodeError – If the bytes are not a valid
.bloqartifact.
meth
bloq.Bloq.from_binary- static from_text(text: str) Bloq¶
Parses a program from the
.bloqirtext exchange format.- Parameters:
- text: str¶
The
.bloqirtext-format source to parse.
- Raises:
TextParseError – If the text is not a valid
.bloqirprogram.
meth
bloq.Bloq.from_text- static load(path: str | os.PathLike | pathlib.Path) Bloq¶
Loads a program from
path, picking the codec by extension (.bloqirtext,.bloqbinary).- Parameters:
- path: str | os.PathLike | pathlib.Path¶
The file path to read; its extension selects the codec.
- Raises:
InvalidArgumentError – If the extension is neither
.bloqirnor.bloq.OSError – If the file cannot be read.
TextParseError – If a
.bloqirfile fails to parse.BinaryDecodeError – If a
.bloqfile fails to decode.
meth
bloq.Bloq.load-
classical_value(node: int, *, output: ObservableOutput =
ObservableOutput.Corrected, pins: bool | None =None, forced_observables: Mapping[int, bool] | None =None) bool¶ The Boolean value fixed by the predicate assignment.
Complete observables read their assumed corrected
forced_observablesvalues; compute nodes fold their expressions over those values. A decoder flip cannot be inferred from a corrected-value assignment.- Parameters:
- node: int¶
The top-level node id to evaluate.
- output: ObservableOutput =
ObservableOutput.Corrected¶ The selected Corrected or Flip port.
- pins: bool | None =
None¶ One Boolean value for every predicate leaf.
- forced_observables: Mapping[int, bool] | None =
None¶ Observable index to assumed decoded value. Cannot be combined with
pins.
- Raises:
InvalidArgumentError – If both assignment arguments are supplied.
BloqError – If the node is missing, is quantum, is a repeat-until-success region, or reaches an unfixed value.
meth
bloq.Bloq.classical_value- data_inputs(id: int) list[ValueInput]¶
Boolean and structural composition inputs, excluding activation.
meth
bloq.Bloq.data_inputs- detector_bundle(id: int) DetectorBundle¶
A shared detector bundle by pool id.
meth
bloq.Bloq.detector_bundle- deterministic_emit_order() list[int]¶
The deterministic Kahn topological emit order. Raises
BloqErroron a cyclic (unvalidated or hand-built) graph.
meth
bloq.Bloq.deterministic_emit_order- edges() list[BloqEdgeRef]¶
Every top-level edge.
meth
bloq.Bloq.edges- edges_between(from_node: int, to_node: int) list[BloqEdgeRef]¶
Every top-level edge
from_node -> to_node.
meth
bloq.Bloq.edges_between-
emission_plan(node: int, path: collections.abc.Sequence[tuple[int, str]] | None =
None, *, noise: float | None =None) EmissionPlan¶ The node’s merged circuit plus the instance-space id maps — the per-node unit a custom backend emits. See
EmissionPlan.A minimal gate-counting backend over a program with no region nodes:
>>> import bloq >>> program = bloq.compile(bloq.GalleryItem.CNOT.load(), distance=3) >>> gates = 0 >>> for node_id in program.deterministic_emit_order(): ... plan = program.emission_plan(node_id) ... for body in range(plan.circuit.body_count): ... gates += sum(isinstance(op, bloq.ir.CircuitOp.Gate) ... for op in plan.circuit.ops(body)) >>> gates > 0 Truedeterministic_emit_order()lists only the top level, so this loop covers a program with no region nodes. A backend that must handle control flow walks every level instead — iteratewalk()(orlevels()) and pass each node’spath— because region bodies hold quantum nodes this top-level loop never reaches.- Parameters:
- node: int¶
The node’s level-local id.
- path: collections.abc.Sequence[tuple[int, str]] | None =
None¶ The node’s graph level as
(owner_node_id, body_selector)hops from the top level (aWalkNode.path).Noneor empty for a top-level node.- noise: float | None =
None¶ Uniform circuit-level noise probability in
[0, 1].None(the default) leaves the circuit noiseless. Noise is materialized after instance merging. When a moment crosses a repeat boundary, useplan.normalized_template(id) or program.template(id)for the returned plan’s side tables.
- Raises:
InvalidArgumentError – If
noiseis invalid,pathnames a missing region or body, ornodeis not at that level.BloqError – If the node’s instances do not merge.
meth
bloq.Bloq.emission_plan- flatten() None¶
Flattens every
REPEATblock in the program: each looped template is unrolled into a straight-line circuit, repeat-scoped detectors expand into one concrete detector per unrolled iteration (seeded and advanced by the loop-carried recurrence, matching Stim emission semantics), and all loop-carried detector state is cleared. Flattened templates are appended to the pool and instances re-pointed; instance-space references stay valid because each measurement’s final unrolled occurrence keeps its original id.Verify-before-apply — a failed flatten leaves the program untouched. Inserting memory rounds afterwards splices looped padding templates again; flatten again if needed.
- Raises:
BloqError – If a parity references an undefined loop state, an instance references a missing template.
meth
bloq.Bloq.flatten- has_conditional_membership() bool¶
Whether guarded quantum stages, seams, or activated quantum regions need membership selection. Classical activation alone does not require pinning.
meth
bloq.Bloq.has_conditional_membership- has_path(from_node: int, to_node: int) bool¶
Whether a directed top-level path
from_node -> to_nodeexists.
meth
bloq.Bloq.has_path- incoming(id: int) list[BloqEdgeRef]¶
The top-level edges into
id. Empty for a stale id.- Parameters:
- id: int¶
The top-level node whose incoming edges to return.
meth
bloq.Bloq.incoming-
insert_memory_rounds(from_node: int, to_node: int, rounds: int, *, path: collections.abc.Sequence[tuple[int, str]] | None =
None) int¶ Splices memory-round padding into the
Quantumedgefrom_node -> to_nodeatpath, waitingroundsrounds.Self-contained: the padding templates come from the program’s recorded edge-owned padding provenance (stamped at compile time), so a program loaded from a
.bloqneeds neither the source graph nor a recompile. Node ids held across this call must be re-resolved (id recycling).- Parameters:
- from_node: int¶
The source node id of the
Quantumedge to splice into.- to_node: int¶
The target node id of that
Quantumedge.- rounds: int¶
The number of syndrome-extraction rounds the padding waits.
- path: collections.abc.Sequence[tuple[int, str]] | None =
None¶ Optional
(region_id, body_selector)hops. Omit for a top-level edge.
- Returns:
The id of the newly spliced padding node.
- Raises:
BloqError – If the seam carries no padding provenance (hand-built program, or one compiled before provenance recording) or the splice fails.
meth
bloq.Bloq.insert_memory_rounds- insert_memory_rounds_after(node: int, rounds: int, *, path: collections.abc.Sequence[tuple[int, str]]) int¶
Appends physical memory rounds after a terminal quantum node inside a region body. Existing ordering consumers and cross-region detectors are moved to the inserted padding node.
meth
bloq.Bloq.insert_memory_rounds_after- insert_memory_rounds_batch(targets: collections.abc.Sequence[MemoryRoundTarget], rounds: int) list[int]¶
Inserts memory rounds at all targets atomically.
Targets may mix
MemoryRoundTarget.EdgeandMemoryRoundTarget.Afterat different graph levels. Returns inserted node ids in target order; each id belongs to its target’s graph level. An empty list is a no-op. Any error leaves the entire program unchanged.- Parameters:
- targets: collections.abc.Sequence[MemoryRoundTarget]¶
Edge or terminal-region targets, located before the edit.
- rounds: int¶
Number of memory rounds at every target; must be at least 1.
- Raises:
InvalidArgumentError – If a target path cannot be resolved.
BloqError – If membership is unresolved, a target or seam is invalid, or
roundsis zero.
meth
bloq.Bloq.insert_memory_rounds_batch- level_at(path: collections.abc.Sequence[tuple[int, str]]) SubGraph¶
The one graph level
pathnames, as an owned snapshot.pathis the(owner_node_id, body_selector)chainlevels()andWalkNode.pathhand out; an empty path is the top level. This is the targeted read thatlevels()is the exhaustive one of — reach for it instead of scanninglevels()for a known path, which snapshots every level to keep one.- Parameters:
- path: collections.abc.Sequence[tuple[int, str]]¶
The
(owner_id, selector)hops to the level, outermost first.
- Raises:
InvalidArgumentError – If a hop names a missing node, a non-region node, or a body selector the region does not have.
Examples
>>> import bloq >>> program = bloq.compile(bloq.GalleryItem.T_GATE.load(), distance=3) >>> rus = program.regions_of(bloq.ir.RegionKind.RepeatUntilSuccess)[0] >>> program.level_at(rus.path + [(rus.node, "body")]).node_count 5 >>> program.level_at([]).node_ids() == program.node_ids() True
meth
bloq.Bloq.level_at- levels() list[tuple[list[tuple[int, str]], SubGraph]]¶
Every graph level as
(path, level), pre-order top-down: the top level first (empty path), then each region body.pathis the(owner_node_id, body_selector)chain to the level andlevelis an ownedSubGraphsnapshot. Use this for whole-program table passes that read a level as a unit — instance collection, id allocation, per-level edge queries — rather than reacting to individual nodes (that iswalk()).Examples
>>> import bloq >>> program = bloq.compile(bloq.GalleryItem.T_GATE.load(), distance=3) >>> levels = program.levels() >>> path, top = levels[0] >>> path [] >>> top.node_ids() == program.node_ids() True >>> top.node_count == program.node_count TrueA per-level table pass. Summing each level’s node count reaches every nested node, so the total matches the full
walk():>>> sum(level.node_count for _path, level in levels) == len(program.walk()) TrueNested levels carry a non-empty path naming their owner region and body selector:
>>> len(levels) > 1 True >>> nested_paths = [path for path, _level in levels if path] >>> all( ... selector == "body" ... for path in nested_paths ... for _owner, selector in path ... ) True
meth
bloq.Bloq.levels- logical_outputs() list[LogicalOutput]¶
Complete terminal logical X/Z operators used by execution hooks.
meth
bloq.Bloq.logical_outputs- node(id: int) BloqNode | None¶
The top-level node at
id, orNonewhen the id is stale.- Parameters:
- id: int¶
The top-level node id to look up.
meth
bloq.Bloq.node- node_by_block(pos: tuple[int, int, int]) int | None¶
The top-level node fused from the source block at
pos, orNone.- Parameters:
- pos: tuple[int, int, int]¶
The
(x, y, z)source-graph block position.
Examples
>>> import bloq >>> program = bloq.compile(bloq.GalleryItem.X_MEMORY.load(), distance=3) >>> program.node_by_block((0, 0, 0)) is not None True
meth
bloq.Bloq.node_by_block- node_ids() list[int]¶
Every top-level node id, in ascending id order.
meth
bloq.Bloq.node_ids- node_measurement_count(id: int) int¶
The static measurement-site count under
id; all region bodies count.- Parameters:
- id: int¶
The node id whose measurement count to return.
- Raises:
InvalidArgumentError – If no node has the given id (stale id).
meth
bloq.Bloq.node_measurement_count- node_qubits(id: int) list[tuple[int, int]]¶
The layout qubits the node at
idoccupies, sorted by(x, y).- Parameters:
- id: int¶
The node id whose layout qubits to return.
- Raises:
InvalidArgumentError – If no node has the given id (stale id), or translating a template qubit by its instance offset exceeds the 32-bit coordinate range.
meth
bloq.Bloq.node_qubits- nodes() list[tuple[int, BloqNode]]¶
Every top-level
(id, node), in ascending id order.
meth
bloq.Bloq.nodes- open_value_producers() list[int]¶
Top-level value producers with no local consumer; independent of declared outputs.
meth
bloq.Bloq.open_value_producers- outgoing(id: int) list[BloqEdgeRef]¶
The top-level edges out of
id. Empty for a stale id.- Parameters:
- id: int¶
The top-level node whose outgoing edges to return.
meth
bloq.Bloq.outgoing- output_frames() list[FramePair]¶
Per-output terminal Pauli-frame signs, ordered by source output port.
meth
bloq.Bloq.output_frames- pin_membership(choices: Mapping[str, bool]) Bloq¶
Return a selected copy for named structural branches and selective measurements. Keys are
NodeProvenance.BranchSelector.namevalues; every selector must be pinned consistently. Guards implied by the pins are removed. The original program stays unchanged, and RUS regions retain their runtime semantics.- Raises:
BloqError – If pins are missing, unknown, inconsistent, or the program is invalid.
meth
bloq.Bloq.pin_membership- pipe_padding() list[PipePadding]¶
A flattened view of the edge-owned memory-padding provenance. Inspect
BloqEdge.Quantum.paddingwhen the owning seam matters.
meth
bloq.Bloq.pipe_padding- quantum_input(node: int) int¶
The single top-level quantum node feeding
node.
meth
bloq.Bloq.quantum_input- quantum_nodes() list[tuple[int, QuantumNode]]¶
The top level’s
(id, quantum_node)pairs in ascending id order, skipping classical and region nodes. See theSubGraphtwin for why this is not the emit order.
meth
bloq.Bloq.quantum_nodes- quantum_tail() int¶
The unique top-level quantum node with no outgoing quantum edge. Classical consumers do not disqualify it.
- Raises:
BloqError – If the level has zero or several such nodes.
meth
bloq.Bloq.quantum_tail- regions_of(kind: RegionKind) list[RegionRef]¶
Every region of
kind, at every nesting level, in level order.- Parameters:
- kind: RegionKind¶
The
RegionKindto filter by.
Examples
>>> import bloq >>> program = bloq.compile(bloq.GalleryItem.T_GATE.load(), distance=3) >>> len(program.regions_of(bloq.ir.RegionKind.RepeatUntilSuccess)) 1
meth
bloq.Bloq.regions_of-
resolve_classical(node: int, *, output: ObservableOutput =
ObservableOutput.Corrected, pins: bool | None =None, forced_observables: Mapping[int, bool] | None =None) ClassicalResolution¶ Resolves classical dataflow to its measurement parity, constant sign, and decoder observable under a fixed predicate assignment.
- Parameters:
- node: int¶
The top-level classical node id to resolve.
- output: ObservableOutput =
ObservableOutput.Corrected¶ The selected Corrected or Flip port.
- pins: bool | None =
None¶ One Boolean value for every predicate leaf.
- forced_observables: Mapping[int, bool] | None =
None¶ Observable index to assumed decoded value. Cannot be combined with
pins.
- Raises:
InvalidArgumentError – If both assignment arguments are supplied.
BloqError – If the node is missing, is not classical, or its dataflow cannot be resolved.
meth
bloq.Bloq.resolve_classical- save(path: str | os.PathLike | pathlib.Path) None¶
Saves the program to
path, picking the codec by extension (.bloqirtext,.bloqbinary).- Parameters:
- path: str | os.PathLike | pathlib.Path¶
The destination file path; its extension selects the codec.
- Raises:
InvalidArgumentError – If the extension is neither
.bloqirnor.bloq.OSError – If the file cannot be written.
Examples
>>> import bloq, tempfile, os >>> program = bloq.compile(bloq.GalleryItem.X_MEMORY.load(), distance=3) >>> directory = tempfile.mkdtemp() >>> for name in ("prog.bloqir", "prog.bloq"): ... path = os.path.join(directory, name) ... program.save(path) ... bloq.Bloq.load(path).to_binary() == program.to_binary() True True
meth
bloq.Bloq.save- selection_seams() list[tuple[int, int]]¶
The
(source, selection)quantum edges feeding top-level guarded quantum selections, in ascending target- then source-node order.Use these seams as targets for
insert_memory_rounds_batch.- Raises:
BloqError – If a selection has no quantum input edge.
meth
bloq.Bloq.selection_seams- set_template_repetitions(template: int, repetitions: int) None¶
Retunes how many times a template’s
REPEATbody runs.Templates are pooled, so this changes every instance of the template.
repetitionsis the loop’s repeat count, not a round count — the template’s straight-line prologue still runs once on top of it.
meth
bloq.Bloq.set_template_repetitions- sorted_layout_coords() list[tuple[int, int]]¶
The layout qubit coordinates, sorted by
(x, y).- Raises:
InvalidArgumentError – If translating a template qubit by its instance offset exceeds the 32-bit coordinate range.
meth
bloq.Bloq.sorted_layout_coords- stable_key_map() dict[NodeKey, int]¶
Top-level nodes with a compile-stable identity, keyed by that identity.
Use it to carry node references between two compiles of one graph — a program and its
compile_clifford_proxy, say — where ids need not agree but keys do.- Raises:
BloqError – If two nodes share a key, or the graph is cyclic.
Examples
>>> import bloq >>> program = bloq.compile(bloq.GalleryItem.X_MEMORY.load(), distance=3) >>> sorted(program.stable_key_map().values()) [0, 2]
meth
bloq.Bloq.stable_key_map- stats() BloqStats¶
Structural IR inventory across every graph level, without flattening.
Counts include every concrete node/edge type and the compiled template pool. Each stored region body contributes once. Static means no retry regions, node activation, quantum membership guards, guarded seams, or shot discard. Ordinary classical readout/decoder computation and fixed circuit repeats remain static. This does not certify validity or backend emittability. Physical qubits, gates, and measurements are not counted.
- Returns:
- Read-only structural counts;
print(program.stats()) prints a tree summary.
- Read-only structural counts;
- Return type:
- Raises:
InvalidArgumentError – If a structural count overflows.
meth
bloq.Bloq.stats- subdivide_quantum_edge(from_node: int, to_node: int, padding: collections.abc.Sequence[tuple[int, tuple[int, int]]], rounds: int) int¶
Lower-level splice for callers that already hold padding template ids. Verify-before-apply — a failed splice leaves the program untouched.
- Parameters:
- from_node: int¶
The source node id of the
Quantumedge to splice into.- to_node: int¶
The target node id of that
Quantumedge.- padding: collections.abc.Sequence[tuple[int, tuple[int, int]]]¶
The padding instances to insert, each a
(template_id, (offset_x, offset_y))pair.- rounds: int¶
The number of syndrome-extraction rounds the padding waits.
- Returns:
The id of the newly spliced padding node.
- Raises:
BloqError – If the splice fails (any
EditError).
meth
bloq.Bloq.subdivide_quantum_edge- template(id: int) Template¶
The template with the given pool id. Ids run
0..template_countand are stable for the program’s life.- Parameters:
- id: int¶
The template pool id (e.g.
TemplateInstance.template_id).
- Raises:
InvalidArgumentError – If
idis not in the pool.
meth
bloq.Bloq.template- to_binary() bytes¶
Encodes the program as a binary
.bloqartifact.
meth
bloq.Bloq.to_binary-
to_svg(*, include_classical: bool =
True) str¶ Renders the nested IR dependency graph as a self-contained SVG.
Quantum stages and region bodies are always shown. Set
include_classical=Falseto hide classical nodes and their edges. This is a structural view, not a physical circuit or timing diagram.
meth
bloq.Bloq.to_svg- to_text() str¶
Renders the program in the
.bloqirtext exchange format.
meth
bloq.Bloq.to_text- validate() None¶
Checks the program against the IR’s well-formedness rules. Raises
BloqValidationErrorwith the violated rule in the message.
meth
bloq.Bloq.validate- value_consumers(id: int) list[tuple[int, int]]¶
The consumers of
id’s Boolean outputs, as(consumer, slot).- Parameters:
- id: int¶
The producer node id whose
Valueconsumers to return.
meth
bloq.Bloq.value_consumers- value_inputs(id: int) list[ValueInput]¶
The producers wired into
id’sValueslots.- Parameters:
- id: int¶
The consumer node id whose
Valueinputs to return.
meth
bloq.Bloq.value_inputs- walk() list[WalkNode]¶
Every node at every nesting level, as
WalkNodesnapshots in pre-order depth-first order: a level’s nodes in ascending id order, descending into each region node’s bodies before moving on.This is the whole-program counterpart to
nodes()(top level only). It eagerly visits every node, including those in region bodies; there is no pruning or early exit — filter or slice the returned list yourself. The ordering is the deterministic scan order (ascending id per level), not the execution schedule; usedeterministic_emit_order()per level for that.Each
WalkNodecarries the node’spath(the(owner_id, selector)hops to its level, empty at the top level), its level-localid, and an ownednodesnapshot. Dispatch on what a node computes withisinstanceoverw.node.kind(BloqNodeKind.Quantum/Classical/Region).Examples
>>> import bloq >>> program = bloq.compile(bloq.GalleryItem.T_GATE.load(), distance=3) >>> walk = program.walk()The top-level nodes come out first, in node-id order; the walk then descends into region bodies, so it visits more than the top level:
>>> [w.id for w in walk if not w.path] == program.node_ids() True >>> len(walk) > program.node_count TrueDispatch on what each node computes. The magic-state (
T) program lowers to nested control-flow regions, so the walk sees region nodes:>>> tally = {"quantum": 0, "classical": 0, "region": 0} >>> for w in walk: ... kind = w.node.kind ... if isinstance(kind, bloq.ir.BloqNodeKind.Quantum): ... tally["quantum"] += 1 ... elif isinstance(kind, bloq.ir.BloqNodeKind.Classical): ... tally["classical"] += 1 ... elif isinstance(kind, bloq.ir.BloqNodeKind.Region): ... tally["region"] += 1 >>> sum(tally.values()) == len(walk) True >>> tally["region"] > 0 TrueA node inside a region body has a non-empty
path; itstop_level_ancestor()is the outermost enclosing region’s id — and because ids are level-local, that is the only piece of a nested node’sid/paththat is a valid top-levelnode()lookup key:>>> nested = next(w for w in walk if w.path) >>> nested.path[0][1] == "body" True >>> program.node(nested.top_level_ancestor()).is_region() True
meth
bloq.Bloq.walk- boundary_outputs¶
Declared Observable or region ids exporting boundary bindings.
attr
bloq.Bloq.boundary_outputs- detector_bundle_count¶
Number of shared detector bundles in the program pool.
attr
bloq.Bloq.detector_bundle_count- edge_count¶
The number of top-level edges. Use
stats()for all levels.
attr
bloq.Bloq.edge_count- measurement_count¶
The number of static measurement sites. Both branch arms count.
attr
bloq.Bloq.measurement_count- metadata¶
Namespaced producer metadata. Values are Python
intorstrscalars.
attr
bloq.Bloq.metadata- node_count¶
The number of top-level nodes. Use
stats()for all levels.
attr
bloq.Bloq.node_count- quantum_node_count¶
The number of top-level quantum nodes (excludes classical/region).
attr
bloq.Bloq.quantum_node_count- qubit_count¶
The number of distinct layout qubits the program touches.
- Raises:
InvalidArgumentError – If translating a template qubit by its instance offset exceeds the 32-bit coordinate range.
attr
bloq.Bloq.qubit_count- template_count¶
The number of templates in the program’s shared pool.
attr
bloq.Bloq.template_count- value_output¶
The declared Boolean source, or None for constant false.
attr
bloq.Bloq.value_output
class bloq.Bloq
- class BloqStats¶
Structural counts over every level of stored Bloq IR, returned by
Bloq.stats. Physical circuit payloads and runtime executions are not counted.- edge_count¶
Edges at every graph level.
attr
bloq.ir.BloqStats.edge_count- edge_counts¶
Edge counts by Quantum, Value, and Order, including zero-count types.
attr
bloq.ir.BloqStats.edge_counts- is_static¶
no retry regions, node activation, membership guards, guarded seams, or Discard. Ordinary classical readout/decoder computation and fixed circuit repeats remain static. This does not certify validity or backend emittability.
- Type:
Fixed IR execution structure
attr
bloq.ir.BloqStats.is_static- node_count¶
Nodes at every graph level, including region containers.
attr
bloq.ir.BloqStats.node_count- node_counts¶
Node counts by concrete variant, including zero-count types.
attr
bloq.ir.BloqStats.node_counts- template_count¶
Templates in the compiled pool, including unreferenced templates.
attr
bloq.ir.BloqStats.template_count
class bloq.ir.BloqStats
- class SubGraph¶
A nested region body: a graph level with the same read accessors as the top level. Snapshot semantics — this is an owned copy, detached from the program it was read from.
- data_inputs(id: int) list[ValueInput]¶
Boolean and structural composition inputs, excluding activation.
meth
bloq.ir.SubGraph.data_inputs- deterministic_emit_order() list[int]¶
The level’s deterministic Kahn topological order. Raises
BloqErroron a cyclic graph.
meth
bloq.ir.SubGraph.deterministic_emit_order- edges() list[BloqEdgeRef]¶
Every edge at this level.
meth
bloq.ir.SubGraph.edges- edges_between(from_node: int, to_node: int) list[BloqEdgeRef]¶
Every edge
from_node -> to_node(parallel edges are legal).
meth
bloq.ir.SubGraph.edges_between- has_path(from_node: int, to_node: int) bool¶
Whether a directed path
from_node -> to_nodeexists (any edge kind).
meth
bloq.ir.SubGraph.has_path- incoming(id: int) list[BloqEdgeRef]¶
The edges into
id. Empty for a stale id.- Parameters:
- id: int¶
The node whose incoming edges to return.
meth
bloq.ir.SubGraph.incoming- node(id: int) BloqNode | None¶
The node at
id, orNonewhen the id is stale.- Parameters:
- id: int¶
The node id to look up at this level.
meth
bloq.ir.SubGraph.node- node_ids() list[int]¶
Every node id at this level, in ascending id order.
meth
bloq.ir.SubGraph.node_ids- nodes() list[tuple[int, BloqNode]]¶
Every
(id, node)at this level, in ascending id order.
meth
bloq.ir.SubGraph.nodes- open_value_producers() list[int]¶
Value producers with no local consumer; independent of declared outputs.
meth
bloq.ir.SubGraph.open_value_producers- outgoing(id: int) list[BloqEdgeRef]¶
The edges out of
id. Empty for a stale id.- Parameters:
- id: int¶
The node whose outgoing edges to return.
meth
bloq.ir.SubGraph.outgoing- quantum_input(node: int) int¶
The single quantum node at this level feeding
node.
meth
bloq.ir.SubGraph.quantum_input- quantum_nodes() list[tuple[int, QuantumNode]]¶
This level’s
(id, quantum_node)pairs in ascending id order (likenodes()), skipping classical and region nodes.Ascending id, not emit order: a caller appending circuit text wants
deterministic_emit_order()filtered byis_quantum()instead.Examples
>>> import bloq >>> program = bloq.compile(bloq.GalleryItem.T_GATE.load(), distance=3) >>> rus = next(n for _, n in program.nodes() if n.is_region()) >>> len(rus.region.body.quantum_nodes()) 2
meth
bloq.ir.SubGraph.quantum_nodes- quantum_tail() int¶
The unique quantum tail at this level: a quantum node with no outgoing quantum edge. Classical consumers do not disqualify it.
- Raises:
BloqError – If the level has zero or several such nodes.
meth
bloq.ir.SubGraph.quantum_tail- stable_keys() list[tuple[NodeKey, int]]¶
Every
(key, id)at this level, keyed by compile-stable identity.- Raises:
BloqError – If the level is cyclic.
meth
bloq.ir.SubGraph.stable_keys- value_consumers(id: int) list[tuple[int, int]]¶
The consumers of
id’s Boolean outputs, as(consumer, slot).- Parameters:
- id: int¶
The producer node id whose
Valueconsumers to return.
meth
bloq.ir.SubGraph.value_consumers- value_inputs(id: int) list[ValueInput]¶
The producers wired into
id’sValueslots.- Parameters:
- id: int¶
The consumer node id whose
Valueinputs to return.
meth
bloq.ir.SubGraph.value_inputs- boundary_outputs¶
Declared Observable or region ids exporting boundary bindings.
attr
bloq.ir.SubGraph.boundary_outputs- edge_count¶
The number of edges at this level.
attr
bloq.ir.SubGraph.edge_count- node_count¶
The number of nodes at this level.
attr
bloq.ir.SubGraph.node_count- value_output¶
The declared Boolean source, or None for constant false.
attr
bloq.ir.SubGraph.value_output
class bloq.ir.SubGraph
- class BloqNode¶
A graph node:
kindis what it computes,provenanceis why it exists.kindis aBloqNodeKind(quantum, classical, or region); theis_quantum/is_classical/is_regionpredicates dispatch on it without anisinstancecheck.provenancerecords the source-level construct the node realizes.Examples
>>> import bloq >>> program = bloq.compile(bloq.GalleryItem.CNOT.load(), distance=3) >>> node = program.node(0) >>> node.is_quantum(), node.is_classical() (True, False)- block_members() list[tuple[int, int, int]]¶
The source block positions this node was fused from (empty unless the provenance is
BlockComponent).
meth
bloq.ir.BloqNode.block_members- is_classical() bool¶
Trueif the node carries classical dataflow.
meth
bloq.ir.BloqNode.is_classical- is_quantum() bool¶
Trueif the node carries a quantum payload.
meth
bloq.ir.BloqNode.is_quantum- is_region() bool¶
Trueif the node is a control-flow region.
meth
bloq.ir.BloqNode.is_region- memory_rounds() int | None¶
The spliced node’s wait duration in whole syndrome-extraction rounds, or
Nonefor anything other than memory padding.
meth
bloq.ir.BloqNode.memory_rounds- activation¶
Optional activation Value slot. A false activation skips this value or region.
attr
bloq.ir.BloqNode.activation- classical¶
The node’s classical payload, or
Nonefor a quantum or region node.
attr
bloq.ir.BloqNode.classical- kind¶
What the node computes (quantum, classical, or region).
attr
bloq.ir.BloqNode.kind- provenance¶
Why the node exists — its source-level origin.
attr
bloq.ir.BloqNode.provenance- quantum¶
The node’s quantum payload, or
Nonefor a classical or region node.The unwrapping twin of
is_quantum(): reaching a payload throughkindotherwise costs anisinstancenarrowing plus an attribute whose name differs by variant.Examples
>>> import bloq >>> program = bloq.compile(bloq.GalleryItem.X_MEMORY.load(), distance=3) >>> len(program.node(0).quantum.instances) 1 >>> program.node(0).region is None True
attr
bloq.ir.BloqNode.quantum- region¶
The node’s region payload, or
Nonefor a quantum or classical node.Pair with
RegionNode.kindto select one region variant in a single test:>>> import bloq >>> program = bloq.compile(bloq.GalleryItem.T_GATE.load(), distance=3) >>> bodies = [ ... node.region.body ... for _, node in program.nodes() ... if node.region is not None ... and node.region.kind is bloq.ir.RegionKind.RepeatUntilSuccess ... ] >>> len(bodies) 1
attr
bloq.ir.BloqNode.region- stable_key¶
The node’s compile-stable identity, or
Nonefor a provenance that carries none. Computed when the snapshot is taken, because the snapshot does not keep the node it was taken from.
attr
bloq.ir.BloqNode.stable_key
class bloq.ir.BloqNode
- class BloqNodeKind¶
What a
BloqNodecomputes: a quantum payload, classical dataflow, or a control-flow region. Read fromBloqNode.kindand dispatch withisinstance(BloqNodeKind.Quantum/Classical/Region); each variant carries its payload. TheBloqNodepredicatesis_quantum()/is_classical()/is_region()test the variant without anisinstancecheck.- class Classical(node)¶
- node¶
attr
bloq.ir.BloqNodeKind.Classical.node
class
bloq.ir.BloqNodeKind.Classical
class bloq.ir.BloqNodeKind
- class BloqEdge¶
A graph edge payload: a quantum face seam, a classical-bit dataflow dependency, or a payload-free ordering constraint.
- class Compose(slot, role)¶
Structural recipe composition, including raw parity and boundary bindings.
- role¶
attr
bloq.ir.BloqEdge.Compose.role- slot¶
attr
bloq.ir.BloqEdge.Compose.slot
class
bloq.ir.BloqEdge.Compose- class Order¶
Pure sequencing, no data.
class
bloq.ir.BloqEdge.Order
class bloq.ir.BloqEdge
- class BloqEdgeRef¶
One graph edge with its endpoint node ids — the element type of
Bloq.edges(),incoming(),outgoing(), andedges_between()(and theirSubGraphtwins).sourceandtargetare level-local node ids;edgeis theBloqEdgepayload (dispatch on its variant withisinstance).- is_compose() bool¶
Trueif this structurally composes an observable recipe.
meth
bloq.ir.BloqEdgeRef.is_compose- is_order() bool¶
Trueif this is a payload-free ordering constraint.
meth
bloq.ir.BloqEdgeRef.is_order- is_quantum() bool¶
Trueif this is a quantum face seam.Filtering an edge list by payload variant is the common read; the predicates spell it without an
isinstanceonedge.Examples
>>> import bloq >>> program = bloq.compile(bloq.GalleryItem.CNOT.load(), distance=3) >>> [e.source for e in program.incoming(3) if e.is_quantum()] [2]
meth
bloq.ir.BloqEdgeRef.is_quantum- is_value() bool¶
Trueif this is a classical-bit dataflow dependency.
meth
bloq.ir.BloqEdgeRef.is_value- edge¶
The edge payload.
attr
bloq.ir.BloqEdgeRef.edge- source¶
The source node id.
attr
bloq.ir.BloqEdgeRef.source- target¶
The target node id.
attr
bloq.ir.BloqEdgeRef.target
class bloq.ir.BloqEdgeRef
- class WalkNode¶
One node visited by
Bloq.walk(): its owner path, its level-local id, and an owned snapshot of the node itself.A node id is only unique within its graph level, so
idalone does not address a node inside a region body. Thepath— the chain of(owner_node_id, body_selector)hops from the top level down to this node’s level, outermost first — is the missing half of a program-unique address. Each hop is a 2-tuple(owner_id, selector)whereselectoris"body";pathis empty for a top-level node.- Getters:
path: the(owner_id, selector)hops to this node’s level.id: the node’s level-local id.node: an ownedBloqNodesnapshot (dispatch onnode.kind).
Method
top_level_ancestor()returns the outermost enclosing region’s id for a nested node, else the node’s ownid. Because ids are level-local, it is the only piece of a nested node’sid/paththat keys a top-levelBloq.node()lookup.Examples
>>> import bloq >>> program = bloq.compile(bloq.GalleryItem.T_GATE.load(), distance=3) >>> walk = program.walk()The first visited node is the first top-level node — empty path, and
top_level_ancestor()is just its own id:>>> top = walk[0] >>> top.path [] >>> top.id 0 >>> top.top_level_ancestor() 0A node inside a region body carries a non-empty path; its
top_level_ancestor()resolves to the enclosing region at the top level, whichBloq.node()can look up (a body-localidcannot):>>> nested = next(w for w in walk if w.path) >>> owner_id, selector = nested.path[0] >>> selector == "body" True >>> nested.top_level_ancestor() == owner_id True >>> program.node(nested.top_level_ancestor()).is_region() True- top_level_ancestor() int¶
The top-level ancestor node id: the outermost enclosing region’s id for a node inside a region body, else this node’s own
id.Because ids are level-local, this is the only piece of
id/paththat is a valid top-levelBloq.node()lookup key.
meth
bloq.ir.WalkNode.top_level_ancestor- id¶
The node’s level-local id (unique only within its level).
attr
bloq.ir.WalkNode.id- node¶
An owned snapshot of the visited node.
attr
bloq.ir.WalkNode.node- path¶
The
(owner_node_id, body_selector)hops from the top level down to this node’s level, outermost first. Empty for a top-level node; the selector is"body".
attr
bloq.ir.WalkNode.path
class bloq.ir.WalkNode
- class NodeKey¶
A compile-stable node identity.
Two programs compiled from the same block graph — including a
compile_clifford_proxyof it — give corresponding nodes equal keys, even though theirBloqNodeId`s need not match. That makes a key the right thing to carry across compiles, and the right thing to use as a `dictkey;Bloq.stable_key_map()turns keys back into ids for one program.Opaque by design: compare, hash, and print them, but do not parse
str().Examples
>>> import bloq >>> graph = bloq.GalleryItem.X_MEMORY.load() >>> keys = bloq.compile(graph, distance=3).stable_key_map() >>> proxy = bloq.compile_clifford_proxy(graph, [], distance=3).stable_key_map() >>> set(keys) & set(proxy) != set() True- ordinal¶
Tie-break index among nodes that would otherwise share a key, assigned in deterministic emit order.
0unless the program has such a clash.
attr
bloq.ir.NodeKey.ordinal
class bloq.ir.NodeKey
Quantum circuits and templates¶
A Template stores a reusable physical circuit; a TemplateInstance gives
it a program-wide identity and layout offset. A QuantumNode groups placed
instances, detectors, and restart syndromes. Template-local coordinates and
measurement indices acquire their instance meaning only after placement.
QuantumGuard selects conditional membership. TemporalPipe and
PipeSeam describe the quantum connection between stages, including recorded
memory padding. Custom backends must account for guards and seams as well as the
operations in each template.
- class Circuit¶
A physical circuit in qubit-coordinate space: the entry body plus any repeat bodies referenced by
CircuitOp.Repeat, and a measurement registry giving every measurement a stable id independent of qubit order.Read-only snapshot; only the compiler constructs circuits.
Examples
>>> import bloq >>> program = bloq.compile(bloq.GalleryItem.CNOT.load(), distance=3) >>> circuit = program.template(0).circuit >>> circuit.num_qubits == len(circuit.qubits()) True >>> all(record_id < circuit.num_measurements ... for record_id, _qubit in circuit.measurement_records()) True- expanded_measurement_columns() ExpandedMeasurementColumns¶
Where each measurement id lands once every
REPEATblock is unrolled.A measurement inside a repeat body occupies one column per iteration; the reported column is its last occurrence, which is the one a
rec[-k]lookback taken after the loop refers to.- Raises:
InvalidArgumentError – If the circuit references an unknown measurement id or body, or the unrolled column count overflows.
Examples
>>> import bloq >>> program = bloq.compile(bloq.GalleryItem.X_MEMORY.load(), distance=3) >>> node = program.deterministic_emit_order()[0] >>> columns = program.emission_plan(node).circuit.expanded_measurement_columns() >>> columns.count >= len(columns) True
meth
bloq.ir.Circuit.expanded_measurement_columns- measurement_records() list[tuple[int, tuple[int, int]]]¶
Every
(measurement_id, qubit)record, sorted by id.
meth
bloq.ir.Circuit.measurement_records-
ops(body: int | None =
None) list[CircuitOp]¶ The ops of one body, in program order. Defaults to the entry body; pass a
CircuitOp.Repeat’sbodyid to read a repeat body.- Raises:
InvalidArgumentError – If
bodyis not an allocated body id.
meth
bloq.ir.Circuit.ops- qubits() list[tuple[int, int]]¶
Every qubit coordinate touched by any body or measurement record, sorted.
meth
bloq.ir.Circuit.qubits- body_count¶
The number of circuit bodies, including repeat bodies.
attr
bloq.ir.Circuit.body_count- entry_body¶
The id of the top-level (entry) body.
attr
bloq.ir.Circuit.entry_body- num_measurements¶
The number of measurement records in the circuit.
attr
bloq.ir.Circuit.num_measurements- num_qubits¶
The number of distinct qubit coordinates in the circuit.
attr
bloq.ir.Circuit.num_qubits
class bloq.ir.Circuit
- class CircuitOp¶
One operation of a
Circuitbody. Dispatch withisinstanceover the variant classes (CircuitOp.Gate,CircuitOp.Measure,CircuitOp.Mpp,CircuitOp.Tick,CircuitOp.Repeat,CircuitOp.ConditionalPauli). Noise operations useCircuitOp.Depolarize1,CircuitOp.Depolarize2, andCircuitOp.PauliError.Examples
>>> import bloq >>> program = bloq.compile(bloq.GalleryItem.CNOT.load(), distance=3) >>> ops = program.template(0).circuit.ops() >>> isinstance(ops[0], (bloq.ir.CircuitOp.Gate, bloq.ir.CircuitOp.Measure, ... bloq.ir.CircuitOp.Mpp, bloq.ir.CircuitOp.Tick, ... bloq.ir.CircuitOp.Repeat, bloq.ir.CircuitOp.ConditionalPauli, ... bloq.ir.CircuitOp.Depolarize1, bloq.ir.CircuitOp.Depolarize2, ... bloq.ir.CircuitOp.PauliError)) True- class ConditionalPauli(corrections)¶
Physically applied Pauli feedforward: each correction applies its Pauli to its target iff its control bit is 1.
- corrections¶
attr
bloq.ir.CircuitOp.ConditionalPauli.corrections
class
bloq.ir.CircuitOp.ConditionalPauli- class Depolarize1(probability, qubits)¶
Single-qubit depolarizing noise on each target.
- probability¶
attr
bloq.ir.CircuitOp.Depolarize1.probability- qubits¶
attr
bloq.ir.CircuitOp.Depolarize1.qubits
class
bloq.ir.CircuitOp.Depolarize1- class Depolarize2(probability, qubits)¶
Two-qubit depolarizing noise on consecutive target pairs.
- probability¶
attr
bloq.ir.CircuitOp.Depolarize2.probability- qubits¶
attr
bloq.ir.CircuitOp.Depolarize2.qubits
class
bloq.ir.CircuitOp.Depolarize2- class Gate(gate, qubits)¶
A gate applied to its target qubits (pairwise for two-qubit gates).
gateis the Stim-style gate name (e.g."H","CX","R").- gate¶
attr
bloq.ir.CircuitOp.Gate.gate- qubits¶
attr
bloq.ir.CircuitOp.Gate.qubits
class
bloq.ir.CircuitOp.Gate- class Measure(basis, qubits, measurements, flip_probability)¶
A single-qubit measurement of each qubit in
basis;measurements[i]is the record id produced forqubits[i].- basis¶
attr
bloq.ir.CircuitOp.Measure.basis- flip_probability¶
attr
bloq.ir.CircuitOp.Measure.flip_probability- measurements¶
attr
bloq.ir.CircuitOp.Measure.measurements- qubits¶
attr
bloq.ir.CircuitOp.Measure.qubits
class
bloq.ir.CircuitOp.Measure- class Mpp(products, measurements)¶
A multi-Pauli-product measurement (Stim
MPP):products[i]is one jointly measured Pauli-product group as(qubit, pauli)pairs, and produces recordmeasurements[i].- measurements¶
attr
bloq.ir.CircuitOp.Mpp.measurements- products¶
attr
bloq.ir.CircuitOp.Mpp.products
class
bloq.ir.CircuitOp.Mpp- class PauliError(probability, pauli, qubits)¶
A fixed-axis Pauli error on each target.
- pauli¶
attr
bloq.ir.CircuitOp.PauliError.pauli- probability¶
attr
bloq.ir.CircuitOp.PauliError.probability- qubits¶
attr
bloq.ir.CircuitOp.PauliError.qubits
class
bloq.ir.CircuitOp.PauliError- class Repeat(body, repetitions)¶
Executes body
bodyrepetitionstimes (StimREPEAT).- body¶
attr
bloq.ir.CircuitOp.Repeat.body- repetitions¶
attr
bloq.ir.CircuitOp.Repeat.repetitions
class
bloq.ir.CircuitOp.Repeat- class Tick¶
A moment barrier separating parallel operations.
class
bloq.ir.CircuitOp.Tick
class bloq.ir.CircuitOp
- class ConditionalCorrection¶
One feedforward correction inside a
CircuitOp.ConditionalPauli: applypaulitotargetiff thecontrolbit is 1.- control¶
The measurement id whose outcome gates the correction.
attr
bloq.ir.ConditionalCorrection.control- pauli¶
The Pauli axis to apply.
attr
bloq.ir.ConditionalCorrection.pauli- target¶
The qubit coordinate the correction targets.
attr
bloq.ir.ConditionalCorrection.target
class bloq.ir.ConditionalCorrection
- class ExpandedMeasurementColumns¶
Post-unroll measurement columns of one circuit, from
Circuit.expanded_measurement_columns().Behaves like a read-only mapping from measurement id to column:
len(),in,[], anddict(...)all work.countis the total number of columns the unrolled circuit produces, which is larger thanlen()whenever a measurement repeats.- as_dict() dict[int, int]¶
Every
measurement id -> columnpair.
meth
bloq.ir.ExpandedMeasurementColumns.as_dict- column(measurement: int) int | None¶
The column of
measurement, orNoneif the circuit has no such measurement.- Parameters:
- measurement: int¶
The pre-unroll measurement id.
meth
bloq.ir.ExpandedMeasurementColumns.column- count¶
The total number of measurement columns after unrolling.
attr
bloq.ir.ExpandedMeasurementColumns.count
class bloq.ir.ExpandedMeasurementColumns
- class Template¶
A reusable circuit template plus its template-local side tables. Pooled and shared by template id; a
TemplateInstanceplaces one at a layout offset.Examples
>>> import bloq >>> program = bloq.compile(bloq.GalleryItem.CNOT.load(), distance=3) >>> template = program.template(0) >>> len(template.qubits()) > 0 True >>> all(m < template.circuit.num_measurements ... for d in template.detectors for m in d.parity.measurements) True- qubits() list[tuple[int, int]]¶
The template circuit’s qubit coordinates, sorted.
meth
bloq.ir.Template.qubits- boundary_flows¶
Residual open stabilizer flows used to close circuit frontiers.
attr
bloq.ir.Template.boundary_flows- circuit¶
The template’s circuit, in template-local qubit coordinates.
attr
bloq.ir.Template.circuit- detectors¶
Detectors closing entirely within this template.
attr
bloq.ir.Template.detectors- repeat_states¶
Loop-carried detector recurrences for the circuit’s repeat bodies.
attr
bloq.ir.Template.repeat_states- restarts¶
Restart syndromes for RUS regions instantiating this template.
attr
bloq.ir.Template.restarts
class bloq.ir.Template
- class TemplateInstance¶
One placement of a pooled template, listed in
QuantumNode.instances: templatetemplate_id(look it up withBloq.template()) laid down at layout offsetoffset, under instance idid. Instance ids are program-unique — unlike node ids, they are not level-local — and instance-space references such asInstanceMeasurementkey on them.- id¶
The program-unique instance id.
attr
bloq.ir.TemplateInstance.id- offset¶
Layout offset
(x, y).
attr
bloq.ir.TemplateInstance.offset- provenance¶
Compiler provenance for source and spatial-Port-derived instances.
attr
bloq.ir.TemplateInstance.provenance- template_id¶
The id of the backing template in the shared pool.
attr
bloq.ir.TemplateInstance.template_id
class bloq.ir.TemplateInstance
- class QuantumNode¶
A template-backed quantum node’s payload, reached via
BloqNodeKind.QuantumorBloq.quantum_nodes(). Its operations come from instantiating the templates itsinstancesplace from the program’s shared pool (Bloq.template()), not from any per-node circuit.detectorsandrestartsare the node’s instance-space side tables for chains that close across its instances.- detector_bundles¶
Shared detector bundle uses, in row order after inline detectors.
attr
bloq.ir.QuantumNode.detector_bundles- detectors¶
Inline node detectors; shared rows are in
detector_bundles.
attr
bloq.ir.QuantumNode.detectors- guards¶
Conditional registrations. Entries absent from these guards are common.
attr
bloq.ir.QuantumNode.guards- instances¶
The template instances this node places.
attr
bloq.ir.QuantumNode.instances- restarts¶
The node’s repeat-until-success restart syndromes.
attr
bloq.ir.QuantumNode.restarts- timeline¶
Cumulative circuit-round ends from the minimum source z, if present.
attr
bloq.ir.QuantumNode.timeline
class bloq.ir.QuantumNode
- class QuantumGuard¶
Members and side-table entries registered when one Boolean input is true.
- detector_bundles¶
Indices in the owning node’s shared detector bundle use table.
attr
bloq.ir.QuantumGuard.detector_bundles- detector_parities¶
Conditional XOR contributions to detector rows.
attr
bloq.ir.QuantumGuard.detector_parities- detectors¶
Indexes in the owning node’s detector table.
attr
bloq.ir.QuantumGuard.detectors- input¶
The owning quantum node’s selector Value input slot.
attr
bloq.ir.QuantumGuard.input- instances¶
Program-global instance ids selected by this guard.
attr
bloq.ir.QuantumGuard.instances- restart_parities¶
Conditional XOR contributions to restart rows.
attr
bloq.ir.QuantumGuard.restart_parities- restarts¶
Indexes in the owning node’s restart table.
attr
bloq.ir.QuantumGuard.restarts
class bloq.ir.QuantumGuard
- class TemporalPipe¶
A temporal pipe between two source blocks, named by its endpoint lattice positions. Appears on
BloqEdge.Quantumedges as the seam’s connecting pipes, and inNodeProvenance.TemporalPipe/MemoryPaddingstamps.srcanddstare the endpoint block positions(x, y, z);hadamardmarks a basis-swapping (H) pipe.- dst¶
Destination block position
(x, y, z).
attr
bloq.ir.TemporalPipe.dst- hadamard¶
Trueif the seam applies a Hadamard.
attr
bloq.ir.TemporalPipe.hadamard- src¶
Source block position
(x, y, z).
attr
bloq.ir.TemporalPipe.src
class bloq.ir.TemporalPipe
- class PipeSeam¶
One temporal pipe on a
BloqEdge.Quantumseam, with the memory-padding templates the compiler recorded for it.paddingisNoneon a hand-built or synthetic edge, and on a pipe whose patch the compiler could not resolve;Bloq.insert_memory_roundsneeds every pipe on a seam padded.- padding¶
The pipe’s memory-padding provenance, when the compiler recorded it.
attr
bloq.ir.PipeSeam.padding- pipe¶
The seam’s temporal pipe.
attr
bloq.ir.PipeSeam.pipe
class bloq.ir.PipeSeam
- class PipePadding¶
Memory-padding provenance for one temporal pipe on one quantum edge: the precompiled memory-round templates
Bloq.insert_memory_roundsinstantiates and their layout offset. The owningBloqEdge.Quantumdistinguishes temporal-Hadamard sides and may describe a region terminal. Template ids are validBloq.template()/Bloq.subdivide_quantum_edgeinputs.- looped¶
Template implementing
1 + rrounds via aREPEAT rbody.
attr
bloq.ir.PipePadding.looped- offset¶
Layout offset positioning the templates over the pipe’s cross-section.
attr
bloq.ir.PipePadding.offset- one_round¶
Template implementing exactly one memory round.
attr
bloq.ir.PipePadding.one_round
class bloq.ir.PipePadding
- class MemoryRoundTarget¶
A level-qualified target for atomic memory-round insertion.
Edge(from_node, to_node, path=None)splits a quantum edge.After(node, path=...)appends after a region body’s terminal quantum node. Paths use(region_node_id, "body")hops, as returned bywalk().
class bloq.ir.MemoryRoundTarget
Classical control and readouts¶
Value edges connect a producer’s result to a consumer’s input slot. A
ClassicalExpr refers to those slots, not directly to arbitrary graph node IDs.
Keep operand order and parity constants when interpreting expressions.
Regions contain nested graphs and export values across their boundaries. A
retry region exports the accepted attempt’s results. Activation decides whether
a node runs; it is separate from the expression’s parity inputs. ValueRole
records how dependencies participate in corrected readouts and feedback.
- class ClassicalNode¶
A classical-dataflow payload. Observable fragments compose through
Composeedges; indexed observables expose decoder-backedCorrectedandFlipports.- class Compute(expr)¶
Combinational logic over incoming value slots.
- expr¶
attr
bloq.ir.ClassicalNode.Compute.expr
class
bloq.ir.ClassicalNode.Compute- class Discard(condition)¶
Rejects the shot when the condition holds.
- condition¶
attr
bloq.ir.ClassicalNode.Discard.condition
class
bloq.ir.ClassicalNode.Discard- class Observable(index, measurements, operators)¶
A parity and boundary recipe.
Noneidentifies an unindexed fragment; an index identifies a complete logical readout and its decoder query.- index¶
attr
bloq.ir.ClassicalNode.Observable.index- measurements¶
attr
bloq.ir.ClassicalNode.Observable.measurements- operators¶
attr
bloq.ir.ClassicalNode.Observable.operators
class
bloq.ir.ClassicalNode.Observable
class bloq.ir.ClassicalNode
- class ClassicalExpr¶
Combinational logic over a classical node’s incoming
Valueedges: an expression tree walked viakind+operands().In(slot)reads the producer wired into that slot. Python truth testing raisesTypeError; useBloq.classical_valueto evaluate a node with explicit inputs.- is_linear() bool¶
Whether the expression is XOR-expressible (
in/const/not/xor/parityonly) — a static backend can fold a linear condition.
meth
bloq.ir.ClassicalExpr.is_linear- operands() list[ClassicalExpr]¶
Ordered children: one for
not, any number forxor/and/or, and(condition, when_false, when_true)forselect. Leaves and compactparityhave none; read the latter’sinput_slotsandvalueinstead.
meth
bloq.ir.ClassicalExpr.operands- input_slots¶
Ordered input slots for a compact
parity, elseNone.
attr
bloq.ir.ClassicalExpr.input_slots- kind¶
"in","const","not","xor","and","or","select","parity".- Type:
The variant name
attr
bloq.ir.ClassicalExpr.kind- slot¶
The input slot for an
inexpression, elseNone.
attr
bloq.ir.ClassicalExpr.slot- value¶
The constant bit for a
constorparityexpression, elseNone.
attr
bloq.ir.ClassicalExpr.value
class bloq.ir.ClassicalExpr
- class ClassicalResolution¶
What a classical node computes under a fixed predicate assignment.
measurementsis the set of measurement sites XORed together (already cancelled: a site reached an even number of times is absent),signthe constant the parity is XORed with, anddecoder_observablesthe exact decoder-flip basis XORed into the value.The sites are
(instance_id, template_measurement_id)pairs rather thanInstanceMeasurementrecords: a resolution exists to be looked up inEmissionPlan.measurementsandPlanStim.measurement_columns, which are keyed that way, so the recipe indexes the column maps with no conversion.- decoder_observables¶
Decoder observable ids XORed into the value, in ascending order.
attr
bloq.ir.ClassicalResolution.decoder_observables- measurements¶
The measurement sites XORed into the value, in ascending order, as
(instance_id, template_measurement_id)pairs.
attr
bloq.ir.ClassicalResolution.measurements- sign¶
The constant XORed into the parity.
attr
bloq.ir.ClassicalResolution.sign
class bloq.ir.ClassicalResolution
- class ValueInput¶
A Boolean or composition input, returned by
value_inputs()ordata_inputs(). The producer feeds the consumer’s slot with the selected output and role;output=Noneidentifies structural recipe composition.- output¶
Selected Boolean output;
Nonedenotes structural composition.
attr
bloq.ir.ValueInput.output- producer¶
The producer node id.
attr
bloq.ir.ValueInput.producer- role¶
The input edge’s role.
attr
bloq.ir.ValueInput.role- slot¶
The consumer input slot this producer feeds.
attr
bloq.ir.ValueInput.slot
class bloq.ir.ValueInput
- class ObservableOutput¶
Selects an observable’s corrected parity or its decoder flip estimate.
-
Corrected =
ObservableOutput.Corrected¶
attr
bloq.ir.ObservableOutput.Corrected-
Flip =
ObservableOutput.Flip¶
attr
bloq.ir.ObservableOutput.Flip-
Corrected =
class bloq.ir.ObservableOutput
-
class ValueRef(node: int, output: ObservableOutput =
ObservableOutput.Corrected)¶ A level-local Boolean source, including its selected output port.
- node¶
attr
bloq.ir.ValueRef.node- output¶
attr
bloq.ir.ValueRef.output
class bloq.ir.ValueRef
- class ValueRole¶
Edge provenance on a
Valueedge (BloqEdge.Value.role,ValueInput.role): what source construct routed this bit here. Value semantics ignore it — every role reads the producer’s bit the same way.Datais plain dataflow;FeedbackFoldmarks aFeedbackaction’s condition bit folding into an observable.ReadoutFoldfolds an earlier corrected readout; dynamic decoder symptoms exclude both runtime folds.- class Data¶
Plain dataflow.
class
bloq.ir.ValueRole.Data- class FeedbackFold(action)¶
Folds a
Feedbackaction’s condition bit into an observable.actionis the sourceFeedbackordinal.- action¶
attr
bloq.ir.ValueRole.FeedbackFold.action
class
bloq.ir.ValueRole.FeedbackFold- class ReadoutFold¶
Folds an already corrected readout into a composed named parity.
class
bloq.ir.ValueRole.ReadoutFold
class bloq.ir.ValueRole
- class RegionNode¶
A structured control-flow region’s payload, reached via
BloqNodeKind.Region: a node whose body is a nestedSubGraphover the same shared template pool. Restart predicates use incomingValueedges or a body-localrestart_source. Edges never cross region boundaries.- class RepeatUntilSuccess(body, restart_condition, restart_source)¶
Region carrying a postselection/restart predicate. Native execution retries failed physical or decoded attempts up to its configured cap.
- body¶
attr
bloq.ir.RegionNode.RepeatUntilSuccess.body- restart_condition¶
attr
bloq.ir.RegionNode.RepeatUntilSuccess.restart_condition- restart_source¶
The body-local Boolean source feeding the predicate’s unfed slots.
attr
bloq.ir.RegionNode.RepeatUntilSuccess.restart_source
class
bloq.ir.RegionNode.RepeatUntilSuccess- kind¶
Which region this is, without destructuring its payload.
Examples
>>> import bloq >>> program = bloq.compile(bloq.GalleryItem.T_GATE.load(), distance=3) >>> kinds = {n.region.kind for _, n in program.nodes() if n.is_region()} >>> bloq.ir.RegionKind.RepeatUntilSuccess in kinds True
attr
bloq.ir.RegionNode.kind
class bloq.ir.RegionNode
- class RegionKind¶
What kind of control flow a region node implements.
Examples
>>> import bloq >>> program = bloq.compile(bloq.GalleryItem.T_GATE.load(), distance=3) >>> len(program.regions_of(bloq.ir.RegionKind.RepeatUntilSuccess)) >= 1 True-
RepeatUntilSuccess =
RegionKind.RepeatUntilSuccess¶
attr
bloq.ir.RegionKind.RepeatUntilSuccess-
RepeatUntilSuccess =
class bloq.ir.RegionKind
- class RegionRef¶
A program-unique address of one region node: the level it lives at plus its level-local id.
pathis in the same(owner_node_id, body_selector)shape asWalkNode.path, so it can be passed straight back to anypath=argument.- body(program: Bloq) SubGraph¶
The body subgraph this region executes, read out of
program.- Parameters:
- Raises:
InvalidArgumentError – If
programhas no such region.
meth
bloq.ir.RegionRef.body- resolve(program: Bloq) RegionNode¶
The region node this reference names, read out of
program.- Parameters:
- Raises:
InvalidArgumentError – If
programhas no such region — the usual cause is an edit that recycled node ids since the query ran.
meth
bloq.ir.RegionRef.resolve- node¶
The region node’s level-local id.
attr
bloq.ir.RegionRef.node- path¶
The
(owner_id, selector)hops to the region’s level; empty at the top.
attr
bloq.ir.RegionRef.path
class bloq.ir.RegionRef
- class InstanceMeasurement¶
One measurement site inside a template instance (
i<instance>:m<index>).Ordered by
(instance, measurement), matching the IR’s own order, so a collection gathered from several detectors has a stable total order. Instance ids are allocation order, not emission order: an edit that splices padding into the middle of a program gives it the highest instance id, so sorting does not recover the emission sequence.- instance¶
The template instance id.
attr
bloq.ir.InstanceMeasurement.instance- measurement¶
The measurement index within that instance’s template circuit.
attr
bloq.ir.InstanceMeasurement.measurement
class bloq.ir.InstanceMeasurement
- class FramePair¶
One terminal output’s logical Pauli-frame sign bits: the
Computenodes carrying the X- and Z-frame corrections for the source output port.- port¶
The source output port this frame corrects.
attr
bloq.ir.FramePair.port- x¶
The node id whose bit is the X-frame sign.
attr
bloq.ir.FramePair.x- z¶
The node id whose bit is the Z-frame sign.
attr
bloq.ir.FramePair.z
class bloq.ir.FramePair
Detectors and restart syndromes¶
Detector and restart parities reference measurements in template-local or placed-instance coordinates. A parity can also contain a constant sign or loop-carried state, so a list of measurement indices alone may be incomplete.
Quantum nodes contain both inline detector rows and shared DetectorBundleUse
entries. Each use binds a bundle’s owner slots to instances and adds its placement
offset. Include both representations when counting or emitting detectors; use
activation applies to the whole bundle use.
- class DetectorTerm¶
One XOR term of a
NodeParity: a template-instance measurement or a loop-carried detector state.- class LoopState(state)¶
- state¶
attr
bloq.ir.DetectorTerm.LoopState.state
class
bloq.ir.DetectorTerm.LoopState- class Measurement(measurement)¶
- measurement¶
attr
bloq.ir.DetectorTerm.Measurement.measurement
class
bloq.ir.DetectorTerm.Measurement
class bloq.ir.DetectorTerm
- class TemplateParity¶
An XOR of template-local terms — a detector, restart, or recurrence parity in template space. Measurement terms are circuit-local measurement ids of the owning template’s circuit; translate them to instance space through an
EmissionPlan’smeasurementsmap.- loop_states¶
The loop-state-id terms, in term order.
attr
bloq.ir.TemplateParity.loop_states- measurements¶
The template-local measurement-id terms, in term order.
attr
bloq.ir.TemplateParity.measurements- sign¶
Whether this parity includes the constant XOR term.
attr
bloq.ir.TemplateParity.sign
class bloq.ir.TemplateParity
- class TemplateDetector¶
A detector closing entirely within one template, in template-local measurement space.
- body¶
The repeat body the detector fires in, or
Noneat the template’s top level.
attr
bloq.ir.TemplateDetector.body- coords¶
Decoder coordinates, or
Nonewhen the detector carries none.
attr
bloq.ir.TemplateDetector.coords- parity¶
The detector’s measurement parity.
attr
bloq.ir.TemplateDetector.parity
class bloq.ir.TemplateDetector
- class TemplateRestart¶
A
RepeatUntilSuccessrestart syndrome in template-local measurement space: when the parity is odd (failure), the enclosing RUS attempt restarts.- parity¶
The restart syndrome parity.
attr
bloq.ir.TemplateRestart.parity
class bloq.ir.TemplateRestart
- class TemplateRepeatState¶
A loop-carried detector recurrence on one repeat body, in template-local measurement space:
initialseeds the state on the first iteration,nextadvances it on every later one.- body¶
The repeat body id.
attr
bloq.ir.TemplateRepeatState.body- initial¶
The parity on the first iteration.
attr
bloq.ir.TemplateRepeatState.initial- next¶
The parity recurrence for subsequent iterations.
attr
bloq.ir.TemplateRepeatState.next- state¶
The loop-state id this recurrence carries.
attr
bloq.ir.TemplateRepeatState.state
class bloq.ir.TemplateRepeatState
- class NodeParity¶
An XOR of measurement (and loop-state) terms — a detector or restart parity in instance-measurement space.
- is_empty() bool¶
Trueif the parity has no terms.
meth
bloq.ir.NodeParity.is_empty- loop_states() list[int]¶
The loop-state ids among the terms (a loop-carried detector recurrence), in term order.
meth
bloq.ir.NodeParity.loop_states- measurements() list[InstanceMeasurement]¶
The measurement terms, dropping any loop-state terms.
meth
bloq.ir.NodeParity.measurements- terms() list[DetectorTerm]¶
All XOR terms in order: measurements and loop states.
meth
bloq.ir.NodeParity.terms- sign¶
Whether this parity includes the constant XOR term.
attr
bloq.ir.NodeParity.sign
class bloq.ir.NodeParity
- class NodeDetector¶
One top-level detector on a quantum node: a parity and optional decoder coordinates.
- coords¶
Decoder coordinates, or
Nonewhen the detector carries none.
attr
bloq.ir.NodeDetector.coords- parity¶
The detector’s measurement parity.
attr
bloq.ir.NodeDetector.parity
class bloq.ir.NodeDetector
- class NodeRestart¶
A
RepeatUntilSuccessrestart syndrome composed across a template seam, listed inQuantumNode.restarts. Produced when a post-selected parity’s chain crosses two template instances, so neither template’s local table can carry it. Whenparityis odd (the failure syndrome), the enclosing repeat-until-success attempt restarts. Only meaningful on a node inside aRepeatUntilSuccessbody.- parity¶
The parity that, when odd (failure syndrome), restarts the RUS attempt.
attr
bloq.ir.NodeRestart.parity
class bloq.ir.NodeRestart
- class DetectorBundle¶
Detector rows shared by placed uses of a bundle.
- detectors¶
Rows in bundle order.
attr
bloq.ir.DetectorBundle.detectors- owner_templates¶
Expected template id for each owner slot.
attr
bloq.ir.DetectorBundle.owner_templates
class bloq.ir.DetectorBundle
- class DetectorBundleUse¶
Bind a shared detector bundle to instances at a layout offset.
- bundle¶
Shared bundle pool id.
attr
bloq.ir.DetectorBundleUse.bundle- instances¶
Instance id bound to each owner slot.
attr
bloq.ir.DetectorBundleUse.instances- offset¶
Added to each detector center when placed.
attr
bloq.ir.DetectorBundleUse.offset
class bloq.ir.DetectorBundleUse
- class BundleDetector¶
One shared detector row in bundle-local owner coordinates.
- coords¶
Decoder coordinates before placement offset.
attr
bloq.ir.BundleDetector.coords- sign¶
Expected parity sign.
attr
bloq.ir.BundleDetector.sign- terms¶
Ordered measurement and loop-state terms.
attr
bloq.ir.BundleDetector.terms
class bloq.ir.BundleDetector
- class BundleDetectorTerm¶
One shared detector row in bundle-local owner coordinates.
- class LoopState(state)¶
- state¶
attr
bloq.ir.BundleDetectorTerm.LoopState.state
class
bloq.ir.BundleDetectorTerm.LoopState- class Measurement(owner, measurement)¶
- measurement¶
attr
bloq.ir.BundleDetectorTerm.Measurement.measurement- owner¶
attr
bloq.ir.BundleDetectorTerm.Measurement.owner
class
bloq.ir.BundleDetectorTerm.Measurement
class bloq.ir.BundleDetectorTerm
Logical boundaries and provenance¶
Boundary operators bind logical Pauli support to a particular instance and its
input or output face. A LogicalOutput retains its owning instance: equal
qubit support does not make two physical cuts interchangeable.
Node and instance provenance link compiled work to source blocks, actions, and module placements. Use that provenance for source-aware inspection, and allow for synthetic nodes that have no authored origin.
- class LogicalOutput¶
Complete terminal logical operators for one output, in layout-global coordinates.
- instance¶
Template instance that owns this output’s logical worldline.
attr
bloq.ir.LogicalOutput.instance- port¶
attr
bloq.ir.LogicalOutput.port- x¶
attr
bloq.ir.LogicalOutput.x- z¶
attr
bloq.ir.LogicalOutput.z
class bloq.ir.LogicalOutput
- class InstanceBoundaryOperator¶
A symbolic logical boundary operator at one template instance’s face — one binding payload element of
ClassicalNode.Observable.operators. Names the operator by instance reference, never per-shot bits:instanceis theTemplateInstance.id,facethe temporal face bound, andoperatormaps instance-global(x, y)qubit coordinates toPauliterms.- face¶
Which temporal face (
InputorOutput) the operator binds.
attr
bloq.ir.InstanceBoundaryOperator.face- instance¶
The template instance id this operator binds to.
attr
bloq.ir.InstanceBoundaryOperator.instance- operator¶
The operator as a dict mapping instance-global
(x, y)qubit coordinates toPauliterms.
attr
bloq.ir.InstanceBoundaryOperator.operator
class bloq.ir.InstanceBoundaryOperator
- class BoundaryFace¶
Which temporal face of a template instance a boundary operator binds — see
InstanceBoundaryOperator.face. The face fixes emission position: anInputoperator reads the qubit state entering the instance, so it emits before the instance circuit; anOutputoperator reads on exit, so it emits after.-
Input =
BoundaryFace.Input¶
attr
bloq.ir.BoundaryFace.Input-
Output =
BoundaryFace.Output¶
attr
bloq.ir.BoundaryFace.Output-
Input =
class bloq.ir.BoundaryFace
- class BoundaryFlow¶
One compiler-authored open stabilizer flow in template-local coordinates.
measurementscontains template-local measurement ids. Resolve them through the owning instance and an [PyEmissionPlan]’smeasurementsmap before emitting records.- center¶
Flow-center coordinate, when present.
attr
bloq.ir.BoundaryFlow.center- end¶
Pauli boundary leaving the template.
attr
bloq.ir.BoundaryFlow.end- marker¶
One of
"detector","discard", or"restart".
attr
bloq.ir.BoundaryFlow.marker- measurements¶
Template-local measurement ids accumulated by the flow.
attr
bloq.ir.BoundaryFlow.measurements- sign¶
Expected XOR when both boundaries have positive eigenvalue.
attr
bloq.ir.BoundaryFlow.sign- start¶
Pauli boundary entering the template.
attr
bloq.ir.BoundaryFlow.start
class bloq.ir.BoundaryFlow
- class NodeProvenance¶
Node provenance: what source-level construct an IR node realizes, read from
BloqNode.provenance. Quantum nodes tie back to source blocks and pipes, classical nodes to stabilizer generators, source actions, or output frames. Provenance is never consulted for emission semantics — its consumers areBloq.output_frames(), program views, and decoder runtimes. Dispatch on the variant withisinstance.- class Action(ordinal)¶
A classical node lowered from the source action at
ordinal.- ordinal¶
attr
bloq.ir.NodeProvenance.Action.ordinal
class
bloq.ir.NodeProvenance.Action- class BlockComponent(members)¶
A fused component of source blocks.
- members¶
attr
bloq.ir.NodeProvenance.BlockComponent.members
class
bloq.ir.NodeProvenance.BlockComponent- class BranchSelector(name)¶
A structural branch or selective measurement’s scoped selector name.
- name¶
attr
bloq.ir.NodeProvenance.BranchSelector.name
class
bloq.ir.NodeProvenance.BranchSelector- class Generator(ordinal)¶
A classical node realizing stabilizer generator
ordinal’s readout.- ordinal¶
attr
bloq.ir.NodeProvenance.Generator.ordinal
class
bloq.ir.NodeProvenance.Generator- class MemoryPadding(pipe, rounds)¶
Memory-round padding spliced into a seam post-compile, waiting
roundssyndrome-extraction rounds.- pipe¶
attr
bloq.ir.NodeProvenance.MemoryPadding.pipe- rounds¶
attr
bloq.ir.NodeProvenance.MemoryPadding.rounds
class
bloq.ir.NodeProvenance.MemoryPadding- class Missing¶
No provenance: hand-built or synthetic.
class
bloq.ir.NodeProvenance.Missing- class OutputFrame(port, basis)¶
The
basis-frame correction bit for source outputport.- basis¶
attr
bloq.ir.NodeProvenance.OutputFrame.basis- port¶
attr
bloq.ir.NodeProvenance.OutputFrame.port
class
bloq.ir.NodeProvenance.OutputFrame
class bloq.ir.NodeProvenance
- class InstanceProvenance¶
-
class Block(pos: tuple[int, int, int], kind: BlockKind | str, height: str =
'd', tag: str | None =None, role: str ='auto')¶ - source¶
attr
bloq.ir.InstanceProvenance.Block.source
class
bloq.ir.InstanceProvenance.Block-
class Pipe(src: tuple[int, int, int], direction: Direction | str, hadamard: bool =
False, tag: str | None =None)¶ - dst¶
attr
bloq.ir.InstanceProvenance.Pipe.dst- src¶
attr
bloq.ir.InstanceProvenance.Pipe.src
class
bloq.ir.InstanceProvenance.Pipe- class Source¶
class
bloq.ir.InstanceProvenance.Source- class SpatialPortSubstitution(source, role, part)¶
- part¶
attr
bloq.ir.InstanceProvenance.SpatialPortSubstitution.part- role¶
attr
bloq.ir.InstanceProvenance.SpatialPortSubstitution.role- source¶
attr
bloq.ir.InstanceProvenance.SpatialPortSubstitution.source
class
bloq.ir.InstanceProvenance.SpatialPortSubstitution-
class Block(pos: tuple[int, int, int], kind: BlockKind | str, height: str =
class bloq.ir.InstanceProvenance
Stim backend¶
Use compile_to_stim for a one-call source-to-circuit workflow, or
emit_stim to reuse an existing IR program. Both return a stim.Circuit;
use its to_file(path) method to save it. Stim emission requires a supported
fixed execution structure; retry regions and unpinned guarded quantum membership
need dynamic execution or an explicit supported projection first.
Emission plans and per-node segments expose the lowering for custom consumers.
Select dialect="stim" or dialect="clifft" where a text-emission API accepts
it. Isolated T-attempt artifacts expose cultivation circuits separately from the
whole program. See emission for backend restrictions.
-
compile_to_stim(graph: BlockGraph, distance: int =
3, *, noise: float | None =None, limits: Mapping[str, int | None] | None =None) stim.Circuit¶ Compiles
graphand emits the resulting program as astim.Circuit.The whole pipeline in one call, for the common case that never needs the intermediate program. Compiles with the given distance and limits, then calls
emit_stim(program, noise=noise)with identical results.- Parameters:
- graph: BlockGraph¶
The block graph to compile.
- distance: int =
3¶ The odd code distance to compile at, in
3..=255.- noise: float | None =
None¶ Uniform circuit-level noise probability, as in
emit_stim.- limits: Mapping[str, int | None] | None =
None¶ Compiler budget overrides; non-negative counts impose a budget,
Nonedisables it, and omitted fields keep their defaults.
- Raises:
CompileError – If compilation fails.
InvalidArgumentError – If the distance, noise, or a limit field is invalid.
OverflowError – If a limit count is negative or exceeds
usize.StimEmissionError – If the static backend cannot emit the program.
- Emits:
CompileWarning: Once per advisory the compile recorded.
Examples
>>> import bloq >>> circuit = bloq.compile_to_stim(bloq.GalleryItem.X_MEMORY.load()) >>> circuit.num_qubits > 0 True >>> "DEPOLARIZE" in str(bloq.compile_to_stim(bloq.GalleryItem.X_MEMORY.load(), noise=0.001)) True
func bloq.compile_to_stim
-
emit_stim(program: Bloq, *, noise: float | None =
None, validate: bool =False, align_moments: bool =False) stim.Circuit¶ Emits
programas astim.Circuit, ready for sampling or saving withcircuit.to_file(path). Usestr(circuit)when circuit text is needed.- Parameters:
- program: Bloq¶
The compiled Bloq IR program to emit.
- noise: float | None =
None¶ Uniform probability for gate, reset, measurement, and idle noise, in the inclusive range
[0, 1].None(the default) emits the noiseless circuit.- validate: bool =
False¶ Check the program’s well-formedness before emitting. Defaults to
False, which suits a program that came straight fromcompile()in this process. PassTruefor one loaded withBloq.from_binary()/Bloq.load(), edited by hand, or produced elsewhere: the backend assumes a well-formed program and may otherwise emit a silently wrong circuit.- align_moments: bool =
False¶ Flatten repeats and merge compatible Clifford moments by z layer. Requires noiseless emission; defaults to
False.
- Raises:
InvalidArgumentError – If
noiseis not finite or outside[0, 1].StimEmissionError – If the static backend cannot emit the program (dynamic control regions, non-Clifford gates).
BloqValidationError – If
validateis set and the program is malformed.
Examples
>>> import bloq >>> program = bloq.compile(bloq.GalleryItem.X_MEMORY.load(), distance=3) >>> bloq.emit_stim(program).num_qubits > 0 True >>> "DEPOLARIZE1" in str(bloq.emit_stim(program, noise=1e-3)) True >>> bloq.emit_stim(bloq.Bloq.from_binary(program.to_binary()), validate=True) \ ... == bloq.emit_stim(program) True
func bloq.emit_stim
- class EmissionPlan¶
A quantum node’s merged circuit plus the maps relating instance-space ids to it — the per-node unit a backend emits.
measurementstranslates each instance-space measurement (as an(instance_id, template_measurement_id)key, matchingInstanceMeasurement.instance/.measurement) to its merged circuit measurement id;bodiestranslates each(instance_id, template_body_id)to its merged body id. Side tables named in instance space (node detectors, accumulates, repeat states) resolve against these maps.Classical and region nodes have no instances and yield an empty plan.
- normalized_template(id: int) Template | None¶
The source template after repeat expansion for this noisy plan, or
Nonewhen the original program template still applies.Read side tables using
plan.normalized_template(id) or program.template(id). Expansion keeps template ids but updates detector/restart recurrences and body scopes.
meth
bloq.ir.EmissionPlan.normalized_template- bodies¶
(instance_id, template_body_id)→ merged body id.
attr
bloq.ir.EmissionPlan.bodies- circuit¶
The node’s instances translated by offset and merged into one circuit.
attr
bloq.ir.EmissionPlan.circuit- measurements¶
(instance_id, template_measurement_id)→ merged measurement id.
attr
bloq.ir.EmissionPlan.measurements
class bloq.ir.EmissionPlan
-
emit_plan_stim(plan: EmissionPlan, qubit_map: Mapping[tuple[int, int], int], *, dialect: str =
'stim', tag: str | None =None) PlanStim¶ Lowers one node’s emission plan to Stim text over a caller-chosen layout.
- Parameters:
- plan: EmissionPlan¶
The node’s
EmissionPlan(fromBloq.emission_plan).- qubit_map: Mapping[tuple[int, int], int]¶
Every
(x, y)layout coordinate the plan touches, mapped to its Stim qubit index. A coordinate the plan uses but the map omits is an error, so the usual source is the whole program’s layout:{c: i for i, c in enumerate(program.sorted_layout_coords())}.- dialect: str =
'stim'¶ "stim"(default) or"clifft", case-insensitive. Stim usesS/S_DAGtaggedHONEST_Tfor T gates; Clifft keeps literalT/T_DAGgates.- tag: str | None =
None¶ Optional Stim instruction tag applied to the emitted gates.
- Raises:
InvalidArgumentError – If the dialect name is unknown.
StimEmissionError – If the plan cannot be lowered — most often a coordinate missing from
qubit_map.
Examples
>>> import bloq >>> program = bloq.compile(bloq.GalleryItem.X_MEMORY.load(), distance=3) >>> layout = {c: i for i, c in enumerate(program.sorted_layout_coords())} >>> node = program.deterministic_emit_order()[0] >>> lowered = bloq.emit_plan_stim(program.emission_plan(node), layout) >>> lowered.measurement_count > 0 True >>> max(lowered.measurement_columns.values()) < lowered.measurement_count True
func bloq.emit_plan_stim
- class PlanStim¶
One lowered emission plan: its Stim text plus where its measurements landed.
measurement_columnsmaps each instance-space measurement — the(instance_id, template_measurement_id)keys ofEmissionPlan.measurements— to its column intextafter everyREPEATblock is unrolled, counting from 0 within this plan. Add the plan’s own offset to place it in a larger circuit.measurement_countis how many columnstextproduces in total, which is that same offset for the next plan.- measurement_columns¶
(instance_id, template_measurement_id)→ post-unroll column.
attr
bloq.PlanStim.measurement_columns- measurement_count¶
Total measurement columns the text produces.
attr
bloq.PlanStim.measurement_count- text¶
The lowered Stim text.
attr
bloq.PlanStim.text
class bloq.PlanStim
-
emit_stim_segments(program: Bloq, *, noise: float | None =
None, validate: bool =False) StimSegments¶ Emits
programas per-node Stim segments (seeStimSegments).- Parameters:
- program: Bloq¶
The compiled Bloq IR program to emit.
- noise: float | None =
None¶ Uniform probability for gate, reset, measurement, and idle noise, in the inclusive range
[0, 1].None(the default) emits the noiseless circuit.- validate: bool =
False¶ Check the program’s well-formedness before emitting; see
emit_stim(). Defaults toFalse.
- Raises:
InvalidArgumentError – If
noiseis not finite or outside[0, 1].StimEmissionError – If the static backend cannot emit the program.
BloqValidationError – If
validateis set and the program is malformed.
Examples
>>> import bloq >>> program = bloq.compile(bloq.GalleryItem.X_MEMORY.load(), distance=3) >>> segments = bloq.emit_stim_segments(program) >>> segments.header.startswith("QUBIT_COORDS") True >>> len(segments.segments) == program.node_count True >>> import stim >>> stim.Circuit(segments.to_text()) == bloq.emit_stim(program) True >>> stim.Circuit(bloq.emit_stim_segments(program, noise=1e-3).to_text()) \ ... == bloq.emit_stim(program, noise=1e-3) True
func bloq.emit_stim_segments
- emit_stim_segments_pair(program: Bloq, *, noise: float) tuple[StimSegments, StimSegments]¶
Emits
programtwice from one pass: noiseless segments and segments with uniform circuit-level noise, chunked identically.Both results carry the same node ids and the same measurement spans, so a caller comparing or splicing the two — the usual noise-injection workflow — no longer has to emit twice and assert the chunkings agree.
- Parameters:
- Returns:
The clean and noisy segmentations.
- Return type:
tuple[StimSegments, StimSegments]
- Raises:
InvalidArgumentError – If
noiseis not finite or outside[0, 1].StimEmissionError – If the static backend cannot emit the program.
Examples
>>> import bloq >>> program = bloq.compile(bloq.GalleryItem.X_MEMORY.load(), distance=3) >>> clean, noisy = bloq.emit_stim_segments_pair(program, noise=1e-3) >>> [s.node_id for s in clean.segments] == [s.node_id for s in noisy.segments] True >>> import stim >>> stim.Circuit(clean.to_text()) == bloq.emit_stim(program) True
func bloq.emit_stim_segments_pair
- class StimSegment¶
One node’s chunk of a segmented Stim program.
textis the node’s Stim text;measurement_startis the absolute column of its first measurement in the whole program, andmeasurement_counthow many it contributes — so this segment owns program columnsmeasurement_start .. measurement_start + measurement_count.Examples
>>> import bloq >>> program = bloq.compile(bloq.GalleryItem.X_MEMORY.load(), distance=3) >>> segment = bloq.emit_stim_segments(program).segments[0] >>> segment.measurement_start 0 >>> segment.measurement_count > 0 True- measurement_count¶
How many measurement columns this chunk contributes.
attr
bloq.StimSegment.measurement_count- measurement_start¶
Absolute column of this chunk’s first measurement.
attr
bloq.StimSegment.measurement_start- node_id¶
The id of the node this chunk was emitted from.
attr
bloq.StimSegment.node_id- text¶
The node’s Stim text.
attr
bloq.StimSegment.text
class bloq.StimSegment
- class StimSegments¶
A Stim program linearized into per-node chunks, for callers transforming nodes in isolation (chiefly noise injection that must leave port
MPPboundaries noiseless). Plain emission wantsemit_stim.The
headerholds the sharedQUBIT_COORDSpreamble;segmentsholds oneStimSegmentper node in emission order.to_text()reassembles the raw circuit text;stim.Circuit(segments.to_text())matchesemit_stim(program).Examples
>>> import bloq >>> program = bloq.compile(bloq.GalleryItem.X_MEMORY.load(), distance=3) >>> segments = bloq.emit_stim_segments(program) >>> "QUBIT_COORDS" in segments.header True >>> len(segments.segments) >= 1 True >>> import stim >>> stim.Circuit(segments.to_text()) == bloq.emit_stim(program) True- to_text() str¶
The header and every chunk concatenated — the whole-program Stim text.
meth
bloq.StimSegments.to_text- header¶
QUBIT_COORDSpreamble shared by every node.
attr
bloq.StimSegments.header- num_measurements¶
The total number of measurement columns across every chunk.
attr
bloq.StimSegments.num_measurements- segments¶
Per-node chunks in emission order.
attr
bloq.StimSegments.segments
class bloq.StimSegments
-
remap_stim_circuit(text: str, qubit_map: Mapping[int, int], *, skip: set[str] | None =
None, honest_t: bool =False, observable_map: Mapping[int, collections.abc.Sequence[int]] | None =None, measurement_map: Mapping[int, int] | None =None, source_measurement_start: int =0, destination_measurement_start: int =0) tuple[str, dict[int, int]]¶ Flattens and remaps Stim circuit text without constructing Python target objects.
- Parameters:
- text: str¶
A Stim circuit.
- qubit_map: Mapping[int, int]¶
Source qubit index to destination qubit index.
- skip: set[str] | None =
None¶ Instruction names to omit; defaults to omitting nothing. A skipped instruction still advances the source measurement offset, so dropping
"QUBIT_COORDS"does not shift the record mapping.- honest_t: bool =
False¶ Replace tags on
SandS_DAGwithHONEST_T.- observable_map: Mapping[int, collections.abc.Sequence[int]] | None =
None¶ Optional source observable to destination observables.
- measurement_map: Mapping[int, int] | None =
None¶ Optional absolute source-to-destination measurement map.
- source_measurement_start: int =
0¶ Absolute source measurement offset.
- destination_measurement_start: int =
0¶ Absolute destination measurement offset.
- Returns:
Remapped Stim text and newly produced measurement-map entries.
- Return type:
tuple[str, dict[int, int]]
- Raises:
InvalidArgumentError – If Stim text or a requested mapping is invalid.
func bloq.remap_stim_circuit
- stim_to_clifft_text(text: str) str¶
Rewrites Stim text into Clifft text: every
S/S_DAGtaggedHONEST_T_TAGbecomesT/T_DAG, and untagged gates are left alone.- Parameters:
- text: str¶
Stim circuit text.
Examples
>>> import bloq >>> bloq.stim_to_clifft_text("S[HONEST_T] 0") 'T 0' >>> bloq.stim_to_clifft_text("S 0") 'S 0'
func bloq.stim_to_clifft_text
- clifft_to_stim_text(text: str) str¶
Rewrites Clifft text into Stim text: every
T/T_DAGbecomesS/S_DAGtaggedHONEST_T_TAG.Lossy in one direction:
EXP_VALlines have no Stim spelling and are dropped.stim_to_clifft_textinverts the gate mapping, not the drop.- Parameters:
- text: str¶
Clifft circuit text.
Examples
>>> import bloq >>> bloq.clifft_to_stim_text("T 0") 'S[HONEST_T] 0'
func bloq.clifft_to_stim_text
- emit_isolated_t_attempts(program: Bloq, *, noise: float) IsolatedTAttemptArtifacts¶
Emits the physical S/honest-T circuits and shared causal GAP companions for one isolated T-source attempt.
programmust be the compiled isolatedT -> +Z -> Portfixture. Insert any required memory rounds withBloq.insert_memory_rounds_afterbefore calling this function.- Parameters:
- program: Bloq¶
The prepared isolated-T Bloq IR program.
- noise: float¶
Uniform circuit-level error probability in the inclusive range
[0, 1], spelled as inemit_stim().
- Raises:
InvalidArgumentError – If
noiseis not finite or outside[0, 1].StimEmissionError – If the program is not the supported isolated-T shape, or emission fails.
func bloq.emit_isolated_t_attempts
- class IsolatedTAttemptArtifacts¶
Clifft physical circuits and Stim causal companions for one isolated-T source attempt.
- companion¶
Partially-noiseless S-proxy Stim companion carrying the GAP pair.
attr
bloq.IsolatedTAttemptArtifacts.companion- manifest¶
Dense-column alignment data shared by all four circuits.
attr
bloq.IsolatedTAttemptArtifacts.manifest- physical_s¶
Full-noise S-control Clifft circuit with direct X/Y/Z probes.
attr
bloq.IsolatedTAttemptArtifacts.physical_s- physical_t¶
Full-noise honest-T Clifft circuit with direct X/Y/Z probes.
attr
bloq.IsolatedTAttemptArtifacts.physical_t- sheets¶
Terminal stabilizer frontier sheets, as a suffix to
companion.
attr
bloq.IsolatedTAttemptArtifacts.sheets
class bloq.IsolatedTAttemptArtifacts
- class IsolatedTAttemptManifest¶
Detector, frame, GAP, frontier, and probe columns for one isolated-T attempt artifact.
The
frontier_*lists are parallel. Frontier entryimaps to synthetic detectorbase_companion_detector_count + iin the causal DEM transform. Theexp_val_signslist is ordered X, Y, Z. Each corrected frame XORs its measurement parity,frame_*_sign, and the associated GAP prediction.- detector_signs¶
Expected noiseless parity of every detector, in emitted order.
attr
bloq.IsolatedTAttemptManifest.detector_signs- exp_val_signs¶
Static signs multiplying direct X/Y/Z
EXP_VALprobes.
attr
bloq.IsolatedTAttemptManifest.exp_val_signs- frame_x_gap_observable¶
GAP observable whose correction contributes to the X-frame bit.
attr
bloq.IsolatedTAttemptManifest.frame_x_gap_observable- frame_x_measurements¶
Measurement columns XORed into the output X-frame bit.
attr
bloq.IsolatedTAttemptManifest.frame_x_measurements- frame_x_sign¶
Constant XORed with the X-frame measurement parity before decoder correction.
attr
bloq.IsolatedTAttemptManifest.frame_x_sign- frame_z_gap_observable¶
GAP observable whose correction contributes to the Z-frame bit.
attr
bloq.IsolatedTAttemptManifest.frame_z_gap_observable- frame_z_measurements¶
Measurement columns XORed into the output Z-frame bit.
attr
bloq.IsolatedTAttemptManifest.frame_z_measurements- frame_z_sign¶
Constant XORed with the Z-frame measurement parity before decoder correction.
attr
bloq.IsolatedTAttemptManifest.frame_z_sign- frontier_bases¶
Frontier stabilizer bases.
attr
bloq.IsolatedTAttemptManifest.frontier_bases- frontier_observables¶
Frontier observable ids, parallel with
frontier_basesandfrontier_signs.
attr
bloq.IsolatedTAttemptManifest.frontier_observables- frontier_signs¶
Expected frontier parities.
attr
bloq.IsolatedTAttemptManifest.frontier_signs- gap_x_observable¶
Logical-X GAP observable id.
attr
bloq.IsolatedTAttemptManifest.gap_x_observable- gap_z_observable¶
Logical-Z GAP observable id.
attr
bloq.IsolatedTAttemptManifest.gap_z_observable- postselection_detectors¶
Detector columns used for physical restart postselection.
attr
bloq.IsolatedTAttemptManifest.postselection_detectors- rus_path¶
(region_id, body_selector)hops to the emitted RUS body.
attr
bloq.IsolatedTAttemptManifest.rus_path
class bloq.IsolatedTAttemptManifest
VM verification¶
lower_vm prepares a reusable VmProgram; each run executes a dynamic
shot with its own seed and runtime limits. Check VmRunResult.discarded before
requesting corrected logical Bloch vectors, because a discarded shot has no
committed logical result.
The VM verifies measurement-dependent control and physical output states using mock decoding and timing models. Its traces explain execution, but are not a stable program persistence format or a claim about decoder performance. See VM verification for model assumptions.
-
lower_vm(program: Bloq, *, noise: float =
0.0, gate_duration: float =1.0, decoder_latency_rounds: int =10, source_release_time: float =0.0, input_release_time: float | None =None, factory_release_time: float =0.0, max_quantum_variants: int | None =None) VmProgram¶ Lowers Bloq IR once to a reusable dynamic VM program.
- Parameters:
- program: Bloq¶
Compiled Bloq IR.
- noise: float =
0.0¶ Uniform circuit and idle-noise probability.
- gate_duration: float =
1.0¶ Duration of each nonempty physical moment; must exceed
1e-9.- decoder_latency_rounds: int =
10¶ Mock-decoder latency in local memory rounds.
- source_release_time: float =
0.0¶ Release epoch for Clifford sources.
- input_release_time: float | None =
None¶ External-input release epoch; defaults to
source_release_time.- factory_release_time: float =
0.0¶ Release epoch for resource factories.
- max_quantum_variants: int | None =
None¶ Maximum jointly reachable guard tuples for one quantum stage. Defaults to 4096.
- Returns:
A reusable
VmProgram. Prepared-Y release offsets follow the emitted preparation durations.- Raises:
InvalidArgumentError – If an option is out of range.
LowerError – If the IR cannot be lowered exactly within the bound.
Examples
>>> import bloq >>> ir = bloq.compile(bloq.GalleryItem.X_MEMORY.load(), distance=3) >>> vm = bloq.lower_vm(ir) >>> vm.task_count > 0 True
func bloq.lower_vm
- class VmProgram¶
A Bloq IR program lowered once to the dynamic VM instruction stream.
-
run(*, seed: int =
0, input_state: str ='plus', decoder_acceptance: float =0.8, accepted_accuracy: float =0.999, rejected_accuracy: float =0.9, gap_threshold: float =0.5, idle_error_rate: float | None =None, max_steps: int =1000000, max_attempts: int =100) VmRunResult¶ Runs one dynamic shot with the Ticit tableau backend.
- Parameters:
- seed: int =
0¶ Root seed for physical noise and mock-decoder draws.
- input_state: str =
'plus'¶ Encoded external input,
"plus"or"zero".- decoder_acceptance: float =
0.8¶ Mock decoder acceptance probability.
- accepted_accuracy: float =
0.999¶ Residual accuracy for accepted solves.
- rejected_accuracy: float =
0.9¶ Residual accuracy for rejected solves.
- gap_threshold: float =
0.5¶ Confidence threshold, strictly between 0 and 1.
- idle_error_rate: float | None =
None¶ Optional dynamic-idle depolarization rate per time unit. Omit to use the rate derived from
lower_vm(noise=...).- max_steps: int =
1000000¶ Maximum task, moment, and memory-round executions.
- max_attempts: int =
100¶ Maximum attempts of one repeat-until-success task.
- seed: int =
- Raises:
InvalidArgumentError – If an option is out of range.
RuntimeError – If execution fails.
meth
bloq.VmProgram.run-
to_json(*, indent: int | None =
None) str¶ Serializes the complete executable instruction stream as JSON.
This includes tasks, gates, dependencies, entry stream, logical inputs, logical outputs, and source release times. It is an inspection format, not a stable persistence protocol.
- Parameters:
- indent: int | None =
None¶ Nonefor compact JSON or2for pretty JSON.
- indent: int | None =
- Raises:
InvalidArgumentError – If
indentis neitherNonenor2.RuntimeError – If JSON serialization fails.
meth
bloq.VmProgram.to_json- decoder_latency_rounds¶
Configured mock-decoder latency carried by this program, in memory rounds.
attr
bloq.VmProgram.decoder_latency_rounds- output_count¶
Number of terminal logical outputs.
attr
bloq.VmProgram.output_count- qubit_count¶
Number of simulator qubits.
attr
bloq.VmProgram.qubit_count- sources¶
Independently released source tasks in dense task order.
attr
bloq.VmProgram.sources- task_count¶
Number of VM tasks across all dynamic regions.
attr
bloq.VmProgram.task_count-
run(*, seed: int =
class bloq.VmProgram
- class VmRunResult¶
One dynamic shot: a JSON-ready trace and terminal logical Bloch vectors.
-
logical_bloch(output: int =
0) tuple[float, float, float]¶ Terminal
(X, Y, Z)Bloch vector with Pauli-frame corrections.- Parameters:
- output: int =
0¶ Zero-based logical-output index.
- output: int =
- Raises:
InvalidArgumentError – If
outputis out of range.RuntimeError – If the shot has no committed logical result.
meth
bloq.VmRunResult.logical_bloch-
to_json(*, indent: int | None =
None) str¶ Serializes the trace as compact JSON, or pretty JSON for
indent=2.- Parameters:
- indent: int | None =
None¶ Nonefor compact JSON or2for pretty JSON.
- indent: int | None =
- Raises:
InvalidArgumentError – If
indentis neitherNonenor2.RuntimeError – If JSON serialization fails.
meth
bloq.VmRunResult.to_json- discarded¶
Whether the shot ended without a committed result.
attr
bloq.VmRunResult.discarded- finished_at¶
Physical stop time.
attr
bloq.VmRunResult.finished_at- peak_rank¶
Highest stabilizer-frame rank reached by the backend.
attr
bloq.VmRunResult.peak_rank- trace¶
Full execution trace as nested Python dictionaries and lists.
attr
bloq.VmRunResult.trace-
logical_bloch(output: int =
class bloq.VmRunResult
ZX logical analysis¶
Convert a source graph to its ZX diagram and inspect logical stabilizers before physical compilation. This layer describes the source’s logical relation; physical detectors, scheduling, and decoder behavior require the compiled IR and its backends. See logical correlations for the relation between stabilizers and named readouts.
- to_zx_graph(graph: BlockGraph) ZXGraph¶
Converts a
BlockGraphinto its ZX diagram, a read-onlyZXGraph.- Parameters:
- graph: BlockGraph¶
The block graph to lower.
- Returns:
The derived ZX diagram.
- Return type:
- Raises:
BlockGraphError – If the graph does not lower to ZX (e.g. a malformed pipe structure).
Examples
>>> import bloq >>> zx = bloq.to_zx_graph(bloq.GalleryItem.CNOT.load()) >>> zx.node_count > 0 True >>> zx.is_clifford_computation True
func bloq.to_zx_graph
- class ZXGraph¶
The ZX diagram derived from a
BlockGraph(read-only view).Build one with
to_zx_graph. The ZX layer is an analysis product of a block graph, not an authoring surface: its nodes, edges, and relations are queryable but immutable.Examples
>>> import bloq >>> zx = bloq.to_zx_graph(bloq.GalleryItem.CNOT.load()) >>> zx.node_count == len(zx.nodes()) True >>> zx.is_open True >>> zx.is_clifford_computation True- edge_between(n1: int, n2: int) ZXEdge | None¶
An edge between two node ids, or
None. The ZX layer is a multigraph: with parallel edges this returns one of them.
meth
bloq.ZXGraph.edge_between- edge_id(n1: int, n2: int) int | None¶
The id of an edge between two node ids, or
None. The ZX layer is a multigraph: with parallel edges this returns one of them.
meth
bloq.ZXGraph.edge_id- is_output_port(node_id: int) bool¶
True if
node_idis an output port — a Port whose neighbors all sit at a lower z-coordinate.- Parameters:
- node_id: int¶
The node to classify.
- Raises:
InvalidArgumentError – If
node_idis not a valid node id.
meth
bloq.ZXGraph.is_output_port- measurement_columns() dict[str, int]¶
Measurement variable name -> column id in the combined node/edge id space.
meth
bloq.ZXGraph.measurement_columns- neighbors(node_id: int) list[int] | None¶
Neighbor node ids of
node_id, orNonefor an unknown id.- Parameters:
- node_id: int¶
The node whose neighbors to return.
meth
bloq.ZXGraph.neighbors- node_at(pos: tuple[int, int, int]) ZXNode | None¶
The node at
pos, orNone.- Parameters:
- pos: tuple[int, int, int]¶
The
(x, y, z)position to look up.
meth
bloq.ZXGraph.node_at- output_ports() list[tuple[int, int, int]]¶
Output-port positions as
(x, y, z)triples, sorted.A layout column is not a qubit: two output ports can share
(x, y)when the second patch reuses tiles the first vacated, so ports are reported with their z-coordinate rather than collapsed to columns.
meth
bloq.ZXGraph.output_ports- stabilizers() list[StabilizerGenerator]¶
Derives the stabilizer generators of the diagram.
- Raises:
BlockGraphError – If the stabilizers cannot be derived.
meth
bloq.ZXGraph.stabilizers- validate_for_program() None¶
Validates that the graph’s actions form a runnable program.
- Raises:
BlockGraphError – If the actions do not form a runnable program.
meth
bloq.ZXGraph.validate_for_program- z_layers() list[int]¶
The distinct z-coordinates that carry nodes, ascending.
meth
bloq.ZXGraph.z_layers- edge_count¶
The number of edges.
attr
bloq.ZXGraph.edge_count- is_clifford_computation¶
True if the computation is Clifford (no T or selective nodes).
attr
bloq.ZXGraph.is_clifford_computation- is_open¶
True if the graph still has unfilled ports.
attr
bloq.ZXGraph.is_open- node_count¶
The number of nodes.
attr
bloq.ZXGraph.node_count- total_ids¶
The size of the combined node+edge id space.
attr
bloq.ZXGraph.total_ids
class bloq.ZXGraph
- class ZXNode¶
A node (spider or boundary) of a ZX diagram (read-only view).
Nodes come from
ZXGraph.nodes()(indexable byid) orZXGraph.node_at(). Interior nodes are"X","Y", or"Z"spiders; the boundary kinds are"Port"(an open boundary left by a port block),"T"(a magic-state boundary), and"Selective(...)"(a boundary whose basis is resolved at runtime).is_boundaryis true for all three boundary kinds.- id¶
The node id; also its index into
ZXGraph.nodes().
attr
bloq.ZXNode.id- is_boundary¶
Port, T, and Selective.
- Type:
True for all boundary node kinds
attr
bloq.ZXNode.is_boundary- is_port¶
Trueif this node is a port.
attr
bloq.ZXNode.is_port- is_t¶
Trueif this node is a T node.
attr
bloq.ZXNode.is_t- kind¶
"X","Y","Z","Port","T", or"Selective(<bases>)".- Type:
The node kind
attr
bloq.ZXNode.kind- pos¶
The node’s
(x, y, z)position.
attr
bloq.ZXNode.pos
class bloq.ZXNode
- class ZXEdge¶
An edge of a ZX diagram (read-only view).
Edges come from
ZXGraph.edges()orZXGraph.edge_between(); each one connects the two nodesn1/n2(by node id) and may carry a Hadamard. Edge ids live in the same id space as node ids, following them (the space thatZXGraph.total_idssizes and stabilizer Pauli strings index into).- hadamard¶
True if the edge carries a Hadamard.
attr
bloq.ZXEdge.hadamard- id¶
The edge id (edge ids follow node ids in the graph’s id space).
attr
bloq.ZXEdge.id- n1¶
The id of one endpoint node.
attr
bloq.ZXEdge.n1- n2¶
The id of the other endpoint node.
attr
bloq.ZXEdge.n2
class bloq.ZXEdge
- class Stabilizer¶
A stabilizer of the block graph’s ZX diagram (read-only view).
Rows come from
BlockGraph.stabilizers()(asStabilizerGenerator.stabilizer) andfill_ports_auto(). The operator itself ispaulis, a dense Pauli string over the ZX graph’s combined node+edge id space; the same support is also broken out by graph element:port_stabilizermaps open-port positions to Paulis (how the stabilizer meets the outside world), whileinterior_nodes/interior_edgescover spiders and edges inside the diagram. Row role and measurement identity live onStabilizerGenerator.- interior_edges¶
Pauli support on interior edges, keyed by endpoint-position pairs in ascending order.
attr
bloq.Stabilizer.interior_edges- interior_nodes¶
Pauli support on interior node positions, in ascending position order.
attr
bloq.Stabilizer.interior_nodes- paulis¶
The dense Pauli string over the ZX graph’s combined node+edge id space.
attr
bloq.Stabilizer.paulis- port_stabilizer¶
Pauli support on external port positions, in ascending position order.
attr
bloq.Stabilizer.port_stabilizer
class bloq.Stabilizer
- class StabilizerGenerator¶
A stabilizer generator row: a stabilizer plus its role.
One row of the generator basis returned by
BlockGraph.stabilizers()orZXGraph.stabilizers(). Thekindsays what the row does:"logical"rows generate the diagram’s logical stabilizer group,"measurement"rows witness a named measurement variable, and"selective_fixing"rows pin the measurement basis of one or more selective nodes. Internal rank rows are not exposed through the graph API.- is_measurement¶
Trueif this row witnesses a measurement variable.
attr
bloq.StabilizerGenerator.is_measurement- kind¶
"measurement","selective_fixing", or"logical".- Type:
The row’s role
attr
bloq.StabilizerGenerator.kind- measurement_name¶
The measurement variable name, for
"measurement"rows.
attr
bloq.StabilizerGenerator.measurement_name- selective_fixing_targets¶
(position, forbidden_pauli)pairs, for"selective_fixing"rows.
attr
bloq.StabilizerGenerator.selective_fixing_targets- stabilizer¶
The underlying stabilizer.
attr
bloq.StabilizerGenerator.stabilizer
class bloq.StabilizerGenerator
Pauli operators and geometry¶
Pauli operators and measurement bases describe logical and physical support.
PauliString represents Hermitian Pauli support without an explicit phase;
retain signs separately when an enclosing readout or parity carries them.
Direction identifies a signed lattice direction, while UDirection
identifies an unsigned axis.
- class Pauli¶
A single-qubit Pauli operator (
I,X,Y, orZ).Examples
>>> from bloq import Pauli >>> Pauli.X.anticommutes(Pauli.Z) True >>> Pauli.X.anticommutes(Pauli.X) False >>> str(Pauli.X.flip()) 'Z'- anticommutes(other: Pauli) bool¶
Two Paulis anticommute iff both are non-identity and differ.
meth
bloq.Pauli.anticommutes-
I =
Pauli.I¶
attr
bloq.Pauli.I-
X =
Pauli.X¶
attr
bloq.Pauli.X-
Y =
Pauli.Y¶
attr
bloq.Pauli.Y-
Z =
Pauli.Z¶
attr
bloq.Pauli.Z
class bloq.Pauli
- class PauliBasis¶
A single-qubit Pauli basis:
X,Y, orZ(excludes identity).A non-identity Pauli axis — unlike
Pauli, which also hasI. Use it where an axis must be definite: measurement bases in actions, feedback targets (FeedbackTarget.pauli), and compiled-circuit operations. APIs taking one usually also accept its"X"/"Y"/"Z"letter.-
X =
PauliBasis.X¶
attr
bloq.PauliBasis.X-
Y =
PauliBasis.Y¶
attr
bloq.PauliBasis.Y-
Z =
PauliBasis.Z¶
attr
bloq.PauliBasis.Z-
X =
class bloq.PauliBasis
- class PauliString(init: int | str)¶
A dense Pauli operator over a fixed number of qubits.
Build from a qubit count (identity string) or a literal like
"XZ_Y"(_orImarks identity). Indexable and mutable like a list ofPauli.Examples
>>> from bloq import PauliString >>> ps = PauliString("XZ_Y") >>> len(ps), ps.weight() (4, 3) >>> str(ps[0]) 'X' >>> str(PauliString(4)) '____'- weight() int¶
Returns the number of non-identity Paulis.
meth
bloq.PauliString.weight
class bloq.PauliString
- class Basis¶
A measurement basis: the Pauli
XorZaxis.Examples
>>> from bloq import Basis >>> str(Basis.X) 'X' >>> str(Basis.X.flip()) 'Z'-
X =
Basis.X¶
attr
bloq.Basis.X-
Z =
Basis.Z¶
attr
bloq.Basis.Z-
X =
class bloq.Basis
- class Direction¶
A signed axis direction in the block-graph lattice (
+X,-X, …,-Z).Python variant names are
X_PLUS-style;str(d)andDirection.parse(...)use the BLOG spelling ("+X","-Z", …).Examples
>>> from bloq import Direction >>> str(Direction.parse("+X")) '+X' >>> str(Direction.X_PLUS.negate()) '-X' >>> Direction.X_PLUS.vector() (1, 0, 0)- static parse(text: str) Direction¶
Parses a BLOG direction spelling like
"+X"or"-Z".- Parameters:
- text: str¶
A BLOG direction spelling, e.g.
"+X"or"-Z"(case-insensitive).
- Raises:
InvalidArgumentError – If
textis not a valid direction spelling.
meth
bloq.Direction.parse- is_spatial() bool¶
Returns
Truefor theX/Y(spatial) directions.
meth
bloq.Direction.is_spatial- vector() tuple[int, int, int]¶
Returns the unit axis vector
(x, y, z)pointing along this direction.
meth
bloq.Direction.vector-
X_MINUS =
Direction.X_MINUS¶
attr
bloq.Direction.X_MINUS-
X_PLUS =
Direction.X_PLUS¶
attr
bloq.Direction.X_PLUS-
Y_MINUS =
Direction.Y_MINUS¶
attr
bloq.Direction.Y_MINUS-
Y_PLUS =
Direction.Y_PLUS¶
attr
bloq.Direction.Y_PLUS-
Z_MINUS =
Direction.Z_MINUS¶
attr
bloq.Direction.Z_MINUS-
Z_PLUS =
Direction.Z_PLUS¶
attr
bloq.Direction.Z_PLUS
class bloq.Direction
- class UDirection¶
An unsigned axis (
X,Y, orZ).A lattice axis with no orientation — a
Directionwith its sign dropped. Use it where only the axis matters, such as the rotation axis ofBlockGraph.rotate_about_origin. APIs taking one usually also accept its"X"/"Y"/"Z"letter.-
X =
UDirection.X¶
attr
bloq.UDirection.X-
Y =
UDirection.Y¶
attr
bloq.UDirection.Y-
Z =
UDirection.Z¶
attr
bloq.UDirection.Z-
X =
class bloq.UDirection
Errors and warnings¶
Catch BloqError for domain failures, or a specific subclass when recovery
depends on the stage: parsing, compilation, IR validation, emission, or VM
execution. A resource-limited CompileError means computation stopped at a
budget; it does not prove that the source is invalid.
Normal Python errors retain their meaning. Sequence indexing and mapping lookup
raise IndexError and KeyError; argument coercion can raise TypeError
or OverflowError; filesystem access raises OSError. CompileWarning
uses Python’s warnings system for advisories from successful compilation.
- class BloqError¶
Base exception for bloq domain failures.
Python protocol, argument-conversion, and I/O errors retain their standard builtins, including IndexError, KeyError, TypeError, OverflowError, and OSError.
Examples
>>> import bloq >>> issubclass(bloq.CompileError, bloq.BloqError) True >>> issubclass(bloq.ParseError, bloq.BloqError) True
class bloq.BloqError
- class InvalidArgumentError¶
An argument failed validation, or a lookup key (id, name, position, basis/kind spelling) was not found.
class bloq.InvalidArgumentError
- class ParseError¶
A
.blogsource failed to parse (message carries the rendered diagnostic).
class bloq.ParseError
- class BlockGraphError¶
A block-graph operation or validation failed.
class bloq.BlockGraphError
- class CompileError¶
Lowering a block graph to Bloq IR failed.
class bloq.CompileError
- class CompileWarning¶
An advisory raised by a successful compile.
A
UserWarningsubclass, so it is filterable on its own:warnings.filterwarnings("error", category=bloq.CompileWarning)turns compile advisories into exceptions without touching other warnings.Examples
>>> import bloq, warnings >>> issubclass(bloq.CompileWarning, UserWarning) True
class bloq.CompileWarning
- class BloqValidationError¶
A Bloq IR program failed validation.
class bloq.BloqValidationError
- class TextParseError¶
A
.bloqirtext payload failed to parse.
class bloq.TextParseError
- class BinaryDecodeError¶
A
.bloqbinary payload failed to decode.
class bloq.BinaryDecodeError
- class StimEmissionError¶
Emitting a Stim circuit from Bloq IR failed.
class bloq.StimEmissionError
- class LowerError¶
Lowering Bloq IR to a VM program failed.
class bloq.LowerError
- class RuntimeError¶
Executing a lowered VM program failed.
class bloq.RuntimeError
Format and metadata constants¶
Use these constants for codec extensions, version checks, and compiler metadata
lookups instead of duplicating their string values. Exchange-format versions
identify the serialized layout; __version__ identifies the installed Python
package. See Bloq IR for persistence choices.
-
BLOQ_TEXT_EXTENSION =
'bloqir'¶
data bloq.ir.BLOQ_TEXT_EXTENSION
-
BLOQ_TEXT_VERSION =
1¶
data bloq.ir.BLOQ_TEXT_VERSION
-
BLOQ_BINARY_EXTENSION =
'bloq'¶
data bloq.ir.BLOQ_BINARY_EXTENSION
-
BLOQ_BINARY_VERSION =
1¶
data bloq.ir.BLOQ_BINARY_VERSION
-
CODE_DISTANCE_METADATA_KEY =
'bloq_compile.code_distance'¶
data bloq.ir.CODE_DISTANCE_METADATA_KEY
-
CONVENTION_METADATA_KEY =
'bloq_compile.convention'¶
data bloq.ir.CONVENTION_METADATA_KEY
-
CLIFFORD_PROXY_SEED_METADATA_KEY =
'bloq_compile.clifford_proxy_seed'¶
data bloq.ir.CLIFFORD_PROXY_SEED_METADATA_KEY
-
HONEST_T_TAG =
'HONEST_T'¶
data bloq.HONEST_T_TAG
-
__version__ =
'0.1.2-dev'¶
data bloq.__version__