Module builder¶
oracq.infrastructure.builder
Python generation-stage builder. Generated results contain no Python callables.
- oracq.infrastructure.builder.RegisterSpec¶
a mapping from register names to storage types.
- Type:
Register specification for the Builder constructor
alias of
dict[str,RegType]
- class oracq.infrastructure.builder.Operation(module, dependencies=())[source]¶
Bases:
objectA complete RIR operation consisting of an entry module and its dependency modules.
- Variables:
module (oracq.infrastructure.ir.Module) – entry module definition.
dependencies (tuple[oracq.infrastructure.ir.Module, ...]) – all dependency module definitions other than the entry.
- module¶
- dependencies = ()¶
- unitary()[source]¶
Full-register unitary action of the RIR operation; does not claim any target matrix is itself unitary.
- Returns:
returns itself unchanged; an RIR operation is itself unitary evolution on the full register space.
- Return type:
- state_preparation()[source]¶
Prepare U|0> with the full public register space as target.
- Returns:
state preparation object obtained by wrapping this operation via
from_unitary.- Return type:
- block_encoding()[source]¶
Trivial alpha=1, zero-signal block encoding of the full unitary itself; keeps the original module calls.
- Returns:
trivial block encoding with scaling factor 1.0 and a zero-width signal register.
- Return type:
- classmethod from_program(program)[source]¶
Recover the entry operation and module dependencies from a serialized program, keeping open declarations.
- Parameters:
program – complete RIR program to recover; modules other than the entry become dependencies.
- Returns:
operation whose entry is
program.mainand whose dependencies are all remaining modules.- Return type:
- program()[source]¶
Merge the entry module and dependencies into a validated
Programwith modules sorted by name.- Returns:
program with
module.nameas its entry.- Return type:
- Raises:
ValidationError – conflicting definitions exist for a dependency module name, or the merged result violates RIR rules.
- quantikz(**kwargs)[source]¶
Render the entry module as quantikz code; structure stays RIR with no call expansion.
- Returns:
quantikz LaTeX source produced by rendering the entry module.
- Return type:
str
- class oracq.infrastructure.builder.Builder(name, registers, resources=None, *, attributes=None)[source]¶
Bases:
objectGeneration-stage builder that constructs an RIR module instruction by instruction; generated results contain no Python callables.
- Parameters:
name – name of the module to generate.
registers – mapping from public register names to bit width types.
resources – mapping from public QRAM resource names to specifications; omitted means the module has no resources.
attributes – attribute key-value pairs appended to the module; omitted means empty.
Public registers are accessed as
Refviews viabuilder[name],localdeclares module-private work registers, andfinishcloses the builder and returns a validatedOperation.- name¶
- attributes¶
- registers¶
- resources¶
- locals¶
- local(name, type)[source]¶
Declare a module-private work register and return the full view covering it.
- Parameters:
name – register name; must not collide with a public register, a resource or another local register.
type – type and bit width of the register.
- Returns:
reference covering the entire new register.
- Return type:
- Raises:
ValidationError – name conflict, or the builder is already closed.
- emit(instruction)[source]¶
Append one RIR instruction to the current innermost scope.
- Parameters:
instruction – RIR
Instructionnode.- Raises:
ValidationError – the builder is already closed.
- gate(name, target, angle=None)[source]¶
Emit a single-target
Primitivegate.- Parameters:
name – gate name.
target – target view.
angle – angle in radians for rotation gates; omitted for non-rotation gates.
- ry(target, angle)[source]¶
Emit a gate rotating by
angleradians about the Y axis.- Parameters:
target – target view the rotation acts on.
angle – rotation angle in radians.
- rz(target, angle)[source]¶
Emit a gate rotating by
angleradians about the Z axis.- Parameters:
target – target view the rotation acts on.
angle – rotation angle in radians.
- xor(source, target)[source]¶
Emit a bitwise XOR gate that XORs the value of
sourceinto the equal-widthtarget.- Parameters:
source – source view providing the value to XOR in.
target – target view of the same width as
sourcethat accumulates the XOR result.
- swap(first, second)[source]¶
Emit a swap gate between the two equal-width views
firstandsecond.- Parameters:
first – first view participating in the swap.
second – second view of the same width as
first.
- add_const(target, value)[source]¶
Emit a constant-addition gate that adds
valuetotargetwith wraparound by bit width.- Parameters:
target – target view that undergoes the addition and wraps by its bit width.
value – integer constant to add.
- global_phase(angle)[source]¶
Emit a global phase gate that multiplies the overall amplitude by
exp(i*angle).- Parameters:
angle – overall phase angle in radians.
- qram(resource, address, data)[source]¶
Emit a QRAM read: query
resourceataddressand XOR the hit word intodata.- Parameters:
resource – name of the QRAM resource to query.
address – view providing the address of the cell to query.
data – equal-width target view into which the hit data word is XORed.
- store(resource, address, data)[source]¶
Emit a QRAM write: store
datainto celladdressofresource.- Parameters:
resource – name of the target QRAM resource to write.
address – address view specifying the cell to write.
data – source view providing the data to write.
- call(operation, *, resources=None, **arguments)[source]¶
Call another
Operationas-is; the callee stays aCallnode and is not expanded at generation time.- Parameters:
operation – operation to call; its module and dependencies are registered as dependencies of this module.
resources – mapping from callee resource formal parameter names to this module’s resource names.
**arguments – argument views keyed by callee register names.
- Raises:
ValidationError – quantum or resource argument names do not match, or conflicting definitions exist for a dependency module name.
- repeat(count)[source]¶
Open a scope repeated
counttimes; body instructions aggregate into aRepeatnode whose count stays symbolic and is not expanded at generation time.- Parameters:
count – number of times the body scope repeats; stored symbolically in the
Repeatnode.- Returns:
scope context for use with a
withstatement; all blocks must be closed per theirwithnesting beforefinish.
- control(register, value=None)[source]¶
Open a controlled scope; body instructions aggregate into a
Controlnode.- Parameters:
register – control view.
value – integer mask that activates the scope; omitted means the all-ones mask of the register.
- Returns:
scope context for use with a
withstatement; all blocks must be closed per theirwithnesting beforefinish.
- adjoint()[source]¶
Open an inverse scope; body instructions aggregate into an
Adjointblock executed in reverse order with each operation inverted.- Returns:
scope context for use with a
withstatement; all blocks must be closed per theirwithnesting beforefinish.
- finish()[source]¶
Finish construction: assemble the module with all registered dependencies, validate and return an
Operation, then close the builder.- Returns:
operation whose entry is the module built here and whose dependencies are the registered callees.
- Return type:
- Raises:
ValidationError – the builder is already closed, scopes are still open, or the assembled result violates RIR rules.