mimiqcircuits.backends.measure_analysis

Final-block analysis.

extract_projection() scans a circuit backwards, moves its trailing measurements, resets and classical logic into a small projection circuit of classical-bit instructions, and returns the remaining quantum circuit together with that projection. A backend then evolves the quantum circuit once and maps every sampled shot through the projection:

quantum_circuit, projection_circuit = extract_projection(c)
# evolve through quantum_circuit once
# for each shot:
#     sample = quantum_state.sample()
#     cstate = evaluate_projection(projection_circuit, sample)

The projection circuit only contains classical-bit operations:

  • Measure(q, b) — cstate[b] = sample[q]

  • SetBit0(b) / SetBit1(b) — bit is classically known

  • Not(b) — cstate[b] = !cstate[b]

  • And / Or / Xor — bits[0] is the target, the rest are read

Evaluate it one shot at a time with evaluate_projection(), or a whole block at a time with CompiledProjection, which has the same semantics compiled once instead of interpreted per shot.

Functions

any_mixed_unitary(circuit)

Return whether circuit has a mixed-unitary Kraus channel.

evaluate_projection(projection, sample)

Run a projection circuit on one shot.

extract_projection(circuit)

Split circuit into the part to evolve and a classical projection.

needs_loss_sampling(circuit)

Return whether circuit has loss operations to resolve before simulation.

needs_trajectories(circuit)

Return whether circuit must be evolved once per shot.

remap_projection_qubits(projection, ...)

Rename the qubits read by the Measure instructions of a projection.

extract_projection(circuit)[source]

Split circuit into the part to evolve and a classical projection.

The circuit is scanned backwards. Trailing operations that do not change the final quantum state seen by the remaining operations move into a projection circuit of classical-bit instructions; the rest is the quantum circuit the simulator evolves.

Absorbed operations:

  • Trailing Measure(q, b) / MeasureReset(q, b) → Measure(q, b).

  • Trailing classical logic: Not, And, Or, Xor, SetBit0, SetBit1. These read other classical bits, so what comes out is a straight-line program over the register rather than a per-bit map, emitted in source order. A logic op is absorbed only when no operation left in quantum_circuit touches any of its bits, otherwise moving it past that operation would change what it reads.

  • Trailing GateID — dropped as a no-op. GateX / GateY / GateZ are NOT absorbed: Y and Z carry phases that would corrupt amplitude lookups, and X absorbed alone would require XOR-ing the qubit-flip pattern into every user-supplied bitstrings entry for amplitudes to stay consistent. Keeping all Paulis in quantum_circuit means the projection only contains phase-free transformations and amplitude lookups need no compensation.

  • Reset(q) whose qubit has captured pending bits → those bits become classical constants (SetBit0 / SetBit1 based on absorbed X parity). A Reset whose qubit has no captured bits is harmless and dropped.

Anything that cannot be absorbed (other gates, Kraus channels, IfStatement, Amplitude, ExpectationValue, …) blocks its qubits and survives into quantum_circuit.

If the source has no classical register, the projection is synthesised over an identity bit↔qubit mapping (bit i mirrors qubit i, length = circuit.num_qubits()).

A write to a classical bit that a later absorbed operation overwrites without reading is dropped from the projection, so reusing a classical bit does not cost a per-shot write that nothing observes.

Parameters:

circuit (Circuit) – The circuit to split. It is not changed.

Returns:

A tuple (quantum_circuit, projection_circuit).

Return type:

Tuple[Circuit, Circuit]

Examples

>>> from mimiqcircuits import *
>>> from mimiqcircuits.backends import evaluate_projection, extract_projection
>>> c = Circuit().push(GateH(), 0).push(GateCX(), 0, 1)
>>> c.push(Measure(), range(2), range(2))
2-qubit, 2-bit circuit with 4 instructions:
├── H @ q[0]
├── CX @ q[0], q[1]
├── M @ q[0], c[0]
└── M @ q[1], c[1]

>>> quantum, projection = extract_projection(c)

The measurements leave the quantum circuit …

>>> quantum
2-qubit circuit with 2 instructions:
├── H @ q[0]
└── CX @ q[0], q[1]

… and become the projection, applied to each sampled shot.

>>> projection
2-qubit, 2-bit circuit with 2 instructions:
├── M @ q[0], c[0]
└── M @ q[1], c[1]

>>> evaluate_projection(projection, BitString("11")) == BitString("11")
True
evaluate_projection(projection, sample)[source]

Run a projection circuit on one shot.

The projection may hold Measure, SetBit0, SetBit1, Not, And, Or and Xor. The logic operations take their target as the first bit and read the rest, and the whole right-hand side is evaluated before the target is written, so a target that is also one of its own operands reads its old value. A Measure(q, b) whose qubit is outside sample leaves bit b untouched, so it reads zero unless an earlier instruction set it.

This is the reference implementation; CompiledProjection gives the same results faster for many shots.

Parameters:
Returns:

The classical register after the projection, of length projection.num_bits().

Raises:

ValueError – If projection holds any other operation.

Return type:

BitString

See also

extract_projection(), whose example uses it.

needs_trajectories(circuit)[source]

Return whether circuit must be evolved once per shot.

That is the case when it has an operation on qubits that is not unitary (a measurement, a reset, a noise channel, …). Operations on no qubits and operations that declare themselves unitary (gates, annotations, ExpectationValue, …) are ignored. Apply it to the quantum circuit returned by extract_projection(), since trailing measurements do not need per-shot evolution.

Parameters:

circuit (Circuit) – The circuit to check.

Returns:

True if a non-unitary operation acts on qubits.

Return type:

bool

needs_loss_sampling(circuit)[source]

Return whether circuit has loss operations to resolve before simulation.

Parameters:

circuit (Circuit) – The circuit to check.

Returns:

True if it has a Loss, Reload, Check or MeasureCheck instruction at the top level.

Return type:

bool

any_mixed_unitary(circuit)[source]

Return whether circuit has a mixed-unitary Kraus channel.

This is the default of LocalBackend.recompile_per_trajectory().

Parameters:

circuit (Circuit) – The circuit to check.

Returns:

True if a top-level Kraus channel has ismixedunitary() true.

Return type:

bool

remap_projection_qubits(projection, qubit_order, do_remap)[source]

Rename the qubits read by the Measure instructions of a projection.

Every Measure(q, b) becomes Measure(qubit_order[q], b); other instructions are copied as they are.

Parameters:
  • projection (Circuit) – The projection circuit.

  • qubit_order (list[int]) – The new index of each qubit, 0-based.

  • do_remap (bool) – When False, projection itself is returned unchanged.

Returns:

A new circuit, or projection when do_remap is False. The input is never changed.

Return type:

Circuit