Skip to content

Testing

A test suite that drives TensorKeeper usually runs in two places: on a machine with a licence, where circuits are really simulated, and in CI, where there is often no licence and no wish to spend minutes simulating. MockTensorKeeper serves the second case. It takes the same arguments as TkSimulator, refuses the same inputs, and returns real QCSResults without simulating anything; Without a licence describes exactly what it guarantees.

This page covers how to set up a project and its tests around the two.

One suite, two backends

Pick the backend in one place, a fixture, and let every test take it. An environment variable makes the choice explicit:

# conftest.py
import os

import pytest
import tensorkeeper as tk

MOCK = os.environ.get("TK_MOCK") == "1"


@pytest.fixture
def simulator():
    # Same keywords for both: a knob the real simulator rejects is rejected
    # by the mock too, so CI catches it without a licence.
    cls = tk.MockTensorKeeper if MOCK else tk.TkSimulator
    return cls(bonddim=64)


# For tests that check physics rather than plumbing.
needs_simulation = pytest.mark.skipif(MOCK, reason="MockTensorKeeper simulates nothing")
# test_pipeline.py
import mimiqcircuits as mc

from conftest import needs_simulation


def bell():
    c = mc.Circuit()
    c.push(mc.GateH(), 0)
    c.push(mc.GateCX(), 0, 1)
    c.push(mc.Measure(), [0, 1], [0, 1])
    return c


def test_results_round_trip(simulator, tmp_path):
    # Plumbing: runs the same against the mock and the real simulator.
    res = simulator.execute(bell(), nsamples=100, seed=1)
    assert len(res.cstates) == 100
    res.saveproto(tmp_path / "out.pb")


@needs_simulation
def test_bell_correlations(simulator):
    # Physics: the mock returns all-zero samples, so this only means
    # something on the real simulator.
    res = simulator.execute(bell(), nsamples=1000, seed=1)
    assert {bs.to01() for bs in res.cstates} == {"00", "11"}

Run TK_MOCK=1 pytest in CI and plain pytest on a licensed machine.

Use an explicit switch like TK_MOCK rather than detecting the licence. tk.has_kernels() says whether the build can simulate, not whether a licence is present: it is True for the licensed package on a machine without a licence, where TkSimulator then refuses at the first run.

Installing for CI

The same package everywhere

MockTensorKeeper is in every build, so the licensed package serves both sides. Without a licence it imports, the mock runs, and only TkSimulator refuses. Nothing in the project changes between the two environments: install mimiq-tensorkeeper from the QPerfect index as described in Getting Started, and set TK_MOCK=1 in CI.

Without a licence, importing tensorkeeper prints one line to standard error, tk: license init failed: .... It is expected and does not stop the mock.

This is the simplest setup whenever CI can reach the QPerfect index or the wheel file.

CI without access to the licensed package

If CI cannot reach the QPerfect index, it can install the public build from PyPI instead. It holds the same tensorkeeper module with every compute kernel left out, so the mock is the only backend that runs on it. It is published under two names with the same contents: mimiq-tensorkeeper, the licensed package's own name, and mimiq-tensorkeeper-mock.

Never install the public and the licensed build in one environment, under either name: they write the same files. Under two different names, nothing warns you about it.

Under its own name

mimiq-tensorkeeper-mock lets a project declare both builds without any index configuration. With pip, keep them in separate requirements files, one per environment. In a uv project, two dependency groups that exclude each other do it from one pyproject.toml and one lockfile. Here the licensed build comes as a wheel file:

[dependency-groups]
licensed = ["mimiq-tensorkeeper"]
mock = ["mimiq-tensorkeeper-mock"]

[tool.uv]
# Both install the `tensorkeeper` package, so they can never be installed
# together.
conflicts = [[{ group = "licensed" }, { group = "mock" }]]

[tool.uv.sources]
mimiq-tensorkeeper = { path = "wheels/mimiq_tensorkeeper-<version>-<tags>.whl" }

The wheel file is needed to create or update the lockfile, not to install the mock:

uv sync --group licensed          # licensed machine, wheel file present
uv sync --frozen --group mock     # CI, no wheel file, no credentials

To take the licensed build from the QPerfect index instead, point its source at it as Getting Started shows.

Under the same name

The two builds also share the name mimiq-tensorkeeper, so that code and imports do not change between them. In a uv project, two dependency groups that exclude each other give each environment its build from its own index:

[dependency-groups]
licensed = ["mimiq-tensorkeeper"]
mock = ["mimiq-tensorkeeper"]

[tool.uv]
# The two groups install the same package from different places, so they can
# never be installed together.
conflicts = [[{ group = "licensed" }, { group = "mock" }]]

[[tool.uv.index]]
name = "qperfect"
url = "https://license.qperfect.io/pypi/simple"
explicit = true

[[tool.uv.index]]
name = "pypi"
url = "https://pypi.org/simple"
explicit = true

[tool.uv.sources]
mimiq-tensorkeeper = [
  { index = "qperfect", group = "licensed" },
  { index = "pypi", group = "mock" },
]

The lockfile records both builds, so creating or updating it needs the QPerfect credentials (UV_INDEX_QPERFECT_USERNAME=mimiq and your token as UV_INDEX_QPERFECT_PASSWORD). Installing does not, for the mock:

uv sync --group licensed          # licensed machine
uv sync --frozen --group mock     # CI, no credentials needed

If you receive the licensed build as a wheel file rather than from the index, use a path for the licensed source instead: { path = "wheels/mimiq_tensorkeeper-<version>-<tags>.whl", group = "licensed" }.

The public build is published with the same version numbers as the licensed one, but may lag behind it. Check that the version CI installs is the one you test against.