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 knownNot(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
|
Return whether |
|
Run a projection circuit on one shot. |
|
Split |
|
Return whether |
|
Return whether |
|
Rename the qubits read by the |
- extract_projection(circuit)[source]¶
Split
circuitinto 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 inquantum_circuittouches any of its bits, otherwise moving it past that operation would change what it reads.Trailing
GateID— dropped as a no-op.GateX/GateY/GateZare NOT absorbed:YandZcarry phases that would corrupt amplitude lookups, andXabsorbed alone would require XOR-ing the qubit-flip pattern into every user-suppliedbitstringsentry for amplitudes to stay consistent. Keeping all Paulis inquantum_circuitmeans 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/SetBit1based on absorbed X parity). AResetwhose 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 intoquantum_circuit.If the source has no classical register, the projection is synthesised over an identity bit↔qubit mapping (bit
imirrors qubiti, 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:
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,OrandXor. 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. AMeasure(q, b)whose qubit is outsidesampleleaves bitbuntouched, so it reads zero unless an earlier instruction set it.This is the reference implementation;
CompiledProjectiongives the same results faster for many shots.- Parameters:
projection (Circuit) – The projection circuit returned by
extract_projection().sample (BitString) – One computational-basis outcome, one bit per qubit of the evolved state.
- Returns:
The classical register after the projection, of length
projection.num_bits().- Raises:
ValueError – If
projectionholds any other operation.- Return type:
See also
extract_projection(), whose example uses it.
- needs_trajectories(circuit)[source]¶
Return whether
circuitmust 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 byextract_projection(), since trailing measurements do not need per-shot evolution.
- needs_loss_sampling(circuit)[source]¶
Return whether
circuithas loss operations to resolve before simulation.
- any_mixed_unitary(circuit)[source]¶
Return whether
circuithas a mixed-unitary Kraus channel.This is the default of
LocalBackend.recompile_per_trajectory().
- remap_projection_qubits(projection, qubit_order, do_remap)[source]¶
Rename the qubits read by the
Measureinstructions of a projection.Every
Measure(q, b)becomesMeasure(qubit_order[q], b); other instructions are copied as they are.- Parameters:
- Returns:
A new circuit, or
projectionwhendo_remapisFalse. The input is never changed.- Return type: