Skip to main content

SPARQ

Tests PyPI License DOI

sparq (installed as sparq-triage) is a Python package for one question that comes up again and again in quantum-optics labs: does this light source give out one photon at a time? A source that does is a single-photon emitter, the building block of many quantum communication and computing schemes. The standard test is to split the light onto two detectors and record how often both fire at nearly the same moment. A true single emitter almost never makes both fire together, so the record shows a dip at zero delay.

The package works from that record (a histogram, the raw detector time stamps, or a PicoQuant PTU file) and answers:

  • How deep is the dip, and how sure can I be about it?
  • Does the usual dip formula describe my data at all, and if not, which shape does?
  • Is the dip deep enough to call the site a single emitter, and with what probability?
  • How long must I measure before the answer is reliable, for a typical run or for 9 runs out of 10, and can a longer run ever make it reliable?
  • Can I stop measuring early because the data already suffice, with error rates that hold without knowing the emitter?
  • Did the source stay the same during the run, so that the snapshots may be added up?
  • How much do background light, blinking, detector dead time and afterpulsing change the numbers, and how sure is the background correction when the background itself is measured?
  • For a pulsed laser, what is g2(0) when the peaks of the comb overlap?
  • For a heralded pair source, what purity does a measured coincidence-to-accidental ratio allow, with my detectors?
  • How bright does the emitter get, and at what excitation power does it saturate?
  • How long does the emitter stay excited (its lifetime), from the photons' delays after the pulses of a pulsed laser, with a Gaussian or a measured detector response?

The analysis core needs only NumPy and SciPy. An optional machine-learning layer (PyTorch) holds the neural estimators and the automated triage agent of the paper "Closed-Loop, Event-Driven Machine Learning for Autonomous Triage of Single-Photon Emitters" (T. M. Mahim, M. N. Islam, M. M. Rahman and A. S. M. Mohsin), ACS Photonics 13(21), 6545-6554 (2026), doi:10.1021/acsphotonics.6c01984; the companion repository reproduces the paper itself.

Install

pip install sparq-triage        # analysis core: NumPy and SciPy only
pip install sparq-triage[ml]    # adds PyTorch for the machine-learning layer

It needs Python 3.10 or newer, NumPy 1.24 or newer and SciPy 1.10 or newer. The ml extra adds PyTorch 2.0 or newer. import sparq and everything listed under "Analysis core" below work without PyTorch.

Units and conventions are listed in the guide.

A first look

1. Analyze a measured histogram

import os, tempfile
import numpy as np
from sparq import (EmitterSite, HBTConfig, expected_histogram,
                   save_hbt_csv, load_hbt_csv, analyze_histogram)

# Illustrative emitter: 15 ns antibunching time, 250 ns shelving time,
# 150 kcps detected, 95 % of the light from the emitter itself.
site = EmitterSite(dict(tau1=15.0, tau2=250.0, a=0.3, rate_kcps=150.0,
                        rho=0.95, blinking=False), n_emitters=1)
print(f"true g2(0) of this site: {site.g2_0:.4f}")

# A simulated 30 s measurement on a 0.5 ns grid whose zero is shifted
# by 7 ns, as a correlator with a cable delay would record it.
grid = HBTConfig(tau_max=90.25, n_bins=361)
rng = np.random.default_rng(0)
counts = rng.poisson(expected_histogram(site, 30.0, grid))

path = os.path.join(tempfile.mkdtemp(), "my_hbt.csv")
save_hbt_csv(path, grid.bin_centers + 7.0, counts)   # header: delay_ns,counts
delay, counts = load_hbt_csv(path)

# the detectors' count rates, as read from the counters (counts/s)
res = analyze_histogram(delay, counts, T_s=30.0, singles_cps=(75e3, 75e3),
                        n_bootstrap=30, seed=1)
print(f"dip found at {res['center']:.2f} ns")
print(f"g2(0) = {res['g2_0']:.3f}, 68 % interval "
      f"[{res['g2_0_low']:.3f}, {res['g2_0_high']:.3f}]")
print("below 0.5:", res["single_emitter"],
      "| whole interval below 0.5:", res["single_emitter_confident"])
print(f"fit p-value {res['fit_p_value']:.2f}; limits that matter: "
      f"{res['bound_matters']}")
true g2(0) of this site: 0.0975
dip found at 6.90 ns
g2(0) = 0.096, 68 % interval [0.075, 0.114]
below 0.5: True | whole interval below 0.5: True
fit p-value 0.69; limits that matter: False

