"""uniqc_cpp StatevectorSimulator adapters.
``UniqcStatevector`` is an ``option``-mode backend: the threads tier is
applied via the simulator's global multithreading API
(``uniqc_cpp.set_num_threads`` + ``set_parallel_enabled``), so exact
probabilities and trajectory shots both run in a single process with a
parallel gate kernel. ``UniqcStatevectorBatch`` keeps the historical
``batch`` baseline: trajectories run single-threaded in parallel worker
processes (multiprocessing) and measure sampling throughput. Noise
channels are stochastic on the statevector (quantum-trajectory Monte
Carlo), one trajectory per shot.
"""
from __future__ import annotations
import concurrent.futures
import uniqc_cpp
from benchmark.backends.base import BenchmarkBackend
from benchmark.backends.util import prob_q0_1_from_statevector
from benchmark.ir import Circuit
from benchmark.registry import register_backend
def _apply_ops(sim, ops) -> None:
"""Apply IR ops (gates + channels) to a uniqc simulator object.
Works for both simulator classes: they share the same Python API for
gates, and the noise channels are stochastic on the statevector but
deterministic superoperators on the density operator.
"""
for op in ops:
name, q = op.name, op.qubits
if name == "h":
sim.hadamard(q[0])
elif name == "x":
sim.x(q[0])
elif name == "y":
sim.y(q[0])
elif name == "z":
sim.z(q[0])
elif name == "s":
sim.s(q[0])
elif name == "sdg":
sim.s(q[0], dagger=True)
elif name == "t":
sim.t(q[0])
elif name == "tdg":
sim.t(q[0], dagger=True)
elif name in ("rx", "ry", "rz"):
getattr(sim, name)(q[0], op.params[0])
elif name == "cx":
sim.cnot(q[0], q[1])
elif name == "cz":
sim.cz(q[0], q[1])
elif name == "swap":
sim.swap(q[0], q[1])
elif name == "depol1":
sim.depolarizing(q[0], op.params[0])
elif name == "depol2":
sim.twoqubit_depolarizing(q[0], q[1], op.params[0])
elif name == "adamp":
sim.amplitude_damping(q[0], op.params[0])
elif name == "bitflip":
sim.bitflip(q[0], op.params[0])
else: # pragma: no cover
raise ValueError(f"unmapped op {name}")
def _exact_p_q0_1(circuit: Circuit) -> float:
"""Exact P(q0=1) from one statevector simulation."""
sim = uniqc_cpp.StatevectorSimulator()
sim.init_n_qubit(circuit.n_qubits)
_apply_ops(sim, circuit.ops)
if circuit.n_qubits <= 22: # avoid copying >4M-amplitude vectors
return prob_q0_1_from_statevector(sim.state)
return sim.pmeasure(0)[1]
def _trajectory_shots(payload: tuple) -> int:
"""Worker: run ``n_shots`` stochastic trajectories, return # of 1-outcomes."""
ops, n_qubits, n_shots, seed = payload
uniqc_cpp.seed(seed)
ones = 0
for _ in range(n_shots):
sim = uniqc_cpp.StatevectorSimulator()
sim.init_n_qubit(n_qubits)
_apply_ops(sim, ops)
# Keep the list overload so comparisons against 1.0.1 remain usable:
# that release's scalar overload recurses into itself forever.
ones += sim.measure_single_shot([0])
return ones
[文档]
@register_backend
class UniqcStatevector(BenchmarkBackend):
name = "uniqc_sv"
label = "uniqc_cpp StatevectorSimulator"
import_name = "uniqc_cpp"
kind = "statevector"
threads_mode = "option"
max_qubits = 30
supports_channels = True
supports_shots = True
homepage = "https://github.com/IAI-USTC-Quantum/uniqc-cppsimulator"
[文档]
def set_threads(self, n: int) -> None:
"""Apply the threads tier to the simulator's global kernel threads."""
uniqc_cpp.set_num_threads(n)
uniqc_cpp.set_parallel_enabled(n > 1)
[文档]
def run(self, circuit: Circuit, shots: int, seed: int) -> dict:
if shots == 0:
return {"p_q0_1": _exact_p_q0_1(circuit)}
# one process, kernel-parallel gates inside each trajectory
ones = _trajectory_shots((circuit.ops, circuit.n_qubits, shots, seed))
return {"p_q0_1": ones / shots}
[文档]
@register_backend
class UniqcStatevectorBatch(BenchmarkBackend):
name = "uniqc_sv_batch"
label = "uniqc_cpp StatevectorSimulator (process-batch shots)"
import_name = "uniqc_cpp"
kind = "statevector"
threads_mode = "batch"
max_qubits = 30
supports_channels = True
supports_shots = True
homepage = "https://github.com/IAI-USTC-Quantum/uniqc-cppsimulator"
[文档]
def run(self, circuit: Circuit, shots: int, seed: int) -> dict:
if shots == 0:
# batch mode only parallelizes shot sampling; exact runs stay
# single-threaded (multi-thread tiers are skipped via supports())
return {"p_q0_1": _exact_p_q0_1(circuit)}
workers = max(1, self.threads)
per = shots // workers
if workers == 1 or per == 0:
ones = _trajectory_shots((circuit.ops, circuit.n_qubits, shots, seed))
return {"p_q0_1": ones / shots}
base = (seed * 1000003 + 7919) & 0xFFFFFFFF # C++ seed() takes 32-bit
payloads = [
(circuit.ops, circuit.n_qubits, per, (base + i * 104729) & 0xFFFFFFFF)
for i in range(workers)
]
remainder = shots - per * workers
ctx = concurrent.futures.ProcessPoolExecutor(max_workers=workers)
with ctx as pool:
ones = sum(pool.map(_trajectory_shots, payloads))
if remainder: # pragma: no cover - matrix uses divisible shot counts
ones += _trajectory_shots(
(circuit.ops, circuit.n_qubits, remainder,
(base + workers) & 0xFFFFFFFF))
return {"p_q0_1": ones / shots}