A fast stabilizer circuit simulator

Related tags

Serialization Stim
Overview

Stim

Stim is a fast simulator for non-adaptive quantum stabilizer circuits. Stim is based on the stabilizer tableau representation introduced in Scott Aaronson et al's CHP simulator. Stim makes three key improvements over CHP.

First, the stabilizer tableau that is being tracked is inverted. The tableau tracked by Stim indexes how each qubit's X and Z observables at the current time map to compound observables at the start of time (instead of mapping from the start of time to the current time). This is done so that the sign of the tracked observables directly store the measurement result to return when a measurement is deterministic. As a result, deterministic measurements can be completed in linear time instead of quadratic time.

Second, when producing multiple samples, the initial stabilizer simulation is executed without noise in order to create a reference sample. Once a reference sample from the circuit is available, all that is needed is to track a Pauli frame through the circuit, using the original sample as a template whose results are flipped or not flipped by the passing Pauli frame. As long as all errors are probabilistic Pauli operations, and as long as 50/50 probability Z errors are placed after every reset and every measurement, the Pauli frame can track these errors and the resulting samples will come from the same distribution as a full stabilizer simulation. This ensures every gate has a worst case complexity of O(1), instead of O(n) or O(n^2).

Third, data is laid out in a cache friendly way and operated on using vectorized 256-bit-wide SIMD instructions. This makes key operations fast. For example, Stim can multiply a Pauli string with a hundred billion terms into another in under a second. Pauli string multiplication is a key bottleneck operation when updating a stabilizer tableau. Tracking Pauli frames can also benefit from vectorization, by combining them into batches and computing thousands of samples at a time.

Usage (python)

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 two supported use cases: interactive usage and high speed sampling.

You can use the Tableau simulator in an interactive fashion:

import stim

s = stim.TableauSimulator()

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

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

Alternatively, you can compile a circuit and then begin generating samples from it:

import stim

# Create a circuit that measures a large GHZ state.
c = stim.Circuit()
c.append_operation("H", [0])
for k in range(1, 30):
    c.append_operation("CNOT", [0, k])
