mimiqcircuits.backends.compiled_projection¶
Compiled form of a projection circuit.
extract_projection()
returns the projection as a Circuit, and
evaluate_projection()
interprets it one instruction at a time, for one shot at a time. That
is the reference semantics, and it costs one interpreted loop per shot.
CompiledProjection compiles the same circuit once, into a
straight-line program over classical bits, and runs it over a whole
block of shots. Two tiers:
Tier.IDENTITY. Bitbreads qubitbfor every bit and the sample is exactly as wide as the register, so the samples are already the answer and are returned unchanged. This is the common case:measure(range, range),measure_all(), and any circuit with no classical register, sinceextract_projectionsynthesises the identity mapping for those.Tier.PROGRAM. Everything else. The block becomes one(shots, num_bits)array and each operation is a single vector operation over its columns, so a permuted read-out, a reset-folded constant and aXortail all cost the same handful of them.
The tier depends on the width of the samples as well as on the
circuit, because a Measure reading past the end of a sample
contributes a zero rather than a qubit. It is therefore chosen the
first time a block of a given width is evaluated, and cached.
Semantics are those of evaluate_projection, which stays the
reference implementation the compiled form is tested against.
Classes
|
A projection circuit compiled for block evaluation. |
|
Names of the two evaluation strategies, in increasing generality. |
- class CompiledProjection(num_bits, steps=<factory>, _plans=<factory>)[source]¶
Bases:
objectA projection circuit compiled for block evaluation.
Build one with
from_circuit()and evaluate a whole block of shots withevaluate_batch(). An instance can be reused across blocks and holds no per-shot state. It gives the same results asevaluate_projection()applied to each shot.- num_bits¶
Width of the classical bitstring the projection produces, equal to
projection.num_bits().- Type:
- steps¶
The straight-line program, before it is specialised to a sample width. Internal representation, subject to change.
Examples
>>> from mimiqcircuits import * >>> from mimiqcircuits.backends import CompiledProjection, extract_projection
A Bell circuit whose measurements are swapped, with the parity of the two outcomes written to a third bit.
>>> c = Circuit().push(GateH(), 0).push(GateCX(), 0, 1) >>> c.push(Measure(), 1, 0).push(Measure(), 0, 1).push(Xor(3), 2, 0, 1) 2-qubit, 3-bit circuit with 5 instructions: ├── H @ q[0] ├── CX @ q[0], q[1] ├── M @ q[1], c[0] ├── M @ q[0], c[1] └── c[2] = c[0] ^ c[1]
Split off the trailing classical part and compile it.
>>> quantum, projection = extract_projection(c) >>> proj = CompiledProjection.from_circuit(projection)
Each sample has one bit per qubit; each result has one bit per classical bit.
>>> proj.evaluate_batch([BitString("00"), BitString("11"), BitString("01")]) [bs"000", bs"110", bs"101"]
- classmethod from_circuit(projection)[source]¶
Compile
projectionfor block evaluation.- Parameters:
projection (Circuit) – The projection circuit returned by
extract_projection(), with qubit indices in the frame the samples will arrive in. Apply any qubit remapping first, or a permuted read-out is mistaken for an identity one.- Returns:
The compiled form, of width
projection.num_bits().- Raises:
ValueError – If
projectionholds an operation other thanMeasure,SetBit0,SetBit1,Not,And,OrandXor.- Return type: