Skip to main content

A fast library for analyzing with quantum stabilizer circuits.

Project description

Stim

Stim is a fast simulator for quantum stabilizer circuits.

API references are available on the stim github wiki: https://github.com/quantumlib/stim/wiki

Stim can be installed into a python 3 environment using pip:

pip install stim

Once stim is installed, you can import stim and use it. There are three supported use cases:

  1. Interactive simulation with stim.TableauSimulator.
  2. High speed sampling with samplers compiled from stim.Circuit.
  3. Independent exploration using stim.Tableau and stim.PauliString.

Interactive Simulation

Use stim.TableauSimulator to simulate operations one by one while inspecting the results:

import stim

s = stim.TableauSimulator()

# Create a GHZ state.
s.h(0)
s.cnot(0, 1)
s.cnot(0, 2)

# Look at the simulator state re-inverted to be forwards:
t = s.current_inverse_tableau()
print(t**-1)
# prints:
# +-xz-xz-xz-
# | ++ ++ ++
# | ZX _Z _Z
# | _X XZ __
# | _X __ XZ

# Measure the GHZ state.
print(s.measure_many(0, 1, 2))
# prints one of:
# [True, True, True]
# or:
# [False, False, False]

High Speed Sampling

By creating a stim.Circuit and compiling it into a sampler, samples can be generated very quickly:

import stim

# Create a circuit that measures a large GHZ state.
c = stim.Circuit()
c.append("H", [0])
for k in range(1, 30):
    c.append("CNOT", [0, k])
c.append("M", range(30))

# Compile the circuit into a high performance sampler.
sampler = c.compile_sampler()

# Collect a batch of samples.
# Note: the ideal batch size, in terms of speed per sample, is roughly 1024.
# Smaller batches are slower because they are not sufficiently vectorized.
# Bigger batches are slower because they use more memory.
batch = sampler.sample(1024)
print(type(batch))  # numpy.ndarray
print(batch.dtype)  # numpy.uint8
print(batch.shape)  # (1024, 30)
print(batch)
# Prints something like:
# [[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
#  [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
#  [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
#  ...
#  [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
#  [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
#  [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
#  [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]]

This also works on circuits that include noise:

import stim
import numpy as np

c = stim.Circuit("""
    X_ERROR(0.1) 0
    Y_ERROR(0.2) 1
    Z_ERROR(0.3) 2
    DEPOLARIZE1(0.4) 3
    DEPOLARIZE2(0.5) 4 5
    M 0 1 2 3 4 5
""")
batch = c.compile_sampler().sample(2**20)
print(np.mean(batch, axis=0).round(3))
# Prints something like:
# [0.1   0.2   0.    0.267 0.267 0.266]

You can also sample annotated detection events using stim.Circuit.compile_detector_sampler.

For a list of gates that can appear in a stim.Circuit, see the latest readme on github.

Independent Exploration

Stim provides data types stim.PauliString and stim.Tableau, which support a variety of fast operations.

import stim

xx = stim.PauliString("XX")
yy = stim.PauliString("YY")
assert xx * yy == -stim.PauliString("ZZ")

s = stim.Tableau.from_named_gate("S")
print(repr(s))
# prints:
# stim.Tableau.from_conjugated_generators(
#     xs=[
#         stim.PauliString("+Y"),
#     ],
#     zs=[
#         stim.PauliString("+Z"),
#     ],
# )

s_dag = stim.Tableau.from_named_gate("S_DAG")
assert s**-1 == s_dag
assert s**1000000003 == s_dag

cnot = stim.Tableau.from_named_gate("CNOT")
cz = stim.Tableau.from_named_gate("CZ")
h = stim.Tableau.from_named_gate("H")
t = stim.Tableau(5)
t.append(cnot, [1, 4])
t.append(h, [4])
t.append(cz, [1, 4])
t.prepend(h, [4])
assert t == stim.Tableau(5)

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

stim-1.13.0.tar.gz (743.8 kB view hashes)

Uploaded Source

Built Distributions

stim-1.13.0-cp312-cp312-win_amd64.whl (2.3 MB view hashes)

Uploaded CPython 3.12 Windows x86-64

stim-1.13.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.3 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

stim-1.13.0-cp312-cp312-macosx_11_0_arm64.whl (3.0 MB view hashes)

Uploaded CPython 3.12 macOS 11.0+ ARM64

stim-1.13.0-cp312-cp312-macosx_10_9_x86_64.whl (3.3 MB view hashes)

Uploaded CPython 3.12 macOS 10.9+ x86-64

stim-1.13.0-cp311-cp311-win_amd64.whl (2.3 MB view hashes)

Uploaded CPython 3.11 Windows x86-64

stim-1.13.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.3 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

stim-1.13.0-cp311-cp311-macosx_11_0_arm64.whl (3.0 MB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

stim-1.13.0-cp311-cp311-macosx_10_9_x86_64.whl (3.3 MB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

stim-1.13.0-cp310-cp310-win_amd64.whl (2.3 MB view hashes)

Uploaded CPython 3.10 Windows x86-64

stim-1.13.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.3 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

stim-1.13.0-cp310-cp310-macosx_11_0_arm64.whl (3.0 MB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

stim-1.13.0-cp310-cp310-macosx_10_9_x86_64.whl (3.3 MB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

stim-1.13.0-cp39-cp39-win_amd64.whl (2.4 MB view hashes)

Uploaded CPython 3.9 Windows x86-64

stim-1.13.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.3 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

stim-1.13.0-cp39-cp39-macosx_11_0_arm64.whl (3.0 MB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

stim-1.13.0-cp39-cp39-macosx_10_9_x86_64.whl (3.3 MB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

stim-1.13.0-cp38-cp38-win_amd64.whl (2.3 MB view hashes)

Uploaded CPython 3.8 Windows x86-64

stim-1.13.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.3 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

stim-1.13.0-cp38-cp38-macosx_11_0_arm64.whl (3.0 MB view hashes)

Uploaded CPython 3.8 macOS 11.0+ ARM64

stim-1.13.0-cp38-cp38-macosx_10_9_x86_64.whl (3.3 MB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

stim-1.13.0-cp37-cp37m-win_amd64.whl (2.3 MB view hashes)

Uploaded CPython 3.7m Windows x86-64

stim-1.13.0-cp37-cp37m-win32.whl (2.0 MB view hashes)

Uploaded CPython 3.7m Windows x86

stim-1.13.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.4 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

stim-1.13.0-cp37-cp37m-manylinux_2_17_i686.manylinux2014_i686.whl (4.6 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ i686

stim-1.13.0-cp37-cp37m-macosx_10_9_x86_64.whl (3.2 MB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

stim-1.13.0-cp36-cp36m-win_amd64.whl (2.3 MB view hashes)

Uploaded CPython 3.6m Windows x86-64

stim-1.13.0-cp36-cp36m-win32.whl (2.0 MB view hashes)

Uploaded CPython 3.6m Windows x86

stim-1.13.0-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.4 MB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ x86-64

stim-1.13.0-cp36-cp36m-manylinux_2_17_i686.manylinux2014_i686.whl (4.6 MB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ i686

stim-1.13.0-cp36-cp36m-macosx_10_9_x86_64.whl (3.2 MB view hashes)

Uploaded CPython 3.6m macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page