Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed
- The stabilizer-rank strategies compile components of the same shape once, and
no longer run components with a single output without compiling them. A
20-qubit Clifford circuit with `strategy="cat5"` samples 8 times faster and its
first call takes 1.1 s instead of 1.8 s. Seeded samples are unchanged.

## [1.1.0] - 2026-11-12

Tsim 1.1 adds a GPU backend for Apple GPUs. For example, end-to-end
Expand Down
10 changes: 5 additions & 5 deletions src/tsim/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import secrets
import warnings
from dataclasses import dataclass
from dataclasses import dataclass, replace
from math import ceil
from typing import TYPE_CHECKING, Literal, overload

Expand Down Expand Up @@ -125,10 +125,10 @@ def sample_component(
(batch_size, num_outputs_for_component).

"""
# Skip JIT for small components (overhead not worth it)
if len(component.output_indices) <= 1:
return _sample_component(component, f_params, key)
return _sample_component_jit(component, f_params, key)
# NOTE: output_indices is a static field and so part of the jit cache key.
# Sampling does not read it; without it, components of the same shape share one
# compilation (a Clifford circuit has one such component per measured qubit).
return _sample_component_jit(replace(component, output_indices=()), f_params, key)
Comment thread
rafaelha marked this conversation as resolved.
Resolved


def _check_norm(max_norm_deviation: float | jax.Array, threshold: float) -> None:
Expand Down
39 changes: 39 additions & 0 deletions test/integration/test_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,45 @@ def fake_sample_component(component, f_params, key):
)


def _jit_cache_key(component: CompiledComponent):
graphs = (component.f_selection, component.compiled_scalar_graphs)
leaves, structure = jax.tree_util.tree_flatten(graphs)
return str(structure), tuple((jnp.shape(x), jnp.result_type(x)) for x in leaves)


def test_components_of_one_shape_share_a_compilation(monkeypatch):
# NOTE: three Bell-like pairs and three lone qubits, all Clifford.
circuit = Circuit("""
H 0 1 2 3 4 5 6 7 8
CZ 0 1 2 3 4 5
S 1 3 7
H 0 2 4
M 0 1 2 3 4 5 6 7 8
""")
sampler = circuit.compile_sampler(strategy="cat5", seed=5)
components = [
c for c in sampler._program.components if isinstance(c, CompiledComponent)
]
shapes = {_jit_cache_key(c) for c in components}
assert len(shapes) < len(components)

# NOTE: under jit, _sample_component runs once per compilation, not per call.
traced = []
sample_eagerly = sampler_module._sample_component

def spy(component, f_params, key):
traced.append(component)
return sample_eagerly(component, f_params, key)

monkeypatch.setattr(sampler_module, "_sample_component", spy)
jitted = sampler.sample(64)
assert len(traced) <= len(shapes)

monkeypatch.setattr(sampler_module, "sample_component", sample_eagerly)
eager = circuit.compile_sampler(strategy="cat5", seed=5).sample(64)
np.testing.assert_array_equal(jitted, eager)


@pytest.mark.parametrize(
"code_task",
[
Expand Down
Loading