QRAM Operators¶
QRAM (Quantum Random Access Memory) operators implement quantum parallel data access.
Overview¶
Operator |
Operation |
Unitarity class |
|---|---|---|
|
Standard QRAM load |
SelfAdjoint |
|
Optimized QRAM load |
SelfAdjoint |
Quantum Parallel Data Access¶
QRAM implements quantum parallel memory access:
This allows quantum algorithms to access multiple data items simultaneously.
—
QRAMLoad¶
Operation: Loads data from the QRAM circuit into a quantum register
Type constraints:
- Address register: UnsignedInteger
- Data register: UnsignedInteger or General
Bit constraints: - The address size must match the QRAM configuration - The data size must match the QRAM data width
import pysparq as ps
ps.System.clear()
# Create a QRAM circuit (qutrit version)
addr_size = 4 # 4-bit address → 16 memory cells
data_size = 8 # 8-bit data
# Set the memory contents (memory must be passed in at construction)
memory = [i * 10 for i in range(16)] # 0, 10, 20, ..., 150
qram = ps.QRAMCircuit_qutrit(addr_size, data_size, memory)
# Create registers
ps.System.add_register("addr", ps.UnsignedInteger, addr_size)
ps.System.add_register("data", ps.UnsignedInteger, data_size)
state = ps.SparseState()
# Uniform superposition on the address register
ps.Hadamard_Int_Full("addr")(state)
# QRAM load: access all addresses in parallel
ps.QRAMLoad(qram, "addr", "data")(state)
ps.pprint(state)
# The output contains 16 states:
# |addr=0,data=0⟩, |addr=1,data=10⟩, ...
# QRAMLoad is self-adjoint; applying it again undoes the load
ps.QRAMLoad(qram, "addr", "data")(state)
# The data register is zeroed
QRAMLoadFast¶
Operation: Optimized QRAM load
Characteristics: Optimized for specific memory patterns, with higher performance.
# Fast load (same interface)
ps.QRAMLoadFast(qram, "addr", "data")(state)
—
QRAMCircuit_qutrit¶
PySparQ provides the QRAMCircuit_qutrit class for configuring a QRAM:
Note
QRAMCircuit_qubit (the qubit-version QRAM in the C++ API) is not available in PySparQ. PySparQ provides only the qutrit version.
Creating and Configuring a QRAM Circuit¶
import pysparq as ps
# Create a QRAM circuit
addr_bits = 4 # number of address bits
data_bits = 8 # number of data bits
# Set the memory contents (memory must be passed in at construction)
memory_list = [0, 10, 20, 30, 40, 50, 60, 70,
80, 90, 100, 110, 120, 130, 140, 150]
qram = ps.QRAMCircuit_qutrit(addr_bits, data_bits, memory_list)
# Note: the set_memory() method is not available in PySparQ
# memory must be passed in when constructing QRAMCircuit_qutrit
# Query information
print(f"Address size: {qram.addr_size}")
print(f"Data size: {qram.data_size}")
Conditional Loading¶
QRAM loading supports conditional execution:
op = ps.QRAMLoad(qram, "addr", "data")
# Load only when control is nonzero
op.conditioned_by_nonzeros("control")(state)
# Load only when bit 0 of flag is 1
op.conditioned_by_bit("flag", 0)(state)
Use Cases¶
Quantum Database Search¶
# Database contents
database = [42, 17, 99, 5, ...]
# Create the QRAM (memory is passed in at construction)
qram = ps.QRAMCircuit_qutrit(addr_bits, data_bits, database)
# Address register in superposition
ps.Hadamard_Int_Full("addr")(state)
# Load all data items in parallel
ps.QRAMLoad(qram, "addr", "data")(state)
# Now you can search for a specific value...
Quantum Machine Learning¶
# Load training data (memory is passed in at construction)
qram = ps.QRAMCircuit_qutrit(addr_bits, data_bits, training_data)
ps.Hadamard_Int_Full("sample_id")(state)
ps.QRAMLoad(qram, "sample_id", "sample_data")(state)
# All training samples can now be processed in parallel
中文版 ===
QRAM 算子¶
QRAM (Quantum Random Access Memory) 算子实现量子并行数据访问。
概述¶
算子 |
操作 |
幺正类 |
|---|---|---|
|
标准 QRAM 加载 |
SelfAdjoint |
|
优化 QRAM 加载 |
SelfAdjoint |
量子并行数据访问¶
QRAM 实现量子并行存储访问:
这使得量子算法可以同时访问多个数据项。
—
QRAMLoad¶
操作: 从 QRAM 电路加载数据到量子寄存器
类型约束:
- 地址寄存器: UnsignedInteger
- 数据寄存器: UnsignedInteger 或 General
位约束: - 地址大小必须匹配 QRAM 配置 - 数据大小必须匹配 QRAM 数据宽度
import pysparq as ps
ps.System.clear()
# 创建 QRAM 电路(qutrit 版本)
addr_size = 4 # 4 位地址 → 16 个存储单元
data_size = 8 # 8 位数据
# 设置存储内容(memory 需在构造时传入)
memory = [i * 10 for i in range(16)] # 0, 10, 20, ..., 150
qram = ps.QRAMCircuit_qutrit(addr_size, data_size, memory)
# 创建寄存器
ps.System.add_register("addr", ps.UnsignedInteger, addr_size)
ps.System.add_register("data", ps.UnsignedInteger, data_size)
state = ps.SparseState()
# 地址寄存器均匀叠加
ps.Hadamard_Int_Full("addr")(state)
# QRAM 加载:并行访问所有地址
ps.QRAMLoad(qram, "addr", "data")(state)
ps.pprint(state)
# 输出包含 16 个状态:
# |addr=0,data=0⟩, |addr=1,data=10⟩, ...
# QRAMLoad 是自伴的,再次应用撤销
ps.QRAMLoad(qram, "addr", "data")(state)
# 数据寄存器清零
QRAMLoadFast¶
操作: 优化的 QRAM 加载
特点: 针特定存储模式优化,性能更高。
# 快速加载(相同接口)
ps.QRAMLoadFast(qram, "addr", "data")(state)
—
QRAMCircuit_qutrit¶
PySparQ 提供 QRAMCircuit_qutrit 类用于配置 QRAM:
Note
``QRAMCircuit_qubit``(C++ API 中的 qubit 版本 QRAM)在 PySparQ 中**不可用**。PySparQ 仅提供 qutrit 版本。
创建和配置 QRAM 电路¶
import pysparq as ps
# 创建 QRAM 电路
addr_bits = 4 # 地址位数
data_bits = 8 # 数据位数
# 设置存储内容(memory 需在构造时传入)
memory_list = [0, 10, 20, 30, 40, 50, 60, 70,
80, 90, 100, 110, 120, 130, 140, 150]
qram = ps.QRAMCircuit_qutrit(addr_bits, data_bits, memory_list)
# 注意:set_memory() 方法在 PySparQ 中不可用
# memory 必须在构造 QRAMCircuit_qutrit 时传入
# 查询信息
print(f"地址大小: {qram.addr_size}")
print(f"数据大小: {qram.data_size}")
条件加载¶
QRAM 加载支持条件执行:
op = ps.QRAMLoad(qram, "addr", "data")
# 仅当 control 非零时加载
op.conditioned_by_nonzeros("control")(state)
# 仅当 flag 的第 0 位为 1 时加载
op.conditioned_by_bit("flag", 0)(state)
使用场景¶
量子数据库搜索¶
# 数据库内容
database = [42, 17, 99, 5, ...]
# 创建 QRAM(memory 在构造时传入)
qram = ps.QRAMCircuit_qutrit(addr_bits, data_bits, database)
# 地址叠加态
ps.Hadamard_Int_Full("addr")(state)
# 并行加载所有数据项
ps.QRAMLoad(qram, "addr", "data")(state)
# 现在可以搜索特定值...
量子机器学习¶
# 加载训练数据(memory 在构造时传入)
qram = ps.QRAMCircuit_qutrit(addr_bits, data_bits, training_data)
ps.Hadamard_Int_Full("sample_id")(state)
ps.QRAMLoad(qram, "sample_id", "sample_data")(state)
# 现在可以并行处理所有训练样本