c.append_operation("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]]

The circuit can also include noise:

import stim
import numpy as np

c = stim.Circuit()
c.append_from_stim_program_text("""
    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 detection events using stim.Circuit.compile_detector_sampler.

Usage (command line)

Stim reads a quantum circuit from stdin and writes measurement results to stdout. The input format is a series of lines, each starting with an optional gate (e.g. CNOT 0 1) and ending with an optional comment prefixed by #. See below for a list of supported gates. The default output format is a string of "0" and "1" characters, indicating measurement results. The order of the results is the same as the order of the measurement commands in the input.

Examples

Bit flip and measure a qubit:

echo "
  X 0
  M 0
" | ./stim --sample
1

Create and measure a GHZ state, five times:

echo "
  H 0
  CNOT 0 1 0 2
  M 0 1 2
" | ./stim --sample=5
111
111
000
111
000

Sample several runs of a small noisy surface code with phenomenological type noise:

echo "
  REPEAT 20 {
    DEPOLARIZE1(0.001) 0 1 2 3 4 5 6 7 8
    H 3 5
    CNOT 4 1 3 6 5 8
    CNOT 2 1 8 7 3 4
    CNOT 0 1 6 7 5 4
    CNOT 4 7 3 0 5 2
    H 3 5
    M 1 7 3 5
    R 1 7 3 5
  }
  M 0 2 4 6 8
" | ./stim --sample=10
0010001000100010001000100010001000100010001000100010001000100010011011000101010111010
0000000000000000000000000000000000000000000000000000000000001001100110011001100110011
0010001000100010001000100010001000100010001000100110001000100010001000100010001010110
0001000100010001000100010001000100010001000100010001000100010001000100010001000101101
0010001000100010001000100010001000100010001000100011001100110011001100110011001110110
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0010001000100010001000100010001000100010001000101000100010001000100010001000001000000
0010001000100010101000100010001000110001000100010001000100000001000100010001000100000
0010001000100010001000100010001000100010001100100010101010101010101010101010101010011
0000000000000000000000000000000000000000000001000100010001001000100011001100110000100

Sample detection events in a repetition code with circuit level noise, include two qubit depolarizing noise when performing a CNOT. Instead of listing all 0s and 1s, print the locations of the 1s in each line:

echo "
  M 1 3 5 7
  REPEAT 100 {
    DEPOLARIZE2(0.001) 0 1 2 3 4 5 6 7
    DEPOLARIZE1(0.001) 8
    CNOT 0 1 2 3 4 5 6 7
    DEPOLARIZE2(0.001) 8 7 6 5 4 3 2 1
    DEPOLARIZE1(0.001) 0
    CNOT 8 7 6 5 4 3 2 1
    DEPOLARIZE1(0.001) 0 1 2 3 4 5 6 7 8
    MR 1 3 5 7
    # Parity measurements should stay consistent over time.
    DETECTOR rec[-1] rec[-5]
    DETECTOR rec[-2] rec[-6]
    DETECTOR rec[-3] rec[-7]
    DETECTOR rec[-4] rec[-8]
  }
  M 0 2 4 6 8
  # Data measurements should agree with parity measurements.
  DETECTOR rec[-1] rec[-2] rec[-6]
  DETECTOR rec[-2] rec[-3] rec[-7]
  DETECTOR rec[-3] rec[-4] rec[-8]
  DETECTOR rec[-4] rec[-5] rec[-9]
  # Any one of the data qubit measurements can be the logical measurement result.
  OBSERVABLE_INCLUDE(0) rec[-1]
" | ./stim --detect=10 --out_format=hits
85,89
83


98,103,242,243
125,129,241,245
144,152,153,176,180,238,242
162,166
147
204

Compute the circuit's detector hypergraph (the graph whose nodes are detectors/observables and whose edges are errors grouped into equivalence classes based on which detectors and observables they invert). Output the graph's hyper edges as a series of lines like error(probability) D0 D1 L2:

echo "
  M 0 1
  H 0
  CNOT 0 1
  DEPOLARIZE1(0.01) 0
  X_ERROR(0.1) 1
  CNOT 0 1
  H 0
  M 0 1
  DETECTOR rec[-1] rec[-3]
  DETECTOR rec[-2] rec[-4]
" | ./stim --detector_hypergraph
error(0.1026756153132975941) D0
error(0.003344519141621982161) D0 D1
error(0.003344519141621982161) D1

Command line flags

  • --help: Prints usage examples and exits.

Modes

Only one mode can be specified.

  • --repl: Interactive mode. Print measurement results interactively as a circuit is typed into stdin. Note that this mode, unlike the other modes, prints output before operations that mutate the measurement record (e.g. CNOT rec[-1] rec[-2]) are applied.
  • --sample or --sample=#: Measurement sampling mode. Output measurement results from the given circuit. If an integer argument is specified, run that many shots of the circuit.
  • --detect or --detect=#: Detection event sampling mode. Outputs whether or not measurement sets specified by DETECTOR instructions have been flipped by noise. Assumes (does not verify) that all DETECTOR instructions corresponding to measurement sets with deterministic parity. See also --prepend_observables, --append_observables. If an integer argument is specified, run that many shots of the circuit.
  • --detector_hypergraph: Detector graph creation mode. Computes equivalence classes of errors based on the detectors and observables that the error inverts. Each equivalence class is a hyper edge in the graph, and is weighted with a probability such that independently sampling each edge and inverting the associated detectors is equivalent to sampling from the original circuit. The output is given as series of lines like error(probability) D1 D2 L3 where D# is a detector and L# is a logical observable.

Modifiers

Modifiers tweak how a mode runs. Not all modifiers apply to all modes.

  • --frame0: Significantly improve the performance of measurement sampling mode by asserting that it is possible to take a sample from the given circuit where all measurement results are 0. Allows the frame simulator to start immediately, without waiting for a reference sample from the tableau simulator. If this assertion is wrong, the output samples can be corrected by xoring them against a valid sample from the circuit. Requires measurement sampling mode.
  • --append_observables: Requires detection event sampling mode. In addition to outputting the values of detectors, output the values of logical observables built up using OBSERVABLE_INCLUDE instructions. Put these observables' values into the detection event output as if they were additional detectors at the end of the circuit.
  • --prepend_observables: Requires detection event sampling mode. In addition to outputting the values of detectors, output the values of logical observables built up using OBSERVABLE_INCLUDE instructions. Put these observables' values into the detection event output as if they were additional detectors at the start of the circuit
  • --in=FILEPATH: Specifies a file to read a circuit from. If not specified, the stdin pipe is used. Incompatible with interactive mode.
  • --out=FILEPATH: Specifies a file to create or overwrite with results. If not specified, the stdout pipe is used. Incompatible with interactive mode.
  • --out_format=[name]: Output format to use. Incompatible with interactive mode. Definition: a "sample" is one measurement result in measurement sampling mode or one detector/observable result in detection event sampling mode. Definition: a "shot" is composed of all of the samples from a circuit. Definition: a "sample location" is a measurement gate in measurement sampling mode, or a detector/observable in detection event sampling mode.
    • 01 (default): Human readable ASCII format. Prints all the samples from one shot before moving on to the next. Prints '0' or '1' for each sample. Prints '\n' at the end of each shot. Example all-true ASCII data (for 10 measurements, 4 shots):
      1111111111
      1111111111
      1111111111
      1111111111
      
    • hits: Human readable ASCII format. Writes the decimal indices of samples equal to 1, separated by a comma. Shots are separated by a newline. This format is more useful in --detect mode, where 1s are rarer. Example all-true output data (for 10 measurements, 4 shots):
      0,1,2,3,4,5,6,7,8,9
      0,1,2,3,4,5,6,7,8,9
      0,1,2,3,4,5,6,7,8,9
      0,1,2,3,4,5,6,7,8,9
      
    • dets: Human readable ASCII format. Similar to hits, except each line is prefixed by shot , hits are separated by spaces, and each hit is prefixed by a character indicating its type (M for measurement, D for detector, L for logical observable). Example output data (for 3 detectors, 2 observables):
      shot
      shot L0 L1 D0 D1 D2
      shot L0 D0
      
    • b8: Binary format. Writes all the samples from one shot before moving on to the next. The number of samples is padded up to a multiple of 8 using fake samples set to 0. Samples are combined into groups of 8. The sample results for a group are bit packed into a byte, ordered from least significant bit to most significant bit. The byte for the first sample group is printed, then the second, and so forth for all groups in order. There is no separator between shots (other than the fake zero sample padding). Example all-true output hex data (for 10 measurements, 4 shots):
      FF 30 FF 30 FF 30 FF 30
      
    • ptb64: Partially transposed binary format. The number of shots is padded up to a multiple of 64 using fake shots where all samples are 0. Shots are combined into groups of 64. All the samples from one shot group are written before moving on to the next group. Within a shot group, each of the circuit sample locations has 64 results (one from each shot). These 64 bits of information are packed into 8 bytes, ordered from first file byte to last file byte and then least significant bit to most significant bit. The 8 bytes for the first sample location are output, then the 8 bytes for the next, and so forth for all sample locations. There is no separator between shot groups (the reader must know how many measurements are expected). Example all-true output hex data (for 3 measurements, 81 shots):
      FF FF FF FF FF FF FF FF
      FF FF FF FF FF FF FF FF
      FF FF FF FF FF FF FF FF
      FF FF F1 00 00 00 00 00
      FF FF F1 00 00 00 00 00
      FF FF F1 00 00 00 00 00
      
    • r8: Binary run-length format. Each byte is the length of a run of samples that were all False. If the run length is non-maximal (less than 255), the next measurement result is a 1. For example, 0x00 means [1], 0x03 means [0, 0, 0, 1], 0xFE means [0] * 254 + [1], and 0xFF means [0] * 255. A fake "True" sample is appended to the end of each shot, and the data for a shot ends on the byte that decodes to produce this fake appended True. Note that this means the reader must know how many measurement results are expected, and that the data will never end with a 0xFF. There is no separator between shots, other than padding implicit in appending the fake "True" samples. Example data for an all-false shot then an all-true shot then an all-false shot (with 5 measurements per shot):
      0x05 0x00 0x00 0x00 0x00 0x00 0x00 0x05
      
      Note the extra 0x00 due to the fake appended True.
  • --distance=#: Distance to use in circuit generation mode. Defaults to 3.
  • --rounds=#: Number of rounds to use in circuit generation mode. Defaults to the same as distance.
  • --noise_level=%f: Strength of depolarizing noise, from 0 to 1, to insert into generated circuits. Defaults to 0 (none).

Supported Gates

General facts about all gates.

  • Qubit Targets: Qubits are referred to by non-negative integers. There is a qubit 0, a qubit 1, and so forth (up to an implemented-defined maximum of 16777215). For example, the line X 2 says to apply an X gate to qubit 2. Beware that touching qubit 999999 implicitly tells simulators to resize their internal state to accommodate a million qubits.

  • Measurement Record Targets: Measurement results are referred to by rec[-#] arguments, where the index within the square brackets uses python-style negative indices to refer to the end of the growing measurement record. For example, CNOT rec[-1] 3 says "toggle qubit 3 if the most recent measurement returned True and CZ 1 rec[-2] means "phase flip qubit 1 if the second most recent measurement returned True. There is implementation-defined maximum lookback of -16777215 when accessing the measurement record. Non-negative indices are not permitted.

  • Broadcasting: Most gates support broadcasting over multiple targets. For example, H 0 1 2 will broadcast a Hadamard gate over qubits 0, 1, and 2. Two qubit gates can also broadcast, and do so over aligned pair of targets. For example, CNOT 0 1 2 3 will apply CNOT 0 1 and then CNOT 2 3. Broadcasting is always evaluated in left-to-right order.

Single qubit gates

  • Z: Pauli Z gate. Phase flip.
  • Y: Pauli Y gate.
  • X: Pauli X gate. Bit flip.
  • H (alternate name H_XZ): Hadamard gate. Swaps the X and Z axes. Unitary equals (X + Z) / sqrt(2).
  • H_XY: Variant of the Hadamard gate that swaps the X and Y axes (instead of X and Z). Unitary equals (X + Y) / sqrt(2).
  • H_YZ: Variant of the Hadamard gate that swaps the Y and Z axes (instead of X and Z). Unitary equals (Y + Z) / sqrt(2).
  • S (alternate name SQRT_Z): Principle square root of Z gate. Equal to diag(1, i).
  • S_DAG (alternate name SQRT_Z_DAG): Adjoint square root of Z gate. Equal to diag(1, -i).
  • SQRT_Y: Principle square root of Y gate. Equal to H_YZ*S*H_YZ.
  • SQRT_Y_DAG: Adjoint square root of Y gate. Equal to H_YZ*S_DAG*H_YZ.
  • SQRT_X: Principle square root of X gate. Equal to H*S*H.
  • SQRT_X_DAG: Adjoint square root of X gate. Equal to H*S_DAG*H.
  • I: Identity gate. Does nothing. Why is this even here? Probably out of a misguided desire for closure.

Two qubit gates

  • SWAP: Swaps two qubits.
  • ISWAP: Swaps two qubits while phasing the ZZ observable by i. Equal to SWAP * CZ * (S tensor S).
  • ISWAP_DAG: Swaps two qubits while phasing the ZZ observable by -i. Equal to SWAP * CZ * (S_DAG tensor S_DAG).
  • CNOT (alternate names CX, ZCX): Controlled NOT operation. Qubit pairs are in name order (first qubit is the control, second is the target). This gate can be controlled by on the measurement record. Examples: unitary CNOT 1 2, feedback CNOT rec[-1] 4.
  • CY (alternate name ZCY): Controlled Y operation. Qubit pairs are in name order (first qubit is the control, second is the target). This gate can be controlled by on the measurement record. Examples: unitary CY 1 2, feedback CY rec[-1] 4.
  • CZ (alternate name ZCZ): Controlled Z operation. This gate can be controlled by on the measurement record. Examples: unitary CZ 1 2, feedback CZ rec[-1] 4 or CZ 4 rec[-1].
  • YCZ: Y-basis-controlled Z operation (i.e. the reversed-argument-order controlled-Y). Qubit pairs are in name order. This gate can be controlled by on the measurement record. Examples: unitary YCZ 1 2, feedback YCZ 4 rec[-1].
  • YCY: Y-basis-controlled Y operation.
  • YCX: Y-basis-controlled X operation. Qubit pairs are in name order.
  • XCZ: X-basis-controlled Z operation (i.e. the reversed-argument-order controlled-not). Qubit pairs are in name order. This gate can be controlled by on the measurement record. Examples: unitary XCZ 1 2, feedback XCZ 4 rec[-1].
  • XCY: X-basis-controlled Y operation. Qubit pairs are in name order.
  • XCX: X-basis-controlled X operation.

Collapsing gates

  • M: Z-basis measurement. Examples: M 0, M 2 1, M 0 !3 1 2. Collapses the target qubits and reports their values (optionally flipped). Prefixing a target with a ! indicates that the measurement result should be inverted when reported. In the tableau simulator, this operation may require a transpose and so is more efficient when grouped (e.g. prefer M 0 1 \n H 0 over M 0 \n H 0 \n M 1).
  • R: Reset to |0>. Examples: R 0, R 2 1, R 0 3 1 2. Silently measures the target qubits and bit flips them if they're in the |1> state. Equivalently, discards the target qubits for zero'd qubits. In the tableau simulator, this operation may require a transpose and so is more efficient when grouped (e.g. prefer R 0 1 \n X 0 over R 0 \n X 0 \ nR 1).
  • MR: Z-basis measurement and reset. Examples: MR 0, MR 2 1, MR 0 !3 1 2. Collapses the target qubits, reports their values (optionally flipped), then resets them to the |0> state. Prefixing a target with a ! indicates that the measurement result should be inverted when reported. (The ! does not change that the qubit is reset to |0>.) In the tableau simulator, this operation may require a transpose and so is more efficient when grouped (e.g. prefer MR 0 1 \n H 0 over MR 0 \n H 0 \n MR 1).

Noise Gates

  • DEPOLARIZE1(p): Single qubit depolarizing error. Examples: DEPOLARIZE1(0.001) 1, DEPOLARIZE1(0.0003) 0 2 4 6. With probability p, applies independent single-qubit depolarizing kicks to the given qubits. A single-qubit depolarizing kick is X, Y, or Z chosen uniformly at random.

  • DEPOLARIZE2(p): Two qubit depolarizing error. Examples: DEPOLARIZE2(0.001) 0 1, DEPOLARIZE2(0.0003) 0 2 4 6. With probability p, applies independent two-qubit depolarizing kicks to the given qubit pairs. A two-qubit depolarizing kick is IX, IY, IZ, XI, XX, XY, XZ, YI, YX, YY, YZ, ZI, ZX, ZY, ZZ chosen uniformly at random.

  • X_ERROR(p): Single-qubit probabilistic X error. Examples: X_ERROR(0.001) 0 1. For each target qubit, independently applies an X gate With probability p.

  • Y_ERROR(p): Single-qubit probabilistic Y error. Examples: Y_ERROR(0.001) 0 1. For each target qubit, independently applies a Y gate With probability p.

  • Z_ERROR(p): Single-qubit probabilistic Z error. Examples: Z_ERROR(0.001) 0 1. For each target qubit, independently applies a Z gate With probability p.

  • CORRELATED_ERROR(p) (alternate name E) and ELSE_CORRELATED_ERROR(p): Pauli product error cases. Probabilistically applies a Pauli product error with probability p, unless the "correlated error occurred" flag is already set. CORRELATED_ERROR is equivalent to ELSE_CORRELATED_ERROR except that CORRELATED_ERROR starts by clearing the "correlated error occurred" flag. Both operations set the "correlated error occurred" flag if they apply their error. Example:

      # With 40% probability, uniformly pick X1*Y2 or Z2*Z3 or X1*Y2*Z3.
      CORRELATED_ERROR(0.2) X1 Y2
      ELSE_CORRELATED_ERROR(0.25) Z2 Z3
      ELSE_CORRELATED_ERROR(0.33333333333) X1 Y2 Z3
    

Annotations

  • DETECTOR: Asserts that a set of measurements have a deterministic result, and that this result changing can be used to detect errors. Ignored in measurement sampling mode. In detection sampling mode, a detector produces a sample indicating if it was inverted by noise or not. Example: DETECTOR rec[-1] rec[-2].
  • OBSERVABLE_INCLUDE(k): Adds physical measurement locations to a specified logical observable. The logical measurement result is the parity of all physical measurements added to it. Behaves similarly to a Detector, except observables can be built up globally over the entire circuit instead of being defined locally. Ignored in measurement sampling mode. In detection sampling mode, a logical observable can produce a sample indicating if it was inverted by noise or not. These samples are dropped or put before or after detector samples, depending on command line flags. Examples: OBSERVABLE_INCLUDE(0) rec[-1] rec[-2], OBSERVABLE_INCLUDE(3) rec[-7].

Other

  • TICK: Optional command indicating the end of a layer of gates. May be ignored, may force processing of internally queued operations and flushing of queued measurement results.
  • REPEAT N { ... }: Repeats the instructions in its body N times.

Building

CMake Build

cmake .
make stim
# ./out/stim

To control the vectorization (e.g. this is done for testing), use cmake . -DSIMD_WIDTH=256 (implying -mavx2) or cmake . -DSIMD_WIDTH=128 (implying -msse2) or cmake . -DSIMD_WIDTH=64 (implying no machine architecture flag). If SIMD_WIDTH is not specified, -march=native is used.

Bazel Build

bazel build stim
# bazel run stim

Manual Build

find src | grep "\\.cc" | grep -v "\\.\(test\|perf\|pybind\)\\.cc" | xargs g++ -pthread -std=c++11 -O3 -march=native
# ./a.out

Python Package Build

Environment requirements:

pip install -y pybind11 cibuildwheel

Build source distribution (fallback for missing binary wheels):

python setup.py sdist

Output in dist directory.

Build manylinux binary distributions (takes 30+ minutes):

python -m cibuildwheel --output-dir wheelhouse --platform=linux

Output in wheelhouse directory.

Build stimcirq package:

cd glue/cirq
python setup.py sdist

Output in glue/cirq/dist directory.

Testing

Run tests using CMAKE

Unit testing with CMAKE requires GTest to be installed on your system and discoverable by CMake. Follow the "Standalone CMake Project" from the GTest README.

Run tests with address and memory sanitization, but without optimizations:

cmake .
make stim_test
./out/stim_test

To force AVX vectorization, SSE vectorization, or no vectorization pass -DSIMD_WIDTH=256 or -DSIMD_WIDTH=128 or -DSIMD_WIDTH=64to thecmake` command.

Run tests with optimizations without sanitization:

cmake .
make stim_test_o3
./out/stim_test_o3

Run tests using Bazel

Run tests with whatever settings Bazel feels like using:

bazel :stim_test

Run python binding tests

In a fresh virtual environment:

pip install -e .
pip install -y numpy pytest
python -m pytest src

Benchmarking

cmake .
make stim_benchmark
./out/stim_benchmark

This will output results like:

[....................*....................] 460 ns (vs 450 ns) ( 21 GBits/s) simd_bits_randomize_10K
[...................*|....................]  24 ns (vs  20 ns) (400 GBits/s) simd_bits_xor_10K
[....................|>>>>*...............] 3.6 ns (vs 4.0 ns) (270 GBits/s) simd_bits_not_zero_100K
[....................*....................] 5.8 ms (vs 6.0 ms) ( 17 GBits/s) simd_bit_table_inplace_square_transpose_diam10K
[...............*<<<<|....................] 8.1 ms (vs 5.0 ms) ( 12 GOpQubits/s) FrameSimulator_depolarize1_100Kqubits_1Ksamples_per1000
[....................*....................] 5.3 ms (vs 5.0 ms) ( 18 GOpQubits/s) FrameSimulator_depolarize2_100Kqubits_1Ksamples_per1000

The bars on the left show how fast each task is running compared to baseline expectations (on my dev machine). Each tick away from the center | is 1 decibel slower or faster (i.e. each < or > represents a factor of 1.26).

Basically, if you see [......*<<<<<<<<<<<<<|....................] then something is seriously wrong, because the code is running 25x slower than expected.

The benchmark binary supports a --only=BENCHMARK_NAME filter flag. Multiple filters can be specified by separating them with commas --only=A,B. Ending a filter with a * turns it into a prefix filter --only=sim_*.

Issues
  • 'pip install stim' failing with error 'src/stim.cc:17:10: fatal error: 'stim.h' file not found'

    'pip install stim' failing with error 'src/stim.cc:17:10: fatal error: 'stim.h' file not found'

    I'm a novice with these sorts of things, so any help appreciated. I'm on a Mac running Monterey 12.1. I have a conda environment activated, and from inside that I write 'pip install stim' in the terminal. Here's the output:

    (FaultTolerantQPU) [email protected] FaultTolerantQPU % pip install stim              
    Collecting stim
      Using cached stim-1.8.0.tar.gz (164 kB)
      Installing build dependencies ... done
      Getting requirements to build wheel ... done
      Preparing metadata (pyproject.toml) ... done
    Requirement already satisfied: numpy in /opt/homebrew/Caskroom/miniconda/base/envs/FaultTolerantQPU/lib/python3.10/site-packages (from stim) (1.22.3)
    Building wheels for collected packages: stim
      Building wheel for stim (pyproject.toml) ... error
      error: subprocess-exited-with-error
      
      × Building wheel for stim (pyproject.toml) did not run successfully.
      │ exit code: 1
      ╰─> [29 lines of output]
          /private/var/folders/6x/zhywbg6x4h5grk1nhzkr3l8c0000gn/T/pip-build-env-k7_z1q49/overlay/lib/python3.10/site-packages/setuptools/_distutils/extension.py:131: UserWarning: Unknown Extension options: 'headers'
            warnings.warn(msg)
          running bdist_wheel
          running build
          running build_py
          creating python_build_stim
          creating python_build_stim/lib.macosx-11.1-arm64-cpython-310
          creating python_build_stim/lib.macosx-11.1-arm64-cpython-310/stim
          copying glue/python/src/stim/__init__.py -> python_build_stim/lib.macosx-11.1-arm64-cpython-310/stim
          running build_ext
          building 'stim._stim_march_polyfill' extension
          creating python_build_stim/temp.macosx-11.1-arm64-cpython-310
          creating python_build_stim/temp.macosx-11.1-arm64-cpython-310/src
          creating python_build_stim/temp.macosx-11.1-arm64-cpython-310/src/stim
          creating python_build_stim/temp.macosx-11.1-arm64-cpython-310/src/stim/circuit
          creating python_build_stim/temp.macosx-11.1-arm64-cpython-310/src/stim/dem
          creating python_build_stim/temp.macosx-11.1-arm64-cpython-310/src/stim/gen
          creating python_build_stim/temp.macosx-11.1-arm64-cpython-310/src/stim/io
          creating python_build_stim/temp.macosx-11.1-arm64-cpython-310/src/stim/mem
          creating python_build_stim/temp.macosx-11.1-arm64-cpython-310/src/stim/py
          creating python_build_stim/temp.macosx-11.1-arm64-cpython-310/src/stim/simulators
          creating python_build_stim/temp.macosx-11.1-arm64-cpython-310/src/stim/stabilizers
          clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O2 -Wall -fPIC -O2 -isystem /opt/homebrew/Caskroom/miniconda/base/envs/FaultTolerantQPU/include -arch arm64 -fPIC -O2 -isystem /opt/homebrew/Caskroom/miniconda/base/envs/FaultTolerantQPU/include -arch arm64 -I/private/var/folders/6x/zhywbg6x4h5grk1nhzkr3l8c0000gn/T/pip-build-env-k7_z1q49/overlay/lib/python3.10/site-packages/pybind11/include -Isrc -I/opt/homebrew/Caskroom/miniconda/base/envs/FaultTolerantQPU/include/python3.10 -c src/stim.cc -o python_build_stim/temp.macosx-11.1-arm64-cpython-310/src/stim.o -std=c++11 -fno-strict-aliasing -O3 -g0 -DVERSION_INFO=1.8.0 -mno-avx2 -DSTIM_PYBIND11_MODULE_NAME=_stim_march_polyfill
          clang: warning: argument unused during compilation: '-mno-avx2' [-Wunused-command-line-argument]
          src/stim.cc:17:10: fatal error: 'stim.h' file not found
          #include "stim.h"
                   ^~~~~~~~
          1 error generated.
          error: command '/usr/bin/clang' failed with exit code 1
          [end of output]
      
      note: This error originates from a subprocess, and is likely not a problem with pip.
      ERROR: Failed building wheel for stim
    Failed to build stim
    ERROR: Could not build wheels for stim, which is required to install pyproject.toml-based projects
    

    I've tried upgrading my pip - no joy. When I type 'clang --version' I get:

    (FaultTolerantQPU) [email protected] FaultTolerantQPU % clang --version
    Apple clang version 13.1.6 (clang-1316.0.21.2)
    Target: arm64-apple-darwin21.2.0
    Thread model: posix
    InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
    

    What else can I try / what other info can I give that might be useful? Thanks in advance!

    opened by OzYossarian 12
  • Stim v1.7.0 installation fails for Apple Silicon

    Stim v1.7.0 installation fails for Apple Silicon

    Unfortunately the Python interpreter crashes (using an M1-based mac) when importing stim after installing v1.7.0 with the prebuilt binaries:

    Python 3.9.6 (default, Jun 29 2021, 06:20:32) 
    [Clang 12.0.0 (clang-1200.0.32.29)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import stim
    zsh: illegal hardware instruction  python
    

    I tried instead installing from source with the pip --no-binary flag which raises this exception instead:

      src/stim.cc:17:10: fatal error: 'stim.h' file not found
      #include "stim.h"
               ^~~~~~~~
      1 error generated.
      error: command '/usr/bin/clang' failed with exit code 1
      ----------------------------------------
      ERROR: Failed building wheel for stim
    

    since the stim.h file is no longer in the source distribution. From a comparison with the v1.6.0 source distribution it looks like all the header files are missing in the v1.7.0 PyPI source distribution.

    opened by oscarhiggott 10
  • Stim package cannot run properly on PyCharm

    Stim package cannot run properly on PyCharm

    Install the stim package as required and import it. But it cannot run properly on PyCharm. The result is "ImportError: PauliString: PyType_Ready failed (UnicodeDecodeError: 'utf-8' codec can't decode bytes in position 46-47: invalid continuation byte)!" issue

    opened by Oblak13 10
  • Random exception in python when using multiprocessing due to rdseed failure

    Random exception in python when using multiprocessing due to rdseed failure

    When I am running a large number of samples simulations simultaneously using stim in a massively multi-process environment (python not supporting multi-threading, I use multi-process computation using python multithreading starmap), this happens semi-randomly when I run a multi-threaded simulation, see the stack trace below:

    image001

    This seems to be related to a multi-threading bug in libstdc++, so perhaps using the approach used by the google cloud c++ api would be a good approach. See:

    https://github.com/googleapis/google-cloud-cpp-common/pull/208

    https://github.com/googleapis/google-cloud-cpp-common/pull/272

    seeing as the call to random_device is only done once per process in PYBIND_SHARED_RNG(), it is probably safe performance-wise to use the second fix, as the performance regression should be very minor.

    This seems to be related to the shared rdseed buffer in certain Intel cpus.

    opened by alexisshaw 6
  • Add support for randomly sampling Clifford circuits

    Add support for randomly sampling Clifford circuits

    A useful feature for this package (e.g. for something like simulating shadow tomography) would be to add functionality for randomly sampling Clifford circuits.

    If there is no obvious way of doing this a-priori with the code as it is now, I can have a go at implementing this myself - the procedure itself is not difficult though doing it in such a way that keeps up with Stims impressive scaling performance may require some time.

    Best, QCmonk

    opened by QCmonk 5
  • Basic readers

    Basic readers

    The API is simple, but supports rather varied file formats (one vs many measure records per file, known vs unknown record length) in a consistent way.

    Tests fall into three groups: counterparts to tests for writers, write-read-compare tests and additional tests for multi-record file formats.

    Follow-ups:

    • benchmarking tests for the readers,
    • integration with MeasureRecord,
    • support for PTB64 file format,
    • read counterpart to write_table_data,
    • possible refactoring of write_table_data to fit it into MeasureRecordWriters and similar for the read side (contingent on a fix for the performance regression this is causing).
    opened by viathor 5
  • Compressing circuit representation

    Compressing circuit representation

    Many simulators typically group noisy channels as pairs (unitary, error channel). When translating this into Stim, it can cause a significant blowup in the representation of the circuit (e.g. requiring a new line for each noisy gate). It would be a nice feature if Stim had a function that could automate the compression of such a circuit into an equivalent representation that minimized the number of lines.

    opened by newmanmg 5
  • Observations about SIMD, benchmarking, possible improvements

    Observations about SIMD, benchmarking, possible improvements

    First of all, thank you for creating this, it is incredibly useful!!! Not the least as a reference for other code writers!

    I am the developer of QuantumClifford.jl, which implements some useful Clifford circuit tools. It does not have the elegant Pauli frame tracking that enables your ridiculously high speeds, and generally, it has not been hand-tuned, but it is fairly fast and it has its uses elsewhere ;)

    I made some recent improvements in the inner-loop methods (the Pauli multiplication method) that were inspired by your work and wanted to see how it compares against stim. I was surprised that in one (very restricted) micro-benchmark the pure julia code was faster than your C++ SIMD code.

    Here is the example:

    julia> using QuantumClifford, BenchmarkTools
    julia> a = random_pauli(1_000_000);
    julia> b = random_pauli(1_000_000);
    julia> @btime a*b;
      41.598 μs (4 allocations: 244.39 KiB)
    
    julia> b = random_pauli(1_000_000_000);
    julia> a = random_pauli(1_000_000_000);
    julia> @btime a*b;
      101.032 ms (4 allocations: 238.42 MiB)
    
    In [2]: from stim import *
    In [3]: a = PauliString.random(1_000_000);
    In [4]: b = PauliString.random(1_000_000);
    In [5]: %timeit a*b
    42.7 µs ± 17.8 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
    
    In [6]: b = PauliString.random(1_000_000_000);
    In [7]: a = PauliString.random(1_000_000_000);
    In [8]: %timeit a*b
    214 ms ± 4.65 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
    

    The results are basically the same, except for larger (really large) Pauli's, julia is faster by a factor of 2ish. I do not know whether this is just some slowdown caused by the python interface. Or it might be due to the version of gcc/llvm that was used to compile stim.

    Just to be clear, I am not claiming that my julia library is generally faster: its aim is somewhat different from the aim of Stim and I am certain that, holistically, Stim blows everyone out of the water for the task for which it was designed. But given how crazy fast Stim is, I assumed that you would want to know that there might be still some more optimizations to be done. I can try to dig up the machine code that julia compiles if that would be of use. Here is the julia code performing this operation, which is fairly similar to your C++ code.

    opened by Krastanov 5
  • Stop adding dummy edges to ensure one connected component for pymatching

    Stop adding dummy edges to ensure one connected component for pymatching

    • pymatching no longer requires the input to have just one connected component
    • Use new 'fault_ids' instead of old 'qubit_id' on edges
    • Add a test that invincible observables work correctly
    • Loosen sinter's declared requirements
    opened by Strilanc 4
  • Add support for reading detection and measurement events files using the Python API

    Add support for reading detection and measurement events files using the Python API

    I noticed that while there's a sample_write method to write samples of detection events to file, I can't find a method to read these file formats using the Python API (unless it's there and I can't find it). The same is true of the measurement sampler sample_write method. It would be convenient to have a sample_read method or equivalent to read these file formats.

    opened by oscarhiggott 4
  • add preprocessor clauses for bare support for aarch64

    add preprocessor clauses for bare support for aarch64

    Stim includes some C preprocessor logic which assists in looking up processor intrinsics for specialized / accelerated operator evaluation. Currently, the logic forks on x86 Windows / x86 not-Windows, which breaks on modern Apple devices with ARM64 processors. This PR adds basic support for these processors by adding an extra (dummy) clause to the preprocessor logic, but it does not supply any specialized support for ARM64 features.

    This borrows from the ARM64 clause here: https://opensource.apple.com/source/WTF/WTF-7601.1.46.42/wtf/Platform.h.auto.html .

    opened by ecpeterson 4
  • [WIP] Add stim.Circuit.diagram

    [WIP] Add stim.Circuit.diagram

    • Fix PAULI_CHANNEL_2 not being marked as targeting pairs
    • Fix stim.Circuit.flattened and stim.Circuit.without_noise not fusing all operations
          /-----------------\     /---------------\ /---------------\            /REP 999998      /---------------\ /---------------\            \
     q0: -QUBIT_COORDS(0,0)[email protected]|[email protected]|-M[39999960]-
                                                          |           |          |                                        |           |          |
     q1: -QUBIT_COORDS(1,0)[email protected]@-----------------------|[email protected][0]--|[email protected]@-----------------------|[email protected][40]-|-------------
                                | |                       |                      |              | |                       |                      |
     q2: -QUBIT_COORDS(2,0)-R---X-|-----------------------|[email protected]|--------------X-|-----------------------|[email protected]|-M[39999961]-
                                  |                       | |         |          |                |                       | |         |          |
     q3: -QUBIT_COORDS(3,0)[email protected]|[email protected]|-|[email protected][1]--|[email protected]|[email protected]|-|[email protected][41]-|-------------
                                | | |                     | |                    |              | | |                     | |                    |
     q4: -QUBIT_COORDS(4,0)-R---X-|-|---------------------|-|[email protected]|--------------X-|-|---------------------|-|[email protected]|-M[39999962]-
                                  | |                     | | |       |          |                | |                     | | |       |          |
     q5: -QUBIT_COORDS(5,0)[email protected]|-|[email protected]|-|-|[email protected][2]--|[email protected]|-|[email protected]|-|-|[email protected][42]-|-------------
                                | | | |                   | | |                  |              | | | |                   | | |                  |
     q6: -QUBIT_COORDS(6,0)-R---X-|-|-|-------------------|-|-|[email protected]|--------------X-|-|-|-------------------|-|-|[email protected]|-M[39999963]-
                                  | | |                   | | | |     |          |                | | |                   | | | |     |          |
     q7: -QUBIT_COORDS(7,0)[email protected]|-|-|[email protected]|-|-|-|[email protected][3]--|[email protected]|-|-|[email protected]|-|-|-|[email protected][43]-|-------------
                                | | | | |                 | | | |                |              | | | | |                 | | | |                |
     q8: -QUBIT_COORDS(8,0)-R---X-|-|-|-|-----------------|-|-|-|[email protected]|--------------X-|-|-|-|-----------------|-|-|-|[email protected]|-M[39999964]-
                                  | | | |                 | | | | |              |                | | | |                 | | | | |              |
     q9: -QUBIT_COORDS(0,1)-R---X-|-|-|-|-X---------------X-|-|-|-|-------MR[4]--|--------------X-|-|-|-|-X---------------X-|-|-|-|-------MR[44]-|-------------
                                | | | | | |                 | | | |              |              | | | | | |                 | | | |              |
    q10: -QUBIT_COORDS(1,1)[email protected]|-|-|-|---------X-------|-|-|-|[email protected]|[email protected]|-|-|-|---------X-------|-|-|-|[email protected]|-M[39999965]-
                                    | | | |         |       | | | |   |          |                  | | | |         |       | | | |   |          |
    q11: -QUBIT_COORDS(2,1)-R---X---|-|-|-|-X-------|-------X-|-|-|---X---MR[5]--|--------------X---|-|-|-|-X-------|-------X-|-|-|---X---MR[45]-|-------------
                                |   | | | | |       |         | | |              |              |   | | | | |       |         | | |              |
    q12: -QUBIT_COORDS(3,1)[email protected]|-|-|-|-------|-X-------|-|-|[email protected]|[email protected]|-|-|-|-------|-X-------|-|-|[email protected]|-M[39999966]-
                                      | | | |       | |       | | |   |          |                    | | | |       | |       | | |   |          |
    q13: -QUBIT_COORDS(4,1)-R---X-----|-|-|-|-X-----|-|-------X-|-|---X---MR[6]--|--------------X-----|-|-|-|-X-----|-|-------X-|-|---X---MR[46]-|-------------
                                |     | | | | |     | |         | |              |              |     | | | | |     | |         | |              |
    q14: -QUBIT_COORDS(5,1)[email protected]|-|-|-|-----|-|-X-------|-|[email protected]|[email protected]|-|-|-|-----|-|-X-------|-|[email protected]|-M[39999967]-
                                        | | | |     | | |       | |   |          |                      | | | |     | | |       | |   |          |
    q15: -QUBIT_COORDS(6,1)-R---X-------|-|-|-|-X---|-|-|-------X-|---X---MR[7]--|--------------X-------|-|-|-|-X---|-|-|-------X-|---X---MR[47]-|-------------
                                |       | | | | |   | | |         |              |              |       | | | | |   | | |         |              |
    q16: -QUBIT_COORDS(7,1)[email protected]|-|-|-|---|-|-|-X-------|[email protected]|[email protected]|-|-|-|---|-|-|-X-------|[email protected]|-M[39999968]-
                                          | | | |   | | | |       |   |          |                        | | | |   | | | |       |   |          |
    q17: -QUBIT_COORDS(8,1)-R-------------|-|-|-|-X-|-|-|-|-------X---X---MR[8]--|------------------------|-|-|-|-X-|-|-|-|-------X---X---MR[48]-|-------------
                                          | | | | | | | | |                      |                        | | | | | | | | |                      |
    q18: -QUBIT_COORDS(0,2)[email protected]|-|-|-|-|-|-|-|[email protected]|[email protected]|-|-|-|-|-|-|-|[email protected]|-M[39999969]-
                                            | | | | | | | | |         |          |                          | | | | | | | | |         |          |
    q19: -QUBIT_COORDS(1,2)[email protected]@---------|-|-|-|[email protected]|-|-|-|[email protected][9]--|[email protected]@---------|-|-|-|[email protected]|-|-|-|[email protected][49]-|-------------
                                | |         | | | |   | | | |                    |              | |         | | | |   | | | |                    |
    q20: -QUBIT_COORDS(2,2)-R---X-|[email protected]|-|-|---|-|-|-|[email protected]|--------------X-|[email protected]|-|-|---|-|-|-|[email protected]|-M[39999970]-
                                  |           | | |   | | | | |       |          |                |           | | |   | | | | |       |          |
    q21: -QUBIT_COORDS(3,2)[email protected]|[email protected]|-|-|[email protected]|-|-|-|[email protected][10]-|[email protected]|[email protected]|-|-|[email protected]|-|-|-|[email protected][50]-|-------------
                                | | |         | | |     | | | |                  |              | | |         | | |     | | | |                  |
    q22: -QUBIT_COORDS(4,2)-R---X-|-|[email protected]|-|-----|-|-|-|[email protected]|--------------X-|-|[email protected]|-|-----|-|-|-|[email protected]|-M[39999971]-
                                  | |           | |     | | | | |     |          |                | |           | |     | | | | |     |          |
    q23: -QUBIT_COORDS(5,2)[email protected]|-|[email protected]|-|[email protected]|-|-|-|[email protected][11]-|[email protected]|-|[email protected]|-|[email protected]|-|-|-|[email protected][51]-|-------------
                                | | | |         | |       | | | |                |              | | | |         | |       | | | |                |
    q24: -QUBIT_COORDS(6,2)-R---X-|-|-|[email protected]|-------|-|-|-|[email protected]|--------------X-|-|-|[email protected]|-------|-|-|-|[email protected]|-M[39999972]-
                                  | | |           |       | | | | |   |          |                | | |           |       | | | | |   |          |
    q25: -QUBIT_COORDS(7,2)[email protected]|-|-|[email protected]|[email protected]|-|-|-|[email protected][12]-|[email protected]|-|-|[email protected]|[email protected]|-|-|-|[email protected][52]-|-------------
                                | | | | |         |         | | | |              |              | | | | |         |         | | | |              |
    q26: -QUBIT_COORDS(8,2)-R---X-|-|-|-|[email protected]|-|-|-|[email protected]|--------------X-|-|-|-|[email protected]|-|-|-|[email protected]|-M[39999973]-
                                  | | | |                   | | | | |            |                | | | |                   | | | | |            |
    q27: -QUBIT_COORDS(0,3)-R---X-|-|-|-|-X-----------------X-|-|-|-|-----MR[13]-|--------------X-|-|-|-|-X-----------------X-|-|-|-|-----MR[53]-|-------------
                                | | | | | |                   | | | |            |              | | | | | |                   | | | |            |
    q28: -QUBIT_COORDS(1,3)[email protected]|-|-|-|---------X---------|-|-|-|[email protected]|[email protected]|-|-|-|---------X---------|-|-|-|[email protected]|-M[39999974]-
                                    | | | |         |         | | | | |          |                  | | | |         |         | | | | |          |
    q29: -QUBIT_COORDS(2,3)-R---X---|-|-|-|-X-------|---------X-|-|-|-X---MR[14]-|--------------X---|-|-|-|-X-------|---------X-|-|-|-X---MR[54]-|-------------
                                |   | | | | |       |           | | |            |              |   | | | | |       |           | | |            |
    q30: -QUBIT_COORDS(3,3)[email protected]|-|-|-|-------|-X---------|-|-|[email protected]|[email protected]|-|-|-|-------|-X---------|-|-|[email protected]|-M[39999975]-
                                      | | | |       | |         | | | |          |                    | | | |       | |         | | | |          |
    q31: -QUBIT_COORDS(4,3)-R---X-----|-|-|-|-X-----|-|---------X-|-|-X---MR[15]-|--------------X-----|-|-|-|-X-----|-|---------X-|-|-X---MR[55]-|-------------
                                |     | | | | |     | |           | |            |              |     | | | | |     | |           | |            |
    q32: -QUBIT_COORDS(5,3)[email protected]|-|-|-|-----|-|-X---------|-|[email protected]|[email protected]|-|-|-|-----|-|-X---------|-|[email protected]|-M[39999976]-
                                        | | | |     | | |         | | |          |                      | | | |     | | |         | | |          |
    q33: -QUBIT_COORDS(6,3)-R---X-------|-|-|-|-X---|-|-|---------X-|-X---MR[16]-|--------------X-------|-|-|-|-X---|-|-|---------X-|-X---MR[56]-|-------------
                                |       | | | | |   | | |           |            |              |       | | | | |   | | |           |            |
    q34: -QUBIT_COORDS(7,3)[email protected]|-|-|-|---|-|-|-X---------|[email protected]|[email protected]|-|-|-|---|-|-|-X---------|[email protected]|-M[39999977]-
                                          | | | |   | | | |         | |          |                        | | | |   | | | |         | |          |
    q35: -QUBIT_COORDS(8,3)-R-------------|-|-|-|-X-|-|-|-|---------X-X---MR[17]-|------------------------|-|-|-|-X-|-|-|-|---------X-X---MR[57]-|-------------
                                          | | | | | | | | |                      |                        | | | | | | | | |                      |
    q36: -QUBIT_COORDS(0,4)[email protected]|-|-|-|-|-|-|-|[email protected]|[email protected]|-|-|-|-|-|-|-|[email protected]|-M[39999978]-
                                            | | | | | | | | |         |          |                          | | | | | | | | |         |          |
    q37: -QUBIT_COORDS(1,4)[email protected]@---------|-|-|-|[email protected]|-|-|-|[email protected][18]-|[email protected]@---------|-|-|-|[email protected]|-|-|-|[email protected][58]-|-------------
                                | |         | | | |   | | | |                    |              | |         | | | |   | | | |                    |
    q38: -QUBIT_COORDS(2,4)-R---X-|[email protected]|-|-|---|-|-|-|[email protected]|--------------X-|[email protected]|-|-|---|-|-|-|[email protected]|-M[39999979]-
                                  |           | | |   | | | | |       |          |                |           | | |   | | | | |       |          |
    q39: -QUBIT_COORDS(3,4)[email protected]|[email protected]|-|-|[email protected]|-|-|-|[email protected][19]-|[email protected]|[email protected]|-|-|[email protected]|-|-|-|[email protected][59]-|-------------
                                | | |         | | |     | | | |                  |              | | |         | | |     | | | |                  |
    q40: -QUBIT_COORDS(4,4)-R---X-|-|[email protected]|-|-----|-|-|-|[email protected]|--------------X-|-|[email protected]|-|-----|-|-|-|[email protected]|-M[39999980]-
                                  | |           | |     | | | | |     |          |                | |           | |     | | | | |     |          |
    q41: -QUBIT_COORDS(5,4)[email protected]|-|[email protected]|-|[email protected]|-|-|-|[email protected][20]-|[email protected]|-|[email protected]|-|[email protected]|-|-|-|[email protected][60]-|-------------
                                | | | |         | |       | | | |                |              | | | |         | |       | | | |                |
    q42: -QUBIT_COORDS(6,4)-R---X-|-|-|[email protected]|-------|-|-|-|[email protected]|--------------X-|-|-|[email protected]|-------|-|-|-|[email protected]|-M[39999981]-
                                  | | |           |       | | | | |   |          |                | | |           |       | | | | |   |          |
    q43: -QUBIT_COORDS(7,4)[email protected]|-|-|[email protected]|[email protected]|-|-|-|[email protected][21]-|[email protected]|-|-|[email protected]|[email protected]|-|-|-|[email protected][61]-|-------------
                                | | | | |         |         | | | |              |              | | | | |         |         | | | |              |
    q44: -QUBIT_COORDS(8,4)-R---X-|-|-|-|[email protected]|-|-|-|[email protected]|--------------X-|-|-|-|[email protected]|-|-|-|[email protected]|-M[39999982]-
                                  | | | |                   | | | | |            |                | | | |                   | | | | |            |
    q45: -QUBIT_COORDS(0,5)-R---X-|-|-|-|-X-----------------X-|-|-|-|-----MR[22]-|--------------X-|-|-|-|-X-----------------X-|-|-|-|-----MR[62]-|-------------
                                | | | | | |                   | | | |            |              | | | | | |                   | | | |            |
    q46: -QUBIT_COORDS(1,5)[email protected]|-|-|-|---------X---------|-|-|-|[email protected]|[email protected]|-|-|-|---------X---------|-|-|-|[email protected]|-M[39999983]-
                                    | | | |         |         | | | | |          |                  | | | |         |         | | | | |          |
    q47: -QUBIT_COORDS(2,5)-R---X---|-|-|-|-X-------|---------X-|-|-|-X---MR[23]-|--------------X---|-|-|-|-X-------|---------X-|-|-|-X---MR[63]-|-------------
                                |   | | | | |       |           | | |            |              |   | | | | |       |           | | |            |
    q48: -QUBIT_COORDS(3,5)[email protected]|-|-|-|-------|-X---------|-|-|[email protected]|[email protected]|-|-|-|-------|-X---------|-|-|[email protected]|-M[39999984]-
                                      | | | |       | |         | | | |          |                    | | | |       | |         | | | |          |
    q49: -QUBIT_COORDS(4,5)-R---X-----|-|-|-|-X-----|-|---------X-|-|-X---MR[24]-|--------------X-----|-|-|-|-X-----|-|---------X-|-|-X---MR[64]-|-------------
                                |     | | | | |     | |           | |            |              |     | | | | |     | |           | |            |
    q50: -QUBIT_COORDS(5,5)[email protected]|-|-|-|-----|-|-X---------|-|[email protected]|[email protected]|-|-|-|-----|-|-X---------|-|[email protected]|-M[39999985]-
                                        | | | |     | | |         | | |          |                      | | | |     | | |         | | |          |
    q51: -QUBIT_COORDS(6,5)-R---X-------|-|-|-|-X---|-|-|---------X-|-X---MR[25]-|--------------X-------|-|-|-|-X---|-|-|---------X-|-X---MR[65]-|-------------
                                |       | | | | |   | | |           |            |              |       | | | | |   | | |           |            |
    q52: -QUBIT_COORDS(7,5)[email protected]|-|-|-|---|-|-|-X---------|[email protected]|[email protected]|-|-|-|---|-|-|-X---------|[email protected]|-M[39999986]-
                                          | | | |   | | | |         | |          |                        | | | |   | | | |         | |          |
    q53: -QUBIT_COORDS(8,5)-R-------------|-|-|-|-X-|-|-|-|---------X-X---MR[26]-|------------------------|-|-|-|-X-|-|-|-|---------X-X---MR[66]-|-------------
                                          | | | | | | | | |                      |                        | | | | | | | | |                      |
    q54: -QUBIT_COORDS(0,6)[email protected]|-|-|-|-|-|-|-|[email protected]|[email protected]|-|-|-|-|-|-|-|[email protected]|-M[39999987]-
                                            | | | | | | | | |         |          |                          | | | | | | | | |         |          |
    q55: -QUBIT_COORDS(1,6)[email protected]@---------|-|-|-|[email protected]|-|-|-|[email protected][27]-|[email protected]@---------|-|-|-|[email protected]|-|-|-|[email protected][67]-|-------------
                                | |         | | | |   | | | |                    |              | |         | | | |   | | | |                    |
    q56: -QUBIT_COORDS(2,6)-R---X-|[email protected]|-|-|---|-|-|-|[email protected]|--------------X-|[email protected]|-|-|---|-|-|-|[email protected]|-M[39999988]-
                                  |           | | |   | | | | |       |          |                |           | | |   | | | | |       |          |
    q57: -QUBIT_COORDS(3,6)[email protected]|[email protected]|-|-|[email protected]|-|-|-|[email protected][28]-|[email protected]|[email protected]|-|-|[email protected]|-|-|-|[email protected][68]-|-------------
                                | | |         | | |     | | | |                  |              | | |         | | |     | | | |                  |
    q58: -QUBIT_COORDS(4,6)-R---X-|-|[email protected]|-|-----|-|-|-|[email protected]|--------------X-|-|[email protected]|-|-----|-|-|-|[email protected]|-M[39999989]-
                                  | |           | |     | | | | |     |          |                | |           | |     | | | | |     |          |
    q59: -QUBIT_COORDS(5,6)[email protected]|-|[email protected]|-|[email protected]|-|-|-|[email protected][29]-|[email protected]|-|[email protected]|-|[email protected]|-|-|-|[email protected][69]-|-------------
                                | | | |         | |       | | | |                |              | | | |         | |       | | | |                |
    q60: -QUBIT_COORDS(6,6)-R---X-|-|-|[email protected]|-------|-|-|-|[email protected]|--------------X-|-|-|[email protected]|-------|-|-|-|[email protected]|-M[39999990]-
                                  | | |           |       | | | | |   |          |                | | |           |       | | | | |   |          |
    q61: -QUBIT_COORDS(7,6)[email protected]|-|-|[email protected]|[email protected]|-|-|-|[email protected][30]-|[email protected]|-|-|[email protected]|[email protected]|-|-|-|[email protected][70]-|-------------
                                | | | | |         |         | | | |              |              | | | | |         |         | | | |              |
    q62: -QUBIT_COORDS(8,6)-R---X-|-|-|-|[email protected]|-|-|-|[email protected]|--------------X-|-|-|-|[email protected]|-|-|-|[email protected]|-M[39999991]-
                                  | | | |                   | | | | |            |                | | | |                   | | | | |            |
    q63: -QUBIT_COORDS(0,7)-R---X-|-|-|-|-X-----------------X-|-|-|-|-----MR[31]-|--------------X-|-|-|-|-X-----------------X-|-|-|-|-----MR[71]-|-------------
                                | | | | | |                   | | | |            |              | | | | | |                   | | | |            |
    q64: -QUBIT_COORDS(1,7)[email protected]|-|-|-|---------X---------|-|-|-|[email protected]|[email protected]|-|-|-|---------X---------|-|-|-|[email protected]|-M[39999992]-
                                    | | | |         |         | | | | |          |                  | | | |         |         | | | | |          |
    q65: -QUBIT_COORDS(2,7)-R---X---|-|-|-|-X-------|---------X-|-|-|-X---MR[32]-|--------------X---|-|-|-|-X-------|---------X-|-|-|-X---MR[72]-|-------------
                                |   | | | | |       |           | | |            |              |   | | | | |       |           | | |            |
    q66: -QUBIT_COORDS(3,7)[email protected]|-|-|-|-------|-X---------|-|-|[email protected]|[email protected]|-|-|-|-------|-X---------|-|-|[email protected]|-M[39999993]-
                                      | | | |       | |         | | | |          |                    | | | |       | |         | | | |          |
    q67: -QUBIT_COORDS(4,7)-R---X-----|-|-|-|-X-----|-|---------X-|-|-X---MR[33]-|--------------X-----|-|-|-|-X-----|-|---------X-|-|-X---MR[73]-|-------------
                                |     | | | | |     | |           | |            |              |     | | | | |     | |           | |            |
    q68: -QUBIT_COORDS(5,7)[email protected]|-|-|-|-----|-|-X---------|-|[email protected]|[email protected]|-|-|-|-----|-|-X---------|-|[email protected]|-M[39999994]-
                                        | | | |     | | |         | | |          |                      | | | |     | | |         | | |          |
    q69: -QUBIT_COORDS(6,7)-R---X-------|-|-|-|-X---|-|-|---------X-|-X---MR[34]-|--------------X-------|-|-|-|-X---|-|-|---------X-|-X---MR[74]-|-------------
                                |       | | | | |   | | |           |            |              |       | | | | |   | | |           |            |
    q70: -QUBIT_COORDS(7,7)[email protected]|-|-|-|---|-|-|-X---------|[email protected]|[email protected]|-|-|-|---|-|-|-X---------|[email protected]|-M[39999995]-
                                          | | | |   | | | |         | |          |                        | | | |   | | | |         | |          |
    q71: -QUBIT_COORDS(8,7)-R-------------|-|-|-|-X-|-|-|-|---------X-X---MR[35]-|------------------------|-|-|-|-X-|-|-|-|---------X-X---MR[75]-|-------------
                                          | | | | | | | | |                      |                        | | | | | | | | |                      |
    q72: -QUBIT_COORDS(0,8)[email protected]|-|-|-|-|-|-|-|-----------X----------|[email protected]|-|-|-|-|-|-|-|-----------X----------|-M[39999996]-
                                            | | | | | | | |           |          |                          | | | | | | | |           |          |
    q73: -QUBIT_COORDS(1,8)[email protected]|-|-|-|[email protected]|-|-|[email protected][36]-|[email protected]|-|-|-|[email protected]|-|-|[email protected][76]-|-------------
                                |           | | | |   | | |                      |              |           | | | |   | | |                      |
    q74: -QUBIT_COORDS(2,8)[email protected]|-|-|---|-|-|-----------X----------|[email protected]|-|-|---|-|-|-----------X----------|-M[39999997]-
                                              | | |   | | |           |          |                            | | |   | | |           |          |
    q75: -QUBIT_COORDS(3,8)[email protected]|-|-|[email protected]|-|[email protected][37]-|[email protected]|-|-|[email protected]|-|[email protected][77]-|-------------
                                |             | | |     | |                      |              |             | | |     | |                      |
    q76: -QUBIT_COORDS(4,8)[email protected]|-|-----|-|-----------X----------|[email protected]|-|-----|-|-----------X----------|-M[39999998]-
                                                | |     | |           |          |                              | |     | |           |          |
    q77: -QUBIT_COORDS(5,8)[email protected]|-|[email protected]|[email protected][38]-|[email protected]|-|[email protected]|[email protected][78]-|-------------
                                |               | |       |                      |              |               | |       |                      |
    q78: -QUBIT_COORDS(6,8)[email protected]|-------|-----------X----------|[email protected]|-------|-----------X----------|-M[39999999]-
                                                  |       |           |          |                                |       |           |          |
    q79: -QUBIT_COORDS(7,8)[email protected]|[email protected]@-H-MR[39]-|[email protected]|[email protected]@-H-MR[79]-|-------------
                                |                 |                              |              |                 |                              |
    q80: -QUBIT_COORDS(8,8)[email protected]|[email protected]|-M[40000000]-
          \-----------------/     \---------------/ \---------------/            \                \---------------/ \---------------/            /
    
    opened by Strilanc 0
  • Fix build linking issues on mac

    Fix build linking issues on mac

    The flag -march=native is not present in clang, which required me to specify -DSIMD_WIDTH=64. When I do so I get this error:

    src/stim/circuit/circuit.cc:81:6: error: SSE2 register return with SSE2 disabled
    void validate_gate(const Gate &gate, ConstPointerRange<GateTarget> targets, ConstPointerRange<double> args) {
         ^
    In file included from src/stim/circuit/circuit.cc:15:
    src/stim/circuit/circuit.h:360:21: error: SSE2 register return with SSE2 disabled
        double result = strtod(buf, &end);
                        ^
    
    opened by oscarhiggott 0
  • Add stim.Circuit.diagram

    Add stim.Circuit.diagram

    Attempt at signature (oh boy it's a big one):

    def stim.Circuit.diagram(
        self,
        *,
        style: Literal['timeline', 'layers', '3d'],
        format: Literal['ascii', 'utf-8', 'katex', 'svg', 'html', 'gltf'],
        file: Union[None, str, pathlib.Path, io.BaseTextIO] = None,
        highlights: Iterable[Union[str, CircuitErrorLocation]] = (),
        annotations: Iterable[Union[str, CircuitErrorLocation]] = ("detectors", "observables", "ticks"),
        time: Literal['right', 'up', 'down', 'left', 'toward', 'away'] = 'right',
        projection: Optional[Callable[[int, Tuple[float, ...]], Union[float, Iterable[float]]]] = None,
    ) -> Optional[str]:
        """Creates a diagram of the circuit.
    
        Args:
            style: The type of diagram to make. Available diagram styles are:
                "timeline": A traditional quantum circuit diagram. A musical staff of gates to play.
                "layers": Groups operations into 2d layers separated by TICKs.
                "3d": Produces a 3d model of the circuit.
            format: The desired format to save the diagram in. Available formats are:
                "ascii": Produces a minimalist text diagram that can be shown in any monospace font.
                    Requires style="timeline" or style="layers".
                "utf-8": Produce a fancy text diagram with box drawing characters.
                    Requires style="timeline" or style="layers".
                    See: https://en.wikipedia.org/wiki/Box-drawing_character
                    Beware that it's unfortunately common for "monospace" fonts to not correctly monospace
                    all box drawing characters. If you look at this diagram in such a font, it will be a mess. It is
                    especially frustrating that this behavior can be operating system specific or browser specific.
                    For example, if you put this diagram in a markdown code block on github, it will look fine in
                    [[[[X ON Y]]]] and be mangled by [[[[X ON Y]]]].
                "katex": Produce katex code that can be pasted into a latex document.
                    Requires style="timeline" or style="layers".
                    See: https://katex.org/
                "svg": Produce an SVG image.
                    Requires style="timeline" or style="layers".
                    See the SVG 2 specification: https://www.w3.org/TR/SVG2/
                "gltf": Produce a GL Transmission Format representation of the diagram.
                    Requires style="3d".
                    See the GLTF 2.0 specification: https://github.com/KhronosGroup/glTF/blob/main/specification/2.0/Specification.adoc
                "html": Produce an HTML file with an embedded viewer for the diagram.
                    Works with all styles.
                    For example, if you don't have software to view 3d models, this is the easiest way to look at the 3d model.
                    Note that this HTML can be embedded into a jupyter notebook result.
            file: Defaults to None. When None, the diagram is returned from the method as a string.
                When not None, the diagram is written to the given location and None is returned from the method.
            time: Specifies the time direction (right, up down, left, toward, or away. Defaults to "right".
                "toward" and "away" require style="3d".
            projection: A callable that takes a qubit index and coordinates and returns the coordinates used in the diagram.
            highlights: Specifies a list of things to highlight in the diagram. Examples of things that can be highlighted:
                "M#": Highlight the error sensitivity region of a measurement.
                "D#", "detectors", "gauge-detectors": Highlight the error sensitivity region of a detector.
                "L#", "observables", "gauge-observables": Highlight the error sensitivity region of an observable.
                stim.CircuitErrorLocation: Custom highlight region.
            annotations: Specifies a list of things to annotate in the diagram. Examples of things that can be annotated:
                "shortest-graphlike-error": Finds and annotates a shortest graphlike error.
                stim.CircuitErrorLocation: Annotate a custom error.
                "D#", "detectors", "gauge-detectors": Annotate a detector or set of detectors. Detectors are annotated by place "D#" next to each measurement used by the detector.
                "L#", "observables", "gauge-observables": Annotate a detector or set of detectors. Detectors are annotated by place "D#" next to each measurement used by the detector.
    
    opened by Strilanc 0
Releases(v1.9.0)
Owner
quantumlib
Code for the Quantum World
quantumlib
A fast, byte-code interpreted language

Minima Minima is a small, portable, and fast programming language written in C. The Syntax Minima's syntax is optimized for a fast byte-code translati

null 42 May 10, 2022
Naive Circuit Simulator

Naive Circuit Simulator It's an electric circuit solver written in C++, and it's called naive because it can only solve series and parallel circuits.

Mehrshad Jafari Farahani 9 Jul 5, 2022
MPEG-TS stream selector and stabilizer

tsreadex - MPEG-TSのストリーム選択と安定化のためのツール 使用法: tsreadex [-z ignored][-s seek][-l limit][-t timeout][-m mode][-x pids][-n prog_num_or_index][-a aud1][-b

null 15 Jul 15, 2022
An ATTiny85 implementation of the well known sleep aid. Includes circuit, software and 3d printed case design

dodowDIY An ATTiny85 implementation of the well known sleep aid. Includes circuit, software and 3d printed case design The STL shells are desiged arou

null 14 Nov 7, 2021
A Motorola 68K simulator based on Musashi-master 68K simulator.

cpmsim Originally referenced as http://home.earthlink.net/~schultdw/cpm68/simulator.html (now 404) This simple CP/M-68K simulator, is built around the

Neil Cherry 8 Oct 26, 2021
Mobile robot simulator

The Stage Simulator This is the Stage README file, containing an introduction, license and citation information. Stage is a 2(.5)D robotics standalone

Richard Vaughan 350 Jul 29, 2022
Real-time 2D fluid simulator with lots of visualization options.

Fluid Simulator Building Start by cloning the program and all submodules using the following command: git clone --recursive https://github.com/linusmo

Linus Mossberg 26 Jun 20, 2022
Advanced, modular, coupled geomorpohology simulator for real-time procedural terrain generation.

SoilMachine Advanced, modular, coupled geomorpohology simulator for real-time procedural terrain generation in C++. Visualization with TinyEngine. Art

Nick McDonald 110 Aug 5, 2022
Orbiter is a spaceflight simulator based on Newtonian mechanics.

Orbiter Space Flight Simulator Orbiter is a spaceflight simulator based on Newtonian mechanics. Its playground is our solar system with many of its ma

null 1.4k Aug 6, 2022
Open-source repository of Orbiter Space Flight Simulator

Orbiter Space Flight Simulator Orbiter is a spaceflight simulator based on Newtonian mechanics. Its playground is our solar system with many of its ma

Orbiter Space Flight Simulator 1.4k Jul 27, 2022
Restoration of The Linux Scheduler Simulator (LinSched)

Restoration of The Linux Scheduler Simulator (LinSched)

Jim Huang 13 Sep 3, 2021
HiFi error modeler and simulator with ploidy

A HiFi Shotgun Simulator Author: Gene Myers First: Aug 1, 2021 Current: Aug 1, 2021 Commands Himodel HIsim The Error Model Commands This module contai

Eugene W Myers Jr 9 Aug 4, 2022
GrandOrgue is a sample based pipe organ simulator.

GrandOrgue is a sample based pipe organ simulator. It currently supports Linux, Windows and OS X. Porting to other OS supported by RtMidi,

GrandOrgue 62 Jul 26, 2022
a Living ENsemble Simulator -- a lens to help you watch biophysics

aLENS (a Living ENsemble Simulator) The motivation, algorithm and examples are discussed in this paper: aLENS: towards the cellular-scale simulation o

wenyan4work 7 Jul 3, 2022
A simple mirror simulator made with OpenGL 3.3 and C++

Mirror-Example A simple mirror example using OpenGL 3.3 with C++. Theory It basically constructs a camera at the mirror location, by determining its v

Mohammad Issawi 4 Feb 10, 2022
Biological evolution simulator

biosim4 What is this? This pile of code was used to simulate biological creatures that evolve through natural selection. The results of the experiment

David R. Miller 2.2k Jul 31, 2022
Multi-Joint dynamics with Contact. A general purpose physics simulator.

Multi-Joint dynamics with Contact. A general purpose physics simulator.

DeepMind 4.7k Aug 10, 2022
JaBS - Jaakko's Backscattering Simulator

JaBS - Jaakko's Backscattering Simulator

null 3 Mar 31, 2022
A command line poker simulator made in C++.

C Plus Poker (C++ Poker) Introduction This is a C++ console application that simulates the scoring of a Poker game. Unlike typical Poker, this applica

Zach Schickler 1 Oct 21, 2021
a Little Computer 3 simulator

smol3 ??️ a Little Computer 3 simulator smol3 is a tiny LC3 simulator written in C. It aims to be fast and correct. TODO read input binary files write

pry 1 Oct 22, 2021
Using Pi Pico to provide trimwheel for Flightgear Flight Simulator

Trimwheel for FGFS This uses a Rotary Encoder (RE) and a Raspberry Pi Pico (Pico) to connect the RE to FlightGear Flight Simulator as a Pitch Trimwhee

Dave Attwood 1 Dec 14, 2021
Stock exchange simulator made in Swing using Java with logic backend in C++ giving it faster load time and better data control

StockSimulator Stock exchange simulator made in Swing using Java with logic backend in C++ giving it faster load time and better data control Features

Dušan Todorović 0 Mar 1, 2022
A legacy OpenGL simulator for OpenGL 4.4, written in C++.

the-ancient-tri A legacy OpenGL simulator for OpenGL 4.4, written in C++. Why? My Uni forces us to use legacy OpenGL (eww!), and I didn't want to lear

Mohammad Issawi 4 Feb 10, 2022
gem5 simulator to evaluate the design idea proposed in ASAP: A Speculative Approach to Persistence

About this repository This repository contains the gem5 simulator code for evaluating the design idea proposed in ASAP: A Speculative Approach to Pers

Multifacet 2 May 23, 2022
An Electronic voting machine simulator using C

EVM-using-C An Electronic voting machine simulator using C The project is divided into few parts 1.The program first asks to enter the information of

BABA SHANKAR S N 1 Oct 24, 2021
A tiny evolution simulator, which uses SDL2 and is written in C

evosim Evosim is a small evolution simulator. Evosim uses the SDL2 library for graphics. The program can be compiled by simply running make in the evo

Victor Ocampo 2 Nov 20, 2021
Pedestrian simulator powered by the social force model

Pedestrian simulator powered by the social force model

Social Robotics Lab, University of Freiburg 285 Aug 5, 2022
Adaptive Runtime AUTOSAR Linux Simulator

Adaptive-AUTOSAR Adaptive AUTOSAR is a simulated Adaptive Platform enviroment over Linux defined by AUTOSAR. The goal of this project is to implement

Armin Kassemi Langroodi 91 Aug 5, 2022
Fastest-lap is an optimal laptime simulator, written in C++, and with a python API.

Fastest-lap ?? ?? Fastest-lap is an optimal laptime simulator, written in C++, and with a python API. What can be done Numerical G-G diagram: given a

Juan Manzanero 289 Aug 9, 2022