Circuit Execution¶
What the simulator accepts, how measurement and noise are handled, and how to read results back.
How it works¶
execute does three things:
- Compile.
compile(circuit)wraps the circuit for evolution. It is pure: no randomness, no sampling, no state. The same compiled circuit serves every shot. - Evolve. Each instruction is routed to the Clifford frame, the MPS, or the classical registers, per the table in Concepts.
- Read out. Measurement outcomes land in the classical register, observables
in the complex register, and both are collected into a
QCSResults.
Execution modes¶
MIMIQ picks the mode from the circuit's content.
Sampling mode¶
A circuit with no mid-circuit randomness is evolved once, then sampled
nsamples times. Trailing measurements are stripped and replaced by sampling the
final state, which is much cheaper than repeating the evolution.
from mimiqcircuits import Circuit, GateH, GateCX
from tensorkeeper import TkSimulator
c = Circuit()
c.push(GateH(), 0)
c.push(GateCX(), 0, 1)
sim = TkSimulator()
results = sim.execute(c, nsamples=1000, seed=1)
print(results.histogram())
Trajectory mode¶
A circuit containing noise, mid-circuit measurement, reset or feed-forward is
evolved once per shot, because each shot follows a different branch. This is
nsamples times the work of sampling mode.
The state is reused across shots and reset between them, so the frame, the MPS and the accumulated fidelity all start fresh each time.
Supported operations¶
The declared capabilities cover amplitudes and sampling, the classical bit and
complex z registers, mid-circuit measurement and reset in the \(X\), \(Y\) and \(Z\)
bases, feed-forward through IfStatement, mixed-unitary (Pauli) noise, the
BondDim instruction, and expectation values of Pauli strings and of general one-
and two-qubit operators. API Reference lists the exact
token-to-instruction mapping.
Every declared capability executes, and anything not declared is refused. Where
it can be detected up front, the refusal comes from can_handle before any work
is done rather than part-way through a long run:
from mimiqcircuits import AmplitudeDamping
c = Circuit()
c.push(AmplitudeDamping(0.1), 0)
print(sim.can_handle(c))
# Inadmissible(reason='backend TensorKeeper supports only mixed-unitary (Pauli)
# noise; AmplitudeDamping cannot be absorbed into a Clifford frame')
Measurement and reset¶
Measurement is the one operation that touches both halves of the state. The measured Pauli is read from the frame, and the corresponding projector is applied to the MPS.
from mimiqcircuits import Measure
c = Circuit()
c.push(GateH(), 0)
c.push(GateCX(), 0, 1)
c.push(Measure(), 0, 0) # qubit 0 -> classical bit 0
c.push(Measure(), 1, 1)
results = sim.execute(c, nsamples=100, seed=1)
print(results.histogram()) # only bs"00" and bs"11": perfectly correlated
Reset is not local
Reset measures the qubit and then flips it conditionally. If the qubit is
entangled with the rest of the register, that measurement projects the rest
of the register onto the sampled branch. The reset qubit ends in
\(|0\rangle\) deterministically; the other qubits do not. This is physics, not
an implementation choice, and it surprises people.
Pauli-product measurements, MeasureXX, MeasureYY, MeasureZZ and
MeasurePauli, run as a Clifford basis change, a single-qubit measurement and
the inverse basis change. All of it happens in the frame, so a stabilizer
measurement costs no bond dimension.
PrepareStateT puts a qubit in the magic state \(T|+\rangle\) whatever its
previous state: a reset, then H in the frame, then T on the MPS. The reset
leaves the qubit a product state, so the T lands on a free pivot and costs no
bond dimension either.
Noise¶
Only mixed-unitary channels are supported: those expressible as a
probabilistic mixture of unitaries, which for a Clifford frame means Pauli
mixtures. Depolarizing, PauliX, PauliNoise and friends qualify.
Such a channel costs nothing. One Pauli branch is sampled per shot and folded into the frame at \(O(n)\), leaving the MPS untouched. Noise is therefore free in the same sense a Clifford gate is, and a noisy Clifford circuit still runs at \(\chi = 1\).
from mimiqcircuits import Depolarizing1
c = Circuit()
c.push(GateH(), 0)
c.push(Depolarizing1(0.05), 0)
c.push(Measure(), 0, 0)
results = sim.execute(c, nsamples=2000, seed=1)
print(results.histogram())
A general Kraus channel such as AmplitudeDamping cannot be written as a Pauli
mixture, so there is nothing to sample into the frame. TensorKeeper refuses it
rather than approximating it: silently simulating different physics is worse than
an error.
Observables¶
ExpectationValue computes \(\langle\psi|O|\psi\rangle\) without collapsing or
copying the state. The operator is conjugated through the frame instead of being
applied, so a Pauli string costs one MPS inner product.
from mimiqcircuits import ExpectationValue, PauliString
c = Circuit()
c.push(GateH(), 0)
c.push(GateCX(), 0, 1)
c.push(ExpectationValue(PauliString("ZZ")), 0, 1, 0) # -> z-variable 0
c.push(ExpectationValue(PauliString("XX")), 0, 1, 1)
state = sim.build_state(2, 0, 2)
state, _ = sim.evolve(state, sim.compile(c))
print(state.complex_values) # [1+0j, 1+0j]: both correlators are +1
A general one- or two-qubit operator works too. It is Pauli-decomposed into \(4^m\) terms for \(m\) qubits, each term free in the frame:
from mimiqcircuits import GateZ, GateCZ
c.push(ExpectationValue(GateZ()), 0, 2)
c.push(ExpectationValue(GateCZ()), 0, 1, 3)
The operator must be Hermitian to be an observable at all.
Reading the state directly¶
Outside a circuit, query the state:
import random
from mimiqcircuits import BitString
state = sim.build_state(2)
state, _ = sim.evolve(state, sim.compile(c))
state.amplitude(BitString("00")) # complex
state.probability(BitString("00")) # float
state.sample(100, random.Random(1)) # list of BitString
state.expectation_paulistring("ZZ", [0, 1]) # complex
state.expectation_operator([1, 0, 0, -1], [0]) # column-major 1q operator
state.bond_dim # int
sample does not destroy the state: each draw collapses a throwaway copy, so the
state stays queryable afterwards.
These read-outs do not all cost the same, and the factorization is why. A Pauli
string is conjugated through the frame into another Pauli string, so
expectation_paulistring is one MPS inner product however wide the string and
however scrambled the frame. A computational-basis amplitude is the expensive one:
the frame turns a single basis state of \(|\psi\rangle\) into a superposition over
\(|\varphi\rangle\), so its cost is exponential in the stabilizer entanglement of
the frame rather than in the qubit count. Both are exact, whatever truncation the
evolution did.
Randomness¶
Randomness belongs to the call, not to the simulator: a TkSimulator holds no
seed. What a call is given decides what it draws.
| Passed | Result |
|---|---|
seed=7 (execute only) |
The same seed gives the same results, every time. |
rng=generator |
The call advances the generator. Two calls sharing one generator differ; two generators built from the same seed agree. |
| neither | Fresh entropy. Two calls differ and neither can be repeated. |
execute takes seed= or rng=, a random.Random. The calls below it,
TkSimulator.evolve and TkState.sample, take rng only.
import random
from tensorkeeper import TkSimulator
sim = TkSimulator()
a = sim.execute(circuit, nsamples=1000, seed=7)
b = sim.execute(circuit, nsamples=1000, seed=7) # identical to `a`
# One generator for a whole script: rerunning the script repeats every run,
# and the runs still differ from one another.
rng = random.Random(7)
c = sim.execute(circuit, nsamples=1000, rng=rng)
d = sim.execute(circuit, nsamples=1000, rng=rng) # differs from `c`
# A simulation split over several calls continues one stream, so the
# measurements of the second part are independent of those of the first.
rng = random.Random(7)
state = sim.build_state(n, nbits)
state, _ = sim.evolve(state, sim.compile(first_part), rng=rng)
state, _ = sim.evolve(state, sim.compile(second_part), rng=rng)
shots = state.sample(1000, rng)
Building a new generator from the same seed for each part would hand both parts the same random numbers, and their measurement outcomes would be correlated. Pass the generator along instead.
Results¶
execute returns a mimiqcircuits.QCSResults:
| Field | Contents |
|---|---|
histogram() |
measured bitstrings and their counts |
cstates |
the classical register, one entry per shot |
zstates |
the complex register, one entry per shot |
fidelities |
one fidelity per shot |
amplitudes |
amplitudes for any bitstrings you requested |
The fidelity entries are the accuracy claim for each shot; see Accuracy and Tuning for what the number is worth.
Parameters¶
execute takes the standard MIMIQ driver arguments. The ones that matter here:
| Parameter | Meaning |
|---|---|
nsamples |
shots in sampling mode, trajectories in trajectory mode |
seed |
seeds the run: the same seed gives the same results; see Randomness |
rng |
a random.Random to draw the run's seed from, advanced by the call; passing both is a TypeError |
callback |
called once per instruction as callback(i, total), with a 1-based i |
progress |
display a progress bar, driven by that callback |
simplify |
cancel inverse pairs, drop identities and merge rotations before the run; see below |
simplify=True runs a reduced version of MIMIQ's simplification pass. The
default pass also resynthesizes gate runs into GateU, which this backend
cannot recognize as Clifford, so it would move frame work onto the MPS. The
reduced pass only ever turns gates into the frame's favor: T T becomes S.
execute also takes a SweepExperiment, and returns one result per point of
its parameter table.
Accuracy settings are not per-call. bonddim, sv_cutoff and
disentangle_period are fixed when the simulator is constructed, so build a new
TkSimulator to change one. So is max_num_threads, which caps the threads a
run of that simulator uses for the linear algebra and for sampling.
Next steps¶
- Accuracy and Tuning: choosing
bonddim,sv_cutoffand the disentangler, and reading the fidelity. - Examples: worked scripts for each of the above.