Skip to content

Getting Started

This page installs the package and runs a circuit through both API paths. If the terms Clifford frame, dressed gate or magic are new, read Concepts first; this page assumes them.

Prerequisites

  • Python 3.11 or newer.
  • mimiqcircuits, which supplies Circuit, the gate set and QCSResults.

Installation

Wheels are published on tagged releases. Install with the token that came with your licence:

pip install mimiq-tensorkeeper --index-url https://mimiq:<your-token>@license.qperfect.io/pypi/simple

The index serves every QPerfect product your licence covers and forwards every other name to PyPI, so dependencies resolve from the same URL. To avoid repeating it:

pip config set global.index-url https://mimiq:<your-token>@license.qperfect.io/pypi/simple

then pip install mimiq-tensorkeeper. With uv, pass the same URL as --index, or set UV_INDEX_URL.

The distribution is mimiq-tensorkeeper; the package you import is tensorkeeper.

Use --index-url, not --extra-index-url. Public PyPI carries the same name as an unlicensed mock (see Without a licence), and with both indexes in play pip may take either copy. tk.has_kernels() is True on the licensed wheel.

Wheels exist for:

Platform Python Engines
Linux x86_64, glibc 2.35+ and glibc 2.34 (RHEL 9) 3.11 and later, 3.14t OpenBLAS, MKL, faer, CUDA
macOS arm64 (macOS 26 or newer) 3.11 and later OpenBLAS, faer
Windows x86_64 3.11 and later MKL, faer

On Linux x86_64 and Windows the wheel depends on Intel's mkl package, which pip installs with it: MKL cannot be bundled into a wheel. For TkSimulatorGpu, install the CUDA libraries too; the NVIDIA driver comes from the system:

pip install "mimiq-tensorkeeper[cuda]" --index-url https://mimiq:<your-token>@license.qperfect.io/pypi/simple

A free-threaded interpreter (python3.14t) gets its own wheel automatically; see Free-threaded Python.

Choosing an engine

The residual MPS runs on a linear algebra library, and each library is a separate engine in the wheel. TkSimulator(linalg=...) picks one per simulator:

import tensorkeeper as tk

print(tk.available_engines())       # e.g. ['openblas', 'mkl', 'faer', 'cuda']
sim = tk.TkSimulator(linalg="mkl")  # "openblas", "mkl", "faer", or None
print(sim.linalg)                   # the engine it will run on

linalg=None, the default, takes OpenBLAS where the wheel has it and MKL on Windows. MKL is often faster on Intel CPUs; faer is pure Rust, needs nothing from the system, and is usually the slowest. All three give the same results to rounding. tk.library_info() and tk.compile_info() say what an engine is and how it was built; quote them in a bug report.

From source

cd tensorkeeper-python
uv run maturin develop
uv run pytest tests/ -q

maturin develop compiles the OpenBLAS engine and installs the package in editable mode. Rebuild it after any change to tensorkeeper-rs. It builds no documentation, so tk.docs_dir() is empty there.

Two ways in

TensorKeeper offers the same two paths as any MIMIQ backend.

High level: execute

execute runs the circuit and returns a QCSResults, handling shots, trajectories and measurement bookkeeping for you. Use it unless you need the state itself.

from mimiqcircuits import Circuit, GateH, GateCX, Measure
from tensorkeeper import TkSimulator

c = Circuit()
c.push(GateH(), 0)
c.push(GateCX(), 0, 1)
c.push(Measure(), 0, 0)
c.push(Measure(), 1, 1)

sim = TkSimulator(bonddim=64)
results = sim.execute(c, nsamples=1000, seed=1)

print(results.histogram())      # {bs"00": ~500, bs"11": ~500}
print(results.fidelities[0])    # 1.0

Lower level: build_state + compile + evolve

These are the three primitives execute is built from. Drive them yourself when you want one evolution followed by many queries against the resulting state. Nothing is sampled and nothing is hidden.

from mimiqcircuits import BitString

c = Circuit()
c.push(GateH(), 0)
c.push(GateCX(), 0, 1)

sim = TkSimulator(bonddim=64)
state = sim.build_state(2)                     # |00>
state, fidelity = sim.evolve(state, sim.compile(c))

print(state.amplitude(BitString("00")))        # (0.7071+0j)
print(state.probability(BitString("11")))      # 0.5
print(state.bond_dim)                          # 1
print(fidelity)                                # ExactFidelity()

amplitude, probability, the expectation values and sample are all non-destructive, so one evolution can answer many questions.

compile is pure: no randomness, no sampling, no state. That is what lets a single compiled circuit serve every shot of a run.

Seeing the Clifford frame work

The point of TensorKeeper is visible in one number. Build a deep Clifford circuit and watch the bond dimension stay at 1.

import random
from mimiqcircuits import Circuit, GateH, GateS, GateCX, GateCZ
from tensorkeeper import TkSimulator

rng = random.Random(0)
n = 40
c = Circuit()
for q in range(n):
    c.push(GateH(), q)
for _ in range(400):                            # 400 random Clifford gates
    if rng.random() < 0.5:
        q = rng.randrange(n - 1)                # a neighboring pair
        c.push(rng.choice([GateCX(), GateCZ()]), q, q + 1)
    else:
        c.push(rng.choice([GateH(), GateS()]), rng.randrange(n))

sim = TkSimulator()
state = sim.build_state(n)
state, fidelity = sim.evolve(state, sim.compile(c))

print(state.bond_dim)     # 1: the frame absorbed all 440 gates
print(fidelity)           # ExactFidelity()

40 qubits, 440 gates, a state that is highly entangled, and the MPS never left \(\chi = 1\). A plain MPS simulator would need a bond dimension in the thousands for the same circuit.

Now add magic. A handful of \(T\) gates on a register this wide is still free:

from mimiqcircuits import GateT

for q in range(0, n, 8):                        # 5 T gates
    c.push(GateT(), q)

state = sim.build_state(n)
state, fidelity = sim.evolve(state, sim.compile(c))
print(state.bond_dim)     # still 1
print(fidelity)           # ExactFidelity()

Bond dimension climbs only once enough magic has accumulated, which on \(n\) qubits takes of order \(n\) non-Clifford gates:

for q in range(n):                              # a full T layer, 40 more
    c.push(GateT(), q)

state = sim.build_state(n)
state, fidelity = sim.evolve(state, sim.compile(c))
print(state.bond_dim)     # 2: the T gates now cost bond dimension
print(fidelity)           # TruncationLowerBound(value=1.0)

Two lessons in those three runs. Cost tracks magic, not depth and not entanglement. And the fidelity claim weakened to TruncationLowerBound at the first truncating operation even though its value is exactly 1.0 and no accuracy was lost, which is a distinction worth understanding before you rely on the number: see Accuracy and Tuning.

state.bond_dim is the bond dimension of the residual MPS \(|\varphi\rangle\), not of the physical state. See Concepts.

Qubit and bit indices

Indices are 0-based in Python, matching mimiqcircuits. c.push(GateH(), 0) acts on the first qubit.

Bitstring ordering

BitString("01") has qubit 0 set to 0 and qubit 1 set to 1. The leftmost character is qubit 0. This is the MIMIQ convention and is the reverse of Qiskit's.

Next steps