qram_simulator¶
qram_simulator – Python bindings for the QRAM-Simulator C++ core.
Exports QRAMCircuit for both QRAM architectures (qubit / qutrit), the
full-amplitude bridge QRAMFullAmp, the timing & noise scheduler TimeStep,
and global random-seed control. See the _core module docstring and
the project documentation for details.
Submodules¶
Attributes¶
Classes¶
A single quantum operation: type, target qubits, and optional coefficients (noise parameters). |
|
A set of operations executed concurrently within one time slice (identified by name). |
|
Enum of QRAM operation types. |
|
Branch-based sparse simulator of the qubit-architecture QRAM loading circuit. |
|
Branch-based sparse simulator of the qutrit-architecture QRAM loading circuit. |
|
Bridge from the QRAM circuit to a full-amplitude state vector (qutrit |
|
Named machine-word register file with exact X / controlled-X execution. |
|
Exact amplitudes over arbitrary-width basis indices (dict-backed). |
|
A zero-default sparse memory with arbitrary-width unsigned words. |
|
The complete QRAM loading schedule: a sequence of OperationPacks ordered by time slice. |
|
Timing and noise scheduler for the QRAM loading circuit. |
Functions¶
Package Contents¶
- class qram_simulator.Operation¶
A single quantum operation: type, target qubits, and optional coefficients (noise parameters).
- property type: OperationType¶
Operation type (OperationType).
- class qram_simulator.OperationPack¶
A set of operations executed concurrently within one time slice (identified by name).
- reverse() OperationPack¶
Return the inverse operation group (each operation inverted, then reordered as in the original).
- class qram_simulator.OperationType(value: SupportsInt | SupportsIndex)¶
Enum of QRAM operation types.
Serves both as the key type of noise models (dict keys of set_noise_models) and as the type tag of an Operation. Noise members (BitFlip / PhaseFlip / BitPhaseFlip / Depolarizing / Damping, etc.) take occurrence probabilities in [0, 1]; Damping additionally requires gamma < 1 (gamma = 1 decays all excitations within a single step, breaking sampling and normalization).
Members:
ControlSwap
HadamardData
CopyIn
CopyOut
SwapInternal
FirstCopy
FetchData
SetZero
Damping
Damp_Common
Damp_Full
BitFlip
PhaseFlip
BitPhaseFlip
Depolarizing
Identity_Active
Identity_Inactive
- BitFlip: ClassVar[OperationType]¶
- BitPhaseFlip: ClassVar[OperationType]¶
- ControlSwap: ClassVar[OperationType]¶
- CopyIn: ClassVar[OperationType]¶
- CopyOut: ClassVar[OperationType]¶
- Damp_Common: ClassVar[OperationType]¶
- Damp_Full: ClassVar[OperationType]¶
- Damping: ClassVar[OperationType]¶
- Depolarizing: ClassVar[OperationType]¶
- FetchData: ClassVar[OperationType]¶
- FirstCopy: ClassVar[OperationType]¶
- HadamardData: ClassVar[OperationType]¶
- Identity_Active: ClassVar[OperationType]¶
- Identity_Inactive: ClassVar[OperationType]¶
- PhaseFlip: ClassVar[OperationType]¶
- SetZero: ClassVar[OperationType]¶
- SwapInternal: ClassVar[OperationType]¶
- class qram_simulator.QRAMCircuitQubit(addr_size: SupportsInt | SupportsIndex, data_size: SupportsInt | SupportsIndex)¶
- class qram_simulator.QRAMCircuitQubit(addr_size: SupportsInt | SupportsIndex, data_size: SupportsInt | SupportsIndex, memory: collections.abc.Sequence[SupportsInt | SupportsIndex])
Branch-based sparse simulator of the qubit-architecture QRAM loading circuit.
Carries (address, bus_input) input branches as trajectories, evolves the sparse SystemState along the noisy operation sequence generated by TimeStep, and supports good/bad branch pruning (run_normal), the unpruned reference (run_full), and fidelity sampling.
- Typical workflow:
qram = QRAMCircuitQubit(addr_size, data_size[, memory]) qram.set_noise_models({…}) # optional qram.set_input_uniform(n) # or set_input_random(n) qram.run_normal() # or run_full() / run(“normal”) qram.sample_and_get_fidelity()
Construct the QRAM circuit initialized with the given memory (memory length must be 2^addr_size).
- get_fidelity_conventions() dict¶
Return the three no-post-selection fidelity statistics of the current run as a dict (call after run_full, before sample_and_get_fidelity):
- overlap_fid: |<psi_ideal|psi_traj>|^2, tree-sensitive – statistically
matches the channel-level <psi_ideal|rho|psi_ideal> of the baseline;
- fid_nopost: same inner product but tree-insensitive (bus-correct
amplitudes with residual tree excitations still counted);
fid_incoh: incoherent (branch-projected) combination of the same.
- get_logical_query_fidelity() float¶
Fidelity of the address/bus reduced state after tracing out the routing tree. Requires canonical zero-bus input with one column per address. No postselection, measurement or RNG draws; call after run, before tree/output sampling. This differs from full-circuit overlap_fid and from the older tree-insensitive conventions.
- get_noise_models() dict[OperationType, float]¶
Return the current noise models (reference view).
- get_output_distribution() dict[str, float]¶
Return the marginal (address:bus) output distribution of the current branch groups as a {“addr:bus”: prob} dict (call after run_full / run_normal; mutating calls such as sample_and_get_fidelity invalidate it).
- get_qubit_num() int¶
Return the number of physical qubits used by the circuit: 2 * (2^addr_size - 1).
- initialize_system() None¶
Generate the noisy operation sequence via TimeStep and reset all branch groups (called internally by the run family).
- prepare_observables() None¶
Materialize implicit pruned outputs before reading distribution/overlap; does not measure the tree or consume random draws. Call after run_normal, before sampling.
- run() None¶
- run(version: str) None
Run by version string: “full” (unpruned) / “normal” (pruned) / “fast” (good branches only).
- run_full() None¶
Unpruned run over all branches, serving as the ground-truth reference of the pruned result.
- run_normal() None¶
Pruned run: all bad branches plus one good branch as reference (the default entry point for daily use).
- sample_and_get_fidelity() float¶
Sample the output branches and compute the loading fidelity (squared modulus of the inner product between the ideal and actual states).
- set_input_addresses(addresses: collections.abc.Sequence[SupportsInt | SupportsIndex]) None¶
Equal coherent address superposition with an initially zero bus; addresses must be unique.
- set_input_random(n_inputs: SupportsInt | SupportsIndex) None¶
Randomly sample n input branches, drawing input probabilities ~ U(0,1) then normalizing.
- set_input_uniform(n_inputs: SupportsInt | SupportsIndex) None¶
Randomly sample n input branches with equal input probability each.
- set_input_zerobus() None¶
Set the zerobus input: an equal-probability branch per address, all with bus input 0 (deterministic, no RNG draw; the discriminating input used by the circuit-level baseline comparison).
- set_memory(memory: collections.abc.Sequence[SupportsInt | SupportsIndex]) None¶
Set the data tree (memory length must be 2^addr_size).
- set_noise_models(noises: collections.abc.Mapping[OperationType, SupportsFloat | SupportsIndex]) None¶
Set noise models {OperationType: probability}.
Probabilities must lie in [0,1]; Damping requires gamma < 1, otherwise a RuntimeError is raised.
- class qram_simulator.QRAMCircuitQutrit(addr_size: SupportsInt | SupportsIndex, data_size: SupportsInt | SupportsIndex)¶
- class qram_simulator.QRAMCircuitQutrit(addr_size: SupportsInt | SupportsIndex, data_size: SupportsInt | SupportsIndex, memory: collections.abc.Sequence[SupportsInt | SupportsIndex])
Branch-based sparse simulator of the qutrit-architecture QRAM loading circuit.
Each tree node is a three-level system (addr ∈ {W, L, R}, data), evolved along the noisy operation sequence via SubBranch trajectories. The workflow matches QRAMCircuitQubit: construct -> set memory -> set noise -> set_input_* -> run_normal/run_full -> sample_and_get_fidelity.
Construct the QRAM circuit initialized with the given memory.
- get_branch_probs() list[float]¶
Return the normalized probabilities of all branches (reference view).
- get_noise_models() dict[OperationType, float]¶
Return the current noise models (reference view).
- get_qubit_num() int¶
Return the number of physical qubits used by the circuit: 2 * (2^address_size - 1).
- initialize_system() None¶
Generate the noisy operation sequence via TimeStep and initialize the branch structure (called internally by the run family).
- run(version: str) None¶
Run by version string (“full” / “normal”, etc.; same as QRAMCircuitQubit.run).
- run_full() None¶
Unpruned run over all branches, serving as the ground-truth reference of the pruned result.
- run_normal() None¶
Pruned run: all bad branches plus one good branch as reference (the default entry point for daily use).
- set_input_random(n_inputs: SupportsInt | SupportsIndex) None¶
Randomly sample n input branches and fill the branch probabilities.
- set_input_uniform(n_inputs: SupportsInt | SupportsIndex) None¶
Randomly sample n input branches with equal input probability each.
- set_memory(memory: collections.abc.Sequence[SupportsInt | SupportsIndex]) None¶
Set the data tree.
- set_noise_models(noises: collections.abc.Mapping[OperationType, SupportsFloat | SupportsIndex]) None¶
Set noise models {OperationType: probability}.
Probabilities must lie in [0,1]; Damping requires gamma < 1, otherwise a RuntimeError is raised.
- class qram_simulator.QRAMFullAmp(addr_size: SupportsInt | SupportsIndex, data_size: SupportsInt | SupportsIndex, memory: collections.abc.Sequence[SupportsInt | SupportsIndex])¶
Bridge from the QRAM circuit to a full-amplitude state vector (qutrit architecture only).
An external full-amplitude simulator (or a user script) supplies the state vector and qubit mapping; apply composites one QRAM loading onto that state vector and returns the new vector, embedding QRAM simulation into a larger circuit-evolution pipeline.
Example
manipulator = QRAMFullAmp(2, 2, [0, 1, 2, 3]) manipulator.set_noise_models({OperationType.Depolarizing: 1e-3}) out = manipulator.apply(state, [0, 1], [2, 3], [], version=”normal”)
Construct the bridge: internally creates a qutrit QRAMCircuit loaded with the given memory.
- apply(state: collections.abc.Sequence[SupportsComplex | SupportsFloat | SupportsIndex], address_qubits: collections.abc.Sequence[SupportsInt | SupportsIndex], data_qubits: collections.abc.Sequence[SupportsInt | SupportsIndex], other_qubits: collections.abc.Sequence[SupportsInt | SupportsIndex], version: str = 'normal') list[complex]¶
Composite one QRAM loading onto a full-amplitude state vector.
- Parameters:
state – list of complex amplitudes (length 2^total qubit count)
other_qubits (address_qubits / data_qubits /) – indices of the three qubit groups
version – “normal” (pruned) or “full” (unpruned)
- Returns:
the new state vector after loading (list of complex numbers)
- set_noise_models(noises: collections.abc.Mapping[OperationType, SupportsFloat | SupportsIndex]) None¶
Set the noise models of the internal QRAMCircuit (same constraints as QRAMCircuitQutrit).
- class qram_simulator.ReferenceWordBackend(machine_word_bits: int = 64)[source]¶
Named machine-word register file with exact X / controlled-X execution.
Every operation is an exact unitary permutation of the amplitudes — a genuine statevector execution, not a call recorder. Bit indices are confined to one machine word; wider logical registers span several word registers (see
word_layout()).- X_Bool(name: str, bit_index: int) WordXOperator[source]¶
- make_state() SparseStateVector[source]¶
- class qram_simulator.SparseStateVector[source]¶
Exact amplitudes over arbitrary-width basis indices (dict-backed).
- class qram_simulator.SparseWordMemory(address_width: int, data_width: int, cells: collections.abc.Mapping[int, int])[source]¶
A zero-default sparse memory with arbitrary-width unsigned words.
address_widthanddata_widthare logical widths in bits; every stored value keeps its exact high bits in arbitrary-precision Python integers, so words are never packed into a single 64-bit machine word. Cells mapped to zero are dropped: the memory is zero by default.- apply_xor(state, address_bits: Sequence[BitCoordinate], data_bits: Sequence[BitCoordinate], backend, *, conditions=tuple()) None[source]¶
Apply the exact coherent query on native machine-word bit coordinates.
address_bits[i]/data_bits[i]are the(word_register_name, bit_index)coordinates of logical biti(little-endian) of the address / data registers; a logical register wider than one machine word spans several word registers.backendmust be the caller’s real native backend (see the module docstring protocol), not a mock. Conditions are(little_endian_bits, expected_value)pairs; a stored cell is queried only on basis states satisfying all of them. Shared bits between conditions and the address register are merged — a zero control coinciding with an address bit restricts the query to cells whose address agrees with it, and never re-addresses a cell.For each stored cell the emission is: toggle the zero-valued address/control bits to 1, apply one controlled native X per set data bit conditioned on all required bits, then restore the toggles. This is exact: basis states not matching the merged pattern are untouched by the transient toggles.
- class qram_simulator.TimeSlices¶
The complete QRAM loading schedule: a sequence of OperationPacks ordered by time slice.
- reverse() TimeSlices¶
Return the reversed schedule (overall order flipped and each group inverted).
- property time_slices: list[OperationPack]¶
List of time slices (list[OperationPack]; reads return a copy).
- class qram_simulator.TimeStep(addr_size: SupportsInt | SupportsIndex, data_size: SupportsInt | SupportsIndex)¶
Timing and noise scheduler for the QRAM loading circuit.
Derives the routing, copy, and swap operations of each time slice from the address / data widths, and can insert noise operations after every time slice according to the noise models, producing the complete TimeSlices schedule. QRAMCircuit holds a TimeStep instance internally and calls generate in initialize_system to produce the noisy operation sequence.
Construct the scheduler.
- Parameters:
addr_size – address width
data_size – data width
- generate(noises: collections.abc.Mapping[OperationType, SupportsFloat | SupportsIndex], arch_type: SupportsInt | SupportsIndex) TimeSlices¶
Generate the noisy schedule.
- Parameters:
noises – noise models, a {OperationType: probability} dict (may be empty)
arch_type – architecture constant, ARCH_QUBIT or ARCH_QUTRIT
- generate_step(step: SupportsInt | SupportsIndex) OperationPack¶
Generate the operation group of the given time slice (noise-free).
- Parameters:
step – time-slice index (1-based)
- get_bad_range_qubit(bad_qubit: SupportsInt | SupportsIndex) tuple[int, int]¶
Qubit architecture: the bad-branch address range [lo, hi] derived from the bad qubit.
- get_bad_range_qutrit(bad_qubit: SupportsInt | SupportsIndex) tuple[int, int]¶
Qutrit architecture: the bad-branch address range [lo, hi] derived from the bad qubit.
- is_bad_branch(addr: SupportsInt | SupportsIndex) bool¶
Check whether address addr falls inside the bad-branch range (bad range).
- layer_entangle_max(step: SupportsInt | SupportsIndex) int¶
Maximum number of tree layers entangled at the given time slice.
Position-sampled noise of that slice lives on the tree positions [0, 2*(2^l - 1)); steps with l = 0 carry none (addr_size = 1 therefore has no position-sampled noise; under Damping only Damp_Common acts).
- property time_slices_noise_free: TimeSlices¶
Noise-free schedule (TimeSlices).