Oracle declarations and implementations¶
oracq.algorithms.input_model.oracles
Catalog of oracle paradigms and plain gate/QRAM implementations. Mathematical correctness certification is out of scope for this module.
- oracq.algorithms.input_model.oracles.PARADIGMS = {'algorithm_stage': 'an unfinished algorithm stage with an explicit register interface', 'block_encoding': 'combined convention of the zero-signal projected-angle block and be_alpha', 'database_xor': '|address,data> -> |address,data XOR memory[address]>', 'phase_oracle': 'applies a phase to target basis states per a defined predicate', 'reversible_function': 'domain computation interface with inputs preserved and outputs reversibly updated', 'sparse_entry_xor': 'CKS P_A(2): |row,column,data> -> |row,column,data XOR A[row,column]>', 'sparse_location_inplace': 'CKS P_A(1): |column,index> -> |column,nu(column,index)>', 'state_prep_isometry': 'prepares the target state from the zero-state subspace; adjoint and controlled use requires a reversible extension', 'unitary': 'quantum operation interface over the full register space'}¶
Built-in oracle role conventions; applications may use custom identifiers without modifying this catalog.
- oracq.algorithms.input_model.oracles.declare(name, registers, *, paradigm='unitary', resources=None, attributes=None, supports_adjoint=True, supports_controlled=True)[source]¶
Declare an open oracle operation.
Generates an
Operationwith an empty body andimplementation_statusset tounresolved; the structure is validated at generation time, and the implementation can be closed viaannotateor binding.- Parameters:
name – Module name.
registers – Ordered mapping from register names to types such as
Bits.paradigm – Built-in or application-defined role identifier; the role name itself proves no mathematical property.
resources – Mapping from resource names to resource types such as
QRAM.attributes – Extra attributes merged into the module.
supports_adjoint – Whether to declare support for the adjoint operation.
supports_controlled – Whether to declare support for the controlled operation.
- Returns:
The unresolved open declaration.
- Return type:
- Raises:
ValidationError – The paradigm name is invalid, or the generated module fails structural validation.
- oracq.algorithms.input_model.oracles.annotate(operation, paradigm, **attributes)[source]¶
Register the paradigm and attributes of a generated implementation operation.
Sets
implementation_statustoconstructedand writesoracle_paradigm; adjoint/controlled capabilities explicitly restricted toFalsecannot be re-granted through annotation. When the paradigm isstate_prep_isometry, thezero_inputandclean_workpromises are added by default.- Parameters:
operation – The generated
Operation.paradigm – Built-in or application-defined role identifier.
**attributes – Additional key-value pairs merged into the module attributes.
- Returns:
A new
Operationwith the attributes replaced.- Return type:
- Raises:
ValidationError – The input type or paradigm name is invalid, or a restricted capability is being restored.
- oracq.algorithms.input_model.oracles.resources_for(*items)[source]¶
Aggregate the resources of several operations and rename them as
prefix__resource_name.- Parameters:
*items – Pairs of the form
(prefix, operation), whereoperationis anOperationcarrying resource declarations.- Returns:
Mapping from
prefix__resource_namekeys to resource types, for the nestedBuilderto declare resources.- Return type:
dict
- oracq.algorithms.input_model.oracles.invoke(builder, operation, prefix='', **arguments)[source]¶
Invoke an operation in a builder with a prefixed resource mapping.
- Parameters:
builder – The current
Builder, whose resources must already be declared under the same prefix.operation – The
Operationto invoke.prefix – Resource name prefix; an empty string passes resource names through unchanged.
**arguments – Bindings from the invoked module’s register names to argument views.
- Raises:
ValidationError – A register or resource argument name does not match the invoked module, or a dependency definition conflicts.
- class oracq.algorithms.input_model.oracles.XorDatabase(operation)[source]¶
Bases:
OracleViewXOR database view:
|address,data> -> |address,data XOR memory[address]>.Reversible quantum access interface for a classical lookup table; the wrapped
Operationcontains exactly the two bits registersaddressanddata, and the XOR semantics is self-inverse under any initial value.- Variables:
operation (oracq.infrastructure.builder.Operation) – The wrapped
Operation.
- oracle_kind = 'database_xor'¶
- operation¶
- xor_database()[source]¶
Return self; the view adapter method implementing
XorDatabaseProtocol.- Returns:
This view itself.
- Return type:
- property address_width¶
Bit width of the
addressregister.
- property data_width¶
Bit width of the
dataregister.
- class oracq.algorithms.input_model.oracles.StatePreparation(operation)[source]¶
Bases:
OracleViewZero-input state preparation view: an isometry acting from the zero-state subspace.
The convention is
V|0,0> = |psi,0>, i.e. theworkregister is restored to zero; the adjoint and controlled operations require a reversible extension to exist. The wrappedOperationcontains exactly the two bits registerstargetandwork.- Variables:
operation (oracq.infrastructure.builder.Operation) – The wrapped
Operation.
- oracle_kind = 'state_prep_isometry'¶
- operation¶
- state_preparation()[source]¶
Return self; the view adapter method implementing
StatePreparationProtocol.- Returns:
This view itself.
- Return type:
- classmethod from_unitary(operation, *, target=None, work=None, clean_work=False)[source]¶
Explicitly assign the U|0> zero-input role; with a work register, the caller promises it is restored to zero.
- Parameters:
operation – The full-unitary
Operation.target – Name of the register carrying the state preparation; by default all registers are concatenated into target.
work – Work register name; may only be given when target is given explicitly.
clean_work – Must be True when work has nonzero width, promising that work is restored to zero after preparation.
- Returns:
The preparation view wrapping the operation in the zero-input role.
- Return type:
- property width¶
Bit width of the
targetregister.
- property work_width¶
Bit width of the
workregister.
- class oracq.algorithms.input_model.oracles.StateOracle(operation)[source]¶
Bases:
OracleViewState output view: the success subspace is defined by
signalbeing all zeros.The output carrier for algorithm stages such as solver kernels; the wrapped
Operationcontains exactly the two bits registerstargetandsignal, and reading thesignal == 0branch yields the target state.- Variables:
operation (oracq.infrastructure.builder.Operation) – The wrapped
Operation.
- oracle_kind = 'state_oracle'¶
- operation¶
- state_oracle()[source]¶
Return self; the view adapter method implementing
StateOracleProtocol.- Returns:
This view itself.
- Return type:
- property width¶
Bit width of the
targetregister.
- property signal_qubits¶
Bit width of the
signalregister.
- class oracq.algorithms.input_model.oracles.SparseAccess(location, entry, width, value_width, sparsity)[source]¶
Bases:
OracleViewBundle view of CKS sparse access: a composition of the position and entry query operations, not disguised as a total unitary.
- Variables:
location (oracq.infrastructure.builder.Operation) – Position operation
P_A(1), permutingindexin place, with signaturecolumn,index,work.entry (oracq.infrastructure.builder.Operation) – Entry operation
P_A(2), queryable at any row and column, with signaturerow,column,data.width (int) – Number of bits of the matrix dimension, range 1..64.
value_width (int) – Word width of an entry value, range 1..64.
sparsity (int) – Upper bound on nonzeros per column, range 1..2**width.
- location¶
- entry¶
- width¶
- value_width¶
- sparsity¶
- sparse_access()[source]¶
Return self; the view adapter method implementing
CKSSparseProtocol.- Returns:
This view itself.
- Return type:
- describe()[source]¶
Aggregate the two components into a read-only description of the whole bundle.
The type is
cks_sparseandanc_qubitis alwaysNone; the adjoint and controlled capabilities are the conjunction of the two components, the bundle is markedclosedonly when every component is closed, and thesparsityandvalue_widthparameters are attached.- Returns:
Description of the whole bundle, with components named
positionandentry.- Return type:
- oracq.algorithms.input_model.oracles.abstract_database(name, address_width, data_width)[source]¶
Return an open XOR database declaration with no bound implementation.
- Parameters:
name – Declaration name.
address_width – Bit width of the
addressregister.data_width – Bit width of the
dataregister.
- Returns:
An open declaration with an empty body, closed by binding an implementation.
- Return type:
- oracq.algorithms.input_model.oracles.abstract_state_prep(name, width, work_width=0, *, reversible=True)[source]¶
Return an open state preparation declaration with no bound implementation.
- Parameters:
name – Declaration name.
width – Bit width of the
targetregister.work_width – Bit width of the
workregister, zero by default.reversible – When
False, declare no support for adjoint or controlled.
- Returns:
An open declaration carrying the
zero_inputandclean_workpromises.- Return type:
- oracq.algorithms.input_model.oracles.abstract_block_encoding(name, width, signal_width, alpha)[source]¶
Return an open block encoding declaration with no bound implementation.
- Parameters:
name – Declaration name.
width – Bit width of the
targetregister.signal_width – Bit width of the
signalregister.alpha – Normalization constant, written into the
be_alphaattribute.
- Returns:
An open declaration with an empty body.
- Return type:
- oracq.algorithms.input_model.oracles.abstract_sparse_access(name, width, value_width, sparsity, work_width=None)[source]¶
Return an open sparse-access bundle with no bound implementation.
Generates the two open operations
name_positionandname_entry; the position operation carries thesparsityand full permutation extension attributes.- Parameters:
name – Declaration name prefix.
width – Number of bits of the matrix dimension.
value_width – Word width of an entry value.
sparsity – Upper bound on nonzeros per column, at most
2**width.work_width –
workbit width of the position operation, defaults towidth.
- Returns:
A bundle of the two open declarations for position and entry.
- Return type:
- Raises:
ValidationError –
sparsityexceeds the dimension.
- oracq.algorithms.input_model.oracles.gate_database(address_width, data_width, table, *, name=None)[source]¶
Implement an XOR database with a gate-level truth table.
Nonzero table words apply X gates bit by bit under address control; the gate count grows with the table size, suitable as a small-instance witness.
- Parameters:
address_width – Bit width of the
addressregister.data_width – Bit width of the
dataregister.table – Mapping from addresses to words, or a word sequence enumerated by address.
name – Module name; generated deterministically from the content by default.
- Returns:
Gate-level implementation annotated
implementation="gate_truth_table".- Return type:
- Raises:
ValidationError – A table entry is not an integer, or an address or word exceeds its bit-width range.
- oracq.algorithms.input_model.oracles.qram_database(address_width, data_width, *, name=None)[source]¶
Implement an XOR database with a QRAM resource.
Declares a
QRAM(address_width, data_width)resource namedtableand emits one query primitive; the data table is supplied at execution time as a memory and never enters the IR.- Parameters:
address_width – Bit width of the
addressregister.data_width – Bit width of the
dataregister.name – Module name; generated from the bit widths by default.
- Returns:
Resource-based implementation annotated
implementation="qram".- Return type:
- oracq.algorithms.input_model.oracles.basis_state(width, value=0, *, work_width=0)[source]¶
Prepare the computational basis state
|value>with bit-by-bit X gates.- Parameters:
width – Bit width of the
targetregister.value – Integer index of the target basis state, range
0 .. 2**width - 1.work_width – Bit width of the
workregister, zero by default.
- Returns:
Gate-level preparation carrying the
zero_inputpromise.- Return type:
- Raises:
ValidationError –
valueis not an integer or is out of range.
- oracq.algorithms.input_model.oracles.uniform_state(width, *, work_width=0)[source]¶
Apply Hadamard to the whole
targetto prepare the uniform superposition state.- Parameters:
width – Bit width of the
targetregister.work_width – Bit width of the
workregister, zero by default.
- Returns:
Gate-level preparation carrying the
zero_inputpromise.- Return type:
- oracq.algorithms.input_model.oracles.gate_state_prep(amplitudes, *, work_width=0, name=None)[source]¶
Prepare an arbitrary complex-amplitude state with a multiplexed Ry rotation tree.
The amplitudes decompose over a binary tree into controlled
Ryangles, and nonzero phase components are compensated pointwise by a controlledglobal_phase, so amplitudes may be complex.- Parameters:
amplitudes – Finite complex amplitude sequence of power-of-two length with nonzero norm.
work_width – Bit width of the
workregister, zero by default.name – Module name; generated deterministically from the amplitude content by default.
- Returns:
Gate-level preparation annotated
implementation="multiplexed_rotations".- Return type:
- Raises:
ValidationError – The amplitude vector is invalid, or there is a single amplitude and no target bit.
- oracq.algorithms.input_model.oracles.qram_state_prep(width, angle_width=8)[source]¶
Implement rotation-tree state preparation with a QRAM angle table.
Layer by layer, the high-bit prefix of
targetis swapped into the address and the angle table is queried; controlled Ry rotations are applied according to the bits of the angle word and the address is then restored to zero, with two queries per layer; the angle resolution2*pi/2**angle_widthis the source of the quantization error.- Parameters:
width – Bit width of the
targetregister.angle_width – Bit width of the angle word.
- Returns:
Resource-based preparation annotated
implementation="qram_rotation_tree";workismax(1, width) + angle_widthbits, withqram_queriesattached as an attribute.- Return type:
- oracq.algorithms.input_model.oracles.qram_state_angles(amplitudes, angle_width=8)[source]¶
Generate the QRAM rotation-tree angle table from an amplitude vector.
Keys are tree node indices of the form
(1 << depth) - 1 + prefixand values are Ry angle words quantized to the2*pi/2**angle_widthgrid, supplied at execution time as the angle-table memory.- Parameters:
amplitudes – Nonnegative real amplitude sequence of power-of-two length with nonzero norm.
angle_width – Bit width of the angle word.
- Returns:
Mapping from tree node indices to angle words.
- Return type:
dict
- Raises:
ValidationError – The amplitude vector is invalid, or contains amplitudes with negative real part or nonzero imaginary part.
- oracq.algorithms.input_model.oracles.phase_marks(width, marked)[source]¶
Generate a phase oracle applying phase
pito the given computational basis states.- Parameters:
width – Bit width of the
targetregister.marked – Iterable of integer indices of basis states to mark; duplicates are removed automatically.
- Returns:
Operation annotated with the
phase_oracleparadigm.- Return type:
- oracq.algorithms.input_model.oracles.sparse_location_gate(width, permutations, *, work_width=None, name=None)[source]¶
Implement the CKS in-place position query with a gate-level permutation network.
Each column decomposes its permutation into transpositions by cycle under column control, and each transposition becomes a round-trip path of controlled X; the gate count grows with
2**width, suitable only as a small-instance witness.- Parameters:
width – Bit width of the
columnandindexregisters.permutations –
2**widthpermutations given per column, each a complete arrangement of0 .. 2**width - 1.work_width – Bit width of the
workregister, defaults towidth.name – Module name; generated deterministically from the content by default.
- Returns:
Operation annotated with the
sparse_location_inplaceparadigm and the full permutation extension attribute.- Return type:
- Raises:
ValidationError – The number of columns is not
2**width, or some column is not a complete permutation.
- oracq.algorithms.input_model.oracles.sparse_location_qram(width)[source]¶
Implement the CKS in-place position query with a forward and an inverse QRAM table.
Done in three steps:
work ^= forward[column, index],swap(index, work),work ^= inverse[column, index]; the last step restoresworkto zero whileindexholds the new value.- Parameters:
width – Bit width of the
column,index, andworkregisters.- Returns:
Operation annotated with the
sparse_location_inplace- Return type:
paradigm, declaring the two
QRAM(2*width, width)resourcesforwardandinverse.
- oracq.algorithms.input_model.oracles.sparse_entry(database, width)[source]¶
Adapt an XOR database into a matrix entry query at any row and column.
rowandcolumnconcatenate into a2*width-bit address driving the database, anddataholds the XOR result.- Parameters:
database – An XOR database whose
address_widthis exactly2*width.width – Bit width of rows and columns respectively.
- Returns:
Entry query operation annotated with the
sparse_entry_xorparadigm.- Return type:
- Raises:
ValidationError – The database address width is not
2*width.
- oracq.algorithms.input_model.oracles.diagonal_block_encoding(database, *, alpha=1.0, angle_scale=None)[source]¶
Control a signal Ry by the queried word, encoding the diagonal matrix determined by that angle table.
- Parameters:
database – XOR database storing rotation angle words, with address driving target.
alpha – Block encoding normalization constant.
angle_scale – Angle step of bit k of the data word; defaults to
2π/2**data_width.
- Returns:
Block encoding whose diagonal entries are
alpha*cos(encoded_angle/2).- Return type:
- oracq.algorithms.input_model.oracles.banked_database(address_width, data_width, *, name='BankedData', abstract=False)[source]¶
Split a wide data word into registers and QRAM banks of at most 64 bits each.
- Parameters:
address_width – Bit width of the address word.
data_width – Total bit width of the logical data word, which must be a positive integer.
name – Name of the generated operation,
BankedDataby default.abstract – When True, generate an open declaration with an empty body instead of a QRAM implementation.
- Returns:
Database operation split little-endian into banks of at most 64 bits each.
- Return type: