Python API
Simulator
Interactive simulator.
Missing qubits are automatically appended in the all-zero state before a mutating operation.
__init__
Create an empty interactive simulator.
Missing qubits are automatically appended in the all-zero state before a mutating operation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
int | None
|
Optional unsigned 64-bit seed for measurements and noise. |
None
|
Examples:
num_qubits
property
x
Apply Pauli X sequentially to each target.
Raises:
| Type | Description |
|---|---|
IndexError
|
If a target is negative. |
y
Apply Pauli Y sequentially to each target.
Raises:
| Type | Description |
|---|---|
IndexError
|
If a target is negative. |
z
Apply Pauli Z sequentially to each target.
Raises:
| Type | Description |
|---|---|
IndexError
|
If a target is negative. |
s
Apply the phase gate S sequentially to each target.
Raises:
| Type | Description |
|---|---|
IndexError
|
If a target is negative. |
s_dag
Apply S-dagger sequentially to each target.
Raises:
| Type | Description |
|---|---|
IndexError
|
If a target is negative. |
t
Apply a T gate sequentially to each target.
Raises:
| Type | Description |
|---|---|
IndexError
|
If a target is negative. |
t_dag
Apply a T-dagger gate sequentially to each target.
Raises:
| Type | Description |
|---|---|
IndexError
|
If a target is negative. |
cnot
Apply CNOTs to consecutive control-target pairs.
All targets are validated before the simulator grows or mutates.
Raises:
| Type | Description |
|---|---|
ValueError
|
If there is an odd target count or a pair repeats a qubit. |
IndexError
|
If a target is negative. |
cz
Apply controlled-Z gates to consecutive target pairs.
Raises:
| Type | Description |
|---|---|
ValueError
|
If there is an odd target count or a pair repeats a qubit. |
IndexError
|
If a target is negative. |
swap
Swap consecutive target pairs.
Raises:
| Type | Description |
|---|---|
ValueError
|
If there is an odd target count or a pair repeats a qubit. |
IndexError
|
If a target is negative. |
ccz
Apply CCZ gates to consecutive target triples.
Raises:
| Type | Description |
|---|---|
ValueError
|
If the target count is not divisible by three or a triple repeats a qubit. |
IndexError
|
If a target is negative. |
measure
Measure one qubit in the Z basis and collapse the state.
Returns:
| Type | Description |
|---|---|
bool
|
|
Raises:
| Type | Description |
|---|---|
IndexError
|
If |
Examples:
measure_z
measure_many
measure_x
Measure one qubit in the X basis and collapse the state.
Raises:
| Type | Description |
|---|---|
IndexError
|
If |
MeasurementError
|
If the projected state is not supported by the DCP representation. Failure does not mutate state or RNG. |
Examples:
measure_observable
Measure a signed Hermitian Pauli product.
The compact observable syntax has an optional leading sign followed by
positional X, Y, Z, I, or _ factors, for example -X_YZ.
Readout flipping occurs after a successful physical projection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observable
|
str
|
Compact positional Pauli string. |
required |
flip_probability
|
float
|
Probability of flipping the reported bit. |
0.0
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the observable or probability is invalid. |
MeasurementError
|
If the projection is incompatible. Failure does not mutate state or RNG. |
Examples:
peek_observable_expectation
Return a Pauli expectation without mutating state, size, or RNG.
Untracked factors are evaluated against implicit all-zero qubits. The
result is a general floating-point value in [-1, 1].
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
peek_probability
Return the probability that a Z measurement reports True.
This does not mutate state, grow the simulator, or consume randomness. An untracked target has probability zero.
Raises:
| Type | Description |
|---|---|
IndexError
|
If |
Examples:
peek_z_probability is an exact alias.
peek_z_probability
peek_x_probability
Return the probability that an X measurement reports True.
Raises:
| Type | Description |
|---|---|
IndexError
|
If |
MeasurementError
|
If the corresponding X measurement is incompatible with the DCP representation. |
Examples:
x_error
y_error
z_error
depolarize1
depolarize2
Apply uniform two-qubit depolarizing noise to consecutive pairs.
The non-identity Pauli product is chosen uniformly from 15 choices.
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
IndexError
|
If a target is negative. |
Examples:
depolarize3
Apply uniform three-qubit depolarizing noise to target triples.
The non-identity Pauli product is chosen uniformly from 63 choices.
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
IndexError
|
If a target is negative. |
Examples:
state_vector
Return a normalized, canonical-phase state vector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
endian
|
Literal['little', 'big']
|
Whether qubit 0 is the least- or most-significant index bit. |
'little'
|
Returns:
| Type | Description |
|---|---|
NDArray[complex64]
|
A one-dimensional NumPy |
This query does not mutate state or consume randomness.
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
reset
reset_z
reset_x
copy
Copy the quantum state with configurable RNG behavior.
By default the copy receives a fresh entropy-seeded RNG. Set
copy_rng=True to reproduce the exact RNG state, or pass seed to use
a chosen new stream.
Raises:
| Type | Description |
|---|---|
ValueError
|
If both |
Examples:
CircuitSampler
Parse-once sampler backed by Simulator.
A private noiseless replay is performed during construction to define the detector and observable reference parities. Every attempted shot otherwise starts from a fresh all-zero state and executes the circuit in source order.
__init__
Create a sampler and eagerly compute its noiseless reference.
The circuit is parsed once. Each attempted shot then starts from a fresh
all-zero Simulator and executes the circuit in source order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
circuit
|
str
|
Circuit source in the supported extended Stim format. |
required |
seed
|
int | None
|
Optional unsigned 64-bit seed for reproducible sampling. |
None
|
postselect
|
Sequence[int] | None
|
Detector indices whose normalized |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the circuit or postselection mask is invalid. |
MeasurementError
|
If noiseless reference generation encounters an incompatible measurement. |
num_measurements
property
Number of measurement-record columns in a complete shot.
num_fault_sites
property
Number of explicit noise and readout-fault sites.
Batched single-qubit noise targets count separately, while two- and three-qubit noise groups count once. A measurement target is counted only when its readout probability was written explicitly, including an explicit probability of zero.
sample
Sample the circuit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shots
|
int
|
Number of shots to attempt. Rejected postselected attempts are not replaced. |
required |
bit_packed
|
bool
|
Pack each boolean output family independently into
little-endian |
False
|
num_faults
|
int | None
|
Sample uniformly from executions where exactly |
None
|
Returns:
| Type | Description |
|---|---|
SampleResult
|
Measurement records, detector flips, observable flips, expectation |
SampleResult
|
values, and attempted/accepted shot counts. Postselected attempts |
SampleResult
|
are stopped at the rejecting detector and omitted from all arrays. |
SampleResult
|
Successive calls continue the sampler's RNG stream. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
MeasurementError
|
If a shot encounters an incompatible measurement. Randomness consumed before the failure remains consumed. |
Examples:
from merlin_sim import CircuitSampler
sampler = CircuitSampler(
'''
RX 0
T 0
EXP_VAL X0 Z0
MX 0
DETECTOR rec[-1]
OBSERVABLE_INCLUDE(0) rec[-1]
''',
seed=1234,
postselect=[0],
)
assert sampler.num_qubits == 1
assert sampler.num_measurements == 1
assert sampler.num_detectors == 1
assert sampler.num_observables == 1
assert sampler.num_exp_vals == 2
result = sampler.sample(100)
assert result.total_shots == 100
assert result.discards == result.total_shots - result.accepted_shots
assert result.measurements.shape == (result.accepted_shots, 1)
assert result.detectors.shape == (result.accepted_shots, 1)
assert result.observables.shape == (result.accepted_shots, 1)
assert result.exp_vals.shape == (result.accepted_shots, 2)
packed = sampler.sample(100, bit_packed=True)
assert packed.measurements.dtype.name == "uint8"
See Circuit instructions for the complete accepted circuit language.
SampleResult
Results returned by CircuitSampler.sample.
Each array has one row per accepted shot. With unpacked sampling the three
bit arrays have shape (accepted_shots, count) and NumPy bool dtype.
With bit-packed sampling they have shape
(accepted_shots, ceil(count / 8)), uint8 dtype, and independently
padded little-endian rows. Expectation values are never packed and have
shape (accepted_shots, num_exp_vals).
measurements
property
Raw measurement records in circuit order.
detectors
property
Detector flips relative to the noiseless reference sample.
observables
property
Logical-observable flips relative to the noiseless reference.
exp_vals
property
Expectation values from EXP_VAL, with NumPy float64 dtype.