analyze_histogram finds the dip, re-bins the data onto the 1 ns analysis grid centered on it, and fits the emitter model with the instrument response included. singles_cps are the two detectors' count rates as the counters show them. They fix the flat level (the height the histogram would have if the clicks were unrelated), and without them the fit is much less certain (see below). The interval comes from a bootstrap: the counts are redrawn at random (Poisson) n_bootstrap times and the whole analysis is repeated on each copy. A real analysis should use more redraws than the 30 used here to keep the example fast (the default is 200). single_emitter_confident is True when the whole interval lies on one side of 0.5. The fit p-value says whether the model describes the data (a value near 0 means it does not; see example 10; this one is approximate and errs on the large side), and limits that matter says whether the answer depends on where the fit's search ranges end (example 9). Your own file needs the header delay_ns,counts and one row per bin. For a pulsed-laser measurement (a comb of peaks), use analyze_pulsed instead (example 17).

Why the singles rates matter. With the flat level left to the fit, a stronger, slower shoulder can trade against a lower flat level, and a histogram of ±60 ns cannot tell the two apart. Over 15 simulated runs of this site (in the tests), the histogram-only fit missed the true g2(0) by up to 0.16 (and a pair's by up to 0.24); with the singles rates every run was within 0.06, with an rms error below 0.03. Most of the large histogram-only misses are flagged by a warning (the fit then sits on one of its search limits, and moving the limit moves g2(0)), but not all of them are, so pass the singles rates whenever you have them.

More in the examples.

Where to read on

  • Examples: 26 short examples, each with the output it prints.
  • Tutorials: complete analyses, from data to a result with error bars.
  • Words, units and conventions: the terms used here, explained in plain words, and every unit and sign convention.
  • What is in the package: every function, grouped by task.
  • How the results are checked: what each test compares with and how closely, and the mistakes fixed in earlier versions.
  • Limits: when the package refuses an input and why, and what it deliberately does not do.
  • CHANGELOG: what changed in each version.

Where it comes from

The package is the installable part of the paper "Closed-Loop, Event-Driven Machine Learning for Autonomous Triage of Single-Photon Emitters" by T. M. Mahim, M. N. Islam, M. M. Rahman and A. S. M. Mohsin, ACS Photonics 13(21), 6545-6554 (2026), doi:10.1021/acsphotonics.6c01984. It was published online on 15 October 2026 and appeared in the issue of 4 November 2026. The companion repository reproduces the paper itself.

The experimental quantum-dot HBT measurements used by sparq.datasets.load_fisequr are from the openly licensed sps-quality repository (Kedziora et al., Mach. Learn.: Sci. Technol. 4, 045042 (2023)); they are not redistributed here, and the loader takes the dataset directory explicitly.

Citing, support and license

Please cite the associated paper if you use this code; citation metadata is in CITATION.cff. Every release is archived on Zenodo under the concept DOI 10.5281/zenodo.22278040, which always resolves to the latest version.

The package is maintained by Tanvir Mahmud Mahim (BRAC University), who reviews issues and pull requests. Bug reports, questions and pull requests are welcome through GitHub issues; see CONTRIBUTING.md for the development setup and the design rules. Tagged releases are published to PyPI by CI.

Licensed under Apache-2.0 (see LICENSE).

Metadata

Release files for sparq-triage 0.16.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sparq-triage 0.16.1
File Size Uploaded Provenance Actions
sparq_triage-0.16.1.tar.gz 242.1 kB With provenance Details for sparq_triage-0.16.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sparq-triage 0.16.1
File Interpreter ABI Platform Actions
sparq_triage-0.16.1-py3-none-any.whl Python 3 none any Details for sparq_triage-0.16.1-py3-none-any.whl

Total release size: 379.1 kB

Release files / sparq_triage-0.16.1.tar.gz

Download URL sparq_triage-0.16.1.tar.gz
Size 242.1 kB
Tags Source
SHA-256 checksum
How to use checksums
b3b82a3ff654dcfe1d7dee9b60cc3c16a5dbd3e3f80e067f8ace292bb0e6511c
BLAKE2b-256 checksum
How to use checksums
f770ab119a108adda8e9160bacfbfada17c435e26cfa733b7c5ba65d56beab0b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 3, 2026.

Transparency log

Release files / sparq_triage-0.16.1-py3-none-any.whl

Download URL sparq_triage-0.16.1-py3-none-any.whl
Size 137.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5c26076dd755ccd06db5f60403124f75e0406169432d7810926209c7d38ef35d
BLAKE2b-256 checksum
How to use checksums
3383b50ac70bb0b7837e1a7b0eb165f5e3aeaa7e2bcee5a821ebd66ba2f26167
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 3, 2026.

Transparency log

Security for sparq-triage 0.16.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for sparq_triage-0.16.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 3, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page