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. Bit b reads qubit b for 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, since extract_projection synthesises 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 a Xor tail 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

CompiledProjection(num_bits[, steps, _plans])

A projection circuit compiled for block evaluation.

Tier()

Names of the two evaluation strategies, in increasing generality.

class CompiledProjection(num_bits, steps=<factory>, _plans=<factory>)[source]

Bases: object

A projection circuit compiled for block evaluation.

Build one with from_circuit() and evaluate a whole block of shots with evaluate_batch(). An instance can be reused across blocks and holds no per-shot state. It gives the same results as evaluate_projection() applied to each shot.

num_bits

Width of the classical bitstring the projection produces, equal to projection.num_bits().

Type:

int

steps

The straight-line program, before it is specialised to a sample width. Internal representation, subject to change.

Type:

list[tuple]

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 projection for 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 projection holds an operation other than Measure, SetBit0, SetBit1, Not, And, Or and Xor.

Return type:

CompiledProjection

evaluate_batch(samples)[source]

Evaluate the projection for every shot in samples.

When the projection copies qubit b into bit b for every bit and the samples are as wide as the register, the input bitstrings are returned as they are, without copying.

Parameters:

samples (Sequence[BitString]) – One computational-basis outcome per shot, all of the same width.

Returns:

One classical bitstring per shot, of length num_bits, in the order of samples.

Return type:

List[BitString]

evaluate(sample)[source]

Evaluate the projection for a single shot.

Same result as evaluate_projection(). evaluate_batch() is faster for many shots.

Parameters:

sample (BitString) – One computational-basis outcome.

Returns:

The classical bitstring, of length num_bits.

Return type:

BitString

class Tier[source]

Bases: object

Names of the two evaluation strategies, in increasing generality.